aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorHarry Jeffery <harry@exec64.co.uk>2015-12-10 15:53:43 +0000
committerHarry Jeffery <harry@exec64.co.uk>2015-12-10 15:53:43 +0000
commitc73baf2e8637c8a0d10cff74d0ce1e818f540939 (patch)
treed42944564a47fbca24ee1d647c6625be1d558943 /src
parent3e1523d6a7c83d74673f5106ff87f7da465f467c (diff)
downloadimv-c73baf2e8637c8a0d10cff74d0ce1e818f540939.tar.gz
Move text rendering into a util function
Diffstat (limited to 'src')
-rw-r--r--src/main.c38
-rw-r--r--src/util.c29
-rw-r--r--src/util.h3
3 files changed, 41 insertions, 29 deletions
diff --git a/src/main.c b/src/main.c
index 9e5c171..92b7d40 100644
--- a/src/main.c
+++ b/src/main.c
@@ -42,9 +42,10 @@ struct {
unsigned char bg_g;
unsigned char bg_b;
int overlay;
+ char *overlay_str;
const char *start_at;
const char *font;
-} g_options = {0,0,0,0,0,1,0,0,0,0,0,0,NULL,"FreeMono:24"};
+} g_options = {0,0,0,0,0,1,0,0,0,0,0,0,NULL,NULL,"FreeMono:24"};
void print_usage(const char* name)
{
@@ -253,8 +254,6 @@ int main(int argc, char** argv)
if(!font) {
fprintf(stderr, "Error loading font: %s\n", TTF_GetError());
}
- SDL_Surface *overlay_surf = NULL;
- SDL_Texture *overlay_tex = NULL;
/* create our main classes on the stack*/
struct imv_loader ldr;
@@ -464,16 +463,6 @@ int main(int argc, char** argv)
/* only redraw when something's changed */
if(need_redraw) {
- /* make sure to free any memory used by the old image */
- if(overlay_surf) {
- SDL_FreeSurface(overlay_surf);
- overlay_surf = NULL;
- }
- if(overlay_tex) {
- SDL_DestroyTexture(overlay_tex);
- overlay_tex = NULL;
- }
-
/* update window title */
const char *current_path = imv_navigator_selection(&nav);
char title[256];
@@ -498,9 +487,10 @@ int main(int argc, char** argv)
snprintf(&title[0], sizeof(title), "[%i/%i] %s", nav.cur_path + 1,
nav.num_paths, current_path);
}
- SDL_Color w = {255,255,255,255};
- overlay_surf = TTF_RenderUTF8_Blended(font, &title[0], w);
- overlay_tex = SDL_CreateTextureFromSurface(renderer, overlay_surf);
+ if(g_options.overlay_str) {
+ free(g_options.overlay_str);
+ }
+ g_options.overlay_str = strdup(title);
}
/* first we draw the background */
@@ -529,13 +519,9 @@ int main(int argc, char** argv)
/* if the overlay needs to be drawn, draw that too */
if(g_options.overlay && font) {
- int ow, oh;
- SDL_QueryTexture(overlay_tex, NULL, NULL, &ow, &oh);
- SDL_Rect or = {0,0,ow,oh};
- SDL_SetRenderDrawColor(renderer, 0, 0, 0, 160);
- SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
- SDL_RenderFillRect(renderer, &or);
- SDL_RenderCopy(renderer, overlay_tex, NULL, &or);
+ SDL_Color fg = {255,255,255,255};
+ SDL_Color bg = {0,0,0,160};
+ imv_printf(renderer, font, 0, 0, &fg, &bg, "%s", g_options.overlay_str);
}
/* redraw complete, unset the flag */
@@ -566,12 +552,6 @@ int main(int argc, char** argv)
TTF_CloseFont(font);
}
TTF_Quit();
- if(overlay_surf) {
- SDL_FreeSurface(overlay_surf);
- }
- if(overlay_tex) {
- SDL_DestroyTexture(overlay_tex);
- }
SDL_DestroyTexture(chequered_tex);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
diff --git a/src/util.c b/src/util.c
index 3902157..0e1af92 100644
--- a/src/util.c
+++ b/src/util.c
@@ -126,3 +126,32 @@ int parse_hex_color(const char* str,
*b = (parse_hex_digit(str[4]) << 4) + parse_hex_digit(str[5]);
return 0;
}
+
+void imv_printf(SDL_Renderer *renderer, TTF_Font *font, int x, int y,
+ SDL_Color *fg, SDL_Color *bg, const char *fmt, ...)
+{
+ char line[512];
+ va_list args;
+ va_start(args, fmt);
+ vsnprintf(line, sizeof(line), fmt, args);
+
+ SDL_Surface *surf = TTF_RenderUTF8_Blended(font, &line[0], *fg);
+ SDL_Texture *tex = SDL_CreateTextureFromSurface(renderer, surf);
+
+ SDL_Rect tex_rect = {0,0,0,0};
+ SDL_QueryTexture(tex, NULL, NULL, &tex_rect.w, &tex_rect.h);
+ tex_rect.x = x;
+ tex_rect.y = y;
+
+ /* draw bg if wanted */
+ if(bg->a > 0) {
+ SDL_SetRenderDrawColor(renderer, bg->r, bg->g, bg->b, bg->a);
+ SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
+ SDL_RenderFillRect(renderer, &tex_rect);
+ }
+ SDL_RenderCopy(renderer, tex, NULL, &tex_rect);
+
+ SDL_DestroyTexture(tex);
+ SDL_FreeSurface(surf);
+ va_end(args);
+}
diff --git a/src/util.h b/src/util.h
index fbbd686..760b375 100644
--- a/src/util.h
+++ b/src/util.h
@@ -31,4 +31,7 @@ int parse_hex_color(const char* str,
/* Loads a font using SDL2_ttf given a spec in the format "name:size" */
TTF_Font *load_font(const char *font_spec);
+void imv_printf(SDL_Renderer *renderer, TTF_Font *font, int x, int y,
+ SDL_Color *fg, SDL_Color *bg, const char *fmt, ...);
+
#endif