heed resolution options, and cross-version resizing
[retroray] / src / app.c
1 /*
2 RetroRay - integrated standalone vintage modeller/renderer
3 Copyright (C) 2023  John Tsiombikas <nuclear@mutantstargoat.com>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program.  If not, see <https://www.gnu.org/licenses/>.
17 */
18 #include "config.h"
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <time.h>
24 #include "gaw/gaw.h"
25 #include "app.h"
26 #include "timer.h"
27 #include "rend.h"
28 #include "options.h"
29 #include "font.h"
30 #include "util.h"
31 #include "rtk.h"
32
33 #ifdef GFX_SW
34 #include "gaw/gaw_sw.h"
35 #endif
36
37 static void gui_fill(rtk_rect *rect, uint32_t color);
38 static void gui_blit(int x, int y, rtk_icon *icon);
39 static void gui_drawtext(int x, int y, const char *str);
40 static void gui_textrect(const char *str, rtk_rect *rect);
41
42 int mouse_x, mouse_y, mouse_state[3];
43 unsigned int modkeys;
44 int win_width, win_height;
45 float win_aspect;
46 int fullscr;
47
48 long time_msec;
49
50 struct app_screen *cur_scr;
51
52 struct font *uifont;
53
54 uint32_t *framebuf;
55
56 struct scene *scn;
57
58 /* available screens */
59 #define MAX_SCREENS     8
60 static struct app_screen *screens[MAX_SCREENS];
61 static int num_screens;
62
63
64 int app_init(void)
65 {
66         int i;
67         char *start_scr_name;
68         static rtk_draw_ops guigfx = {gui_fill, gui_blit, gui_drawtext, gui_textrect};
69
70 #if !defined(NDEBUG) && defined(DBG_FPEXCEPT)
71         infomsg("floating point exceptions enabled\n");
72         enable_fpexcept();
73 #endif
74
75 #ifdef GFX_SW
76         gaw_sw_init();
77 #endif
78         rend_init();
79
80         app_vsync(opt.vsync);
81         if(opt.fullscreen) {
82                 app_fullscreen(1);
83         }
84
85         /*dtx_target_user(txdraw, 0);*/
86         dtx_target_raster((unsigned char*)framebuf, win_width, win_height);
87         dtx_set(DTX_RASTER_THRESHOLD, 127);
88
89         uifont = malloc_nf(sizeof *uifont);
90         if(load_font(uifont, "data/uifont14.gmp") == -1) {
91                 free(uifont);
92                 return -1;
93         }
94
95         rtk_setup(&guigfx);
96
97         if(!(scn = create_scene())) {
98                 return -1;
99         }
100
101         /* initialize screens */
102         screens[num_screens++] = &scr_model;
103         screens[num_screens++] = &scr_rend;
104
105         start_scr_name = getenv("START_SCREEN");
106
107         for(i=0; i<num_screens; i++) {
108                 if(screens[i]->init() == -1) {
109                         return -1;
110                 }
111         }
112
113         time_msec = get_msec();
114
115         for(i=0; i<num_screens; i++) {
116                 if(screens[i]->name && start_scr_name && strcmp(screens[i]->name, start_scr_name) == 0) {
117                         app_chscr(screens[i]);
118                         break;
119                 }
120         }
121         if(!cur_scr) {
122                 app_chscr(&scr_model);
123         }
124
125         return 0;
126 }
127
128 void app_shutdown(void)
129 {
130         int i;
131
132         putchar('\n');
133
134         save_options("retroray.cfg");
135
136         for(i=0; i<num_screens; i++) {
137                 if(screens[i]->destroy) {
138                         screens[i]->destroy();
139                 }
140         }
141
142         destroy_font(uifont);
143         free(uifont);
144
145 #ifdef GFX_SW
146         gaw_sw_destroy();
147 #endif
148
149         free_scene(scn);
150
151         cleanup_logger();
152 }
153
154 void app_display(void)
155 {
156         time_msec = get_msec();
157
158         cur_scr->display();
159 }
160
161 void app_reshape(int x, int y)
162 {
163         int numpix = x * y;
164         int prev_numpix = win_width * win_height;
165
166         if(!framebuf || numpix > prev_numpix) {
167                 void *tmp;
168                 if(!(tmp = realloc(framebuf, numpix * sizeof *framebuf))) {
169                         errormsg("failed to resize framebuffer to %dx%d\n", x, y);
170                         return;
171                 }
172                 framebuf = tmp;
173         }
174 #ifdef GFX_SW
175         gaw_sw_framebuffer(x, y, framebuf);
176 #endif
177         dtx_target_raster((unsigned char*)framebuf, x, y);
178
179         win_width = x;
180         win_height = y;
181         win_aspect = (float)x / (float)y;
182         gaw_viewport(0, 0, x, y);
183
184         if(cur_scr && cur_scr->reshape) {
185                 cur_scr->reshape(x, y);
186         }
187
188         app_redisplay(0, 0, 0, 0);
189 }
190
191 void app_keyboard(int key, int press)
192 {
193         long msec;
194         static long prev_esc;
195
196         if(press) {
197                 switch(key) {
198 #ifdef DBG_ESCQUIT
199                 case 27:
200                         msec = get_msec();
201                         if(msec - prev_esc < 1000) {
202                                 app_quit();
203                                 return;
204                         }
205                         prev_esc = msec;
206                         break;
207 #endif
208
209                 case 'q':
210                         if(modkeys & KEY_MOD_CTRL) {
211                                 app_quit();
212                                 return;
213                         }
214                         break;
215
216                 case '\n':
217                 case '\r':
218                         if(modkeys & KEY_MOD_ALT) {
219                 case KEY_F11:
220                                 app_fullscreen(-1);
221                                 return;
222                         }
223                         break;
224                 }
225         }
226
227         if(cur_scr && cur_scr->keyboard) {
228                 cur_scr->keyboard(key, press);
229         }
230 }
231
232 void app_mouse(int bn, int st, int x, int y)
233 {
234         mouse_x = x;
235         mouse_y = y;
236         if(bn < 3) {
237                 mouse_state[bn] = st;
238         }
239
240         if(cur_scr && cur_scr->mouse) {
241                 cur_scr->mouse(bn, st, x, y);
242         }
243 }
244
245 void app_motion(int x, int y)
246 {
247         if(cur_scr && cur_scr->motion) {
248                 cur_scr->motion(x, y);
249         }
250         mouse_x = x;
251         mouse_y = y;
252 }
253
254 void app_sball_motion(int x, int y, int z)
255 {
256         if(cur_scr->sball_motion) {
257                 cur_scr->sball_motion(x, y, z);
258         }
259 }
260
261 void app_sball_rotate(int x, int y, int z)
262 {
263         if(cur_scr->sball_rotate) {
264                 cur_scr->sball_rotate(x, y, z);
265         }
266 }
267
268 void app_sball_button(int bn, int st)
269 {
270         if(cur_scr->sball_button) {
271                 cur_scr->sball_button(bn, st);
272         }
273 }
274
275 void app_chscr(struct app_screen *scr)
276 {
277         struct app_screen *prev = cur_scr;
278
279         if(!scr) return;
280
281         if(scr->start && scr->start() == -1) {
282                 return;
283         }
284         if(scr->reshape) {
285                 scr->reshape(win_width, win_height);
286         }
287
288         if(prev && prev->stop) {
289                 prev->stop();
290         }
291         cur_scr = scr;
292 }
293
294 static void gui_fill(rtk_rect *rect, uint32_t color)
295 {
296         int i, j;
297         uint32_t *fb = framebuf + rect->y * win_width + rect->x;
298
299         for(i=0; i<rect->height; i++) {
300                 for(j=0; j<rect->width; j++) {
301                         fb[j] = color;
302                 }
303                 fb += win_width;
304         }
305 }
306
307 static void gui_blit(int x, int y, rtk_icon *icon)
308 {
309         int i, j;
310         uint32_t *dest, *src;
311
312         dest = framebuf + y * win_width + x;
313         src = icon->pixels;
314
315         for(i=0; i<icon->height; i++) {
316                 for(j=0; j<icon->width; j++) {
317                         int r = src[j] & 0xff;
318                         int g = (src[j] >> 8) & 0xff;
319                         int b = (src[j] >> 16) & 0xff;
320                         dest[j] = 0xff000000 | (r << 16) | (g << 8) | b;
321                 }
322                 dest += win_width;
323                 src += icon->scanlen;
324         }
325 }
326
327 static void gui_drawtext(int x, int y, const char *str)
328 {
329 }
330
331 static void gui_textrect(const char *str, rtk_rect *rect)
332 {
333         rect->x = rect->y = 0;
334         rect->width = 20;
335         rect->height = 10;/* TODO */
336 }