From 54d323b5b66d766b636561317d4e139c79ad3fc9 Mon Sep 17 00:00:00 2001 From: Harry Jeffery Date: Thu, 5 Nov 2015 23:12:55 +0000 Subject: Add click-drag and mousewheel navigation. --- main.c | 41 +++++++++++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/main.c b/main.c index 2988e12..9174765 100644 --- a/main.c +++ b/main.c @@ -3,6 +3,14 @@ SDL_Texture* imv_load_image(SDL_Renderer *r, const char* path); +double clamp(double v, double min, double max) { + if(v < min) + return min; + if(v > max) + return max; + return v; +} + int main(int argc, char** argv) { if(argc < 2) { @@ -34,7 +42,10 @@ int main(int argc, char** argv) int next_path = 0; SDL_Texture *img = NULL; - double scale = 1; + struct { + double scale; + int x, y; + } view = {1,0,0}; int quit = 0; int redraw = 1; while(!quit) { @@ -59,15 +70,26 @@ int main(int argc, char** argv) next_path = num_paths - 1; break; case SDLK_UP: - scale = (scale + 0.1 < 10) ? scale + 0.1 : scale; + view.scale = clamp(view.scale + 0.1, 1, 10); redraw = 1; break; case SDLK_DOWN: - scale = (scale - 0.1 >= 1) ? scale - 0.1 : scale; + view.scale = clamp(view.scale - 0.1, 1, 10); redraw = 1; break; } break; + case SDL_MOUSEWHEEL: + view.scale = clamp(view.scale + (0.1 * e.wheel.y), 1, 10); + redraw = 1; + break; + case SDL_MOUSEMOTION: + if(e.motion.state & SDL_BUTTON_LMASK) { + view.x += e.motion.xrel; + view.y += e.motion.yrel; + redraw = 1; + } + break; case SDL_WINDOWEVENT: redraw = 1; break; @@ -86,7 +108,9 @@ int main(int argc, char** argv) img = NULL; } img = imv_load_image(renderer, paths[cur_path]); - scale = 1; + view.scale = 1; + view.x = 0; + view.y = 0; redraw = 1; } @@ -97,8 +121,13 @@ int main(int argc, char** argv) int img_w, img_h, img_access; unsigned int img_format; SDL_QueryTexture(img, &img_format, &img_access, &img_w, &img_h); - SDL_Rect area = {0,0,img_w*scale,img_h*scale}; - SDL_RenderCopy(renderer, img, &area, &area); + SDL_Rect view_area = { + view.x, + view.y, + img_w * view.scale, + img_h * view.scale + }; + SDL_RenderCopy(renderer, img, NULL, &view_area); } SDL_RenderPresent(renderer); -- cgit v1.2.3