Debian Codegraph @

terps/glulxe/main.c

raw symbols SCIP
1 /* main.c: Glulxe top-level code.
2 Designed by Andrew Plotkin <[email protected]>
3 http://eblong.com/zarf/glulx/index.html
4 */
5
6 #include "glk.h"
7 #include "glulxe.h"
8
9 int vm_exited_cleanly = TRUE;
10 strid_t gamefile = NULL; /* The stream containing the Glulx file. */
11 glui32 gamefile_start = 0; /* The position within the stream. (This will not
12 be zero if the Glulx file is a chunk inside a Blorb archive.) */
13 glui32 gamefile_len = 0; /* The length within the stream. */
14 char *init_err = NULL;
15 char *init_err2 = NULL;
16
17 glui32 init_rng_seed = 0;
18
19 /* The library_start_hook is called at the beginning of glk_main. This
20 is not normally necessary -- the library can do all its setup work
21 before calling glk_main -- but iosglk has some weird cases which
22 require it. */
23 static void (*library_start_hook)(void) = NULL;
24 /* The library_autorestore_hook is called right after the VM's initial
25 setup. This is an appropriate time to autorestore an initial game
26 state, if the library has that capability. (Currently, only iosglk
27 and remglk do.) */
28 static void (*library_autorestore_hook)(void) = NULL;
29
30 static winid_t get_error_win(void);
31 static void stream_hexnum(glsi32 val);
32
33 /* glk_main():
34 The top-level routine. This does everything, and consequently is
35 very simple.
36 */
37 void glk_main()
38 {
39 vm_exited_cleanly = FALSE;
40
41 if (library_start_hook)
42 library_start_hook();
43
44 if (init_err) {
45 fatal_error_2(init_err, init_err2);
46 return;
47 }
48
49 if (!is_gamefile_valid()) {
50 /* The fatal error has already been displayed. */
51 return;
52 }
53
54 glulx_setrandom(init_rng_seed);
55 #ifdef FLOAT_SUPPORT
56 if (!init_float()) {
57 return;
58 }
59 #endif /* FLOAT_SUPPORT */
60 if (!init_dispatch()) {
61 return;
62 }
63 if (!init_profile()) {
64 return;
65 }
66
67 setup_vm();
68 if (library_autorestore_hook)
69 library_autorestore_hook();
70 execute_loop();
71 finalize_vm();
72
73 gamefile = NULL;
74 gamefile_start = 0;
75 gamefile_len = 0;
76 init_err = NULL;
77 vm_exited_cleanly = TRUE;
78
79 profile_quit();
80 glk_exit();
81 }
82
83 void set_library_start_hook(void (*func)(void))
84 {
85 library_start_hook = func;
86 }
87
88 void set_library_autorestore_hook(void (*func)(void))
89 {
90 library_autorestore_hook = func;
91 }
92
93 /* get_error_win():
94 Return a window in which to display errors. The first time this is called,
95 it creates a new window; after that it returns the window it first
96 created.
97 */
98 static winid_t get_error_win()
99 {
100 static winid_t errorwin = NULL;
101
102 if (!errorwin) {
103 winid_t rootwin = glk_window_get_root();
104 if (!rootwin) {
105 errorwin = glk_window_open(0, 0, 0, wintype_TextBuffer, 1);
106 }
107 else {
108 errorwin = glk_window_open(rootwin, winmethod_Below | winmethod_Fixed,
109 3, wintype_TextBuffer, 0);
110 }
111 if (!errorwin)
112 errorwin = rootwin;
113 }
114
115 return errorwin;
116 }
117
118 /* fatal_error_handler():
119 Display an error in the error window, and then exit.
120 */
121 void fatal_error_handler(char *str, char *arg, int useval, glsi32 val)
122 {
123 winid_t win;
124
125 /* If the debugger is compiled in, send the error message to the debug
126 console. This may also block for debug commands, depending on
127 preferences. */
128 debugger_handle_crash(str);
129
130 win = get_error_win();
131 if (win) {
132 glk_set_window(win);
133 glk_put_string("Glulxe fatal error: ");
134 glk_put_string(str);
135 if (arg || useval) {
136 glk_put_string(" (");
137 if (arg)
138 glk_put_string(arg);
139 if (arg && useval)
140 glk_put_string(" ");
141 if (useval)
142 stream_hexnum(val);
143 glk_put_string(")");
144 }
145 glk_put_string("\n");
146 }
147 glk_exit();
148 }
149
150 /* nonfatal_warning_handler():
151 Display a warning in the error window, and then continue.
152 */
153 void nonfatal_warning_handler(char *str, char *arg, int useval, glsi32 val)
154 {
155 winid_t win = get_error_win();
156 if (win) {
157 strid_t oldstr = glk_stream_get_current();
158 glk_set_window(win);
159 glk_put_string("Glulxe warning: ");
160 glk_put_string(str);
161 if (arg || useval) {
162 glk_put_string(" (");
163 if (arg)
164 glk_put_string(arg);
165 if (arg && useval)
166 glk_put_string(" ");
167 if (useval)
168 stream_hexnum(val);
169 glk_put_string(")");
170 }
171 glk_put_string("\n");
172 glk_stream_set_current(oldstr);
173 }
174 }
175
176 /* stream_hexnum():
177 Write a signed integer to the current Glk output stream.
178 */
179 static void stream_hexnum(glsi32 val)
180 {
181 char buf[16];
182 glui32 ival;
183 int ix;
184
185 if (val == 0) {
186 glk_put_char('0');
187 return;
188 }
189
190 if (val < 0) {
191 glk_put_char('-');
192 ival = -val;
193 }
194 else {
195 ival = val;
196 }
197
198 ix = 0;
199 while (ival != 0) {
200 buf[ix] = (ival % 16) + '0';
201 if (buf[ix] > '9')
202 buf[ix] += ('A' - ('9' + 1));
203 ix++;
204 ival /= 16;
205 }
206
207 while (ix) {
208 ix--;
209 glk_put_char(buf[ix]);
210 }
211 }
212