Fix viewport resize and cleanup code structure of viewport width and height
authorAndreas Eversberg <jolly@eversberg.eu>
Sun, 15 Apr 2018 05:33:20 +0000 (07:33 +0200)
committerAndreas Eversberg <jolly@eversberg.eu>
Sun, 22 Apr 2018 10:02:45 +0000 (12:02 +0200)
src/libovr/ovr.c
src/libsdl/opengl.c
src/libsdl/opengl.h
src/libsdl/sdl.c
src/libsdl/sdl.h
src/mercenary/main.c

index 62c2a03..0bc8eb9 100755 (executable)
@@ -292,16 +292,37 @@ void end_render_ovr(void)
 
 void render_mirror_ovr(int view_width, int view_height)
 {
+       int view_x = 0, view_y = 0;
+       int new_width, new_height;
        int eye = 0; /* left eye */
        int curIndex;
        GLuint chainTexId;
 
+       /* avoid division by zero, if one dimension is too small */
+       if (view_width < 1 || view_height < 1)
+               return;
+
+       /* calculate a viewport that has apect of TextureSize */
+       if (view_height * TextureSize[eye].w > view_width * TextureSize[eye].h) {
+               new_height = view_width * TextureSize[eye].h / TextureSize[eye].w;
+               view_y = view_y + view_height / 2 - new_height / 2;
+               view_height = new_height;
+       } else if (view_height * TextureSize[eye].w < view_width * TextureSize[eye].h) {
+               new_width = view_height * TextureSize[eye].w / TextureSize[eye].h;
+               view_x = view_x + view_width / 2 - new_width / 2;
+               view_width = new_width;
+       }
+
+       /* avoid views that are too small */
+       if (view_width < 1 || view_height < 1)
+               return;
+
        /* clear screen */
-       glClearColor(1.0, 0.0, 0.0, 1.0);
+       glClearColor(0.0, 0.0, 0.0, 1.0);
        glClear(GL_COLOR_BUFFER_BIT);
 
        /* orthogonal viewport */
-       glViewport((GLsizei)0, (GLsizei)0, (GLsizei)view_width, (GLsizei)view_height);
+       glViewport((GLsizei)view_x, (GLsizei)view_y, (GLsizei)view_width, (GLsizei)view_height);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
index 9a6578c..b87e12b 100644 (file)
@@ -34,7 +34,6 @@ static uint8_t *image_rgb = NULL;
 static uint8_t *osd_rgba[MAX_OSD] = { NULL, NULL };
 static GLuint image_name;
 static GLuint osd_name[MAX_OSD];
-static int screen_width, screen_height;
 static int image_width, image_height;
 static int osd_width[MAX_OSD], osd_height[MAX_OSD];
 static int texture_size;
@@ -112,13 +111,6 @@ error:
        return rc;
 }
 
-/* set clip planes so that the image portion of the image texture is centered and pixels are rectengular */
-void resize_opengl(int _screen_width, int _screen_height)
-{
-       screen_width = _screen_width;
-       screen_height = _screen_height;
-}
-
 void opengl_clear(int _flip_y)
 {
        flip_y = _flip_y;
@@ -129,28 +121,17 @@ void opengl_clear(int _flip_y)
 }
 
 /* set viewport for improved rendering */
