 Timestamp:
 20140818 20:43:16 (6 years ago)
 Location:
 2014/30/MikkoI/frakt
 Files:

 6 edited
Legend:
 Unmodified
 Added
 Removed

2014/30/MikkoI/frakt/frakt/frakt/Game1.cs
r5701 r5705 39 39 string errorMessage = ""; 40 40 41 bool rendering = false; 42 43 bool allowPanorama = false; 44 41 45 decimal[,][] pointData; 42 46 43 47 decimal[] gridOffset = new decimal[2] { 0, 0 }; 44 48 decimal[] gridScale = new decimal[2] { 4, 4 }; 45 int[] calcResolution = new int[2] { 10, 10 };49 decimal[] calcResolution = new decimal[2] { 10, 10 }; 46 50 int calcIterations = 1; 47 51 int iterationColors = 1; … … 53 57 54 58 Color[] data; 59 60 int maxThreads = 4; 55 61 56 62 public WooooFract() … … 82 88 void renderImage() 83 89 { 84 90 rendering = true; 85 91 viewportSize.X = Window.ClientBounds.Width; 86 92 viewportSize.Y = Window.ClientBounds.Height; … … 91 97 //if (calcResolution[1] > 2048) { calcResolution[1] = 2048; } 92 98 93 int threads = 1; 94 95 imageSize = new Vector2(calcResolution[0], calcResolution[1]); 99 int threads = Math.Min(maxThreads, (int)Math.Min(calcResolution[0], calcResolution[1])); 100 101 imageSize = new Vector2((int)calcResolution[0], (int)calcResolution[1]); 102 96 103 if (image != null) image.Dispose(); 104 this.pointData = null; 105 97 106 System.Drawing.Bitmap[] images = new System.Drawing.Bitmap[threads]; 98 107 … … 116 125 if (yy == threads  1) { loppu = (int)imageSize.Y; } 117 126 118 Test(alku, loppu);127 images[yy] = Test(alku, loppu); 119 128 }); 120 129 … … 127 136 } 128 137 129 /*//yhdistä threadien omat imaget 130 int totalHeight = 0; 131 for (var g = 0; g < threads; g++) { totalHeight += images[g].Height; } 132 133 image = new System.Drawing.Bitmap((int)imageSize.X, (int)totalHeight); 134 135 */ 138 using (System.Drawing.Graphics grfx = System.Drawing.Graphics.FromImage(image)) 139 { 140 int totalY = 0; 141 for (var y = 0; y < threads; y++) 142 { 143 grfx.DrawImage(images[y], 0, totalY); 144 totalY += images[y].Height; 145 } 146 } 136 147 137 148 Vector2 refSize = new Vector2(Math.Min(image.Width, 2048), Math.Min(image.Height, 2048)); … … 149 160 referenceImage = new Texture2D(GraphicsDevice, (int)refSize.X, (int)refSize.Y); 150 161 referenceImage.SetData<Color>(pixels); 151 } 152 153 void Test(int alku, int loppu) 162 rendering = false; 163 } 164 165 System.Drawing.Bitmap Test(int alku, int loppu) 154 166 { 155 167 Random random = new Random(); … … 168 180 if (doesPointEscape(this.pointData[x, y])) 169 181 { 170 image.SetPixel(x, y, getColor(iterations, iterationColors + 1));182 thisImage.SetPixel(x, y  alku, getColor(iterations, iterationColors + 1)); 171 183 break; 172 184 } … … 178 190 } 179 191 } 192 return thisImage; 180 193 } 181 194 182 195 bool doesPointEscape(decimal[] vec) 183 196 { 184 if (vec[0] * vec[0] + vec[1] * vec[1] > 4) return true; 185 return false; 197 return vec[0] * vec[0] + vec[1] * vec[1] > 4; 186 198 } 187 199 … … 244 256 { 245 257 case "I"://set iterations 246 calcIterations = Convert.ToInt32(commandSplit[1]); 258 switch (commandSplit[1]) 259 { 260 case "C": 261 iterationColors = Convert.ToInt32(commandSplit[2]); 262 break; 263 default: 264 calcIterations = Convert.ToInt32(commandSplit[1]); 265 break; 266 } 247 267 break; 248 268 case "R"://set resolution … … 250 270 { 251 271 case "X": 252 calcResolution [0] = Convert.ToInt32(commandSplit[2]);272 calcResolution = Factorize(calcResolution, decimal.Parse(commandSplit[2]), true); 253 273 break; 254 274 case "Y": 255 calcResolution[1] = Convert.ToInt32(commandSplit[2]); 256 break; 257 case "F": 258 calcResolution[0] = Window.ClientBounds.Width; 259 calcResolution[1] = Window.ClientBounds.Height; 260 break; 261 case "A": 262 calcResolution[0] = Math.Max(calcResolution[0], calcResolution[1]); 263 calcResolution[1] = Math.Max(calcResolution[0], calcResolution[1]); 264 break; 265 case "I": 266 calcResolution[0] = Math.Min(calcResolution[0], calcResolution[1]); 267 calcResolution[1] = Math.Min(calcResolution[0], calcResolution[1]); 275 calcResolution = Factorize(calcResolution, decimal.Parse(commandSplit[2]), false); 276 break; 277 case "S"://Hard set 278 switch (commandSplit[2]) 279 { 280 case "X": 281 calcResolution[0] = Convert.ToInt32(commandSplit[3]); 282 break; 283 case "Y": 284 calcResolution[1] = Convert.ToInt32(commandSplit[3]); 285 break; 286 case "F": 287 calcResolution[0] = Window.ClientBounds.Width; 288 calcResolution[1] = Window.ClientBounds.Height; 289 break; 290 case "A": 291 calcResolution[0] = Math.Max(calcResolution[0], calcResolution[1]); 292 calcResolution[1] = Math.Max(calcResolution[0], calcResolution[1]); 293 break; 294 case "I": 295 calcResolution[0] = Math.Min(calcResolution[0], calcResolution[1]); 296 calcResolution[1] = Math.Min(calcResolution[0], calcResolution[1]); 297 break; 298 default: 299 calcResolution[0] = int.Parse(commandSplit[2]); 300 calcResolution[1] = int.Parse(commandSplit[2]); 301 break; 302 } 268 303 break; 269 304 default: 270 calcResolution[0] = Convert.ToInt32(commandSplit[1]); 271 calcResolution[1] = Convert.ToInt32(commandSplit[1]); 305 calcResolution = Factorize(calcResolution, decimal.Parse(commandSplit[1])); 272 306 break; 273 307 } 274 308 break; 275 case " P"://set position309 case "O"://set offset 276 310 switch (commandSplit[1]) 277 311 { … … 291 325 break; 292 326 } 327 break; 328 case "P": 329 allowPanorama = !allowPanorama; 293 330 break; 294 331 case "H"://hide / show hud … … 343 380 errorMessage = e.Message; 344 381 } 382 } 383 384 decimal[] Factorize(decimal[] input, decimal factor, bool overr = false) 385 { 386 if (input[0] > input[1]  overr) 387 { 388 input[1] = (input[1] / input[0]) * factor; 389 input[0] = factor; 390 } 391 else 392 { 393 input[0] = (input[0] / input[1]) * factor; 394 input[1] = factor; 395 } 396 return input; 397 } 398 399 int[] Factorize(int[] input, double factor, bool overr = false) 400 { 401 if (input[0] > input[1]  overr) 402 { 403 input[1] = (int)(((double)input[1] / (double)input[0]) * factor); 404 input[0] = (int)factor; 405 } 406 else 407 { 408 input[0] = (int)(((double)input[0] / (double)input[1]) * factor); 409 input[1] = (int)factor; 410 } 411 return input; 345 412 } 346 413 … … 479 546 calcResolution[1] = 1; 480 547 } 481 if (calcResolution[0] < 1  calcResolution[1] < 1) { calcResolution = new int[2] { 1, 1 }; };548 if (calcResolution[0] < 1  calcResolution[1] < 1) { calcResolution = new decimal[2] { 1, 1 }; }; 482 549 renderImage(); 483 550 } … … 531 598 } 532 599 533 if (IsActive) 600 if ( 601 IsActive // if window is active 602 && !(Window.ClientBounds.Width == 0  Window.ClientBounds.Height == 0) // and it's not minimized 603 && mouseState.X > 0 && mouseState.Y > 0 // and mouse is 604 && mouseState.X < graphics.PreferredBackBufferWidth && mouseState.Y < graphics.PreferredBackBufferHeight // in the window 605 ) 534 606 { 535 607 if (mouseState.LeftButton == ButtonState.Pressed && oldMouseState.LeftButton != ButtonState.Pressed) 536 608 { 537 609 //mouse is down and dragging 538 if (!(Window.ClientBounds.Width == 0  Window.ClientBounds.Height == 0)) 539 { 540 downPos = getMousePos(mouseState); 541 } 610 downPos = new decimal[2] { mouseState.X, mouseState.Y }; 542 611 } 543 612 if (mouseState.LeftButton == ButtonState.Released && oldMouseState.LeftButton != ButtonState.Released) … … 546 615 if (!(Window.ClientBounds.Width == 0  Window.ClientBounds.Height == 0) && downPos != null) 547 616 { 548 upPos = getMousePos(mouseState);617 upPos = new decimal[2] { mouseState.X, mouseState.Y }; 549 618 rezToMouse(downPos, upPos); 550 619 } … … 560 629 } 561 630 562 decimal[] getMousePos( MouseStatemouse)631 decimal[] getMousePos(decimal[] mouse) 563 632 { 564 633 return new decimal[2] { 565 (( (decimal)mouse.X (decimal)viewportSize.X / 2) / (decimal)viewportSize.X) * gridScale[0] + gridOffset[0],566 (( (decimal)mouse.Y (decimal)viewportSize.Y / 2) / (decimal)viewportSize.Y) * gridScale[1] + gridOffset[1]634 ((mouse[0]  (decimal)viewportSize.X / 2) / (decimal)viewportSize.X) * gridScale[0] + gridOffset[0], 635 ((mouse[1]  (decimal)viewportSize.Y / 2) / (decimal)viewportSize.Y) * gridScale[1] + gridOffset[1] 567 636 }; 568 637 } … … 570 639 void rezToMouse(decimal[] topleft, decimal[] botright) 571 640 { 572 decimal scale = Math.Max(Math.Abs(botright[0]  topleft[0]), Math.Abs(botright[1]  topleft[1])); 573 if (scale == 0) { return; } 641 //if panorama is not allowed, scale is always a square. Otherwise resolution is bound to scale 642 decimal[] scale = new decimal[2] { Math.Abs(botright[0]  topleft[0]), Math.Abs(botright[1]  topleft[1]) }; 643 topleft = getMousePos(topleft); 644 botright = getMousePos(botright); 645 decimal[] gScale = new decimal[2] { Math.Abs(botright[0]  topleft[0]), Math.Abs(botright[1]  topleft[1]) }; 646 647 if (gScale[0] == 0  gScale[1] == 0) { return; } 648 574 649 decimal[] center = new decimal[2] { topleft[0] + (botright[0]  topleft[0]) / 2, topleft[1] + (botright[1]  topleft[1]) / 2 }; 575 650 gridOffset = center; 576 gridScale = new decimal[2] { scale, scale }; 577 renderImage(); 651 652 decimal bigger = Math.Max(gScale[0], gScale[1]); 653 if (allowPanorama) 654 { 655 calcResolution[0] = (int)((scale[0] * (decimal)viewportSize.X) / calcResolution[0]); 656 calcResolution[1] = (int)((scale[1] * (decimal)viewportSize.Y) / calcResolution[1]); 657 } 658 else 659 { 660 gScale = new decimal[2] { bigger, bigger }; 661 } 662 gridScale = gScale; 663 if (!allowPanorama) { renderImage(); } 578 664 } 579 665 … … 600 686 protected override void Draw(GameTime gameTime) 601 687 { 688 if (rendering) { return; } 602 689 GraphicsDevice.Clear(Color.White); 603 690 // TODO: Add your drawing code here … … 609 696 { 610 697 spriteBatch.DrawString(command, 611 "i:" + calcIterations + " r:(" + calcResolution[0] + "," + calcResolution[1] + 698 "i:" + calcIterations + "/" + iterationColors + 699 "\nr:(" + calcResolution[0] + "," + calcResolution[1] + 700 ")\nRT:(" + (calcResolution[0] * calcResolution[1]) + 612 701 ")\nP:(" + gridOffset[0] + 613 702 ",\n" + gridOffset[1] +
Note: See TracChangeset
for help on using the changeset viewer.