1 | using System; |
---|
2 | using System.Collections.Generic; |
---|
3 | using System.Linq; |
---|
4 | using Microsoft.Xna.Framework; |
---|
5 | using Microsoft.Xna.Framework.Audio; |
---|
6 | using Microsoft.Xna.Framework.Content; |
---|
7 | using Microsoft.Xna.Framework.GamerServices; |
---|
8 | using Microsoft.Xna.Framework.Graphics; |
---|
9 | using Microsoft.Xna.Framework.Input; |
---|
10 | using Microsoft.Xna.Framework.Media; |
---|
11 | using System.Threading; |
---|
12 | |
---|
13 | namespace frakt |
---|
14 | { |
---|
15 | /// <summary> |
---|
16 | /// This is the main type for your game |
---|
17 | /// </summary> |
---|
18 | public class WooooFract : Microsoft.Xna.Framework.Game |
---|
19 | { |
---|
20 | GraphicsDeviceManager graphics; |
---|
21 | SpriteBatch spriteBatch; |
---|
22 | Vector2 viewportSize; |
---|
23 | |
---|
24 | Texture2D pixel; |
---|
25 | Texture2D image; |
---|
26 | |
---|
27 | const int threads = 4; |
---|
28 | Thread[] apurit = new Thread[threads]; |
---|
29 | float anim; |
---|
30 | Color[] data; |
---|
31 | |
---|
32 | |
---|
33 | |
---|
34 | public WooooFract() |
---|
35 | { |
---|
36 | graphics = new GraphicsDeviceManager(this); |
---|
37 | graphics.PreferredBackBufferWidth = 800; |
---|
38 | graphics.PreferredBackBufferHeight = 600; |
---|
39 | Content.RootDirectory = "Content"; |
---|
40 | } |
---|
41 | |
---|
42 | /// <summary> |
---|
43 | /// Allows the game to perform any initialization it needs to before starting to run. |
---|
44 | /// This is where it can query for any required services and load any non-graphic |
---|
45 | /// related content. Calling base.Initialize will enumerate through any components |
---|
46 | /// and initialize them as well. |
---|
47 | /// </summary> |
---|
48 | protected override void Initialize() |
---|
49 | { |
---|
50 | // TODO: Add your initialization logic here |
---|
51 | pixel = new Texture2D(GraphicsDevice, 1, 1); |
---|
52 | pixel.SetData(new UInt32[] { UInt32.MaxValue }); |
---|
53 | |
---|
54 | image = new Texture2D(GraphicsDevice, Window.ClientBounds.Width, Window.ClientBounds.Height); |
---|
55 | data = new Color[image.Width * image.Height]; |
---|
56 | image.GetData(data); |
---|
57 | |
---|
58 | for (int y = 0; y < threads; y++) |
---|
59 | { |
---|
60 | int yy = y; |
---|
61 | apurit[y] = new Thread(delegate(object o) |
---|
62 | { |
---|
63 | Test(image.Height / threads * yy, image.Height / threads * (yy + 1)); |
---|
64 | }); |
---|
65 | |
---|
66 | apurit[y].Start(null); |
---|
67 | } |
---|
68 | |
---|
69 | piirraKuva(); |
---|
70 | |
---|
71 | base.Initialize(); |
---|
72 | |
---|
73 | startRender(); |
---|
74 | } |
---|
75 | |
---|
76 | void piirraKuva(float anim = 0) |
---|
77 | { |
---|
78 | GraphicsDevice.Textures[0] = null; |
---|
79 | this.anim = anim; |
---|
80 | |
---|
81 | /* |
---|
82 | for (int y = 0; y < threads; y++) |
---|
83 | { |
---|
84 | //int yy = y; |
---|
85 | //apurit[y] = new Thread(delegate (object o) { |
---|
86 | // Test(image.Height / threads * yy, image.Height / threads * (yy+1), (Color[])o); |
---|
87 | //}); |
---|
88 | if (apurit[y].IsAlive) |
---|
89 | apurit[y].Abort(); |
---|
90 | |
---|
91 | } |
---|
92 | |
---|
93 | |
---|
94 | for (int y = 0; y < threads; y++) |
---|
95 | { |
---|
96 | apurit[y].Join(); |
---|
97 | } |
---|
98 | */ |
---|
99 | |
---|
100 | image.SetData(data); |
---|
101 | } |
---|
102 | |
---|
103 | void Test(int alku, int loppu) |
---|
104 | { |
---|
105 | Random random = new Random(); |
---|
106 | while (true) |
---|
107 | { |
---|
108 | for (int y = alku; y < loppu; y++) |
---|
109 | { |
---|
110 | for (int x = 0; x < image.Width; x++) |
---|
111 | { |
---|
112 | data[x + y * image.Width] = getColor(x + (float)random.NextDouble() * anim / 100 + anim, y + 2); |
---|
113 | } |
---|
114 | } |
---|
115 | } |
---|
116 | } |
---|
117 | |
---|
118 | /// <summary> |
---|
119 | /// LoadContent will be called once per game and is the place to load |
---|
120 | /// all of your content. |
---|
121 | /// </summary> |
---|
122 | protected override void LoadContent() |
---|
123 | { |
---|
124 | // Create a new SpriteBatch, which can be used to draw textures. |
---|
125 | spriteBatch = new SpriteBatch(GraphicsDevice); |
---|
126 | |
---|
127 | // TODO: use this.Content to load your game content here |
---|
128 | } |
---|
129 | |
---|
130 | /// <summary> |
---|
131 | /// UnloadContent will be called once per game and is the place to unload |
---|
132 | /// all content. |
---|
133 | /// </summary> |
---|
134 | protected override void UnloadContent() |
---|
135 | { |
---|
136 | for (int y = 0; y < threads; y++) |
---|
137 | { |
---|
138 | apurit[y].Abort(); |
---|
139 | } |
---|
140 | } |
---|
141 | |
---|
142 | /// <summary> |
---|
143 | /// Allows the game to run logic such as updating the world, |
---|
144 | /// checking for collisions, gathering input, and playing audio. |
---|
145 | /// </summary> |
---|
146 | /// <param name="gameTime">Provides a snapshot of timing values.</param> |
---|
147 | protected override void Update(GameTime gameTime) |
---|
148 | { |
---|
149 | // Allows the game to exit |
---|
150 | |
---|
151 | //if (Keyboard.GetState().IsKeyDown(Keys.Enter)) |
---|
152 | piirraKuva((float)gameTime.TotalGameTime.TotalSeconds * 100); |
---|
153 | |
---|
154 | if (Keyboard.GetState().IsKeyDown(Keys.Escape)) |
---|
155 | this.Exit(); |
---|
156 | |
---|
157 | // TODO: Add your update logic here |
---|
158 | |
---|
159 | base.Update(gameTime); |
---|
160 | } |
---|
161 | |
---|
162 | void startRender() |
---|
163 | { |
---|
164 | viewportSize.X = Window.ClientBounds.Width; |
---|
165 | viewportSize.Y = Window.ClientBounds.Height; |
---|
166 | } |
---|
167 | |
---|
168 | /// <summary> |
---|
169 | /// This is called when the game should draw itself. |
---|
170 | /// </summary> |
---|
171 | /// <param name="gameTime">Provides a snapshot of timing values.</param> |
---|
172 | Color getColor(float index, float size) |
---|
173 | { |
---|
174 | Color[] palette = { new Color(0, 0, 0), new Color(255, 0, 0), new Color(255, 255, 0), new Color(0, 255, 0), new Color(0, 255, 255), new Color(0, 0, 255), new Color(0, 0, 0) }; |
---|
175 | index = index % size; |
---|
176 | size--; |
---|
177 | float realIndex = ((float)Math.Floor(index) / size) * (palette.Length - 1); |
---|
178 | Color col1 = palette[(int)Math.Floor(realIndex)]; |
---|
179 | Color col2 = palette[(int)Math.Ceiling(realIndex)]; |
---|
180 | float rel = realIndex - (float)Math.Floor(realIndex); |
---|
181 | return Color.Lerp(col1, col2, rel); |
---|
182 | } |
---|
183 | protected override void Draw(GameTime gameTime) |
---|
184 | { |
---|
185 | GraphicsDevice.Clear(Color.Black); |
---|
186 | // TODO: Add your drawing code here |
---|
187 | |
---|
188 | spriteBatch.Begin(); |
---|
189 | spriteBatch.Draw(image, new Vector2(0, 0), Color.White); |
---|
190 | spriteBatch.End(); |
---|
191 | |
---|
192 | base.Draw(gameTime); |
---|
193 | } |
---|
194 | } |
---|
195 | } |
---|