Advertisement
Kitomas

tile.cpp as of 2024-04-26

Apr 26th, 2024
759
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.36 KB | None | 0 0
  1. #include <tile.hpp>
  2.  
  3. using namespace kit;
  4.  
  5.  
  6.  
  7.  
  8.  
  9. void Scene::drawBg(){
  10.   if(bmp_bg == nullptr) return;
  11.   shape::point sizeBmp = bmp_bg->getSize();
  12.   if(sizeBmp.x<1 || sizeBmp.y<1) return;
  13.  
  14.  
  15.   if(stretch_bg){ //stretch entire background to entire canvas
  16.     bmp_bg->blitRect();
  17.  
  18.  
  19.   } else { //otherwise, repeat background bitmap in a tile pattern
  20.     shape::rect dst; //destination rectangle
  21.     dst.w = sizeBmp.x;
  22.     dst.h = sizeBmp.y;
  23.  
  24.     shape::point sizeCanvas = gl_win->getCanvasSize();
  25.  
  26.     //increase x, then y until the entire canvas is drawn to
  27.     for(dst.y = 0;  dst.y < sizeCanvas.y;  dst.y += dst.h)
  28.     for(dst.x = 0;  dst.x < sizeCanvas.x;  dst.x += dst.w)
  29.     {
  30.       bmp_bg->blitRect(&dst, nullptr); //whole bitmap is used when src = nullptr
  31.     }
  32.  
  33.  
  34.   }
  35.  
  36. }
  37.  
  38.  
  39.  
  40.  
  41.  
  42. //note: if slowdown gets to a point where it's noticable,
  43.  //implement a scene pattern cache
  44. void Scene::drawTiles(bool drawForeground){
  45.   Tile*              tiles = pat_mg;
  46.   if(drawForeground) tiles = pat_fg;
  47.   if(tiles == nullptr) return;
  48.  
  49.   if(tileset_a >= gl_tilesets_len  ||  tileset_b >= gl_tilesets_len)
  50.     throw "tileset index is out of bounds";
  51.  
  52.   Bitmap* tileset[2];
  53.   tileset[0] = gl_tilesets[tileset_a];
  54.   tileset[1] = gl_tilesets[tileset_b];
  55.  
  56.   shape::rect src(0, 0, 24, 24); //determines section of tileset to copy from
  57.   shape::rect dst(0, 0, 24, 24); //determines section of canvas to paste to
  58.  
  59.   shape::point sizeCanvas = gl_win->getCanvasSize();
  60.  
  61.  
  62.  
  63.   u32 tileindex = 0; //for accessing each tile independent of the 2 for loops
  64.  
  65.   //increase x, then y until the entire canvas is drawn to
  66.   for(dst.y = 0;  dst.y < sizeCanvas.y;  dst.y += dst.h)
  67.   for(dst.x = 0;  dst.x < sizeCanvas.x;  dst.x += dst.w)
  68.   {
  69.     Tile tile = tiles[tileindex++];
  70.     if(!tile.id) continue; //tile 0 is always transparent, so it can be skipped
  71.     src.x = ( tile.id    &0b1111) * src.w; //bits 0-3 are the tileset atlas' x
  72.     src.y = ((tile.id>>4)& 0b111) * src.h; //bits 4-6 are the tileset atlas' y
  73.  
  74.  
  75.     Bitmap* _tileset = tileset[tile.tileset]; //(tile.tileset is 1 bit in size)
  76.     if(_tileset != nullptr){
  77.       _tileset->blitRect(&dst, &src, 0xff00ff); //transparency color is magenta
  78.  
  79.     } else { //tileset doesn't exist; draw 'missing tileset' tile
  80.       gl_tileset_missing->blitRect(&dst, nullptr); //transparency is not used
  81.  
  82.     }
  83.  
  84.   }
  85.  
  86. }
  87.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement