more dos port
[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
87         uifont = malloc_nf(sizeof *uifont);
88         if(load_font(uifont, "data/uifont12.gmp") == -1) {
89                 free(uifont);
90                 return -1;
91         }
92
93         rtk_setup(&guigfx);
94
95         if(!(scn = create_scene())) {
96                 return -1;
97         }
98
99         /* initialize screens */
100         screens[num_screens++] = &scr_model;
101         screens[num_screens++] = &scr_rend;
102
103         start_scr_name = getenv("START_SCREEN");
104
105         for(i=0; i<num_screens; i++) {
106                 if(screens[i]->init() == -1) {
107                         return -1;
108                 }
109         }
110
111         time_msec = app_getmsec();
112
113         for(i=0; i<num_screens; i++) {
114                 if(screens[i]->name && start_scr_name && strcmp(screens[i]->name, start_scr_name) == 0) {
115                         app_chscr(screens[i]);
116                         break;
117                 }
118         }
119         if(!cur_scr) {
120                 app_chscr(&scr_model);
121         }
122
123         return 0;
124 }
125
126 void app_shutdown(void)
127 {
128         int i;
129
130         putchar('\n');
131
132         save_options("retroray.cfg");
133
134         for(i=0; i<num_screens; i++) {
135                 if(screens[i]->destroy) {
136                         screens[i]->destroy();
137                 }
138         }
139
140         destroy_font(uifont);
141         free(uifont);
142
143 #ifdef GFX_SW
144         gaw_sw_destroy();
145 #endif
146
147         free_scene(scn);
148
149         cleanup_logger();
150 }
151
152 void app_display(void)
153 {
154         time_msec = app_getmsec();
155
156         cur_scr->display();
157
158         app_swap_buffers();
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
180         win_width = x;
181         win_height = y;
182         win_aspect = (float)x / (float)y;
183         gaw_viewport(0, 0, x, y);
184
185         if(cur_scr && cur_scr->reshape) {
186                 cur_scr->reshape(x, y);
187         }
188 }
189
190 void app_keyboard(int key, int press)
191 {
192         if(press) {
193                 switch(key) {
194 #ifdef DBG_ESCQUIT
195                 case 27:
196                         app_quit();
197                         return;
198 #endif
199
200                 case '\n':
201                 case '\r':
202                         if(modkeys & KEY_MOD_ALT) {
203                 case KEY_F11:
204                                 app_fullscreen(-1);
205                                 return;
206                         }
207                         break;
208                 }
209         }
210
211         if(cur_scr && cur_scr->keyboard) {
212                 cur_scr->keyboard(key, press);
213         }
214 }
215
216 void app_mouse(int bn, int st, int x, int y)
217 {
218         mouse_x = x;
219         mouse_y = y;
220         if(bn < 3) {
221                 mouse_state[bn] = st;
222         }
223
224         if(cur_scr && cur_scr->mouse) {
225                 cur_scr->mouse(bn, st, x, y);
226         }
227 }
228
229 void app_motion(int x, int y)
230 {
231         if(cur_scr && cur_scr->motion) {
232                 cur_scr->motion(x, y);
233         }
234         mouse_x = x;
235         mouse_y = y;
236 }
237
238 void app_sball_motion(int x, int y, int z)
239 {
240         if(cur_scr->sball_motion) {
241                 cur_scr->sball_motion(x, y, z);
242         }
243 }
244
245 void app_sball_rotate(int x, int y, int z)
246 {
247         if(cur_scr->sball_rotate) {
248                 cur_scr->sball_rotate(x, y, z);
249         }
250 }
251
252 void app_sball_button(int bn, int st)
253 {
254         if(cur_scr->sball_button) {
255                 cur_scr->sball_button(bn, st);
256         }
257 }
258
259 void app_chscr(struct app_screen *scr)
260 {
261         struct app_screen *prev = cur_scr;
262
263         if(!scr) return;
264
265         if(scr->start && scr->start() == -1) {
266                 return;
267         }
268         if(scr->reshape) {
269                 scr->reshape(win_width, win_height);
270         }
271
272         if(prev && prev->stop) {
273                 prev->stop();
274         }
275         cur_scr = scr;
276 }
277
278 static void gui_fill(rtk_rect *rect, uint32_t color)
279 {
280         int i, j;
281         uint32_t *fb = framebuf + rect->y * win_width + rect->x;
282
283         for(i=0; i<rect->height; i++) {
284                 for(j=0; j<rect->width; j++) {
285                         fb[j] = color;
286                 }
287                 fb += win_width;
288         }
289 }
290
291 static void gui_blit(int x, int y, rtk_icon *icon)
292 {
293         int i, j;
294         uint32_t *dest, *src;
295
296         dest = framebuf + y * win_width + x;
297         src = icon->pixels;
298
299         for(i=0; i<icon->height; i++) {
300                 for(j=0; j<icon->width; j++) {
301                         int r = src[j] & 0xff;
302                         int g = (src[j] >> 8) & 0xff;
303                         int b = (src[j] >> 16) & 0xff;
304                         dest[j] = 0xff000000 | (r << 16) | (g << 8) | b;
305                 }
306                 dest += win_width;
307                 src += icon->scanlen;
308         }
309 }
310
311 static void gui_drawtext(int x, int y, const char *str)
312 {
313 }
314
315 static void gui_textrect(const char *str, rtk_rect *rect)
316 {
317         rect->x = rect->y = 0;
318         rect->width = 20;
319         rect->height = 10;/* TODO */
320 }
321
322 static void txdraw(struct dtx_vertex *v, int vcount, struct dtx_pixmap *pixmap, void *cls)
323 {
324 }