diff options
author | KikooDX <kikoodx@paranoici.org> | 2022-03-27 14:56:31 +0200 |
---|---|---|
committer | KikooDX <kikoodx@paranoici.org> | 2022-03-27 14:56:31 +0200 |
commit | f2911ceebdef567e62f18b7037e85566ab10b4c2 (patch) | |
tree | 18f234b41ce7f2fb7b56370e7425f4d8a008571a | |
parent | 2cb24f14fabb1179b38476c8e6a45386a5ab05f0 (diff) | |
download | lzy-f2911ceebdef567e62f18b7037e85566ab10b4c2.tar.gz |
drawtileex
-rw-r--r-- | inc/lzy.h | 79 | ||||
-rw-r--r-- | src/main.c | 5 |
2 files changed, 83 insertions, 1 deletions
@@ -93,6 +93,8 @@ int LZY_DrawLine(int x0, int y0, int x1, int y1); int LZY_DrawRect(int x, int y, unsigned int w, unsigned int h); int LZY_DrawFillRect(int x, int y, unsigned int w, unsigned int h); int LZY_DrawTile(unsigned int id, int x, int y); +int LZY_DrawTileEx(unsigned int id, int x, int y, unsigned int w, + unsigned int h); int LZY_DrawChar(unsigned char chr, int x, int y); int LZY_DrawText(const char *text, int x, int y); LZY_Music *LZY_MusicLoad(const char *path); @@ -311,6 +313,41 @@ int LZY_DrawTile(unsigned int id, int x, int y) { #endif } +int LZY_DrawTileEx(unsigned int id, int x, int y, unsigned int w, + unsigned int h) { +#ifndef LZY_GINT_TILESET + LZY_UNUSED(id); + LZY_UNUSED(x); + LZY_UNUSED(y); + LZY_UNUSED(w); + LZY_UNUSED(h); + return -1; +#else + extern bopti_image_t LZY_GINT_TILESET; + int ix, iy; + + if (w * h == 0) + return -1; + + x += draw_off_x; + y += draw_off_y; + + ix = id % tset_width; + iy = id / tset_width; + + if (ix >= (int)tset_width || ix + w - 1 >= tset_width || + iy >= (int)tset_height || iy + h - 1 >= tset_height) + return -1; + + ix *= LZY_TILE_SIZE; + iy *= LZY_TILE_SIZE; + dsubimage(x, y, &LZY_GINT_TILESET, ix, iy, w * LZY_TILE_SIZE, + h * LZY_TILE_SIZE, DIMAGE_NONE); + + return 0; +#endif +} + int LZY_DrawChar(unsigned char chr, int x, int y) { #ifndef LZY_GINT_FONT LZY_UNUSED(chr); @@ -776,6 +813,48 @@ int LZY_DrawTile(unsigned int id, int x, int y) { return 0; } +int LZY_DrawTileEx(unsigned int id, int x, int y, unsigned int w, + unsigned int h) { + SDL_Rect src, dst; + + if (w * h == 0) { + error = "tile size can't be 0"; + return -1; + } + + if (id >= (unsigned int)(tset_width * tset_height)) { + error = "id exceeds boundaries"; + return -1; + } + + src.x = id % tset_width; + src.y = id / tset_width; + + if (src.x >= tset_width || src.x + w - 1 >= (unsigned int)tset_width || + src.y >= tset_height || + src.y + h - 1 >= (unsigned int)tset_height) { + error = "tile is not in tileset"; + return -1; + } + + src.x *= LZY_TILE_SIZE; + src.y *= LZY_TILE_SIZE; + src.w = w * LZY_TILE_SIZE; + src.h = h * LZY_TILE_SIZE; + + dst.x = x; + dst.y = y; + dst.w = w * LZY_TILE_SIZE; + dst.h = h * LZY_TILE_SIZE; + + if (SDL_RenderCopy(renderer, tset, &src, &dst) < 0) { + error = SDL_GetError(); + return -1; + } + + return 0; +} + int LZY_DrawChar(unsigned char chr, int x, int y) { const unsigned int id = (unsigned int)chr - LZY_FIRST_CHR; SDL_Rect src, dst; @@ -46,7 +46,10 @@ int main(int argc, const char **argv) { LZY_DrawLine(x, y, 0, 0); /* draw player */ - LZY_DrawTile(1, x, y); + if (LZY_DrawTileEx(1, x, y, 3, 2)) + LZY_Log(LZY_GetError()); + if (LZY_DrawTile(1, x, y)) + LZY_Log(LZY_GetError()); } LZY_DrawEnd(); } |