From 048d8fb6a92a6f5924a1ad748af31e8219ff5a35 Mon Sep 17 00:00:00 2001 From: Taylor Richards Date: Tue, 3 Mar 2015 16:33:35 -0500 Subject: [PATCH] clean up gr_init(): - let gr_init() determine config/setup - move cfg stuff from freespace.cpp to gr_init() - cleaner gamma setup --- include/2d.h | 4 +- src/freespace2/freespace.cpp | 47 +----------- src/graphics/2d.cpp | 134 +++++++++++++++++++++++++++------- src/graphics/grgl1texture.cpp | 23 +----- src/menuui/optionsmenu.cpp | 4 - 5 files changed, 116 insertions(+), 96 deletions(-) diff --git a/include/2d.h b/include/2d.h index 0a2b931..e40b03d 100644 --- a/include/2d.h +++ b/include/2d.h @@ -536,7 +536,7 @@ typedef struct screen { #define GR_640 0 // 640 x 480 #define GR_1024 1 // 1024 x 768 -extern int gr_init(int res, int mode, int depth = 16, int fred_x = -1, int fred_y = -1 ); +extern int gr_init(); // Call this when your app ends. extern void gr_close(); @@ -669,7 +669,7 @@ int gr_zbuffer_set(int mode); #define gr_dump_frame_stop GR_CALL(gr_screen.gf_dump_frame_stop) #define gr_dump_frame GR_CALL(gr_screen.gf_dump_frame) -#define gr_set_gamma GR_CALL(gr_screen.gf_set_gamma) +void gr_set_gamma(float gamma); #define gr_lock GR_CALL(gr_screen.gf_lock) #define gr_unlock GR_CALL(gr_screen.gf_unlock) diff --git a/src/freespace2/freespace.cpp b/src/freespace2/freespace.cpp index 7ffbd3d..93d087a 100644 --- a/src/freespace2/freespace.cpp +++ b/src/freespace2/freespace.cpp @@ -2250,7 +2250,7 @@ DCF(force_fullscreen, "Forces game to startup in fullscreen mode") int Framerate_delay = 0; -float Freespace_gamma = 1.0f; +float Freespace_gamma = 1.8f; DCF(gamma,"Sets Gamma factor") { @@ -2396,55 +2396,14 @@ void game_init() ///////////////////////////// // SOUND INIT END ///////////////////////////// - - ptr = os_config_read_string(NULL, NOX("Videocard"), NULL); - if (ptr == NULL) { - STUB_FUNCTION; - exit(1); - } - - // check for hi res pack file - int has_sparky_hi = 0; - - // check if sparky_hi exists -- access mode 0 means does file exist -#ifndef MAKE_FS1 // shoudn't have it so don't check - char sparky_path[MAX_PATH_LEN]; - SDL_snprintf(sparky_path, sizeof(sparky_path), "%s%s%s", Cfile_root_dir, DIR_SEPARATOR_STR, "sparky_hi_fs2.vp"); - - if ( access(sparky_path, 0) == 0 ) { - has_sparky_hi = 1; - } else { - mprintf(("No sparky_hi_fs2.vp in directory %s\n", Cfile_root_dir)); - } -#endif - if ( !Is_standalone && ptr && strstr(ptr, NOX("OpenGL")) ) { - if(has_sparky_hi && strstr(ptr, NOX("(1024x768)"))){ - gr_init(GR_1024, GR_OPENGL); - } else { - gr_init(GR_640, GR_OPENGL); - } + if ( !Is_standalone ) { + gr_init(); } else { STUB_FUNCTION; Int3(); - //gr_init(GR_640, GR_OPENGL); - } - - // Set the gamma - ptr = os_config_read_string(NULL,NOX("Gamma"),NOX("1.80")); - Freespace_gamma = (float)atof(ptr); - if ( Freespace_gamma == 0.0f ) { - Freespace_gamma = 1.80f; - } else if ( Freespace_gamma < 0.1f ) { - Freespace_gamma = 0.1f; - } else if ( Freespace_gamma > 5.0f ) { - Freespace_gamma = 5.0f; } - char tmp_gamma_string[32]; - SDL_snprintf( tmp_gamma_string, sizeof(tmp_gamma_string), NOX("%.2f"), Freespace_gamma ); - os_config_write_string( NULL, NOX("Gamma"), tmp_gamma_string ); - gr_set_gamma(Freespace_gamma); #if defined(FS2_DEMO) || defined(OEM_BUILD) || defined(FS1_DEMO) // add title screen diff --git a/src/graphics/2d.cpp b/src/graphics/2d.cpp index 6829b1e..fec64aa 100644 --- a/src/graphics/2d.cpp +++ b/src/graphics/2d.cpp @@ -483,6 +483,9 @@ #include "grinternal.h" #include "systemvars.h" #include "cmdline.h" +#include "osregistry.h" +#include "cfile.h" +#include "cfilesystem.h" // Includes for different rendering systems #include "gropengl.h" @@ -609,17 +612,93 @@ void gr_set_palette( const char *name, ubyte * palette, int restrict_font_to_128 gr_set_palette_internal( name, palette, restrict_font_to_128 ); } +void gr_set_gamma(float gamma) +{ + int i; + + if ( !Gr_inited ) { + return; + } + + CAP(gamma, 0.1f, 5.0f); + + Gr_gamma = gamma; + Gr_gamma_int = fl2i(gamma * 100.0f); + + // create the gamma lookup table + float gamma_1f = 1.0f / gamma; + + for (i = 0; i < 256; i++) { + int v = fl2i(pow(i2fl(i)/255.0f, gamma_1f)*255.0f); + CAP(v, 0, 255); + Gr_gamma_lookup[i] = v; + } + + // save new value to cfg file + char tmp_gamma_string[10]; + SDL_snprintf( tmp_gamma_string, sizeof(tmp_gamma_string), "%.2f", gamma); + os_config_write_string("Video", "Gamma", tmp_gamma_string); + + // call renderer specific functionality, if needed + if (gr_screen.gf_set_gamma) { + (*gr_screen.gf_set_gamma)(gamma); + } +} + +static int gr_get_best_res(int *max_w, int *max_h) +{ + if (Fred_running || Pofview_running || Nebedit_running) { + (*max_w) = 640; + (*max_h) = 480; + + return GR_640; + } + + // defaults + int res = GR_640; + (*max_w) = 640; + (*max_h) = 480; + + // quickly bail if we are forcing low-res mode + if ( os_config_read_uint("Video", "LowRes", 0) ) { + return res; + } + +#ifndef MAKE_FS1 + // check to see if we have hi-res art + if ( cf_has_packfile("sparky_hi_fs2") ) { + // check desktop res to make sure we should use it + if ( !SDL_InitSubSystem(SDL_INIT_VIDEO) ) { + SDL_DisplayMode desk_mode; + + if ( !SDL_GetDesktopDisplayMode(0, &desk_mode) ) { + if ( (desk_mode.w >= 1024) && (desk_mode.h >= 768) ) { + (*max_w) = 1024; + (*max_h) = 768; + res = GR_1024; + } + } + + SDL_QuitSubSystem(SDL_INIT_VIDEO); + } + } +#endif + + return res; +} //void gr_test(); // -------------------------------------------------------------------------- -int gr_init(int res, int mode, int depth, int fred_x, int fred_y) +int gr_init() { + const char *ptr = NULL; + int mode = GR_OPENGL; + int res = GR_640; int max_w, max_h; - -// gr_test(); + if ( !Gr_inited ) atexit(gr_close); @@ -642,34 +721,26 @@ int gr_init(int res, int mode, int depth, int fred_x, int fred_y) Gr_inited = 1; - max_w = -1; - max_h = -1; - if(!Fred_running && !Pofview_running){ - // set resolution based on the res type - switch(res){ - case GR_640: - max_w = 640; - max_h = 480; - break; - - case GR_1024: - max_w = 1024; - max_h = 768; - break; + if (Fred_running || Pofview_running) { + mode = GR_WXGL; + } else { + ptr = os_config_read_string("Video", "Renderer", "OpenGL"); - default : + if ( !SDL_strcasecmp("OpenGL", ptr) ) { + mode = GR_OPENGL; + } else { Int3(); - break; } - } else { - max_w = fred_x; - max_h = fred_y; } - // Make w a multiple of 8 - max_w = ( max_w / 8 )*8; - if ( max_w < 8 ) max_w = 8; - if ( max_h < 8 ) max_h = 8; + max_w = -1; + max_h = -1; + + // get best available resolution + res = gr_get_best_res(&max_w, &max_h); + + mprintf(("Using %s resolution mode\n", (res == GR_1024) ? "high" : "low" )); + memset( &gr_screen, 0, sizeof(screen) ); @@ -707,7 +778,16 @@ int gr_init(int res, int mode, int depth, int fred_x, int fred_y) memmove( Gr_current_palette, Gr_original_palette, 768 ); gr_set_palette_internal(Gr_current_palette_name, Gr_current_palette,0); - gr_set_gamma(Gr_gamma); + // Set the gamma + extern float Freespace_gamma; + + ptr = os_config_read_string("Video", "Gamma", "1.8"); + + Freespace_gamma = (float)SDL_atof(ptr); + CAP(Freespace_gamma, 0.1f, 5.0f); + + gr_set_gamma(Freespace_gamma); + if ( Gr_cursor == -1 ){ Gr_cursor = bm_load( "cursor" ); diff --git a/src/graphics/grgl1texture.cpp b/src/graphics/grgl1texture.cpp index 7a314a1..2c58432 100644 --- a/src/graphics/grgl1texture.cpp +++ b/src/graphics/grgl1texture.cpp @@ -149,6 +149,8 @@ void opengl1_tcache_init() GL_last_section_x = -1; GL_last_section_y = -1; + + memset(GL_xlat, 0, sizeof(GL_xlat)); } static int opengl1_free_texture ( tcache_slot_opengl *t ) @@ -837,34 +839,17 @@ int gr_opengl1_preload(int bitmap_num, int is_aabitmap) return retval; } -void gr_opengl1_set_gamma(float gamma) +void gr_opengl1_set_gamma(float) { - Gr_gamma = gamma; - Gr_gamma_int = int (Gr_gamma*10); - - // Create the Gamma lookup table int i; - for (i=0;i<256; i++) { - int v = fl2i(pow(i2fl(i)/255.0f, 1.0f/Gr_gamma)*255.0f); - if ( v > 255 ) { - v = 255; - } else if ( v < 0 ) { - v = 0; - } - Gr_gamma_lookup[i] = v; - } // set the alpha gamma settings (for fonts) - for (i=0; i<16; i++) { + for (i = 0; i < 16; i++) { GL_xlat[i] = (ubyte)Gr_gamma_lookup[(i*255)/15]; } GL_xlat[15] = GL_xlat[1]; - for (; i<256; i++) { - GL_xlat[i] = GL_xlat[0]; - } - // Flush any existing textures opengl1_tcache_flush(); } diff --git a/src/menuui/optionsmenu.cpp b/src/menuui/optionsmenu.cpp index 84da5cf..ecc15fe 100644 --- a/src/menuui/optionsmenu.cpp +++ b/src/menuui/optionsmenu.cpp @@ -1021,8 +1021,6 @@ void options_cancel_exit() void options_change_gamma(float delta) { - char tmp_gamma_string[32]; - Freespace_gamma += delta; if (Freespace_gamma < 0.1f) { Freespace_gamma = 0.1f; @@ -1037,8 +1035,6 @@ void options_change_gamma(float delta) } gr_set_gamma(Freespace_gamma); - SDL_snprintf(tmp_gamma_string, sizeof(tmp_gamma_string), NOX("%.2f"), Freespace_gamma); - os_config_write_string(NULL, NOX("Gamma"), tmp_gamma_string); } void options_button_pressed(int n) -- 2.39.2