software rendering
[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
56 /* available screens */
57 #define MAX_SCREENS     8
58 static struct app_screen *screens[MAX_SCREENS];
59 static int num_screens;
60
61
62 int app_init(void)
63 {
64         int i;
65         char *start_scr_name;
66         static rtk_draw_ops guigfx = {gui_fill, gui_blit, gui_drawtext, gui_textrect};
67
68         init_logger();
69
70 #if !defined(NDEBUG) && defined(DBG_FPEXCEPT)
71         printf("floating point exceptions enabled\n");
72         enable_fpexcept();
73 #endif
74
75 #ifdef GFX_SW
76         gaw_sw_init();
77 #endif
78
79         load_options("retroray.cfg");
80         app_resize(opt.xres, opt.yres);
81         app_vsync(opt.vsync);
82         if(opt.fullscreen) {
83                 app_fullscreen(1);
84         }
85
86         dtx_target_user(txdraw, 0);
87
88         uifont = malloc_nf(sizeof *uifont);
89         if(load_font(uifont, "data/uifont12.gmp") == -1) {
90                 free(uifont);
91                 return -1;
92         }
93
94         rtk_setup(&guigfx);
95
96         /* initialize screens */
97         screens[num_screens++] = &scr_model;
98         screens[num_screens++] = &scr_rend;
99
100         start_scr_name = getenv("START_SCREEN");
101
102         for(i=0; i<num_screens; i++) {
103                 if(screens[i]->init() == -1) {
104                         return -1;
105                 }
106         }
107
108         time_msec = app_getmsec();
109
110         for(i=0; i<num_screens; i++) {
111                 if(screens[i]->name && start_scr_name && strcmp(screens[i]->name, start_scr_name) == 0) {
112                         app_chscr(screens[i]);
113                         break;
114                 }
115         }
116         if(!cur_scr) {
117                 app_chscr(&scr_model);
118         }
119
120         return 0;
121 }
122
123 void app_shutdown(void)
124 {
125         int i;
126
127         putchar('\n');
128
129         save_options("retroray.cfg");
130
131         for(i=0; i<num_screens; i++) {
132                 if(screens[i]->destroy) {
133                         screens[i]->destroy();
134                 }
135         }
136
137         destroy_font(uifont);
138         free(uifont);
139
140 #ifdef GFX_SW
141         gaw_sw_destroy();
142 #endif
143
144         cleanup_logger();
145 }
146
147 void app_display(void)
148 {
149         time_msec = app_getmsec();
150
151         cur_scr->display();
152
153         app_swap_buffers();
154 }
155
156 void app_reshape(int x, int y)
157 {
158         int numpix = x * y;
159         int prev_numpix = win_width * win_height;
160
161         printf("reshape(%d, %d)\n", x, y);
162
163         if(!framebuf || numpix > prev_numpix) {
164                 void *tmp;
165                 if(!(tmp = realloc(framebuf, numpix * sizeof *framebuf))) {
166                         errormsg("failed to resize framebuffer to %dx%d\n", x, y);
167                         return;
168                 }
169                 framebuf = tmp;
170         }
171 #ifdef GFX_SW
172         gaw_sw_framebuffer(x, y, framebuf);
173 #endif
174
175         win_width = x;
176         win_height = y;
177         win_aspect = (float)x / (float)y;
178         gaw_viewport(0, 0, x, y);
179
180         if(cur_scr && cur_scr->reshape) {
181                 cur_scr->reshape(x, y);
182         }
183 }
184
185 void app_keyboard(int key, int press)
186 {
187         if(press) {
188                 switch(key) {
189 #ifdef DBG_ESCQUIT
190                 case 27:
191                         app_quit();
192                         return;
193 #endif
194
195                 case '\n':
196                 case '\r':
197                         if(modkeys & KEY_MOD_ALT) {
198                 case KEY_F11:
199                                 app_fullscreen(-1);
200                                 return;
201                         }
202                         break;
203                 }
204         }
205
206         if(cur_scr && cur_scr->keyboard) {
207                 cur_scr->keyboard(key, press);
208         }
209 }
210
211 void app_mouse(int bn, int st, int x, int y)
212 {
213         mouse_x = x;
214         mouse_y = y;
215         if(bn < 3) {
216                 mouse_state[bn] = st;
217         }
218
219         if(cur_scr && cur_scr->mouse) {
220                 cur_scr->mouse(bn, st, x, y);
221         }
222 }
223
224 void app_motion(int x, int y)
225 {
226         if(cur_scr && cur_scr->motion) {
227                 cur_scr->motion(x, y);
228         }
229         mouse_x = x;
230         mouse_y = y;
231 }
232
233 void app_sball_motion(int x, int y, int z)
234 {
235         if(cur_scr->sball_motion) {
236                 cur_scr->sball_motion(x, y, z);
237         }
238 }
239
240 void app_sball_rotate(int x, int y, int z)
241 {
242         if(cur_scr->sball_rotate) {
243                 cur_scr->sball_rotate(x, y, z);
244         }
245 }
246
247 void app_sball_button(int bn, int st)
248 {
249         if(cur_scr->sball_button) {
250                 cur_scr->sball_button(bn, st);
251         }
252 }
253
254 void app_chscr(struct app_screen *scr)
255 {
256         struct app_screen *prev = cur_scr;
257
258         if(!scr) return;
259
260         if(scr->start && scr->start() == -1) {
261                 return;
262         }
263         if(scr->reshape) {
264                 scr->reshape(win_width, win_height);
265         }
266
267         if(prev && prev->stop) {
268                 prev->stop();
269         }
270         cur_scr = scr;
271 }
272
273 static void gui_fill(rtk_rect *rect, uint32_t color)
274 {
275         int i, j;
276         uint32_t *fb = framebuf + rect->y * win_width + rect->x;
277
278         for(i=0; i<rect->height; i++) {
279                 for(j=0; j<rect->width; j++) {
280                         fb[j] = color;
281                 }
282                 fb += win_width;
283         }
284 }
285
286 static void gui_blit(int x, int y, rtk_icon *icon)
287 {
288         int i, j;
289         uint32_t *dest, *src;
290
291         dest = framebuf + y * win_width + x;
292         src = icon->pixels;
293
294         for(i=0; i<icon->height; i++) {
295                 for(j=0; j<icon->width; j++) {
296                         int r = src[j] & 0xff;
297                         int g = (src[j] >> 8) & 0xff;
298                         int b = (src[j] >> 16) & 0xff;
299                         dest[j] = 0xff000000 | (r << 16) | (g << 8) | b;
300                 }
301                 dest += win_width;
302                 src += icon->scanlen;
303         }
304 }
305
306 static void gui_drawtext(int x, int y, const char *str)
307 {
308 }
309
310 static void gui_textrect(const char *str, rtk_rect *rect)
311 {
312         rect->x = rect->y = 0;
313         rect->width = 20;
314         rect->height = 10;/* TODO */
315 }
316
317 static void txdraw(struct dtx_vertex *v, int vcount, struct dtx_pixmap *pixmap, void *cls)
318 {
319 }