From f611ea5ee90acee3d29f9e4385856dfd66cfdccc Mon Sep 17 00:00:00 2001 From: Taylor Richards Date: Sat, 13 Sep 2014 23:58:29 -0400 Subject: [PATCH] support for OpenGL with wxWidgets --- include/2d.h | 1 + include/grwxgl.h | 17 +++ includes.cmake | 1 + src/CMakeLists.txt | 1 + src/graphics/2d.cpp | 12 ++ src/graphics/grgl1.cpp | 2 +- src/graphics/grgl1texture.cpp | 12 +- src/graphics/gropengl.cpp | 6 +- src/graphics/grwxgl.cpp | 267 ++++++++++++++++++++++++++++++++++ 9 files changed, 309 insertions(+), 10 deletions(-) create mode 100644 include/grwxgl.h create mode 100644 src/graphics/grwxgl.cpp diff --git a/include/2d.h b/include/2d.h index 02e8397..ea31104 100644 --- a/include/2d.h +++ b/include/2d.h @@ -527,6 +527,7 @@ typedef struct screen { // Call this at application startup #define GR_OPENGL (100) // OpenGL (generic) +#define GR_WXGL (101) // OpenGL for use with wxWidgets toolkit // resolution constants - always keep resolutions in ascending order and starting from 0 #define GR_NUM_RESOLUTIONS 2 diff --git a/include/grwxgl.h b/include/grwxgl.h new file mode 100644 index 0000000..f51ba78 --- /dev/null +++ b/include/grwxgl.h @@ -0,0 +1,17 @@ +/* + * Copyright (C) Volition, Inc. 1999. All rights reserved. + * + * All source code herein is the property of Volition, Inc. You may not sell + * or otherwise commercially exploit the source or things you created based on + * the source. + */ + +#ifndef GRWXGL_H +#define GRWXGL_H + +void gr_wxgl_init(); +void gr_wxgl_cleanup(); +void gr_wxgl_flip(); +void gr_wxgl_set_viewport(int width, int height); + +#endif // GRWXGL_H diff --git a/includes.cmake b/includes.cmake index d3981ff..bd60ead 100644 --- a/includes.cmake +++ b/includes.cmake @@ -81,6 +81,7 @@ set(fs_INCLUDES include/grgl1.h include/gropengl.h include/gropenglinternal.h + include/grwxgl.h include/helpeddoc.h include/helped.h include/helpedline.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a0e4fee..7204930 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -38,6 +38,7 @@ set(code_SOURCE graphics/grgl1render.cpp graphics/grgl1texture.cpp graphics/gropengl.cpp + graphics/grwxgl.cpp hud/hud.cpp hud/hudartillery.cpp hud/hudbrackets.cpp diff --git a/src/graphics/2d.cpp b/src/graphics/2d.cpp index 653379f..75f8545 100644 --- a/src/graphics/2d.cpp +++ b/src/graphics/2d.cpp @@ -486,6 +486,7 @@ // Includes for different rendering systems #include "gropengl.h" +#include "grwxgl.h" screen gr_screen; @@ -529,6 +530,10 @@ void gr_close() gr_opengl_cleanup(); break; + case GR_WXGL: + gr_wxgl_cleanup(); + break; + default: Int3(); // Invalid graphics mode break; @@ -626,6 +631,9 @@ int gr_init(int res, int mode, int depth, int fred_x, int fred_y) gr_opengl_cleanup(); break; + case GR_WXGL: + gr_wxgl_cleanup(); + default: Int3(); // Invalid graphics mode break; @@ -687,6 +695,10 @@ int gr_init(int res, int mode, int depth, int fred_x, int fred_y) case GR_OPENGL: gr_opengl_init(); break; + case GR_WXGL: + SDL_assert( Pofview_running || Fred_running ); + gr_wxgl_init(); + break; default: Int3(); // Invalid graphics mode break; diff --git a/src/graphics/grgl1.cpp b/src/graphics/grgl1.cpp index 4356c08..6276a1a 100644 --- a/src/graphics/grgl1.cpp +++ b/src/graphics/grgl1.cpp @@ -19,7 +19,7 @@ int OGL_fog_mode = 0; -static int GL_one_inited = 0; +int GL_one_inited = 0; volatile int GL_activate = 0; diff --git a/src/graphics/grgl1texture.cpp b/src/graphics/grgl1texture.cpp index 9678b7a..6266f95 100644 --- a/src/graphics/grgl1texture.cpp +++ b/src/graphics/grgl1texture.cpp @@ -813,18 +813,18 @@ int opengl1_tcache_set(int bitmap_id, int bitmap_type, float *u_scale, float *v_ void gr_opengl1_preload_init() { - if (gr_screen.mode != GR_OPENGL) { - return; - } +// if (gr_screen.mode != GR_OPENGL) { +// return; +// } opengl1_tcache_flush(); } int gr_opengl1_preload(int bitmap_num, int is_aabitmap) { - if ( gr_screen.mode != GR_OPENGL) { - return 0; - } +// if ( gr_screen.mode != GR_OPENGL) { +// return 0; +// } if ( !GL_should_preload ) { return 0; diff --git a/src/graphics/gropengl.cpp b/src/graphics/gropengl.cpp index 5513c4e..b68ba54 100644 --- a/src/graphics/gropengl.cpp +++ b/src/graphics/gropengl.cpp @@ -44,7 +44,7 @@ static size_t render_buffer_size = 0; void opengl_alloc_render_buffer(unsigned int nelems) { - if (nelems < 0) { + if (nelems < 1) { nelems = 1; } @@ -69,7 +69,7 @@ void opengl_free_render_buffer() } } -static void opengl_set_variables() +void opengl_set_variables() { GL_min_texture_height = 16; GL_min_texture_width = 16; @@ -83,7 +83,7 @@ static void opengl_set_variables() } } -static void opengl_init_viewport() +void opengl_init_viewport() { GL_viewport_x = 0; GL_viewport_y = 0; diff --git a/src/graphics/grwxgl.cpp b/src/graphics/grwxgl.cpp new file mode 100644 index 0000000..f4ee62f --- /dev/null +++ b/src/graphics/grwxgl.cpp @@ -0,0 +1,267 @@ +/* + * Copyright (C) Volition, Inc. 1999. All rights reserved. + * + * All source code herein is the property of Volition, Inc. You may not sell + * or otherwise commercially exploit the source or things you created based on + * the source. + */ + +#include "pstypes.h" +#include "2d.h" +#include "grwxgl.h" +#include "gropengl.h" +#include "grgl1.h" +#include "gropenglinternal.h" +#include "grinternal.h" +#include "mouse.h" + + +extern bool OGL_inited; +extern int GL_one_inited; + +extern void opengl_set_variables(); +extern void opengl_init_viewport(); + + +static void wxgl_init_func_pointers() +{ + gr_screen.gf_flip = gr_wxgl_flip; + gr_screen.gf_set_clip = gr_opengl1_set_clip; + gr_screen.gf_reset_clip = gr_opengl1_reset_clip; + + gr_screen.gf_clear = gr_opengl1_clear; + + gr_screen.gf_aabitmap = gr_opengl1_aabitmap; + gr_screen.gf_aabitmap_ex = gr_opengl1_aabitmap_ex; + + gr_screen.gf_rect = gr_opengl1_rect; + gr_screen.gf_shade = gr_opengl1_shade; + gr_screen.gf_string = gr_opengl1_string; + gr_screen.gf_circle = gr_opengl1_circle; + + gr_screen.gf_line = gr_opengl1_line; + gr_screen.gf_aaline = gr_opengl1_aaline; + gr_screen.gf_pixel = gr_opengl1_pixel; + gr_screen.gf_scaler = gr_opengl1_scaler; + gr_screen.gf_tmapper = gr_opengl1_tmapper; + + gr_screen.gf_gradient = gr_opengl1_gradient; + + gr_screen.gf_print_screen = gr_opengl1_print_screen; + + gr_screen.gf_fade_in = gr_opengl1_fade_in; + gr_screen.gf_fade_out = gr_opengl1_fade_out; + gr_screen.gf_flash = gr_opengl1_flash; + + gr_screen.gf_zbuffer_clear = gr_opengl1_zbuffer_clear; + + gr_screen.gf_save_screen = gr_opengl1_save_screen; + gr_screen.gf_restore_screen = gr_opengl1_restore_screen; + gr_screen.gf_free_screen = gr_opengl1_free_screen; + + gr_screen.gf_dump_frame_start = gr_opengl1_dump_frame_start; + gr_screen.gf_dump_frame_stop = gr_opengl1_dump_frame_stop; + gr_screen.gf_dump_frame = gr_opengl1_dump_frame; + + gr_screen.gf_set_gamma = gr_opengl1_set_gamma; + + gr_screen.gf_lock = gr_opengl1_lock; + gr_screen.gf_unlock = gr_opengl1_unlock; + + gr_screen.gf_fog_set = gr_opengl1_fog_set; + + gr_screen.gf_get_region = gr_opengl1_get_region; + + gr_screen.gf_set_cull = gr_opengl1_set_cull; + + gr_screen.gf_cross_fade = gr_opengl1_cross_fade; + + gr_screen.gf_preload_init = gr_opengl1_preload_init; + gr_screen.gf_preload = gr_opengl1_preload; + + gr_screen.gf_zbias = gr_opengl1_zbias; + + gr_screen.gf_force_windowed = gr_opengl_force_windowed; + gr_screen.gf_force_fullscreen = gr_opengl_force_fullscreen; + gr_screen.gf_toggle_fullscreen = gr_opengl_toggle_fullscreen; + + gr_screen.gf_set_viewport = gr_wxgl_set_viewport; + + gr_screen.gf_activate = gr_opengl1_activate; +} + +static void wxgl_init() +{ + if (GL_one_inited) { + return; + } + + glShadeModel(GL_SMOOTH); + glEnable(GL_DITHER); + glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); + glHint(GL_FOG_HINT, GL_NICEST); + + glEnable(GL_DEPTH_TEST); + glEnable(GL_BLEND); + + glEnable(GL_TEXTURE_2D); + + glDepthRange(0.0, 1.0); + + glPixelStorei(GL_PACK_ALIGNMENT, 1); + glPixelStorei(GL_UNPACK_ALIGNMENT, 1); + + glFlush(); + + wxgl_init_func_pointers(); + opengl1_tcache_init(); + + gr_opengl1_clear(); + + GL_one_inited = 1; + +} + +void gr_wxgl_flip() +{ +} + +void gr_wxgl_set_viewport(int width, int height) +{ + GL_viewport_x = 0; + GL_viewport_y = 0; + GL_viewport_w = width; + GL_viewport_h = height; + GL_viewport_scale_w = 1.0f; + GL_viewport_scale_h = 1.0f; + + glViewport(GL_viewport_x, GL_viewport_y, GL_viewport_w, GL_viewport_h); + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glOrtho(0, GL_viewport_w, GL_viewport_h, 0, 0.0, 1.0); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + glScalef(GL_viewport_scale_w, GL_viewport_scale_h, 1.0f); + + // update gr_screen, which will deal with view scaling too + gr_screen.max_w = width; + gr_screen.max_h = height; +} + +void gr_wxgl_cleanup() +{ + gr_opengl_cleanup(); +} + +void gr_wxgl_init() +{ + if ( OGL_inited ) { + gr_opengl_cleanup(); + OGL_inited = false; + } + + mprintf(( "Setting up OpenGL for wxWidgets...\n" )); + + OGL_inited = true; + + const char *gl_version = (const char*)glGetString(GL_VERSION); + int v_major = 0, v_minor = 0; + + sscanf(gl_version, "%d.%d", &v_major, &v_minor); + + GL_version = (v_major * 10) + v_minor; + + // version check, require 1.2+ for sake of simplicity + if (GL_version < 12) { + Error(LOCATION, "Minimum OpenGL version is 1.2!"); + } + + mprintf((" Vendor : %s\n", glGetString(GL_VENDOR))); + mprintf((" Renderer : %s\n", glGetString(GL_RENDERER))); + mprintf((" Version : %s\n", gl_version)); + + // initial viewport setup + opengl_init_viewport(); + + // set up generic variables before further init() calls + opengl_set_variables(); + + // main GL init + wxgl_init(); + + mprintf(("\n")); + + + gr_screen.bits_per_pixel = 16; + gr_screen.bytes_per_pixel = 2; + + // screen values + Gr_red.bits = 5; + Gr_red.shift = 10; + Gr_red.scale = 8; + Gr_red.mask = 0x7C00; + + Gr_green.bits = 5; + Gr_green.shift = 5; + Gr_green.scale = 8; + Gr_green.mask = 0x3E0; + + Gr_blue.bits = 5; + Gr_blue.shift = 0; + Gr_blue.scale = 8; + Gr_blue.mask = 0x1F; + + Gr_alpha.bits = 1; + Gr_alpha.shift = 15; + Gr_alpha.scale = 255; + Gr_alpha.mask = 0x8000; + + // DDOI - set these so no one else does! + // texture values, always 1555 - 16-bit + Gr_t_red.mask = 0x7C00; + Gr_t_red.shift = 10; + Gr_t_red.scale = 8; + + Gr_t_green.mask = 0x3E0; + Gr_t_green.shift = 5; + Gr_t_green.scale = 8; + + Gr_t_blue.mask = 0x1F; + Gr_t_blue.shift = 0; + Gr_t_blue.scale = 8; + + Gr_t_alpha.mask = 0x8000; + Gr_t_alpha.scale = 255; + Gr_t_alpha.shift = 15; + + // alpha-texture values + Gr_ta_red.mask = 0x0f00; + Gr_ta_red.shift = 8; + Gr_ta_red.scale = 16; + + Gr_ta_green.mask = 0x00f0; + Gr_ta_green.shift = 4; + Gr_ta_green.scale = 16; + + Gr_ta_blue.mask = 0x000f; + Gr_ta_blue.shift = 0; + Gr_ta_blue.scale = 16; + + Gr_ta_alpha.mask = 0xf000; + Gr_ta_alpha.shift = 12; + Gr_ta_alpha.scale = 16; + + // default to screen + Gr_current_red = &Gr_red; + Gr_current_blue = &Gr_blue; + Gr_current_green = &Gr_green; + Gr_current_alpha = &Gr_alpha; + + + Mouse_hidden++; + gr_reset_clip(); + gr_clear(); + gr_flip(); + gr_clear(); +} -- 2.39.2