[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;
c.a = 255;
c.a = alpha;
c.r = (col & 0xff0000) >> 16;
c.g = (col & 0x00ff00) >> 8;
c.b = (col & 0x0000ff) >> 0;
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);
}
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);
}
static void draw_glyph(SDL_Renderer* r, const struct ax_glyph* gl)
{
SDL_Rect rect;
switch (gl->ty) {
case AX_GLYPH_RECT:
draw_set_color(r, gl->d.rect.f);
SDL_RenderFillRect(r, &(SDL_Rect) {
.x = gl->d.rect.x,
.y = gl->d.rect.y,
.w = gl->d.rect.w,
.h = gl->d.rect.h
});
draw_set_color(r, to_color(gl->d.rect.f, 0xff));
rect.x = gl->d.rect.x;
rect.y = gl->d.rect.y;
rect.w = gl->d.rect.w;
rect.h = gl->d.rect.h;
SDL_RenderFillRect(r, &rect);
break;
case AX_GLYPH_TEXT: {
SDL_Surface* surf =
TTF_RenderText_Blended(
gl->d.text.h->font,
gl->d.text.t,
uint64_to_color(gl->d.text.f));
SDL_Texture* text =
SDL_CreateTextureFromSurface(r, surf);
SDL_Rect dst;
dst.x = gl->d.text.x;
dst.y = gl->d.text.y;
dst.w = surf->w;
dst.h = surf->h;
SDL_RenderCopy(r, text, NULL, &dst);
SDL_Surface* surf = TTF_RenderText_Blended(
gl->d.text.h->font,
gl->d.text.t,
to_color(gl->d.text.f, 0xff));
SDL_Texture* text = SDL_CreateTextureFromSurface(r, surf);
rect.x = gl->d.text.x;
rect.y = gl->d.text.y;
rect.w = surf->w;
rect.h = surf->h;
SDL_RenderCopy(r, text, NULL, &rect);
SDL_DestroyTexture(text);
SDL_FreeSurface(surf);
break;