font rendering and redraw fixes
[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 "options.h"
27 #include "font.h"
28 #include "util.h"
29 #include "rtk.h"
30
31 #ifdef GFX_SW
32 #include "gaw/gaw_sw.h"
33 #endif
34
35 static void gui_fill(rtk_rect *rect, uint32_t color);
36 static void gui_blit(int x, int y, rtk_icon *icon);
37 static void gui_drawtext(int x, int y, const char *str);
38 static void gui_textrect(const char *str, rtk_rect *rect);
39 static void txdraw(struct dtx_vertex *v, int vcount, struct dtx_pixmap *pixmap, void *cls);
40
41 int mouse_x, mouse_y, mouse_state[3];
42 unsigned int modkeys;
43 int win_width, win_height;
44 float win_aspect;
45 int fullscr;
46
47 long time_msec;
48
49 struct app_screen *cur_scr;
50
51 struct font *uifont;
52
53 uint32_t *framebuf;
54
55 struct scene *scn;
56
57 /* available screens */
58 #define MAX_SCREENS     8
59 static struct app_screen *screens[MAX_SCREENS];
60 static int num_screens;
61
62
63 int app_init(void)
64 {
65         int i;
66         char *start_scr_name;
67         static rtk_draw_ops guigfx = {gui_fill, gui_blit, gui_drawtext, gui_textrect};
68
69 #if !defined(NDEBUG) && defined(DBG_FPEXCEPT)
70         printf("floating point exceptions enabled\n");
71         enable_fpexcept();
72 #endif
73
74 #ifdef GFX_SW
75         gaw_sw_init();
76 #endif
77
78         load_options("retroray.cfg");
79         app_resize(opt.xres, opt.yres);
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 = app_getmsec();
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 = app_getmsec();
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         printf("reshape(%d, %d)\n", x, y);
167
168         if(!framebuf || numpix > prev_numpix) {
169                 void *tmp;
170                 if(!(tmp = realloc(framebuf, numpix * sizeof *framebuf))) {
171                         errormsg("failed to resize framebuffer to %dx%d\n", x, y);
172                         return;
173                 }
174                 framebuf = tmp;
175         }
176 #ifdef GFX_SW
177         gaw_sw_framebuffer(x, y, framebuf);
178 #endif
179         dtx_target_raster((unsigned char*)framebuf, x, y);
180
181         win_width = x;
182         win_height = y;
183         win_aspect = (float)x / (float)y;
184         gaw_viewport(0, 0, x, y);
185
186         if(cur_scr && cur_scr->reshape) {
187                 cur_scr->reshape(x, y);
188         }
189 }
190
191 void app_keyboard(int key, int press)
192 {
193         if(press) {
194                 switch(key) {
195 #ifdef DBG_ESCQUIT
196                 case 27:
197                         app_quit();
198                         return;
199 #endif
200
201                 case 'q':
202                         if(modkeys & KEY_MOD_CTRL) {
203                                 app_quit();
204                                 return;
205                         }
206                         break;
207
208                 case '\n':
209                 case '\r':
210                         if(modkeys & KEY_MOD_ALT) {
211                 case KEY_F11:
212                                 app_fullscreen(-1);
213                                 return;
214                         }
215                         break;
216                 }
217         }
218
219         if(cur_scr && cur_scr->keyboard) {
220                 cur_scr->keyboard(key, press);
221         }
222 }
223
224 void app_mouse(int bn, int st, int x, int y)
225 {
226         mouse_x = x;
227         mouse_y = y;
228         if(bn < 3) {
229                 mouse_state[bn] = st;
230         }
231
232         if(cur_scr && cur_scr->mouse) {
233                 cur_scr->mouse(bn, st, x, y);
234         }
235 }
236
237 void app_motion(int x, int y)
238 {
239         if(cur_scr && cur_scr->motion) {
240                 cur_scr->motion(x, y);
241         }
242         mouse_x = x;
243         mouse_y = y;
244 }
245
246 void app_sball_motion(int x, int y, int z)
247 {
248         if(cur_scr->sball_motion) {
249                 cur_scr->sball_motion(x, y, z);
250         }
251 }
252
253 void app_sball_rotate(int x, int y, int z)
254 {
255         if(cur_scr->sball_rotate) {
256                 cur_scr->sball_rotate(x, y, z);
257         }
258 }
259
260 void app_sball_button(int bn, int st)
261 {
262         if(cur_scr->sball_button) {
263                 cur_scr->sball_button(bn, st);
264         }
265 }
266
267 void app_chscr(struct app_screen *scr)
268 {
269         struct app_screen *prev = cur_scr;
270
271         if(!scr) return;
272
273         if(scr->start && scr->start() == -1) {
274                 return;
275         }
276         if(scr->reshape) {
277                 scr->reshape(win_width, win_height);
278         }
279
280         if(prev && prev->stop) {
281                 prev->stop();
282         }
283         cur_scr = scr;
284 }
285
286 static void gui_fill(rtk_rect *rect, uint32_t color)
287 {
288         int i, j;
289         uint32_t *fb = framebuf + rect->y * win_width + rect->x;
290
291         for(i=0; i<rect->height; i++) {
292                 for(j=0; j<rect->width; j++) {
293                         fb[j] = color;
294                 }
295                 fb += win_width;
296         }
297 }
298
299 static void gui_blit(int x, int y, rtk_icon *icon)
300 {
301         int i, j;
302         uint32_t *dest, *src;
303
304         dest = framebuf + y * win_width + x;
305         src = icon->pixels;
306
307         for(i=0; i<icon->height; i++) {
308                 for(j=0; j<icon->width; j++) {
309                         int r = src[j] & 0xff;
310                         int g = (src[j] >> 8) & 0xff;
311                         int b = (src[j] >> 16) & 0xff;
312                         dest[j] = 0xff000000 | (r << 16) | (g << 8) | b;
313                 }
314                 dest += win_width;
315                 src += icon->scanlen;
316         }
317 }
318
319 static void gui_drawtext(int x, int y, const char *str)
320 {
321 }
322
323 static void gui_textrect(const char *str, rtk_rect *rect)
324 {
325         rect->x = rect->y = 0;
326         rect->width = 20;
327         rect->height = 10;/* TODO */
328 }
329
330 static void txdraw(struct dtx_vertex *v, int vcount, struct dtx_pixmap *pixmap, void *cls)
331 {
332 }