diff options
author | Harry Jeffery <harry@exec64.co.uk> | 2015-11-05 23:12:55 +0000 |
---|---|---|
committer | Harry Jeffery <harry@exec64.co.uk> | 2015-11-05 23:12:55 +0000 |
commit | 54d323b5b66d766b636561317d4e139c79ad3fc9 (patch) | |
tree | 1fadefd2ac72eab5624a15630cd9b5e42400ca5b | |
parent | e830829075ab0dab0cb55e7390e8cb645b354152 (diff) | |
download | imv-54d323b5b66d766b636561317d4e139c79ad3fc9.tar.gz |
Add click-drag and mousewheel navigation.
-rw-r--r-- | main.c | 41 |
1 files changed, 35 insertions, 6 deletions
@@ -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); |