aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHarry Jeffery <harry@exec64.co.uk>2019-06-12 01:36:20 +0100
committerHarry Jeffery <harry@exec64.co.uk>2019-06-12 01:43:14 +0100
commit4d965ab261c4226f3f99c557a44cb69c38d44863 (patch)
tree6c122c6282d2be314963ef64bce20ef351bf7f23
parentdb991685deeff00073dafba885bd729ee359950b (diff)
downloadimv-4d965ab261c4226f3f99c557a44cb69c38d44863.tar.gz
Add support for hidpi rendering
This fixes #148 but only when used with a version of SDL that includes this patch: https://bugzilla.libsdl.org/show_bug.cgi?id=4521
-rw-r--r--src/imv.c4
-rw-r--r--src/viewport.c23
-rw-r--r--src/viewport.h2
3 files changed, 21 insertions, 8 deletions
diff --git a/src/imv.c b/src/imv.c
index 84fc839..8567e38 100644
--- a/src/imv.c
+++ b/src/imv.c
@@ -964,7 +964,7 @@ static bool setup_window(struct imv *imv)
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
imv->initial_width, imv->initial_height,
- SDL_WINDOW_RESIZABLE);
+ SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
if(!imv->window) {
fprintf(stderr, "SDL Failed to create window: %s\n", SDL_GetError());
@@ -1001,7 +1001,7 @@ static bool setup_window(struct imv *imv)
}
imv->image = imv_image_create(imv->renderer);
- imv->view = imv_viewport_create(imv->window);
+ imv->view = imv_viewport_create(imv->window, imv->renderer);
/* put us in fullscren mode to begin with if requested */
if(imv->fullscreen) {
diff --git a/src/viewport.c b/src/viewport.c
index 3d3251b..1ecbea4 100644
--- a/src/viewport.c
+++ b/src/viewport.c
@@ -2,6 +2,7 @@
struct imv_viewport {
SDL_Window *window;
+ SDL_Renderer *renderer;
double scale;
int x, y;
int fullscreen;
@@ -10,10 +11,20 @@ struct imv_viewport {
int locked;
};
-struct imv_viewport *imv_viewport_create(SDL_Window *window)
+static void input_xy_to_render_xy(struct imv_viewport *view, int *x, int *y)
+{
+ int ww, wh, rw, rh;
+ SDL_GetWindowSize(view->window, &ww, &wh);
+ SDL_GetRendererOutputSize(view->renderer, &rw, &rh);
+ *x *= rw / ww;
+ *y *= rh / wh;
+}
+
+struct imv_viewport *imv_viewport_create(SDL_Window *window, SDL_Renderer *renderer)
{
struct imv_viewport *view = malloc(sizeof *view);
view->window = window;
+ view->renderer = renderer;
view->scale = 1;
view->x = view->y = view->fullscreen = view->redraw = 0;
view->playing = 1;
@@ -80,6 +91,7 @@ void imv_viewport_get_scale(struct imv_viewport *view, double *scale)
void imv_viewport_move(struct imv_viewport *view, int x, int y,
const struct imv_image *image)
{
+ input_xy_to_render_xy(view, &x, &y);
view->x += x;
view->y += y;
view->redraw = 1;
@@ -87,7 +99,7 @@ void imv_viewport_move(struct imv_viewport *view, int x, int y,
int w = (int)(imv_image_width(image) * view->scale);
int h = (int)(imv_image_height(image) * view->scale);
int ww, wh;
- SDL_GetWindowSize(view->window, &ww, &wh);
+ SDL_GetRendererOutputSize(view->renderer, &ww, &wh);
if (view->x < -w) {
view->x = -w;
}
@@ -106,7 +118,7 @@ void imv_viewport_zoom(struct imv_viewport *view, const struct imv_image *image,
{
double prev_scale = view->scale;
int x, y, ww, wh;
- SDL_GetWindowSize(view->window, &ww, &wh);
+ SDL_GetRendererOutputSize(view->renderer, &ww, &wh);
const int image_width = imv_image_width(image);
const int image_height = imv_image_height(image);
@@ -114,6 +126,7 @@ void imv_viewport_zoom(struct imv_viewport *view, const struct imv_image *image,
/* x and y cordinates are relative to the image */
if(src == IMV_ZOOM_MOUSE) {
SDL_GetMouseState(&x, &y);
+ input_xy_to_render_xy(view, &x, &y);
x -= view->x;
y -= view->y;
} else {
@@ -168,7 +181,7 @@ void imv_viewport_zoom(struct imv_viewport *view, const struct imv_image *image,
void imv_viewport_center(struct imv_viewport *view, const struct imv_image *image)
{
int ww, wh;
- SDL_GetWindowSize(view->window, &ww, &wh);
+ SDL_GetRendererOutputSize(view->renderer, &ww, &wh);
const int image_width = imv_image_width(image);
const int image_height = imv_image_height(image);
@@ -183,7 +196,7 @@ void imv_viewport_center(struct imv_viewport *view, const struct imv_image *imag
void imv_viewport_scale_to_window(struct imv_viewport *view, const struct imv_image *image)
{
int ww, wh;
- SDL_GetWindowSize(view->window, &ww, &wh);
+ SDL_GetRendererOutputSize(view->renderer, &ww, &wh);
const int image_width = imv_image_width(image);
const int image_height = imv_image_height(image);
diff --git a/src/viewport.h b/src/viewport.h
index fd7a6cf..471d2a5 100644
--- a/src/viewport.h
+++ b/src/viewport.h
@@ -14,7 +14,7 @@ enum imv_zoom_source {
};
/* Creates an instance of imv_viewport */
-struct imv_viewport *imv_viewport_create(SDL_Window *window);
+struct imv_viewport *imv_viewport_create(SDL_Window *window, SDL_Renderer *renderer);
/* Cleans up an imv_viewport instance */
void imv_viewport_free(struct imv_viewport *view);