[sdl] open the opportunity to set color alpha in drawing

This commit is contained in:
Milo Turner 2020-03-06 13:25:19 -05:00
parent 7d2c536d82
commit 5edb05d484
1 changed files with 21 additions and 25 deletions

View File

@ -330,55 +330,51 @@ static void render_windows(struct window_list* wl)
} }
} }
static SDL_Color uint64_to_color(uint64_t col) static inline SDL_Color to_color(uint64_t col, uint8_t alpha)
{ {
SDL_Color c; SDL_Color c;
c.a = 255; c.a = alpha;
c.r = (col & 0xff0000) >> 16; c.r = (col & 0xff0000) >> 16;
c.g = (col & 0x00ff00) >> 8; c.g = (col & 0x00ff00) >> 8;
c.b = (col & 0x0000ff) >> 0; c.b = (col & 0x0000ff) >> 0;
return c; return c;
} }
static void draw_set_color(SDL_Renderer* r, uint64_t col_u64) static void draw_set_color(SDL_Renderer* r, SDL_Color col)
{ {
SDL_Color col = uint64_to_color(col_u64);
SDL_SetRenderDrawColor(r, col.r, col.g, col.b, col.a); SDL_SetRenderDrawColor(r, col.r, col.g, col.b, col.a);
} }
static void draw_clear(SDL_Renderer* r, uint64_t bg) static void draw_clear(SDL_Renderer* r, uint64_t bg)
{ {
draw_set_color(r, bg); draw_set_color(r, to_color(bg, 0xff));
SDL_RenderClear(r); SDL_RenderClear(r);
} }
static void draw_glyph(SDL_Renderer* r, const struct ax_glyph* gl) static void draw_glyph(SDL_Renderer* r, const struct ax_glyph* gl)
{ {
SDL_Rect rect;
switch (gl->ty) { switch (gl->ty) {
case AX_GLYPH_RECT: case AX_GLYPH_RECT:
draw_set_color(r, gl->d.rect.f); draw_set_color(r, to_color(gl->d.rect.f, 0xff));
SDL_RenderFillRect(r, &(SDL_Rect) { rect.x = gl->d.rect.x;
.x = gl->d.rect.x, rect.y = gl->d.rect.y;
.y = gl->d.rect.y, rect.w = gl->d.rect.w;
.w = gl->d.rect.w, rect.h = gl->d.rect.h;
.h = gl->d.rect.h SDL_RenderFillRect(r, &rect);
});
break; break;
case AX_GLYPH_TEXT: { case AX_GLYPH_TEXT: {
SDL_Surface* surf = SDL_Surface* surf = TTF_RenderText_Blended(
TTF_RenderText_Blended( gl->d.text.h->font,
gl->d.text.h->font, gl->d.text.t,
gl->d.text.t, to_color(gl->d.text.f, 0xff));
uint64_to_color(gl->d.text.f)); SDL_Texture* text = SDL_CreateTextureFromSurface(r, surf);
SDL_Texture* text = rect.x = gl->d.text.x;
SDL_CreateTextureFromSurface(r, surf); rect.y = gl->d.text.y;
SDL_Rect dst; rect.w = surf->w;
dst.x = gl->d.text.x; rect.h = surf->h;
dst.y = gl->d.text.y; SDL_RenderCopy(r, text, NULL, &rect);
dst.w = surf->w;
dst.h = surf->h;
SDL_RenderCopy(r, text, NULL, &dst);
SDL_DestroyTexture(text); SDL_DestroyTexture(text);
SDL_FreeSurface(surf); SDL_FreeSurface(surf);
break; break;