Mercurial > molko
comparison libadventure/adventure/state/panic.c @ 162:629f55f3961e
core: rework states
author | David Demelier <markand@malikania.fr> |
---|---|
date | Sun, 18 Oct 2020 12:01:59 +0200 |
parents | libadventure/adventure/panic_state.c@c577c15df07f |
children | 6992085d47fd |
comparison
equal
deleted
inserted
replaced
161:31d7f23c0588 | 162:629f55f3961e |
---|---|
1 /* | |
2 * panic_state.c -- panic state | |
3 * | |
4 * Copyright (c) 2020 David Demelier <markand@malikania.fr> | |
5 * | |
6 * Permission to use, copy, modify, and/or distribute this software for any | |
7 * purpose with or without fee is hereby granted, provided that the above | |
8 * copyright notice and this permission notice appear in all copies. | |
9 * | |
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
17 */ | |
18 | |
19 #include <assert.h> | |
20 #include <stdio.h> | |
21 #include <stdlib.h> | |
22 #include <stdnoreturn.h> | |
23 #include <string.h> | |
24 | |
25 #include <core/error.h> | |
26 #include <core/event.h> | |
27 #include <core/font.h> | |
28 #include <core/game.h> | |
29 #include <core/painter.h> | |
30 #include <core/panic.h> | |
31 #include <core/state.h> | |
32 #include <core/sys.h> | |
33 #include <core/texture.h> | |
34 #include <core/util.h> | |
35 #include <core/window.h> | |
36 | |
37 #include <ui/align.h> | |
38 #include <ui/theme.h> | |
39 | |
40 #include <rpg/map_state.h> | |
41 | |
42 #include "panic.h" | |
43 | |
44 #define BACKGROUND 0x4f5070ff | |
45 #define FOREGROUND 0xffffffff | |
46 | |
47 #define OUT "molko-adventure.txt" | |
48 | |
49 struct view { | |
50 struct { | |
51 struct texture tex; | |
52 int x; | |
53 int y; | |
54 } texts[4]; | |
55 }; | |
56 | |
57 static noreturn void | |
58 die(const char *fmt, ...) | |
59 { | |
60 assert(fmt); | |
61 | |
62 va_list ap; | |
63 | |
64 va_start(ap, fmt); | |
65 fprintf(stderr, "abort: "); | |
66 vfprintf(stderr, fmt, ap); | |
67 va_end(ap); | |
68 abort(); | |
69 exit(1); | |
70 } | |
71 | |
72 static noreturn void | |
73 stop(void) | |
74 { | |
75 die("%s", error()); | |
76 } | |
77 | |
78 static void | |
79 dump(void) | |
80 { | |
81 FILE *fp; | |
82 | |
83 if ((fp = fopen(OUT, "w"))) { | |
84 /* TODO: add more info here. */ | |
85 fprintf(fp, "Molko's Adventure crash dump report\n"); | |
86 fclose(fp); | |
87 } | |
88 | |
89 abort(); | |
90 } | |
91 | |
92 static struct view * | |
93 init(void) | |
94 { | |
95 struct theme *theme; | |
96 struct view *view; | |
97 struct font font; | |
98 | |
99 theme = theme_default(); | |
100 view = ecalloc(1, sizeof (*view)); | |
101 | |
102 /* Generate the texts. */ | |
103 font_shallow(&font, theme->fonts[THEME_FONT_INTERFACE]); | |
104 font.style = FONT_STYLE_ANTIALIASED, | |
105 font.color = FOREGROUND; | |
106 | |
107 if (!font_render(&font, &view->texts[0].tex, "An unrecoverable error occured and the game cannot continue.") || | |
108 !font_render(&font, &view->texts[1].tex, "Please report the detailed error as provided below.") || | |
109 !font_render(&font, &view->texts[2].tex, "Press <s> to save information and generate a core dump.") || | |
110 !font_render(&font, &view->texts[3].tex, "Press <q> to quit without saving information.")) | |
111 die("%s", error()); | |
112 | |
113 /* All align x the same. */ | |
114 for (size_t i = 0; i < NELEM(view->texts); ++i) | |
115 view->texts[i].x = theme->padding; | |
116 | |
117 /* Header (0-1). */ | |
118 view->texts[0].y = theme->padding; | |
119 view->texts[1].y = view->texts[0].y + view->texts[0].tex.h + theme->padding; | |
120 | |
121 /* Footer. (2-3). */ | |
122 view->texts[3].y = window.h - view->texts[2].tex.h - theme->padding; | |
123 view->texts[2].y = view->texts[3].y - view->texts[3].tex.h - theme->padding; | |
124 | |
125 return view; | |
126 } | |
127 | |
128 static void | |
129 handle_keydown(const struct event_key *ev) | |
130 { | |
131 assert(ev); | |
132 | |
133 switch (ev->key) { | |
134 case KEY_q: | |
135 game_quit(); | |
136 break; | |
137 case KEY_s: | |
138 dump(); | |
139 break; | |
140 default: | |
141 break; | |
142 } | |
143 } | |
144 | |
145 static void | |
146 start(struct state *state) | |
147 { | |
148 (void)state; | |
149 | |
150 /* We remove the panic handler to avoid infinite recursion. */ | |
151 panic_handler = stop; | |
152 } | |
153 | |
154 static void | |
155 handle(struct state *state, const union event *ev) | |
156 { | |
157 assert(ev); | |
158 | |
159 (void)state; | |
160 | |
161 switch (ev->type) { | |
162 case EVENT_KEYDOWN: | |
163 handle_keydown(&ev->key); | |
164 break; | |
165 default: | |
166 break; | |
167 } | |
168 } | |
169 | |
170 static void | |
171 draw(struct state *state) | |
172 { | |
173 struct theme *theme = theme_default(); | |
174 struct view *view = state->data; | |
175 struct texture tex; | |
176 struct font font; | |
177 int x, y; | |
178 | |
179 painter_set_color(BACKGROUND); | |
180 painter_clear(); | |
181 | |
182 for (size_t i = 0; i < NELEM(view->texts); ++i) | |
183 texture_draw(&view->texts[i].tex, view->texts[i].x, view->texts[i].y); | |
184 | |
185 /* The error is only available here. */ | |
186 font_shallow(&font, theme->fonts[THEME_FONT_INTERFACE]); | |
187 font.color = FOREGROUND; | |
188 font.style = FONT_STYLE_ANTIALIASED; | |
189 | |
190 if (!font_render(&font, &tex, error())) | |
191 die("%s\n", error()); | |
192 | |
193 align(ALIGN_LEFT, &x, &y, tex.w, tex.h, 0, 0, window.w, window.h); | |
194 | |
195 texture_draw(&tex, x + theme->padding, y); | |
196 texture_finish(&tex); | |
197 } | |
198 | |
199 static void | |
200 finish(struct state *state) | |
201 { | |
202 struct view *view = state->data; | |
203 | |
204 if (!view) | |
205 return; | |
206 | |
207 for (size_t i = 0; i < NELEM(view->texts); ++i) | |
208 texture_finish(&view->texts[i].tex); | |
209 | |
210 free(view); | |
211 memset(state, 0, sizeof (*state)); | |
212 } | |
213 | |
214 void | |
215 panic_state(struct state *state) | |
216 { | |
217 assert(state); | |
218 | |
219 memset(state, 0, sizeof (*state)); | |
220 state->data = init(); | |
221 state->start = start; | |
222 state->handle = handle; | |
223 state->draw = draw; | |
224 state->finish = finish; | |
225 } |