From 84f325bc630e886f6b9abbab9b204c01e37c5a60 Mon Sep 17 00:00:00 2001 From: Harry Jeffery Date: Sat, 14 Nov 2015 16:33:56 +0000 Subject: Add support for chequered/colored backgrounds --- src/main.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- src/texture.c | 7 +++---- 2 files changed, 67 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/main.c b/src/main.c index 16f675f..aa364a7 100644 --- a/src/main.c +++ b/src/main.c @@ -26,13 +26,19 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include "navigator.h" #include "viewport.h" +SDL_Texture *create_chequered(SDL_Renderer *renderer); + struct { int fullscreen; int stdin; int recursive; int actual; int start_at; -} g_options = {0,0,0,0,0}; + int solid_bg; + int bg_r; + int bg_g; + int bg_b; +} g_options = {0,0,0,0,0,0,0,0,0}; void print_usage(const char* name) { @@ -187,6 +193,9 @@ int main(int argc, char** argv) /* Use linear sampling for scaling */ SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1"); + /* construct a chequered background texture */ + SDL_Texture *chequered_tex = create_chequered(renderer); + struct imv_image img; imv_init_image(&img); @@ -297,7 +306,23 @@ int main(int argc, char** argv) } if(view.redraw) { - SDL_RenderClear(renderer); + if(g_options.solid_bg) { + SDL_SetRenderDrawColor(renderer, + g_options.bg_r, g_options.bg_g, g_options.bg_b, 255); + SDL_RenderClear(renderer); + } else { + /* chequered background */ + int ww, wh; + SDL_GetWindowSize(window, &ww, &wh); + int img_w, img_h; + SDL_QueryTexture(chequered_tex, NULL, NULL, &img_w, &img_h); + for(int y = 0; y < wh; y += img_h) { + for(int x = 0; x < ww; x += img_w) { + SDL_Rect dst_rect = {x,y,img_w,img_h}; + SDL_RenderCopy(renderer, chequered_tex, NULL, &dst_rect); + } + } + } imv_texture_draw(&tex, view.x, view.y, view.scale); view.redraw = 0; SDL_RenderPresent(renderer); @@ -310,9 +335,46 @@ int main(int argc, char** argv) imv_destroy_navigator(&nav); imv_destroy_viewport(&view); + SDL_DestroyTexture(chequered_tex); SDL_DestroyRenderer(renderer); SDL_DestroyWindow(window); SDL_Quit(); return 0; } + +SDL_Texture *create_chequered(SDL_Renderer *renderer) +{ + SDL_RendererInfo ri; + SDL_GetRendererInfo(renderer, &ri); + int width = 512; + int height = 512; + if(ri.max_texture_width != 0 && ri.max_texture_width < width) { + width = ri.max_texture_width; + } + if(ri.max_texture_height != 0 && ri.max_texture_height < height) { + height = ri.max_texture_height; + } + const int box_size = 16; + /* Create a chequered texture */ + const unsigned char l = 196; + const unsigned char d = 96; + + size_t pixels_len = 3 * width * height; + unsigned char *pixels = malloc(pixels_len); + for(int y = 0; y < height; y++) { + for(int x = 0; x < width; x += box_size) { + unsigned char color = l; + if(((x/box_size) % 2 == 0) ^ ((y/box_size) % 2 == 0)) { + color = d; + } + memset(pixels + 3 * x + 3 * width * y, color, 3 * box_size); + } + } + SDL_Texture *ret = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGB24, + SDL_TEXTUREACCESS_STATIC, + width, height); + SDL_UpdateTexture(ret, NULL, pixels, 3 * width); + free(pixels); + return ret; +} diff --git a/src/texture.c b/src/texture.c index ea478af..a882320 100644 --- a/src/texture.c +++ b/src/texture.c @@ -125,10 +125,9 @@ void imv_texture_draw(struct imv_texture *tex, int bx, int by, double scale) int offset_y = 0; for(int y = 0; y < tex->num_chunks_tall; ++y) { for(int x = 0; x < tex->num_chunks_wide; ++x) { - int img_w, img_h, img_access; - unsigned int img_format; - SDL_QueryTexture(tex->chunks[x + y * tex->num_chunks_wide], - &img_format, &img_access, &img_w, &img_h); + int img_w, img_h; + SDL_QueryTexture(tex->chunks[x + y * tex->num_chunks_wide], NULL, NULL, + &img_w, &img_h); SDL_Rect view_area = { bx + offset_x, by + offset_y, -- cgit v1.2.3