diff options
author | Paul Gilbert | 2019-11-21 20:02:47 -0800 |
---|---|---|
committer | Paul Gilbert | 2019-11-22 18:49:07 -0800 |
commit | 69f186c66591e6cff41d1444e6da40e45e4d5103 (patch) | |
tree | b96ed46b9576aacd9505ac68aba15ead827e03ba /engines/glk | |
parent | 9df3c85184b10a71be27209313efbc23974d1225 (diff) | |
download | scummvm-rg350-69f186c66591e6cff41d1444e6da40e45e4d5103.tar.gz scummvm-rg350-69f186c66591e6cff41d1444e6da40e45e4d5103.tar.bz2 scummvm-rg350-69f186c66591e6cff41d1444e6da40e45e4d5103.zip |
GLK: MAGNETIC: Moving local method static variables to class fields
Diffstat (limited to 'engines/glk')
-rw-r--r-- | engines/glk/magnetic/glk.cpp | 90 | ||||
-rw-r--r-- | engines/glk/magnetic/magnetic.cpp | 62 | ||||
-rw-r--r-- | engines/glk/magnetic/magnetic.h | 33 | ||||
-rw-r--r-- | engines/glk/magnetic/magnetic_types.h | 3 |
4 files changed, 98 insertions, 90 deletions
diff --git a/engines/glk/magnetic/glk.cpp b/engines/glk/magnetic/glk.cpp index fec8691854..ee4faebdd9 100644 --- a/engines/glk/magnetic/glk.cpp +++ b/engines/glk/magnetic/glk.cpp @@ -44,9 +44,7 @@ const gms_command_t Magnetic::GMS_COMMAND_TABLE[14] = { { nullptr, nullptr, false, false} }; - - -static gms_gamma_t GMS_GAMMA_TABLE[] = { +const gms_gamma_t Magnetic::GMS_GAMMA_TABLE[38] = { { "0.90", { 0, 29, 63, 99, 137, 175, 215, 255 }, true }, { "0.95", { 0, 33, 68, 105, 141, 179, 217, 255 }, true }, { "1.00", { 0, 36, 73, 109, 146, 182, 219, 255 }, false }, @@ -99,9 +97,6 @@ static gms_abbreviation_t GMS_ABBREVIATIONS[] = { /* Module constants */ /*---------------------------------------------------------------------*/ -/* CRC table initialization polynomial. */ -static const glui32 GMS_CRC_POLYNOMIAL = 0xedb88320; - /* Glk Magnetic Scrolls port version number. */ static const glui32 GMS_PORT_VERSION = 0x00010601; @@ -109,15 +104,6 @@ static const glui32 GMS_PORT_VERSION = 0x00010601; static const char *const GMS_INPUT_PROMPT = ">"; /* - * Weighting values for calculating the luminance of a color. There are - * two commonly used sets of values for these -- 299,587,114, taken from - * NTSC (Never The Same Color) 1953 standards, and 212,716,72, which is the - * set that modern CRTs tend to match. The NTSC ones seem to give the best - * subjective results. - */ -static const gms_rgb_t GMS_LUMINANCE_WEIGHTS = { 299, 587, 114 }; - -/* * Maximum number of regions to consider in a single repaint pass. A * couple of hundred seems to strike the right balance between not too * sluggardly picture updates, and responsiveness to input during graphics @@ -293,31 +279,10 @@ int Magnetic::gms_strcasecmp(const char *s1, const char *s2) { /*---------------------------------------------------------------------*/ glui32 Magnetic::gms_get_buffer_crc(const void *void_buffer, size_t length) { - static int is_initialized = false; - static glui32 crc_table[BYTE_MAX + 1]; - const char *buf = (const char *) void_buffer; - glui32 crc; + uint32 crc; size_t index; - /* Build the static CRC lookup table on first call. */ - if (!is_initialized) { - for (index = 0; index < BYTE_MAX + 1; index++) { - int bit; - - crc = (glui32) index; - for (bit = 0; bit < CHAR_BIT; bit++) - crc = crc & 1 ? GMS_CRC_POLYNOMIAL ^ (crc >> 1) : crc >> 1; - - crc_table[index] = crc; - } - - is_initialized = true; - - /* CRC lookup table self-test, after is_initialized set -- recursion. */ - assert(gms_get_buffer_crc("123456789", 9) == 0xcbf43926); - } - /* * Start with all ones in the crc, then update using table entries. Xor * with all ones again, finally, before returning. @@ -508,26 +473,13 @@ glui32 Magnetic::gms_graphics_combine_color(gms_rgbref_t rgb_color) { } int Magnetic::gms_graphics_color_luminance(gms_rgbref_t rgb_color) { - static int is_initialized = false; - static int weighting = 0; - - long luminance; - - /* On the first call, calculate the overall weighting. */ - if (!is_initialized) { - weighting = GMS_LUMINANCE_WEIGHTS.red + GMS_LUMINANCE_WEIGHTS.green - + GMS_LUMINANCE_WEIGHTS.blue; - - is_initialized = true; - } - /* Calculate the luminance and scale back by 1000 to 0-255 before return. */ - luminance = ((long) rgb_color->red * (long) GMS_LUMINANCE_WEIGHTS.red + long luminance = ((long) rgb_color->red * (long) GMS_LUMINANCE_WEIGHTS.red + (long) rgb_color->green * (long) GMS_LUMINANCE_WEIGHTS.green + (long) rgb_color->blue * (long) GMS_LUMINANCE_WEIGHTS.blue); - assert(weighting > 0); - return (int)(luminance / weighting); + assert(luminance_weighting > 0); + return (int)(luminance / luminance_weighting); } int Magnetic::gms_graphics_compare_luminance(const void *void_first, @@ -627,26 +579,9 @@ gms_gammaref_t Magnetic::gms_graphics_equal_contrast_gamma(type16 palette[], lon gms_gammaref_t Magnetic::gms_graphics_select_gamma(type8 bitmap[], type16 width, type16 height, type16 palette[]) { - static int is_initialized = false; - static gms_gammaref_t linear_gamma = NULL; - long color_usage[GMS_PALETTE_SIZE]; int color_count; gms_gammaref_t contrast_gamma; - - /* On first call, find and cache the uncorrected gamma table entry. */ - if (!is_initialized) { - gms_gammaref_t gamma; - - for (gamma = GMS_GAMMA_TABLE; gamma->level; gamma++) { - if (!gamma->is_corrected) { - linear_gamma = gamma; - break; - } - } - - is_initialized = true; - } assert(linear_gamma); /* @@ -1517,8 +1452,6 @@ void Magnetic::gms_graphics_timeout() { } void Magnetic::ms_showpic(type32 picture, type8 mode) { - static glui32 current_crc = 0; /* CRC of the current picture */ - type8 *bitmap, animated; type16 width, height, palette[GMS_PALETTE_SIZE]; long picture_bytes; @@ -1567,7 +1500,7 @@ void Magnetic::ms_showpic(type32 picture, type8 mode) { */ if (width == gms_graphics_width && height == gms_graphics_height - && crc == current_crc + && crc == pic_current_crc && gms_graphics_enabled && gms_graphics_are_displayed()) return; @@ -1591,7 +1524,7 @@ void Magnetic::ms_showpic(type32 picture, type8 mode) { gms_graphics_animated = animated; /* Retain the new picture CRC. */ - current_crc = crc; + pic_current_crc = crc; /* * If graphics are enabled, ensure the window is displayed, set the @@ -2525,9 +2458,6 @@ type16 Magnetic::gms_hint_handle(const ms_hint hints_[], } type8 Magnetic::ms_showhints(ms_hint *hints_) { - static int is_initialized = false; - static glui32 current_crc = 0; - type16 hint_count; glui32 crc; assert(hints_); @@ -2545,7 +2475,7 @@ type8 Magnetic::ms_showhints(ms_hint *hints_) { * this is the first call, assign a new cursor array. */ crc = gms_get_buffer_crc(hints_, hint_count * sizeof(*hints_)); - if (crc != current_crc || !is_initialized) { + if (crc != hints_current_crc || !hints_crc_initialized) { int bytes; /* Allocate new cursors, and set all to zero initial state. */ @@ -2558,8 +2488,8 @@ type8 Magnetic::ms_showhints(ms_hint *hints_) { * Retain the hints_ CRC, for later comparisons, and set is_initialized * flag. */ - current_crc = crc; - is_initialized = true; + hints_current_crc = crc; + hints_crc_initialized = true; } /* diff --git a/engines/glk/magnetic/magnetic.cpp b/engines/glk/magnetic/magnetic.cpp index f8ab15aab6..00aa540c5f 100644 --- a/engines/glk/magnetic/magnetic.cpp +++ b/engines/glk/magnetic/magnetic.cpp @@ -42,14 +42,13 @@ Magnetic::Magnetic(OSystem *syst, const GlkGameDescription &gameDesc) : GlkAPI(s gms_graphics_picture(0), gms_graphics_new_picture(false), gms_graphics_repaint(false), gms_graphics_active(false), gms_graphics_interpreter(false), gms_graphics_off_screen(nullptr), - gms_graphics_on_screen(nullptr),// gms_graphics_current_gamma(Magnetic::GMS_GAMMA_TABLE), + gms_graphics_on_screen(nullptr), gms_graphics_current_gamma(Magnetic::GMS_GAMMA_TABLE), gms_graphics_color_count(GMS_PALETTE_SIZE), gms_status_length(0), - gms_help_requested(false), gms_help_hints_silenced(false), - gms_output_buffer(nullptr), gms_output_allocation(0),gms_output_length(0), - gms_output_prompt(false), gms_hints(nullptr), gms_current_hint_node(0), - gms_hint_cursor(nullptr), gms_input_length(0), gms_input_cursor(0), - gms_undo_notification(false), gms_game_message(nullptr), gms_startup_called(false), - gms_main_called(false), gms_graphics_current_gamma(nullptr), + gms_help_requested(false), gms_help_hints_silenced(false), gms_output_buffer(nullptr), + gms_output_allocation(0),gms_output_length(0), gms_output_prompt(false), + gms_hints(nullptr), gms_current_hint_node(0), gms_hint_cursor(nullptr), + gms_input_length(0), gms_input_cursor(0), gms_undo_notification(false), + gms_game_message(nullptr), gms_startup_called(false), gms_main_called(false), i_count(0), string_size(0), rseed(0), pc(0), arg1i(0), mem_size(0), properties(0), fl_sub(0), fl_tab(0), fl_size(0), fp_tab(0), fp_size(0), zflag(0), nflag(0), cflag(0), vflag(0), byte1(0), byte2(0), regnr(0), admode(0), opsize(0), @@ -65,7 +64,9 @@ Magnetic::Magnetic(OSystem *syst, const GlkGameDescription &gameDesc) : GlkAPI(s pos_table_index(-1), pos_table_max(-1), anim_repeat(0) #endif , hints(nullptr), hint_contents(nullptr), xpos(0), bufpos(0), log_on(0), - ms_gfx_enabled(0), log1(nullptr), log2(nullptr) { + ms_gfx_enabled(0), log1(nullptr), log2(nullptr), GMS_LUMINANCE_WEIGHTS(299, 587, 114), + linear_gamma(nullptr), pic_current_crc(0), hints_current_crc(0), + hints_crc_initialized(false) { Common::fill(&gms_graphics_palette[0], &gms_graphics_palette[GMS_PALETTE_SIZE], 0); Common::fill(&gms_status_buffer[0], &gms_status_buffer[GMS_STATBUFFER_LENGTH], '\0'); @@ -78,11 +79,15 @@ Magnetic::Magnetic(OSystem *syst, const GlkGameDescription &gameDesc) : GlkAPI(s undo_stat[0] = undo_stat[1] = 0; Common::fill(&buffer[0], &buffer[80], 0); Common::fill(&filename[0], &filename[256], 0); + Common::fill(&crc_table[0], &crc_table[BYTE_MAX + 1], 0); #ifndef NO_ANIMATION Common::fill(&pos_table_count[0], &pos_table_count[MAX_POSITIONS], 0); #endif + luminance_weighting = GMS_LUMINANCE_WEIGHTS.red + GMS_LUMINANCE_WEIGHTS.green + + GMS_LUMINANCE_WEIGHTS.blue; + g_vm = this; } @@ -92,6 +97,15 @@ void Magnetic::runGame() { } void Magnetic::initialize() { + initializeSettings(); + initializeCRC(); + initializeLinearGamma(); + + // Close the already opened gamefile, since the Magnetic code will open it on it's own + _gameFile.close(); +} + +void Magnetic::initializeSettings() { // Local handling for Glk special commands if (ConfMan.hasKey("commands_enabled")) gms_commands_enabled = ConfMan.getBool("commands_enabled"); @@ -110,9 +124,37 @@ void Magnetic::initialize() { // Prompt enabled if (ConfMan.hasKey("prompt_enabled")) gms_prompt_enabled = ConfMan.getBool("prompt_enabled"); +} - // Close the already opened gamefile, since the Magnetic code will open it on it's own - _gameFile.close(); +void Magnetic::initializeCRC() { + /* CRC table initialization polynomial. */ + const glui32 GMS_CRC_POLYNOMIAL = 0xedb88320; + uint32 crc; + + for (uint index = 0; index < BYTE_MAX + 1; ++index) { + int bit; + + crc = index; + for (bit = 0; bit < CHAR_BIT; bit++) + crc = crc & 1 ? GMS_CRC_POLYNOMIAL ^ (crc >> 1) : crc >> 1; + + crc_table[index] = crc; + } + + /* CRC lookup table self-test, after is_initialized set -- recursion. */ + assert(gms_get_buffer_crc("123456789", 9) == 0xcbf43926); +} + +void Magnetic::initializeLinearGamma() { + /* Find and cache the uncorrected gamma table entry. */ + gms_gammaref_t gamma; + + for (gamma = GMS_GAMMA_TABLE; gamma->level; gamma++) { + if (!gamma->is_corrected) { + linear_gamma = gamma; + break; + } + } } Common::Error Magnetic::readSaveData(Common::SeekableReadStream *rs) { diff --git a/engines/glk/magnetic/magnetic.h b/engines/glk/magnetic/magnetic.h index 74d5b300da..6f81df41a1 100644 --- a/engines/glk/magnetic/magnetic.h +++ b/engines/glk/magnetic/magnetic.h @@ -54,6 +54,7 @@ typedef gms_command_t *gms_commandref_t; class Magnetic : public GlkAPI { public: static const gms_command_t GMS_COMMAND_TABLE[14]; + static const gms_gamma_t GMS_GAMMA_TABLE[38]; private: GammaMode gms_gamma_mode; bool gms_animation_enabled, gms_prompt_enabled; @@ -244,16 +245,48 @@ private: /* Hint support */ ms_hint *hints; type8 *hint_contents; + + /** + * Weighting values for calculating the luminance of a color. There are + * two commonly used sets of values for these -- 299,587,114, taken from + * NTSC (Never The Same Color) 1953 standards, and 212,716,72, which is the + * set that modern CRTs tend to match. The NTSC ones seem to give the best + * subjective results. + */ + const gms_rgb_t GMS_LUMINANCE_WEIGHTS; private: type8 buffer[80], xpos, bufpos, log_on, ms_gfx_enabled, filename[256]; Common::DumpFile *log1, *log2; private: + /* Method local statics in original code */ + glui32 crc_table[BYTE_MAX + 1]; + int luminance_weighting; + gms_gammaref_t linear_gamma; + uint32 pic_current_crc; /* CRC of the current picture */ + uint32 hints_current_crc; /* CRC of hints */ + bool hints_crc_initialized; +private: /** * Performs initialization */ void initialize(); /** + * Initializes settings from the ScummVM configuration + */ + void initializeSettings(); + + /** + * Initializes the CRC table + */ + void initializeCRC(); + + /** + * Initializes the linear gamma entry + */ + void initializeLinearGamma(); + + /** * Fatal error handler. The function returns, expecting the caller to * abort() or otherwise handle the error. */ diff --git a/engines/glk/magnetic/magnetic_types.h b/engines/glk/magnetic/magnetic_types.h index 0d18614c73..4c39ee3d34 100644 --- a/engines/glk/magnetic/magnetic_types.h +++ b/engines/glk/magnetic/magnetic_types.h @@ -130,6 +130,9 @@ typedef const gms_gamma_t *gms_gammaref_t; /* R,G,B color triple definition. */ struct gms_rgb_t { int red, green, blue; + + gms_rgb_t() : red(0), green(0), blue(0) {} + gms_rgb_t(int r, int g, int b) : red(r), green(b), blue(b) {} }; typedef gms_rgb_t *gms_rgbref_t; |