Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <tile.hpp>
- using namespace kit;
- void Scene::drawBg(){
- if(bmp_bg == nullptr) return;
- shape::point sizeBmp = bmp_bg->getSize();
- if(sizeBmp.x<1 || sizeBmp.y<1) return;
- if(stretch_bg){ //stretch entire background to entire canvas
- bmp_bg->blitRect();
- } else { //otherwise, repeat background bitmap in a tile pattern
- shape::rect dst; //destination rectangle
- dst.w = sizeBmp.x;
- dst.h = sizeBmp.y;
- shape::point sizeCanvas = gl_win->getCanvasSize();
- //increase x, then y until the entire canvas is drawn to
- for(dst.y = 0; dst.y < sizeCanvas.y; dst.y += dst.h)
- for(dst.x = 0; dst.x < sizeCanvas.x; dst.x += dst.w)
- {
- bmp_bg->blitRect(&dst, nullptr); //whole bitmap is used when src = nullptr
- }
- }
- }
- //note: if slowdown gets to a point where it's noticable,
- //implement a scene pattern cache
- void Scene::drawTiles(bool drawForeground){
- Tile* tiles = pat_mg;
- if(drawForeground) tiles = pat_fg;
- if(tiles == nullptr) return;
- if(tileset_a >= gl_tilesets_len || tileset_b >= gl_tilesets_len)
- throw "tileset index is out of bounds";
- Bitmap* tileset[2];
- tileset[0] = gl_tilesets[tileset_a];
- tileset[1] = gl_tilesets[tileset_b];
- shape::rect src(0, 0, 24, 24); //determines section of tileset to copy from
- shape::rect dst(0, 0, 24, 24); //determines section of canvas to paste to
- shape::point sizeCanvas = gl_win->getCanvasSize();
- u32 tileindex = 0; //for accessing each tile independent of the 2 for loops
- //increase x, then y until the entire canvas is drawn to
- for(dst.y = 0; dst.y < sizeCanvas.y; dst.y += dst.h)
- for(dst.x = 0; dst.x < sizeCanvas.x; dst.x += dst.w)
- {
- Tile tile = tiles[tileindex++];
- if(!tile.id) continue; //tile 0 is always transparent, so it can be skipped
- src.x = ( tile.id &0b1111) * src.w; //bits 0-3 are the tileset atlas' x
- src.y = ((tile.id>>4)& 0b111) * src.h; //bits 4-6 are the tileset atlas' y
- Bitmap* _tileset = tileset[tile.tileset]; //(tile.tileset is 1 bit in size)
- if(_tileset != nullptr){
- _tileset->blitRect(&dst, &src, 0xff00ff); //transparency color is magenta
- } else { //tileset doesn't exist; draw 'missing tileset' tile
- gl_tileset_missing->blitRect(&dst, nullptr); //transparency is not used
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement