Debian Codegraph @

terps/bocfel/glkstart.cpp

raw symbols SCIP
1 // Copyright 2010-2024 Chris Spiegel.
2 //
3 // SPDX-License-Identifier: MIT
4
5 #include <initializer_list>
6 #include <string>
7
8 #include "options.h"
9 #include "screen.h"
10 #include "types.h"
11 #include "zterp.h"
12
13 extern "C" {
14 #include <glk.h>
15 }
16
17 #ifdef ZTERP_GLK_BLORB
18 extern "C" {
19 #include <gi_blorb.h>
20 }
21 #endif
22
23 static void load_resources();
24
25 #ifdef ZTERP_GLK_UNIX
26 extern "C" {
27 #include <glkstart.h>
28 }
29
30 // The “standard” function (provided by remglk as an extension) for
31 // getting a filename from a fileref is glkunix_fileref_get_filename;
32 // Gargoyle has always had the function garglk_fileref_get_name for this
33 // purpose, but has now adopted the remglk name for greater
34 // compatibility. If building under garglk, and the newer name is not
35 // available, fall back to the known-to-exist original name. Otherwise,
36 // use the new name.
37 #if defined(GARGLK) && !defined(GLKUNIX_FILEREF_GET_FILENAME)
38 #define glkunix_fileref_get_filename garglk_fileref_get_name
39 #endif
40
41 // Filled in by the Options constructor.
42 glkunix_argumentlist_t glkunix_arguments[128] = {
43 { nullptr, glkunix_arg_End, nullptr }
44 };
45
46 #ifdef ZTERP_GLK_BLORB
47 static strid_t load_file(const std::string &file, StreamRock rock)
48 {
49 return glkunix_stream_open_pathname(const_cast<char *>(file.c_str()), 0, static_cast<glui32>(rock));
50 }
51 #endif
52
53 int glkunix_startup_code(glkunix_startup_t *data)
54 {
55 #ifdef GARGLK
56 garglk_set_program_name("Bocfel");
57 #endif
58 options.process_arguments(data->argc, data->argv);
59
60 if (arg_status.any() || options.show_version || options.show_help) {
61 return 1;
62 }
63
64 // Not interrupt in the sense of SIGINT; it’s called by RemGlk
65 // when doing a -singleturn shutdown. That will involve closing
66 // all streams. So we must finalize any IO objects with Type::Glk
67 // first.
68 glk_set_interrupt_handler(screen_clean_up_glk_streams);
69
70 #ifdef GARGLK
71 if (!game_file.empty()) {
72 auto story_name = game_file;
73 auto slash = story_name.rfind('/');
74
75 if (slash != std::string::npos) {
76 story_name.erase(0, slash + 1);
77 }
78
79 garglk_set_story_name(story_name.c_str());
80 } else {
81 frefid_t ref = glk_fileref_create_by_prompt(fileusage_Data | fileusage_BinaryMode, filemode_Read, 0);
82 if (ref != nullptr) {
83 const char *filename = glkunix_fileref_get_filename(ref);
84 if (filename != nullptr) {
85 game_file = filename;
86 }
87 glk_fileref_destroy(ref);
88 }
89 }
90 #endif
91
92 if (!game_file.empty()) {
93 #ifndef ZTERP_OS_DOS
94 glkunix_set_base_file(&game_file[0]);
95 #endif
96 load_resources();
97 }
98
99 return 1;
100 }
101 #elif defined(ZTERP_GLK_WINGLK)
102 #include <cstdlib>
103
104 #include <WinGlk.h>
105
106 using namespace std::literals;
107
108 extern "C" {
109 int InitGlk(unsigned int);
110 }
111
112 #ifdef ZTERP_GLK_BLORB
113 static strid_t load_file(const std::string &file, StreamRock rock)
114 {
115 frefid_t ref = winglk_fileref_create_by_name(fileusage_BinaryMode | fileusage_Data, const_cast<char *>(file.c_str()), 0, static_cast<glui32>(rock));
116
117 if (ref == nullptr) {
118 return nullptr;
119 }
120
121 strid_t stream = glk_stream_open_file(ref, filemode_Read, 0);
122 glk_fileref_destroy(ref);
123 return stream;
124 }
125 #endif
126
127 static void startup()
128 {
129 winglk_app_set_name("Bocfel");
130 winglk_set_menu_name("&Bocfel");
131 winglk_set_about_text("Windows Bocfel " ZTERP_VERSION);
132 winglk_show_game_dialog();
133
134 options.process_arguments(__argc, __argv);
135
136 if (arg_status.any() || options.show_version || options.show_help) {
137 return;
138 }
139
140 if (game_file.empty()) {
141 const std::string patterns = "*.z1;*.z2;*.z3;*.z4;*.z5;*.z6;*.z7;*.z8;*.zblorb;*.zlb;*.blorb;*.blb";
142 std::string filter;
143 const char *filename;
144
145 filter = "Z-Code Files (" + patterns + ")|" + patterns + "|All Files (*.*)|*.*||";
146
147 filename = winglk_get_initial_filename(nullptr, "Choose a Z-Code Game", filter.c_str());
148 if (filename != nullptr) {
149 game_file = filename;
150 }
151 }
152
153 if (!game_file.empty()) {
154 auto slash = game_file.find_last_of("/\\");
155 std::string game_dir, filename;
156
157 if (slash != std::string::npos) {
158 game_dir = game_file.substr(0, slash);
159 filename = game_file.substr(slash + 1);
160 } else {
161 game_dir = ".";
162 filename = game_file;
163 }
164
165 winglk_set_resource_directory(game_dir.c_str());
166
167 if (!filename.empty()) {
168 auto dot = filename.rfind('.');
169 if (dot != std::string::npos) {
170 filename.resize(dot);
171 }
172
173 sglk_set_basename(&filename[0]);
174 }
175
176 load_resources();
177 }
178 }
179
180 int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
181 {
182 if (!InitGlk(0x00000700)) {
183 std::exit(EXIT_FAILURE);
184 }
185
186 startup();
187
188 glk_main();
189 glk_exit();
190
191 return 0;
192 }
193 #else
194 #ifdef ZTERP_GLK_BLORB
195 #define load_file(file, rock) nullptr
196 #endif
197 #error Glk on this platform is not supported.
198 #endif
199
200 // A Blorb file can contain the story file, or it can simply be an
201 // external package of resources. Try loading the main story file first;
202 // if it contains Blorb resources, use it. Otherwise, try to find a
203 // Blorb file that goes with the selected story.
204 static void load_resources()
205 {
206 #ifdef ZTERP_GLK_BLORB
207 auto set_map = [](const std::string &blorb_file) {
208 strid_t file = load_file(blorb_file, StreamRock::BlorbStream);
209 if (file != nullptr) {
210 if (giblorb_set_resource_map(file) == giblorb_err_None) {
211 screen_load_scale_info(blorb_file);
212
213 return true;
214 }
215 glk_stream_close(file, nullptr);
216 }
217
218 return false;
219 };
220
221 if (set_map(game_file)) {
222 return;
223 }
224
225 for (const auto &ext : {".blb", ".blorb"}) {
226 std::string blorb_file = game_file;
227 auto dot = blorb_file.rfind('.');
228 if (dot != std::string::npos) {
229 blorb_file.replace(dot, std::string::npos, ext);
230 } else {
231 blorb_file += ext;
232 }
233
234 if (set_map(blorb_file)) {
235 return;
236 }
237 }
238 #endif
239 }