diff options
author | kdx <kikoodx@paranoici.org> | 2022-09-06 14:41:26 +0200 |
---|---|---|
committer | kdx <kikoodx@paranoici.org> | 2022-09-06 14:41:26 +0200 |
commit | 5bc4d8910a6791b2474834c3662d759ad8f771e7 (patch) | |
tree | aab9860b3d88d38e7a84f2976e9e9b93575ae6f2 | |
parent | 92a6aaa7e0e30b4e75242953ae7244768cb13a38 (diff) | |
download | lzr-5bc4d8910a6791b2474834c3662d759ad8f771e7.tar.gz |
center draw
-rw-r--r-- | demo.c | 9 | ||||
-rw-r--r-- | lzr.c | 56 | ||||
-rw-r--r-- | lzr.h | 4 |
3 files changed, 37 insertions, 32 deletions
@@ -11,9 +11,10 @@ int main(void) LZR_ImageLoad("tset.bmp"); float darkness = 0.0f; float dark_dir = 1.0f; - int x = 10; - int y = 20; - LZR_ImageDrawSettings stg = {0, 0, -1, -1, 1.0, 1.0, 0.0, false, false}; + int x = 0; + int y = 0; + LZR_ImageDrawSettings stg = {0, 0, -1, -1, 1.0, + 1.0, 0.0, true, false, false}; while (!LZR_ShouldQuit()) { darkness += 0.01f * dark_dir; if (darkness * dark_dir >= 0.5f) { @@ -46,7 +47,7 @@ int main(void) LZR_DrawTile(1, 4, 22, 2); const float shade = 0.9 * (0.5f + darkness); LZR_DrawSetColor(shade, shade, 0.0f); - LZR_DrawImageEx(0, x, y, &stg); + LZR_DrawImageEx(0, x, y, stg); LZR_DrawEnd(); } LZR_Quit(); @@ -315,7 +315,7 @@ int LZR_DrawImage(int id, int x, int y) return 0; } -int LZR_DrawImageEx(int id, int x, int y, const LZR_ImageDrawSettings *stg) +int LZR_DrawImageEx(int id, int x, int y, LZR_ImageDrawSettings stg) { if (id < 0) { dx_log_error("id is negative"); @@ -325,37 +325,41 @@ int LZR_DrawImageEx(int id, int x, int y, const LZR_ImageDrawSettings *stg) dx_log_error("no image with id %d", id); return -1; } - const int width = (stg->width > 0) ? stg->width : images[id].width; - const int height = (stg->height > 0) ? stg->height : images[id].height; - SDL_Rect src = {stg->ix, stg->iy, width, height}; - SDL_Rect dst = {x, y, width * stg->scale_x, height * stg->scale_y}; - if (stg->ix < 0) { - src.w += stg->ix; - dst.x = 0 - stg->ix; - dst.w += stg->ix; - } - if (stg->iy < 0) { - src.y = 0 - stg->iy; - src.h += stg->iy; - dst.y -= stg->iy; - dst.h += stg->iy; - } - if (stg->ix + width > images[id].width) { - src.w = images[id].width - stg->ix; - dst.w = images[id].width - stg->ix; - } - if (stg->iy + height > images[id].height) { - src.h = images[id].height - stg->iy; - dst.h = images[id].height - stg->iy; + const int width = (stg.width > 0) ? stg.width : images[id].width; + const int height = (stg.height > 0) ? stg.height : images[id].height; + if (stg.center) { + x -= stg.scale_x * width / 2; + y -= stg.scale_y * height / 2; + } + SDL_Rect src = {stg.ix, stg.iy, width, height}; + SDL_Rect dst = {x, y, width * stg.scale_x, height * stg.scale_y}; + if (stg.ix < 0) { + src.w += stg.ix; + dst.x = 0 - stg.ix; + dst.w += stg.ix; + } + if (stg.iy < 0) { + src.y = 0 - stg.iy; + src.h += stg.iy; + dst.y -= stg.iy; + dst.h += stg.iy; + } + if (stg.ix + width > images[id].width) { + src.w = images[id].width - stg.ix; + dst.w = images[id].width - stg.ix; + } + if (stg.iy + height > images[id].height) { + src.h = images[id].height - stg.iy; + dst.h = images[id].height - stg.iy; } if (SDL_SetTextureColorMod(images[id].tex, color[0], color[1], color[2]) < 0) { dx_log_error("%s", SDL_GetError()); return -1; } - const int flip = (stg->flip_h ? SDL_FLIP_VERTICAL : 0) | - (stg->flip_v ? SDL_FLIP_HORIZONTAL : 0); - if (SDL_RenderCopyEx(renderer, images[id].tex, &src, &dst, stg->angle, + const int flip = (stg.flip_h ? SDL_FLIP_VERTICAL : 0) | + (stg.flip_v ? SDL_FLIP_HORIZONTAL : 0); + if (SDL_RenderCopyEx(renderer, images[id].tex, &src, &dst, stg.angle, NULL, flip)) { dx_log_error("%s", SDL_GetError()); return -1; @@ -36,7 +36,7 @@ typedef struct LZR_Event { typedef struct LZR_ImageDrawSettings { int ix, iy, width, height; double scale_x, scale_y, angle; - bool flip_v, flip_h; + bool center, flip_v, flip_h; } LZR_ImageDrawSettings; int LZR_Init(LZR_Config cfg); @@ -54,7 +54,7 @@ int LZR_DrawPoint(int x, int y); int LZR_DrawLine(int x0, int y0, int x1, int y1); int LZR_DrawRect(bool fill, int x, int y, int w, int h); int LZR_DrawImage(int id, int x, int y); -int LZR_DrawImageEx(int id, int x, int y, const LZR_ImageDrawSettings *stg); +int LZR_DrawImageEx(int id, int x, int y, LZR_ImageDrawSettings stg); int LZR_DrawTile(int id, int tile, int x, int y); #endif |