initial commit
[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
29 static void txdraw(struct dtx_vertex *v, int vcount, struct dtx_pixmap *pixmap, void *cls);
30
31 int mouse_x, mouse_y, mouse_state[3];
32 unsigned int modkeys;
33 int win_width, win_height;
34 float win_aspect;
35 int fullscr;
36
37 long time_msec;
38
39 struct app_screen *cur_scr;
40
41 struct font *font;
42
43 /* available screens */
44 #define MAX_SCREENS     8
45 static struct app_screen *screens[MAX_SCREENS];
46 static int num_screens;
47
48
49 int app_init(void)
50 {
51         int i;
52         char *start_scr_name;
53
54 #if !defined(NDEBUG) && defined(DBG_FPEXCEPT)
55         printf("floating point exceptions enabled\n");
56         enable_fpexcept();
57 #endif
58
59         load_options("retroray.cfg");
60         app_resize(opt.xres, opt.yres);
61         app_vsync(opt.vsync);
62         if(opt.fullscreen) {
63                 app_fullscreen(1);
64         }
65
66         dtx_target_user(txdraw, 0);
67
68         font = malloc_nf(sizeof *font);
69         if(load_font(uifont, "data/uifont.gmp") == -1) {
70                 free(uifont);
71                 return -1;
72         }
73
74         /* initialize screens */
75         screens[num_screens++] = &scr_model;
76         screens[num_screens++] = &scr_rend;
77
78         start_scr_name = getenv("START_SCREEN");
79
80         for(i=0; i<num_screens; i++) {
81                 if(screens[i]->init() == -1) {
82                         return -1;
83                 }
84         }
85
86         time_msec = app_getmsec();
87
88         for(i=0; i<num_screens; i++) {
89                 if(screens[i]->name && start_scr_name && strcmp(screens[i]->name, start_scr_name) == 0) {
90                         app_chscr(screens[i]);
91                         break;
92                 }
93         }
94         if(!cur_scr) {
95                 app_chscr(&scr_logo);
96         }
97
98         return 0;
99 }
100
101 void app_shutdown(void)
102 {
103         int i;
104
105         putchar('\n');
106
107         save_options(GAME_CFG_FILE);
108
109         for(i=0; i<num_screens; i++) {
110                 if(screens[i]->destroy) {
111                         screens[i]->destroy();
112                 }
113         }
114
115         destroy_font(uifont);
116         free(uifont);
117 }
118
119 void app_display(void)
120 {
121         static long nframes, interv, prev_msec;
122
123         time_msec = app_getmsec();
124
125         cur_scr->display();
126
127         app_swap_buffers();
128 }
129
130 void app_reshape(int x, int y)
131 {
132         win_width = x;
133         win_height = y;
134         win_aspect = (float)x / (float)y;
135         gaw_viewport(0, 0, x, y);
136
137         if(cur_scr && cur_scr->reshape) {
138                 cur_scr->reshape(x, y);
139         }
140 }
141
142 void app_keyboard(int key, int press)
143 {
144         if(press) {
145                 switch(key) {
146 #ifdef DBG_ESCQUIT
147                 case 27:
148                         app_quit();
149                         return;
150 #endif
151
152                 case '\n':
153                 case '\r':
154                         if(modkeys & GKEY_MOD_ALT) {
155                 case GKEY_F11:
156                                 app_fullscreen(-1);
157                                 return;
158                         }
159                         break;
160                 }
161         }
162
163         if(cur_scr && cur_scr->keyboard) {
164                 cur_scr->keyboard(key, press);
165         }
166 }
167
168 void app_mouse(int bn, int st, int x, int y)
169 {
170         mouse_x = x;
171         mouse_y = y;
172         if(bn < 3) {
173                 mouse_state[bn] = st;
174         }
175
176         if(cur_scr && cur_scr->mouse) {
177                 cur_scr->mouse(bn, st, x, y);
178         }
179 }
180
181 void app_motion(int x, int y)
182 {
183         if(cur_scr && cur_scr->motion) {
184                 cur_scr->motion(x, y);
185         }
186         mouse_x = x;
187         mouse_y = y;
188 }
189
190 void app_sball_motion(int x, int y, int z)
191 {
192         if(cur_scr->sball_motion) {
193                 cur_scr->sball_motion(x, y, z);
194         }
195 }
196
197 void app_sball_rotate(int x, int y, int z)
198 {
199         if(cur_scr->sball_rotate) {
200                 cur_scr->sball_rotate(x, y, z);
201         }
202 }
203
204 void app_sball_button(int bn, int st)
205 {
206         if(cur_scr->sball_button) {
207                 cur_scr->sball_button(bn, st);
208         }
209 }
210
211 void app_chscr(struct app_screen *scr)
212 {
213         struct app_screen *prev = cur_scr;
214
215         if(!scr) return;
216
217         if(scr->start && scr->start() == -1) {
218                 return;
219         }
220         if(scr->reshape) {
221                 scr->reshape(win_width, win_height);
222         }
223
224         if(prev && prev->stop) {
225                 prev->stop();
226         }
227         cur_scr = scr;
228 }
229
230 static void txdraw(struct dtx_vertex *v, int vcount, struct dtx_pixmap *pixmap, void *cls)
231 {
232         int i, aref, npix;
233         unsigned char *src, *dest;
234         struct texture *tex = pixmap->udata;
235
236         if(!tex) {
237                 struct img_pixmap *img = img_create();
238                 img_set_pixels(img, pixmap->width, pixmap->height, IMG_FMT_RGBA32, 0);
239
240                 npix = pixmap->width * pixmap->height;
241                 src = pixmap->pixels;
242                 dest = img->pixels;
243                 for(i=0; i<npix; i++) {
244                         dest[0] = dest[1] = dest[2] = 0xff;
245                         dest[3] = *src++;
246                         dest += 4;
247                 }
248
249                 if(!(tex = tex_image(img))) {
250                         return;
251                 }
252                 pixmap->udata = tex;
253         }
254
255         gaw_save();
256         if(dtx_get(DTX_GL_BLEND)) {
257                 gaw_enable(GAW_BLEND);
258                 gaw_blend_func(GAW_SRC_ALPHA, GAW_ONE_MINUS_SRC_ALPHA);
259         } else {
260                 gaw_disable(GAW_BLEND);
261         }
262         if((aref = dtx_get(DTX_GL_ALPHATEST))) {
263                 gaw_enable(GAW_ALPHA_TEST);
264                 gaw_alpha_func(GAW_GREATER, aref);
265         } else {
266                 gaw_disable(GAW_ALPHA_TEST);
267         }
268
269         gaw_set_tex2d(tex->texid);
270
271         gaw_begin(GAW_TRIANGLES);
272         for(i=0; i<vcount; i++) {
273                 gaw_texcoord2f(v->s, v->t);
274                 gaw_vertex2f(v->x, v->y);
275                 v++;
276         }
277         gaw_end();
278
279         gaw_restore();
280         gaw_set_tex2d(0);
281 }