1 /* ScummVM - Graphic Adventure Engine
2 *
3 * ScummVM is the legal property of its developers, whose names
4 * are too numerous to list here. Please refer to the COPYRIGHT
5 * file distributed with this source distribution.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 *
21 */
22
23 #include "glk/magnetic/magnetic.h"
24 #include "glk/magnetic/magnetic_defs.h"
25 #include "common/config-manager.h"
26
27 namespace Glk {
28 namespace Magnetic {
29
30 Magnetic *g_vm;
31
Magnetic(OSystem * syst,const GlkGameDescription & gameDesc)32 Magnetic::Magnetic(OSystem *syst, const GlkGameDescription &gameDesc) : GlkAPI(syst, gameDesc),
33 gms_gamma_mode(GAMMA_NORMAL), gms_animation_enabled(true),
34 gms_prompt_enabled(true), gms_abbreviations_enabled(true), gms_commands_enabled(true),
35 gms_graphics_enabled(true), GMS_PORT_VERSION(0x00010601),
36 gms_main_window(nullptr), gms_status_window(nullptr), gms_graphics_window(nullptr),
37 gms_hint_menu_window(nullptr), gms_hint_text_window(nullptr),
38 gms_transcript_stream(nullptr), gms_readlog_stream(nullptr),
39 gms_inputlog_stream(nullptr), gms_graphics_possible(true),
40 GMS_INPUT_PROMPT(">"), gms_gameid_game_name(nullptr), gms_graphics_bitmap(nullptr),
41 gms_graphics_width(0), gms_graphics_height(0), gms_graphics_animated(false),
42 gms_graphics_picture(0), gms_graphics_new_picture(false),
43 gms_graphics_repaint(false), gms_graphics_active(false),
44 gms_graphics_interpreter(false), gms_graphics_off_screen(nullptr),
45 gms_graphics_on_screen(nullptr), gms_graphics_current_gamma(Magnetic::GMS_GAMMA_TABLE),
46 gms_graphics_color_count(GMS_PALETTE_SIZE), gms_status_length(0),
47 gms_help_requested(false), gms_help_hints_silenced(false), gms_output_buffer(nullptr),
48 gms_output_allocation(0),gms_output_length(0), gms_output_prompt(false),
49 gms_hints(nullptr), gms_current_hint_node(0), gms_hint_cursor(nullptr),
50 gms_input_length(0), gms_input_cursor(0), gms_undo_notification(false),
51 gms_game_message(nullptr), gms_startup_called(false), gms_main_called(false),
52 i_count(0), string_size(0), rseed(0), pc(0), arg1i(0), mem_size(0), properties(0),
53 fl_sub(0), fl_tab(0), fl_size(0), fp_tab(0), fp_size(0), zflag(0), nflag(0),
54 cflag(0), vflag(0), byte1(0), byte2(0), regnr(0), admode(0), opsize(0),
55 arg1(nullptr), arg2(nullptr), is_reversible(0), running(0), lastchar(0), version(0),
56 sd(0), decode_table(nullptr), restart(nullptr), code(nullptr), string(nullptr),
57 string2(nullptr), string3(nullptr), dict(nullptr), quick_flag(0), gfx_ver(0),
58 gfx_buf(nullptr), gfx_data(nullptr), gfx2_hdr(nullptr), gfx2_buf(nullptr),
59 gfx2_name(nullptr), gfx2_hsize(0), gfx_fp(nullptr), snd_buf(nullptr), snd_hdr(nullptr),
60 snd_hsize(0), snd_fp(nullptr), undo_pc(0), undo_size(0), gfxtable(0), table_dist(0),
61 v4_id(0), next_table(1)
62 #ifndef NO_ANIMATION
63 , pos_table_size(0), command_table(nullptr), command_index(-1),
64 pos_table_index(-1), pos_table_max(-1), anim_repeat(0)
65 #endif
66 , hints(nullptr), hint_contents(nullptr), xpos(0), bufpos(0), log_on(0),
67 ms_gfx_enabled(0), _log1(nullptr), _log2(nullptr), GMS_LUMINANCE_WEIGHTS(299, 587, 114),
68 linear_gamma(nullptr), pic_current_crc(0), hints_current_crc(0),
69 hints_crc_initialized(false), _saveData(nullptr), _saveSize(0) {
70
71 Common::fill(&gms_graphics_palette[0], &gms_graphics_palette[GMS_PALETTE_SIZE], 0);
72 Common::fill(&gms_status_buffer[0], &gms_status_buffer[GMS_STATBUFFER_LENGTH], '\0');
73 Common::fill(&gms_input_buffer[0], &gms_input_buffer[GMS_INPUTBUFFER_LENGTH], '\0');
74 Common::fill(&dreg[0], &dreg[8], 0);
75 Common::fill(&areg[0], &areg[8], 0);
76 Common::fill(&tmparg[0], &tmparg[4], 0);
77 Common::fill(&undo_regs[0][0], &undo_regs[2][0], 0);
78 undo[0] = undo[1] = nullptr;
79 undo_stat[0] = undo_stat[1] = 0;
80 Common::fill(&buffer[0], &buffer[80], 0);
81 Common::fill(&filename[0], &filename[256], 0);
82 Common::fill(&crc_table[0], &crc_table[BYTE_MAX_VAL + 1], 0);
83
84 #ifndef NO_ANIMATION
85 Common::fill(&pos_table_count[0], &pos_table_count[MAX_POSITIONS], 0);
86 #endif
87
88 luminance_weighting = GMS_LUMINANCE_WEIGHTS.red + GMS_LUMINANCE_WEIGHTS.green
89 + GMS_LUMINANCE_WEIGHTS.blue;
90
91 g_vm = this;
92 }
93
runGame()94 void Magnetic::runGame() {
95 initialize();
96 gms_main();
97 }
98
initialize()99 void Magnetic::initialize() {
100 initializeSettings();
101 initializeCRC();
102 initializeLinearGamma();
103
104 // Close the already opened gamefile, since the Magnetic code will open it on it's own
105 _gameFile.close();
106 }
107
initializeSettings()108 void Magnetic::initializeSettings() {
109 // Local handling for Glk special commands
110 if (ConfMan.hasKey("commands_enabled"))
111 gms_commands_enabled = ConfMan.getBool("commands_enabled");
112 // Abbreviation expansions
113 if (ConfMan.hasKey("abbreviations_enabled"))
114 gms_abbreviations_enabled = ConfMan.getBool("abbreviations_enabled");
115 // Pictures enabled
116 if (ConfMan.hasKey("graphics_enabled"))
117 gms_graphics_enabled = ConfMan.getBool("graphics_enabled");
118 // Automatic gamma correction on pictures
119 if (ConfMan.hasKey("gamma_mode") && !ConfMan.getBool("gamma_mode"))
120 gms_gamma_mode = GAMMA_OFF;
121 // Animations
122 if (ConfMan.hasKey("animation_enabled"))
123 gms_animation_enabled = ConfMan.getBool("animation_enabled");
124 // Prompt enabled
125 if (ConfMan.hasKey("prompt_enabled"))
126 gms_prompt_enabled = ConfMan.getBool("prompt_enabled");
127 }
128
initializeCRC()129 void Magnetic::initializeCRC() {
130 /* CRC table initialization polynomial. */
131 const glui32 GMS_CRC_POLYNOMIAL = 0xedb88320;
132 uint32 crc;
133
134 for (uint index = 0; index < BYTE_MAX_VAL + 1; ++index) {
135 int bit;
136
137 crc = index;
138 for (bit = 0; bit < BITS_PER_BYTE; bit++)
139 crc = crc & 1 ? GMS_CRC_POLYNOMIAL ^ (crc >> 1) : crc >> 1;
140
141 crc_table[index] = crc;
142 }
143
144 /* CRC lookup table self-test, after is_initialized set -- recursion. */
145 assert(gms_get_buffer_crc("123456789", 9) == 0xcbf43926);
146 }
147
initializeLinearGamma()148 void Magnetic::initializeLinearGamma() {
149 /* Find and cache the uncorrected gamma table entry. */
150 gms_gammaref_t gamma;
151
152 for (gamma = GMS_GAMMA_TABLE; gamma->level; gamma++) {
153 if (!gamma->is_corrected) {
154 linear_gamma = gamma;
155 break;
156 }
157 }
158 }
159
ms_load_file(const char * name,type8 * ptr,type16 size)160 type8 Magnetic::ms_load_file(const char *name, type8 *ptr, type16 size) {
161 _saveData = ptr;
162 _saveSize = size;
163
164 return loadGame().getCode() == Common::kNoError ? 0 : 1;
165 }
166
readSaveData(Common::SeekableReadStream * rs)167 Common::Error Magnetic::readSaveData(Common::SeekableReadStream *rs) {
168 return rs->read(_saveData, _saveSize) == _saveSize ? Common::kNoError : Common::kReadingFailed;
169 }
170
ms_save_file(const char * name,type8 * ptr,type16 size)171 type8 Magnetic::ms_save_file(const char *name, type8 *ptr, type16 size) {
172 _saveData = ptr;
173 _saveSize = size;
174
175 return saveGame().getCode() == Common::kNoError ? 0 : 1;
176 }
177
writeGameData(Common::WriteStream * ws)178 Common::Error Magnetic::writeGameData(Common::WriteStream *ws) {
179 return ws->write(_saveData, _saveSize) == _saveSize ? Common::kNoError : Common::kWritingFailed;
180 }
181
182 } // End of namespace Magnetic
183 } // End of namespace Glk
184