From 45e71052a2ac340fc687b4ab9d7e60190ff8e63b Mon Sep 17 00:00:00 2001 From: kdx Date: Sun, 15 Jan 2023 02:14:22 +0100 Subject: alpha support --- demo.c | 6 +++--- lzr.c | 36 ++++++++++++++++++++++++++---------- lzr.h | 2 +- 3 files changed, 30 insertions(+), 14 deletions(-) diff --git a/demo.c b/demo.c index 90892cf..8c860af 100644 --- a/demo.c +++ b/demo.c @@ -39,13 +39,13 @@ int main(void) LZR_MousePosition(&x, &y); stg.angle = darkness / 16; LZR_DrawBegin(); - LZR_DrawSetColor(0.9f, 0.9f, 0.8f); + LZR_DrawSetColor(0.9f, 0.9f, 0.8f, 1.0f); LZR_DrawClear(); - LZR_DrawSetColor(1.0f, 1.0f, 1.0f); + LZR_DrawSetColor(1.0f, 1.0f, 1.0f, 1.0f); LZR_DrawTile(LZR_IMAGE("tset.bmp"), 2, 2, 2, 0.0, 0); LZR_DrawTile(LZR_IMAGE("tset.bmp"), 4, 22, 2, 0.0, 0); const float shade = 0.9 * (0.5f + darkness); - LZR_DrawSetColor(shade, shade, 0.0f); + LZR_DrawSetColor(shade, shade, 0.0f, shade); LZR_DrawImageEx(LZR_IMAGE("coucou.bmp"), x, y, stg); LZR_DrawCircle(false, x, y, 100); LZR_DrawEnd(); diff --git a/lzr.c b/lzr.c index ee4d1e4..89ef073 100644 --- a/lzr.c +++ b/lzr.c @@ -18,9 +18,10 @@ #include #include -#define UNPACKED_COLOR color[0], color[1], color[2] -#define SCODE_BIND_MENU SDL_SCANCODE_F1 -#define SCODE_FULLSCREEN SDL_SCANCODE_F11 +#define UNPACKED_COLOR color[0], color[1], color[2] +#define UNPACKED_COLOR_RGBA color[0], color[1], color[2], color[3] +#define SCODE_BIND_MENU SDL_SCANCODE_F1 +#define SCODE_FULLSCREEN SDL_SCANCODE_F11 static LZR_Config config = {0}; static char *basepath = NULL; @@ -36,7 +37,7 @@ static struct { char *path; long mtime; } images[LZR_MAX_IMAGES] = {0}; -static unsigned int color[3] = {0}; +static unsigned int color[4] = {0}; static unsigned int map[LZR_BUTTON_COUNT] = { SDL_SCANCODE_LEFT, SDL_SCANCODE_RIGHT, SDL_SCANCODE_UP, SDL_SCANCODE_DOWN, SDL_SCANCODE_X, SDL_SCANCODE_C}; @@ -263,6 +264,8 @@ int LZR_Init(LZR_Config cfg) SDL_Log("%s", SDL_GetError()); return -1; } + if (LZR_DrawSetColor(1.0f, 1.0f, 1.0f, 1.0f)) + return -1; return 0; } @@ -567,7 +570,7 @@ int LZR_DrawEnd(void) SDL_Log("%s", SDL_GetError()); return -1; } - LZR_DrawSetColor(0.0f, 0.0f, 0.0f); + LZR_DrawSetColor(0.0f, 0.0f, 0.0f, 1.0f); if (LZR_DrawClear()) { SDL_Log("LZY_DrawClear failed"); return -1; @@ -598,16 +601,17 @@ int LZR_DrawEnd(void) return 0; } -int LZR_DrawSetColor(float r, float g, float b) +int LZR_DrawSetColor(float r, float g, float b, float a) { const unsigned int ur = (unsigned int)(r * 255) & 255; const unsigned int ug = (unsigned int)(g * 255) & 255; const unsigned int ub = (unsigned int)(b * 255) & 255; - if (SDL_SetRenderDrawColor(renderer, ur, ug, ub, 255) < 0) { + const unsigned int ua = (unsigned int)(a * 255) & 255; + if (SDL_SetRenderDrawColor(renderer, ur, ug, ub, ua) < 0) { SDL_Log("%s", SDL_GetError()); return -1; } - color[0] = ur, color[1] = ug, color[2] = ub; + color[0] = ur, color[1] = ug, color[2] = ub, color[3] = ua; return 0; } @@ -671,7 +675,7 @@ int LZR_DrawCircle(bool fill, int x, int y, int radius) { #ifdef LZR_ENABLE_GFX if ((fill ? filledCircleRGBA : circleRGBA)(renderer, x, y, radius, - UNPACKED_COLOR, 255) < 0) { + UNPACKED_COLOR_RGBA) < 0) { SDL_Log("%s", SDL_GetError()); return -1; } @@ -694,7 +698,7 @@ int LZR_DrawPolygon(bool fill, int *vx, int *vy, int n) for (int i = 0; i < n; i++) x[i] = vx[i], y[i] = vy[i]; if ((fill ? filledPolygonRGBA : polygonRGBA)(renderer, x, y, n, - UNPACKED_COLOR, 255) < 0) { + UNPACKED_COLOR_RGBA) < 0) { SDL_Log("%s", SDL_GetError()); return -1; } @@ -720,6 +724,10 @@ int LZR_DrawImage(int id, int x, int y) SDL_Log("%s", SDL_GetError()); return -1; } + if (SDL_SetTextureAlphaMod(images[id].tex, color[3]) < 0) { + SDL_Log("%s", SDL_GetError()); + return -1; + } const SDL_Rect dest = {x, y, images[id].width, images[id].height}; if (SDL_RenderCopy(renderer, images[id].tex, NULL, &dest) < 0) { SDL_Log("%s", SDL_GetError()); @@ -769,6 +777,10 @@ int LZR_DrawImageEx(int id, int x, int y, LZR_ImageDrawSettings stg) SDL_Log("%s", SDL_GetError()); return -1; } + if (SDL_SetTextureAlphaMod(images[id].tex, color[3]) < 0) { + SDL_Log("%s", SDL_GetError()); + return -1; + } const int flip = (stg.flip_v ? SDL_FLIP_VERTICAL : 0) | (stg.flip_h ? SDL_FLIP_HORIZONTAL : 0); if (SDL_RenderCopyEx(renderer, images[id].tex, &src, &dst, @@ -803,6 +815,10 @@ int LZR_DrawTile(int id, int tile, int x, int y, double rot, int flip) SDL_Log("%s", SDL_GetError()); return -1; } + if (SDL_SetTextureAlphaMod(images[id].tex, color[3]) < 0) { + SDL_Log("%s", SDL_GetError()); + return -1; + } SDL_Rect src; src.x = (tile % img_width) * config.tile_size; src.y = (tile / img_width) * config.tile_size; diff --git a/lzr.h b/lzr.h index 12f2bb5..b12bde5 100644 --- a/lzr.h +++ b/lzr.h @@ -82,7 +82,7 @@ int LZR_ImageLoad(const char *path); int LZR_SoundLoad(const char *path, float volume); int LZR_DrawBegin(void); int LZR_DrawEnd(void); -int LZR_DrawSetColor(float r, float g, float b); +int LZR_DrawSetColor(float r, float g, float b, float a); int LZR_DrawClear(void); int LZR_DrawPoint(int x, int y); int LZR_DrawPoints(int *x, int *y, int n); -- cgit v1.2.3