Changeset 5705 for 2014/30


Ignore:
Timestamp:
2014-08-18 20:43:16 (9 years ago)
Author:
mijoilmo
Message:

update

Location:
2014/30/MikkoI/frakt
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • 2014/30/MikkoI/frakt/frakt/frakt/Game1.cs

    r5701 r5705  
    3939        string errorMessage = ""; 
    4040 
     41        bool rendering = false; 
     42 
     43        bool allowPanorama = false; 
     44 
    4145        decimal[,][] pointData; 
    4246 
    4347        decimal[] gridOffset = new decimal[2] { 0, 0 }; 
    4448        decimal[] gridScale = new decimal[2] { 4, 4 }; 
    45         int[] calcResolution = new int[2] { 10, 10 }; 
     49        decimal[] calcResolution = new decimal[2] { 10, 10 }; 
    4650        int calcIterations = 1; 
    4751        int iterationColors = 1; 
     
    5357 
    5458        Color[] data; 
     59 
     60        int maxThreads = 4; 
    5561 
    5662        public WooooFract() 
     
    8288        void renderImage() 
    8389        { 
    84  
     90            rendering = true; 
    8591            viewportSize.X = Window.ClientBounds.Width; 
    8692            viewportSize.Y = Window.ClientBounds.Height; 
     
    9197            //if (calcResolution[1] > 2048) { calcResolution[1] = 2048; } 
    9298 
    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 
    96103            if (image != null) image.Dispose(); 
     104            this.pointData = null; 
     105 
    97106            System.Drawing.Bitmap[] images = new System.Drawing.Bitmap[threads]; 
    98107 
     
    116125                    if (yy == threads - 1) { loppu = (int)imageSize.Y; } 
    117126 
    118                     Test(alku, loppu); 
     127                    images[yy] = Test(alku, loppu); 
    119128                }); 
    120129 
     
    127136            } 
    128137 
    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            } 
    136147 
    137148            Vector2 refSize = new Vector2(Math.Min(image.Width, 2048), Math.Min(image.Height, 2048)); 
     
    149160            referenceImage = new Texture2D(GraphicsDevice, (int)refSize.X, (int)refSize.Y); 
    150161            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) 
    154166        { 
    155167            Random random = new Random(); 
     
    168180                        if (doesPointEscape(this.pointData[x, y])) 
    169181                        { 
    170                             image.SetPixel(x, y, getColor(iterations, iterationColors + 1)); 
     182                            thisImage.SetPixel(x, y - alku, getColor(iterations, iterationColors + 1)); 
    171183                            break; 
    172184                        } 
     
    178190                } 
    179191            } 
     192            return thisImage; 
    180193        } 
    181194 
    182195        bool doesPointEscape(decimal[] vec) 
    183196        { 
    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; 
    186198        } 
    187199 
     
    244256                { 
    245257                    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                        } 
    247267                        break; 
    248268                    case "R"://set resolution 
     
    250270                        { 
    251271                            case "X": 
    252                                 calcResolution[0] = Convert.ToInt32(commandSplit[2]); 
     272                                calcResolution = Factorize(calcResolution, decimal.Parse(commandSplit[2]), true); 
    253273                                break; 
    254274                            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                                } 
    268303                                break; 
    269304                            default: 
    270                                 calcResolution[0] = Convert.ToInt32(commandSplit[1]); 
    271                                 calcResolution[1] = Convert.ToInt32(commandSplit[1]); 
     305                                calcResolution = Factorize(calcResolution, decimal.Parse(commandSplit[1])); 
    272306                                break; 
    273307                        } 
    274308                        break; 
    275                     case "P"://set position 
     309                    case "O"://set offset 
    276310                        switch (commandSplit[1]) 
    277311                        { 
     
    291325                                break; 
    292326                        } 
     327                        break; 
     328                    case "P": 
     329                        allowPanorama = !allowPanorama; 
    293330                        break; 
    294331                    case "H"://hide / show hud 
     
    343380                errorMessage = e.Message; 
    344381            } 
     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; 
    345412        } 
    346413 
     
    479546                    calcResolution[1] -= 1; 
    480547                } 
    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 }; }; 
    482549                renderImage(); 
    483550            } 
     
    531598            } 
    532599 
    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                ) 
    534606            { 
    535607                if (mouseState.LeftButton == ButtonState.Pressed && oldMouseState.LeftButton != ButtonState.Pressed) 
    536608                { 
    537609                    //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 }; 
    542611                } 
    543612                if (mouseState.LeftButton == ButtonState.Released && oldMouseState.LeftButton != ButtonState.Released) 
     
    546615                    if (!(Window.ClientBounds.Width == 0 || Window.ClientBounds.Height == 0) && downPos != null) 
    547616                    { 
    548                         upPos = getMousePos(mouseState); 
     617                        upPos = new decimal[2] { mouseState.X, mouseState.Y }; 
    549618                        rezToMouse(downPos, upPos); 
    550619                    } 
     
    560629        } 
    561630 
    562         decimal[] getMousePos(MouseState mouse) 
     631        decimal[] getMousePos(decimal[] mouse) 
    563632        { 
    564633            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] 
    567636                }; 
    568637        } 
     
    570639        void rezToMouse(decimal[] topleft, decimal[] botright) 
    571640        { 
    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 
    574649            decimal[] center = new decimal[2] { topleft[0] + (botright[0] - topleft[0]) / 2, topleft[1] + (botright[1] - topleft[1]) / 2 }; 
    575650            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(); } 
    578664        } 
    579665 
     
    600686        protected override void Draw(GameTime gameTime) 
    601687        { 
     688            if (rendering) { return; } 
    602689            GraphicsDevice.Clear(Color.White); 
    603690            // TODO: Add your drawing code here 
     
    609696            { 
    610697                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]) + 
    612701                    ")\nP:(" + gridOffset[0] + 
    613702                    ",\n" + gridOffset[1] + 
Note: See TracChangeset for help on using the changeset viewer.