Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Name: Isometric Tiled
- // Submenu: Stylize
- // Author: Anthony "Mallaboro"
- // Title: Isometric Builder
- // Version: 1.0
- // Desc: Turns orthogonal images into an array of isometric tiles
- // Keywords: Orthogonal, Isometric
- // URL: https://litematrix.blogspot.com/
- // Help: [email protected]
- #region UICode
- bool Amount1 = true;//Render background (black)
- bool Amount2 = true;//Arrange tiles by orthogonal position
- #endregion
- class Tile
- {
- //tile width and height
- public static int Width = 128;
- public static int Height = 64;
- //these indexes are used when arranging tiles by orthogonal position
- public int x = 0;
- public int y = 0;
- //the array of pixel data
- public ColorBgra[,] pixels;
- public Tile(int x, int y, bool checkers)
- {
- pixels = new ColorBgra[ Tile.Width, Tile.Height ];
- this.x = x;
- this.y = y;
- if(checkers)
- {
- //outside
- for( int _y = 0; _y < Tile.Height; _y++ )
- for( int _x = 0; _x < Tile.Width; _x++ )
- {
- if( _y < Tile.Height / 2 )
- if( _x <= ( Tile.Width / 2 ) - ( 2 * _y ) ||
- _x >= Tile.Width / 2 + ( 2 * _y ) )
- {
- pixels[ _x, _y ].R = 0;
- pixels[ _x, _y ].G = 0;
- pixels[ _x, _y ].B = 0;
- pixels[ _x, _y ].A = 255;
- }
- if( _y >= Tile.Height / 2 )
- if( _x <= ( Tile.Width / 2 ) - ( Tile.Width / 2 ) + ( _y - ( Tile.Height / 2 ) ) * 2 ||
- _x >= ( Tile.Width / 2 ) + ( Tile.Width / 2 ) - ( _y - ( Tile.Height / 2 ) ) * 2 )
- {
- pixels[ _x, _y ].R = 0;
- pixels[ _x, _y ].G = 0;
- pixels[ _x, _y ].B = 0;
- pixels[ _x, _y ].A = 255;
- }
- }
- }
- }
- //"Input" the incoming pixel data
- //"x", "y", the pixel index of this tiles pixel array
- public void Set( ColorBgra input, int x, int y )
- {
- //temporary restriction, we do not accept black pixels
- //if( pixels[ x, y ] == ColorBgra.Black )
- // return;
- //if the incoming pixel being set is within these parameters,
- //it is within this tiles shape and will be stored.
- bool left = x <= Tile.Width / 2;
- bool right = x >= Tile.Width / 2;
- bool top = y <= Tile.Height / 2;
- bool topRight = x < ( Tile.Width / 2 ) + ( y * 2 );
- bool topLeft = x > ( Tile.Width / 2 ) - ( y * 2 );
- bool bot = y > Tile.Height / 2;
- bool botLeft = x > ( 2 * ( y - Tile.Height / 2 ) );
- bool botRight = x < ( Tile.Width ) - ( 2 * ( y - Tile.Height / 2 ) );
- //tiles are seporated into 4 quadrants. If the incoming pixel overlaps one of these 4 quadrants it is stored.
- if( top )
- if( left
- && topLeft )
- {
- pixels[ x, y ] = input;
- }
- if( top )
- if( right
- && topRight )
- {
- pixels[ x, y ] = input;
- }
- if( bot )
- if( left
- && botLeft )
- {
- pixels[ x, y ] = input;
- }
- if( bot )
- if( right
- && botRight
- )
- {
- pixels[ x, y ] = input;
- }
- }
- //returns whether or not this tile contains any pixel of color (excluding pure black and pure white)
- public bool IsOccupied()
- {
- for( int y = 0; y < Height; y++ )
- for( int x = 0; x < Width; x++ )
- {
- if( pixels[ x, y ].R > 0 && pixels[ x, y ].R < 255 ||
- pixels[ x, y ].G > 0 && pixels[ x, y ].G < 255 ||
- pixels[ x, y ].B > 0 && pixels[ x, y ].B < 255 )
- return true;
- }
- return false;
- }
- //returns the specified pixel at the x, y index.
- public ColorBgra Get( int x, int y )
- {
- return pixels[ x, y ];
- }
- }
- void Render( Surface dst, Surface src, Rectangle rect )
- {
- //clear the canvas
- for (int y = rect.Top; y < rect.Bottom; y++) {
- if (IsCancelRequested) return;
- for (int x = rect.Left; x < rect.Right; x++)
- dst[x,y] = ColorBgra.Transparent;
- }
- //NOTE TO SELF: top-left is 0, 0
- //a list of occupied tiles containing data
- List<Tile> tiles = new List<Tile>( );
- //sample tile to check whether it has any data
- Tile sample;
- //toggle changes every Y iteration. It keeps the x-axis in the correct position for isometric images
- //(toggle == true == pixel_x = 0), (else pixel_x = -( Tile.Width / 2 );)
- bool toggle = false;
- //pixelX and Y are the top-left corners of the tile being analyzed. These values change.
- int pixel_x = -( Tile.Width / 2 );
- int pixel_y = -( Tile.Height / 2 );
- //easy-access
- int width = src.Bounds.Width;
- int height = src.Bounds.Height;
- //tiles wide and tiles tall are incrimented by 1 to capture the image along the edges
- //to elaborate: tiles[0,0] position is -(Tile.Width / 2, -(Tile.Height / 2
- //tiles tall is doubled to account for Tile.Height being half of Tile.Width
- int tilesWide = (width / Tile.Width) + 1;
- int tilesTall = (height / Tile.Height) * 2 + 1;
- //assign each tile their location
- for (int y = 0; y < tilesTall; y++)
- {
- for (int x = 0; x < tilesWide; x++)
- {
- sample = new Tile(x, y, Amount1);
- for (int Y = 0; Y < Tile.Height; Y++)
- {
- for (int X = 0; X < Tile.Width; X++)
- {
- if ((pixel_x + X < 1) || (pixel_y + Y < 1))
- continue;
- if ((pixel_x + X > width-2) || (pixel_y + Y > height-2))
- continue;
- sample.Set(src[pixel_x + X, pixel_y + Y], X, Y);
- }
- }
- if(sample.IsOccupied())
- tiles.Add(sample);
- pixel_x += Tile.Width;
- }
- toggle = !toggle;
- pixel_x = toggle == true ? 0 : -( Tile.Width / 2);
- pixel_y += Tile.Height / 2;
- }
- int _x = 0;
- int _y = 0;
- if(Amount2 == false)
- {
- for(int i = 0; i < tiles.Count; i++)
- {
- for (pixel_y = 0; pixel_y < Tile.Height; pixel_y++)
- for (pixel_x = 0; pixel_x < Tile.Width; pixel_x++)
- {
- //prevent crashes if screen width or height isn't great enough
- //to fit the tiles
- if((_x * Tile.Width) + pixel_x >= width)
- break;
- if((_y * Tile.Height) + pixel_y >= height)
- break;
- dst[ (_x * Tile.Width) + pixel_x
- , ( _y * Tile.Height) + pixel_y] = tiles[i].pixels[pixel_x, pixel_y];
- }
- _x++;
- if(_x >= tilesWide-1)
- {
- _x = 0;
- _y ++;
- }
- }
- }
- else
- {
- for(int i = 0; i < tiles.Count; i++)
- {
- for (pixel_y = 0; pixel_y < Tile.Height; pixel_y++)
- for (pixel_x = 0; pixel_x < Tile.Width; pixel_x++)
- {
- //prevent crashes if screen width or height isn't great enough
- //to fit the tiles
- if((tiles[i].x * Tile.Width) + pixel_x >= width)
- break;
- if((tiles[i].y * Tile.Height) + pixel_y >= height)
- break;
- dst[ (tiles[i].x * Tile.Width) + pixel_x
- , ( tiles[i].y * Tile.Height) + pixel_y] = tiles[i].pixels[pixel_x, pixel_y];
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement