Changeset 5693 for 2014/30/MikkoI/frakt
- Timestamp:
- 2014-07-25 12:35:58 (8 years ago)
- Location:
- 2014/30/MikkoI/frakt
- Files:
-
- 8 added
- 12 edited
Legend:
- Unmodified
- Added
- Removed
-
2014/30/MikkoI/frakt/frakt.sln
r5558 r5693 4 4 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "frakt", "frakt\frakt\frakt.csproj", "{F9E4E698-C16A-4607-A0C5-F26305364268}" 5 5 EndProject 6 Project("{ 96E2B04D-8817-42C6-938A-82C39BA4D311}") = "fraktContent", "frakt\fraktContent\fraktContent.contentproj", "{223295BE-ACF3-44C6-B8A3-41C3A9AB2E31}"6 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "fraktContent", "frakt\fraktContent\fraktContent.contentproj", "{223295BE-ACF3-44C6-B8A3-41C3A9AB2E31}" 7 7 EndProject 8 8 Global -
2014/30/MikkoI/frakt/frakt/frakt/Game1.cs
r5656 r5693 10 10 using Microsoft.Xna.Framework.Input; 11 11 using Microsoft.Xna.Framework.Media; 12 using System.IO; 12 13 13 14 namespace frakt … … 28 29 SpriteFont command; 29 30 31 Vector2 imageSize = Vector2.Zero; 32 30 33 Texture2D pixel; 31 Texture2D image; 34 System.Drawing.Bitmap image; 35 Texture2D referenceImage; 32 36 33 37 string commandStr = ""; 34 38 bool infoHide = false; 39 string errorMessage = ""; 35 40 36 41 decimal[,][] pointData; … … 65 70 pixel.SetData(new UInt32[] { UInt32.MaxValue }); 66 71 67 image = new Texture2D(GraphicsDevice, 1, 1);68 69 72 base.Initialize(); 70 73 … … 76 79 GraphicsDevice.Textures[0] = null; 77 80 78 image = new Texture2D(GraphicsDevice, (int)calcResolution[0], (int)calcResolution[1]); 81 //if (calcResolution[0] > 2048) { calcResolution[0] = 2048; } 82 //if (calcResolution[1] > 2048) { calcResolution[1] = 2048; } 83 84 int threads = 1; 85 86 imageSize = new Vector2(calcResolution[0], calcResolution[1]); 87 if (image!=null) image.Dispose(); 88 System.Drawing.Bitmap[] images = new System.Drawing.Bitmap[threads]; 79 89 80 90 //alusta fraktaalin numeerinen data 81 this.pointData = new decimal[image.Width, image.Height][]; 82 83 int threads = 4; 91 this.pointData = new decimal[(int)imageSize.X, (int)imageSize.Y][]; 92 93 image = new System.Drawing.Bitmap((int)imageSize.X, (int)imageSize.Y); 94 95 84 96 Thread[] apurit = new Thread[threads]; 85 97 86 this.data = new Color[image.Width * image.Height]; 87 image.GetData(data); 98 this.data = new Color[(int)imageSize.X * (int)imageSize.Y]; 88 99 89 100 for (int y = 0; y < threads; y++) … … 92 103 apurit[y] = new Thread(delegate(object o) 93 104 { 94 int alku = (image.Height / threads) * yy; 95 int loppu = (image.Height / threads) * (yy + 1); 96 if (yy == threads - 1) { loppu = image.Height; } 105 int alku = ((int)imageSize.Y / threads) * yy; 106 int loppu = ((int)imageSize.Y / threads) * (yy + 1); 107 if (yy == threads - 1) { loppu = (int)imageSize.Y; } 108 97 109 Test(alku, loppu); 98 110 }); … … 106 118 } 107 119 108 image.SetData(data); 120 /*//yhdistä threadien omat imaget 121 int totalHeight = 0; 122 for (var g = 0; g < threads; g++) { totalHeight += images[g].Height; } 123 124 image = new System.Drawing.Bitmap((int)imageSize.X, (int)totalHeight); 125 126 */ 127 128 Vector2 refSize = new Vector2(Math.Min(image.Width, 2048), Math.Min(image.Height, 2048)); 129 130 Color[] pixels = new Color[(int)(refSize.X * refSize.Y)]; 131 for (int y = 0; y < refSize.Y; y++) 132 { 133 for (int x = 0; x < refSize.X; x++) 134 { 135 System.Drawing.Color c = image.GetPixel(x, y); 136 pixels[(y * (int)refSize.X) + x] = new Color(c.R, c.G, c.B, c.A); 137 } 138 } 139 140 referenceImage = new Texture2D(GraphicsDevice, (int)refSize.X, (int)refSize.Y); 141 referenceImage.SetData<Color>(pixels); 109 142 } 110 143 … … 112 145 { 113 146 Random random = new Random(); 147 148 System.Drawing.Bitmap thisImage = new System.Drawing.Bitmap((int)imageSize.X, loppu - alku); 149 114 150 for (int y = alku; y < loppu; y++) 115 151 { 116 for (int x = 0; x < image .Width; x++)117 { 118 decimal VectorX = (gridScale[0] / image.Width) * x - (gridScale[0] / 2M) + gridOffset[0];119 decimal VectorY = (gridScale[1] / image.Height) * y - (gridScale[1] / 2M) + gridOffset[1];152 for (int x = 0; x < imageSize.X; x++) 153 { 154 decimal VectorX = (gridScale[0] / (int)imageSize.X) * x - (gridScale[0] / 2M) + gridOffset[0]; 155 decimal VectorY = (gridScale[1] / (int)imageSize.Y) * y - (gridScale[1] / 2M) + gridOffset[1]; 120 156 pointData[x, y] = new decimal[4] { VectorX, VectorY, VectorX, VectorY }; 121 157 for (int iterations = 0; iterations < calcIterations; iterations++) … … 123 159 if (doesPointEscape(this.pointData[x, y])) 124 160 { 125 this.data[x + y * image.Width] = getColor(iterations, calcIterations + 1);161 image.SetPixel(x, y, getColor(iterations, calcIterations + 1)); 126 162 break; 127 163 } … … 144 180 { 145 181 decimal[] nvec = new decimal[4]; 182 decimal branchLevel = 2M; 146 183 nvec[0] = 147 //1M * vec[0] - vec[1] * vec[1] - 1M* vec[1] * vec[0] * vec[0]; // branch148 184 branchLevel * vec[0] - vec[1] * vec[1] - branchLevel * vec[1] * vec[0] * vec[0]; // branch 185 //vec[0] * vec[0] - vec[1] * vec[1] + vec[2]; // mandelbrot 149 186 nvec[1] = 150 //1M * vec[1] - vec[0] * vec[0] - 1M* vec[0] * vec[1] * vec[1]; // branch151 187 branchLevel * vec[1] - vec[0] * vec[0] - branchLevel * vec[0] * vec[1] * vec[1]; // branch 188 //2M * vec[0] * vec[1] + vec[3]; // mandelbrot 152 189 nvec[2] = vec[2]; 153 190 nvec[3] = vec[3]; … … 197 234 switch (commandSplit[0]) 198 235 { 199 case "I": 236 case "I"://set iterations 200 237 calcIterations = Convert.ToInt32(commandSplit[1]); 201 238 break; 202 case "R": 239 case "R"://set resolution 203 240 switch (commandSplit[1]) 204 241 { … … 213 250 calcResolution[1] = Window.ClientBounds.Height; 214 251 break; 252 case "A": 253 calcResolution[0] = Math.Max(calcResolution[0], calcResolution[1]); 254 calcResolution[1] = Math.Max(calcResolution[0], calcResolution[1]); 255 break; 256 case "I": 257 calcResolution[0] = Math.Min(calcResolution[0], calcResolution[1]); 258 calcResolution[1] = Math.Min(calcResolution[0], calcResolution[1]); 259 break; 215 260 default: 216 261 calcResolution[0] = Convert.ToInt32(commandSplit[1]); … … 219 264 } 220 265 break; 221 case "P": 266 case "P"://set position 222 267 switch (commandSplit[1]) 223 268 { … … 238 283 } 239 284 break; 240 case "H": 285 case "H"://hide / show hud 241 286 infoHide = !infoHide; 242 287 break; 243 case "RUN": 288 case "W"://set window size 289 graphics.IsFullScreen = false; 290 switch (commandSplit[1]) 291 { 292 case "X": 293 graphics.PreferredBackBufferWidth = Convert.ToInt32(commandSplit[2]); 294 break; 295 case "Y": 296 graphics.PreferredBackBufferHeight = Convert.ToInt32(commandSplit[2]); 297 break; 298 case "F": 299 graphics.PreferredBackBufferWidth = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width; 300 graphics.PreferredBackBufferHeight = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height; 301 graphics.IsFullScreen = true; 302 break; 303 default: 304 graphics.PreferredBackBufferWidth = Convert.ToInt32(commandSplit[1]); 305 graphics.PreferredBackBufferHeight = Convert.ToInt32(commandSplit[1]); 306 break; 307 } 308 graphics.ApplyChanges(); 309 break; 310 case "S"://Save image 311 string filePath = ""; 312 if (commandSplit.Length == 1) 313 { 314 filePath = DateTime.Now.ToString("MM-dd-yy H;mm;ss") + ".png"; 315 } 316 else 317 { 318 filePath = commandSplit[1] + ".png"; 319 } 320 //Stream stream = File.Create(filePath); 321 image.Save(filePath); 322 //stream.Dispose(); 323 break; 324 case "RUN"://render 244 325 renderImage(); 245 326 break; 246 } 247 } 248 catch (System.Exception) {} 327 case "EXIT"://exit 328 Exit(); 329 break; 330 } 331 } 332 catch (System.Exception e) 333 { 334 errorMessage = e.Message; 335 } 249 336 } 250 337 … … 427 514 /// </summary> 428 515 /// <param name="gameTime">Provides a snapshot of timing values.</param> 429 Color getColor(float index, float size)516 System.Drawing.Color getColor(float index, float size) 430 517 { 431 518 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) }; … … 437 524 Color col2 = palette[(int)Math.Ceiling(realIndex)]; 438 525 float rel = realIndex - (float)Math.Floor(realIndex); 439 return Color.Lerp(col1, col2, rel); 526 return convertColor(Color.Lerp(col1, col2, rel)); 527 } 528 System.Drawing.Color convertColor(Color color) 529 { 530 return System.Drawing.Color.FromArgb(color.R, color.G, color.B); 440 531 } 441 532 protected override void Draw(GameTime gameTime) … … 445 536 446 537 spriteBatch.Begin(); 447 spriteBatch.Draw( image, new Rectangle(0, 0, Window.ClientBounds.Width, Window.ClientBounds.Height), Color.White);538 spriteBatch.Draw(referenceImage, new Rectangle(0, 0, Window.ClientBounds.Width, Window.ClientBounds.Height), Color.White); 448 539 spriteBatch.DrawString(command, commandStr, new Vector2(0, 0), Color.Gray); 449 540 if (!infoHide) 450 spriteBatch.DrawString(command, 451 "i:" + calcIterations + " r:(" + calcResolution[0] + "," + calcResolution[1] + 452 ")\nP:(" + gridOffset[0] + 453 ",\n" + gridOffset[1] + 454 ")\ns:" + gridScale[0] 455 , new Vector2(0, Window.ClientBounds.Height - command.LineSpacing * 4), Color.Red); 541 { 542 spriteBatch.DrawString(command, 543 "i:" + calcIterations + " r:(" + calcResolution[0] + "," + calcResolution[1] + 544 ")\nP:(" + gridOffset[0] + 545 ",\n" + gridOffset[1] + 546 ")\ns:" + gridScale[0] 547 , new Vector2(0, Window.ClientBounds.Height - command.LineSpacing * 4), Color.HotPink); 548 spriteBatch.DrawString(command, errorMessage, new Vector2(Window.ClientBounds.Width - command.MeasureString(errorMessage).X, 0), Color.Red); 549 } 456 550 spriteBatch.End(); 457 551 -
2014/30/MikkoI/frakt/frakt/frakt/frakt.csproj
r5650 r5693 67 67 <Reference Include="mscorlib" /> 68 68 <Reference Include="System" /> 69 <Reference Include="System.Drawing" /> 70 <Reference Include="System.Windows.Forms" /> 69 71 <Reference Include="System.Xml" /> 70 72 <Reference Include="System.Core" />
Note: See TracChangeset
for help on using the changeset viewer.