Add support for chequered/colored backgrounds

This commit is contained in:
Harry Jeffery 2015-11-14 16:33:56 +00:00
parent f4b81bd693
commit 84f325bc63
2 changed files with 67 additions and 6 deletions

View file

@ -26,13 +26,19 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include "navigator.h" #include "navigator.h"
#include "viewport.h" #include "viewport.h"
SDL_Texture *create_chequered(SDL_Renderer *renderer);
struct { struct {
int fullscreen; int fullscreen;
int stdin; int stdin;
int recursive; int recursive;
int actual; int actual;
int start_at; 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) void print_usage(const char* name)
{ {
@ -187,6 +193,9 @@ int main(int argc, char** argv)
/* Use linear sampling for scaling */ /* Use linear sampling for scaling */
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1"); SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1");
/* construct a chequered background texture */
SDL_Texture *chequered_tex = create_chequered(renderer);
struct imv_image img; struct imv_image img;
imv_init_image(&img); imv_init_image(&img);
@ -297,7 +306,23 @@ int main(int argc, char** argv)
} }
if(view.redraw) { if(view.redraw) {
if(g_options.solid_bg) {
SDL_SetRenderDrawColor(renderer,
g_options.bg_r, g_options.bg_g, g_options.bg_b, 255);
SDL_RenderClear(renderer); 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); imv_texture_draw(&tex, view.x, view.y, view.scale);
view.redraw = 0; view.redraw = 0;
SDL_RenderPresent(renderer); SDL_RenderPresent(renderer);
@ -310,9 +335,46 @@ int main(int argc, char** argv)
imv_destroy_navigator(&nav); imv_destroy_navigator(&nav);
imv_destroy_viewport(&view); imv_destroy_viewport(&view);
SDL_DestroyTexture(chequered_tex);
SDL_DestroyRenderer(renderer); SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window); SDL_DestroyWindow(window);
SDL_Quit(); SDL_Quit();
return 0; 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;
}

View file

@ -125,10 +125,9 @@ void imv_texture_draw(struct imv_texture *tex, int bx, int by, double scale)
int offset_y = 0; int offset_y = 0;
for(int y = 0; y < tex->num_chunks_tall; ++y) { for(int y = 0; y < tex->num_chunks_tall; ++y) {
for(int x = 0; x < tex->num_chunks_wide; ++x) { for(int x = 0; x < tex->num_chunks_wide; ++x) {
int img_w, img_h, img_access; int img_w, img_h;
unsigned int img_format; SDL_QueryTexture(tex->chunks[x + y * tex->num_chunks_wide], NULL, NULL,
SDL_QueryTexture(tex->chunks[x + y * tex->num_chunks_wide], &img_w, &img_h);
&img_format, &img_access, &img_w, &img_h);
SDL_Rect view_area = { SDL_Rect view_area = {
bx + offset_x, bx + offset_x,
by + offset_y, by + offset_y,