-void opengl_viewport(int split, int benson_at_line, double fov, double benson_size)
+void opengl_viewport(int view_width, int view_height, int split, int benson_at_line, double fov, double benson_size)
 {
-       int view_x, view_y;
-       int view_width, view_height;
+       int view_x = 0, view_y = 0;
        int new_width, new_height;
 
        /* center view, or put it in the top half of the window */
        if (split == 1) {
-               view_x = 0;
-               view_y = screen_height / 2;
-               view_width = screen_width;
-               view_height = screen_height / 2;
+               view_y = view_height / 2;
+               view_height = view_height / 2;
        } else if (split == 2) {
-               view_x = 0;
-               view_y = 0;
-               view_width = screen_width;
-               view_height = screen_height / 2;
-       } else {
-               view_x = 0;
-               view_y = 0;
-               view_width = screen_width;
-               view_height = screen_height;
+               view_height = view_height / 2;
        }
 
        /* avoid division by zero, if one dimension is too small */
index 6a731a9..b3834b6 100644 (file)
@@ -1,9 +1,8 @@
 
 int init_opengl_image(int _image_width, int _image_height);
 int init_opengl_osd(int num, int _osd_width, int _osd_height);
-void resize_opengl(int _screen_width, int _screen_height);
 void opengl_clear(int flip_y);
-void opengl_viewport(int split, int benson_at_line, double fov, double benson_size); 
+void opengl_viewport(int view_width, int view_height, int split, int benson_at_line, double fov, double benson_size); 
 void opengl_blit_image(uint8_t *rgb, int filter, int benson_at_line, int render_benson_only, double fov, double monitor_distance, double benson_size);
 void opengl_blit_osd(int num, uint8_t *rgba, int filter, int benson_at_line, double fov, double monitor_distnace, double benson_size, double scale_x, double scale_y, double offset_x, double offset_y);
 void opengl_render_color(double r, double g, double b, double a);
index b3d78b7..d7e96ce 100644 (file)
@@ -34,6 +34,7 @@ static SDL_Window *gl_window = NULL;
 static SDL_GLContext gl_context = NULL;
 static void (*keyboard_sdl)(int down, enum keycode keycode) = NULL;
 static void (*audio_sdl)(float *data, int len) = NULL;
+static void (*resize_window_sdl)(int width, int height) = NULL;
 
 static void audio_cb(void __attribute__((unused)) *userdata, Uint8 *stream, int len)
 {
@@ -44,12 +45,13 @@ static void audio_cb(void __attribute__((unused)) *userdata, Uint8 *stream, int
        SDL_MixAudio(stream, (Uint8 *)audio_data, len, SDL_MIX_MAXVOLUME);
 }
 
-int init_sdl(const char *progname, int width, int height, int sound_samplerate, int sound_chunk, void (*keyboard)(int down, enum keycode keycode), void (*audio)(float *data, int len), int multisampling, int vbl_sync)
+int init_sdl(const char *progname, int width, int height, int sound_samplerate, int sound_chunk, void (*keyboard)(int down, enum keycode keycode), void (*audio)(float *data, int len), void (*resize_window)(int width, int height), int multisampling, int vbl_sync)
 {
        int rc;
 
        keyboard_sdl = keyboard;
        audio_sdl = audio;
+       resize_window_sdl = resize_window;
 
        /* init SDL library */
        rc = SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO);
@@ -268,7 +270,7 @@ int event_sdl(void)
                        quit = 1;
                if (event.type == SDL_WINDOWEVENT) {
                        if (event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED)
-                               resize_opengl(event.window.data1, event.window.data2);
+                               resize_window_sdl(event.window.data1, event.window.data2);
                }
                if (event.type == SDL_KEYDOWN) {
                        switch (event.key.keysym.sym) {
index 83882d8..e731599 100644 (file)
@@ -82,7 +82,7 @@ enum keycode {
        KEYCODE_INSERT,
 };
 
-int init_sdl(const char *progname, int width, int height, int sound_samplerate, int sound_chunk, void (*keyboard)(int down, enum keycode keycode), void (*audio)(float *data, int len), int multisampling, int vbl_sync);
+int init_sdl(const char *progname, int width, int height, int sound_samplerate, int sound_chunk, void (*keyboard)(int down, enum keycode keycode), void (*audio)(float *data, int len), void (*resize_window)(int width, int height), int multisampling, int vbl_sync);
 int event_sdl(void);
 void swap_sdl(void);
 void exit_sdl(void);
index 8be3591..62beb7e 100644 (file)
@@ -93,7 +93,7 @@ static uint8_t *info_osd = NULL;
 static int help_view = 1;
 static int32_t osd_timer = 0;
 #ifdef HAVE_OVR
-#define SCREEN_WIDTH   800
+#define SCREEN_WIDTH   672
 #define SCREEN_HEIGHT  800
 #else
 #define SCREEN_WIDTH   (320*3)
@@ -117,6 +117,7 @@ static int render_legacy = 0; /* render original amiga screen, if set */
 static int render_improved = 0; /* render improved image, if set */
 static int debug_opengl = 0; /* render both, amiga screen and  improved image, if set */
 static int intro_skipped = 0; /* indicated if we already have landed */
+static int window_width, window_height;
 
 static const char *home_dir;
 
@@ -267,6 +268,12 @@ illegal_parameter:
        return 0;
 }
 
+static void resize_window(int width, int height)
+{
+       window_width = width;
+       window_height = height;
+}
+
 static void skip_intro(void)
 {
        int event;
@@ -403,7 +410,7 @@ static void main_loop(void)
                        if (render_improved) {
 #ifndef HAVE_OVR
                                /* viewport and frustum is set here */
-                               opengl_viewport((debug_opengl) ? 2 : 0, (double_pixel_size) ? BENSON_AT_LINE * 2 : BENSON_AT_LINE, config_fov, benson_size);
+                               opengl_viewport(window_width, window_height, (debug_opengl) ? 2 : 0, (double_pixel_size) ? BENSON_AT_LINE * 2 : BENSON_AT_LINE, config_fov, benson_size);
 #endif
                                /* render improved graphics, interpolate, if required,
                                 * if no item list is available, for legacy rendering
@@ -435,7 +442,7 @@ static void main_loop(void)
                                        emul_video(image, memory, palette, IMAGE_WIDTH, IMAGE_HEIGHT, IMAGE_DIWSTART, chipreg, 0, BENSON_AT_LINE, double_pixel_size);
 #ifndef HAVE_OVR
                                /* viewport and frustum is set here */
-                               opengl_viewport((debug_opengl) ? 1 : 0, (double_pixel_size) ? BENSON_AT_LINE * 2 : BENSON_AT_LINE, config_fov, 1.0);
+                               opengl_viewport(window_width, window_height, (debug_opengl) ? 1 : 0, (double_pixel_size) ? BENSON_AT_LINE * 2 : BENSON_AT_LINE, config_fov, 1.0);
 #endif
                                opengl_blit_image(image, config_video_filter, (double_pixel_size) ? BENSON_AT_LINE * 2 : BENSON_AT_LINE, 0, config_fov, config_monitor_distance, 1.0);
                                if (help_view)
@@ -451,7 +458,7 @@ static void main_loop(void)
                }
                /* at this point we are ready with our image, so we display */
                end_render_ovr();
-               render_mirror_ovr(SCREEN_WIDTH, SCREEN_HEIGHT);
+               render_mirror_ovr(window_width, window_height);
 #else
                }
 #endif
@@ -996,10 +1003,14 @@ int main(int argc, char *argv[])
        /* init SDL and OpenGL */
 #ifdef HAVE_OVR
        int vbl_sync = 0;
+       window_width = SCREEN_WIDTH;
+       window_height = SCREEN_HEIGHT;
 #else
        int vbl_sync = 1;
+       window_width = (config_debug_opengl) ? SCREEN_WIDTH / 3 * 2 : SCREEN_WIDTH;
+       window_height = (config_debug_opengl) ? SCREEN_HEIGHT / 3 * 4 : SCREEN_HEIGHT;
 #endif
-       rc = init_sdl(argv[0], (config_debug_opengl) ? SCREEN_WIDTH / 3 * 2 : SCREEN_WIDTH, (config_debug_opengl) ? SCREEN_HEIGHT / 3 * 4 : SCREEN_HEIGHT, SOUND_SAMPLERATE, sdl_sound_chunk, keyboard_sdl, audio_sdl, config_multisampling, vbl_sync);
+       rc = init_sdl(argv[0], window_width, window_height, SOUND_SAMPLERATE, sdl_sound_chunk, keyboard_sdl, audio_sdl, resize_window, config_multisampling, vbl_sync);
        if (rc < 0)
                goto done;
 #ifdef HAVE_OVR
@@ -1010,7 +1021,6 @@ int main(int argc, char *argv[])
        rc = init_opengl_image((double_pixel_size) ? IMAGE_WIDTH * 2 : IMAGE_WIDTH, (double_pixel_size) ? IMAGE_HEIGHT * 2 : IMAGE_HEIGHT);
        if (rc < 0)
                goto done;
-       resize_opengl((config_debug_opengl) ? SCREEN_WIDTH / 3 * 2 : SCREEN_WIDTH, (config_debug_opengl) ? SCREEN_HEIGHT / 3 * 4 : SCREEN_HEIGHT);
 
        /* init osd */
        rc = init_opengl_osd(0, IMAGE_WIDTH * 2, IMAGE_HEIGHT * 2);