aboutsummaryrefslogtreecommitdiff
path: root/src/viewport.c
diff options
context:
space:
mode:
authorHarry Jeffery <harry@exec64.co.uk>2015-11-11 15:22:41 +0000
committerHarry Jeffery <harry@exec64.co.uk>2015-11-11 15:22:41 +0000
commit8b527949b0a74a37079210a6fc8fabbd576432eb (patch)
treecfa44059678e1ec1b13a700b325249856437e182 /src/viewport.c
parentd1f0e07abd9bbb72ca825fe8e2fe9fa12cc6746f (diff)
downloadimv-8b527949b0a74a37079210a6fc8fabbd576432eb.tar.gz
Update makefile to use a build directory
Diffstat (limited to 'src/viewport.c')
-rw-r--r--src/viewport.c119
1 files changed, 119 insertions, 0 deletions
diff --git a/src/viewport.c b/src/viewport.c
new file mode 100644
index 0000000..1630604
--- /dev/null
+++ b/src/viewport.c
@@ -0,0 +1,119 @@
+/* Copyright (c) 2015 Harry Jeffery
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal in
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software is furnished to do so,
+subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
+COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
+
+#include "viewport.h"
+#include "image.h"
+
+void imv_init_viewport(struct imv_viewport *view, SDL_Window *window)
+{
+ view->window = window;
+ view->scale = 1;
+ view->x = view->y = view->fullscreen = view->redraw = 0;
+ view->playing = 1;
+}
+
+void imv_destroy_viewport(struct imv_viewport *view)
+{
+ view->window = NULL;
+ return;
+}
+
+void imv_viewport_toggle_fullscreen(struct imv_viewport *view)
+{
+ if(view->fullscreen) {
+ SDL_SetWindowFullscreen(view->window, 0);
+ view->fullscreen = 0;
+ } else {
+ SDL_SetWindowFullscreen(view->window, SDL_WINDOW_FULLSCREEN_DESKTOP);
+ view->fullscreen = 1;
+ }
+}
+
+void imv_viewport_toggle_playing(struct imv_viewport *view, struct imv_image *img)
+{
+ if(view->playing) {
+ view->playing = 0;
+ } else if(imv_image_is_animated(img)) {
+ view->playing = 1;
+ }
+}
+
+void imv_viewport_reset(struct imv_viewport *view)
+{
+ view->scale = 1;
+ view->x = view->y = 0;
+ view->redraw = 1;
+}
+
+void imv_viewport_move(struct imv_viewport *view, int x, int y)
+{
+ view->x += x;
+ view->y += y;
+ view->redraw = 1;
+}
+
+void imv_viewport_zoom(struct imv_viewport *view, int amount)
+{
+ view->scale += amount * 0.1;
+ if(view->scale > 100)
+ view->scale = 10;
+ else if (view->scale < 0.01)
+ view->scale = 0.1;
+ view->redraw = 1;
+}
+
+void imv_viewport_center(struct imv_viewport *view, const struct imv_image* img)
+{
+ int ww, wh;
+ SDL_GetWindowSize(view->window, &ww, &wh);
+
+ view->x = (ww - img->width * view->scale) / 2;
+ view->y = (wh - img->height * view->scale) / 2;
+
+ view->redraw = 1;
+}
+
+void imv_viewport_scale_to_window(struct imv_viewport *view, const struct imv_image* img)
+{
+ int ww, wh;
+ SDL_GetWindowSize(view->window, &ww, &wh);
+
+ double window_aspect = (double)ww / (double)wh;
+ double image_aspect = (double)img->width / (double)img->height;
+
+ if(window_aspect > image_aspect) {
+ /* Image will become too tall before it becomes too wide */
+ view->scale = (double)wh / (double)img->height;
+ } else {
+ /* Image will become too wide before it becomes too tall */
+ view->scale = (double)ww / (double)img->width;
+ }
+
+ imv_viewport_center(view, img);
+}
+
+void imv_viewport_set_redraw(struct imv_viewport *view)
+{
+ view->redraw = 1;
+}
+
+void imv_viewport_set_title(struct imv_viewport *view, char* title)
+{
+ SDL_SetWindowTitle(view->window, title);
+}