Advertisement
Corosus

Basic Tile System Rendering Quick Mockup

Jun 25th, 2012
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.59 KB | None | 0 0
  1. SpriteSheet ss = a loaded spritesheet of 16x16, so the image is 512x512, x:0 y:0 is id:0, x:1 y:0 is id:1, x:1 y:1 is id:17, and so on
  2.  
  3. TileInfo[512] ti;
  4.           ^-- # of unique tile types
  5.  
  6. //Init data
  7. for (int x = 0; x < 16; x++) {
  8.     for (int y = 0; y < 16; y++) {
  9.         ti[(y*16)+x] = new TileInfo();
  10.         ti[(y*16)+x].tileID = (y*16)+x;
  11.     }
  12. }
  13.  
  14. //Render your shit
  15. void renderViewport() {
  16.     //filler data just so i can render, this should of course be somewhere else
  17.     int tileSize = 32;
  18.     int worldSizeX = 9600 (300x32)
  19.     int worldSizeY = 6400 (200x32)
  20.     int viewportWidth = 800;
  21.     int viewportHeight = 600;
  22.     int viewportPosX = 133;
  23.     int viewportPosY = 162;
  24.  
  25.     int viewportGUIX = 60;
  26.     int viewportGUIY = 30;
  27.  
  28.     //now for the render - ignoring the need for smoothly clipping off the tiles along the viewport border
  29.     int tileXStart = ((int)(viewportPosX / tileSize))
  30.     int tileXEnd = ((int)(viewportPosX / tileSize)) + ((int)((viewportWidth / tileSize) + 0.5)) //0.5 to round up for that extra tile
  31.     int tileYStart = ((int)(viewportPosY / tileSize))
  32.     int tileYEnd = ((int)(viewportPosY / tileSize)) + ((int)((viewportHeight / tileSize) + 0.5)) //0.5 to round up for that extra tile
  33.  
  34.     //no wait, noooowwww for the render
  35.     for (int x = tileXStart; x <= tileXEnd; x++) {
  36.         for (int y = tileYStart; y < tileYEnd; y++) {
  37.  
  38.             Image tileImage = ss.getSprite(x, y);
  39.            
  40.             //bitblit?! source, x, y, width, height, dest, x, y, width, height
  41.             OGLRenderMethodWhateverThatIs(tileImage, 0, 0, tileSize, tileSize, screen, viewportGUIX + (x * tileSize), viewportGUIY + (y * tileSize), tileSize, tileSize);
  42.  
  43.         }
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement