2 RetroRay - integrated standalone vintage modeller/renderer
3 Copyright (C) 2023 John Tsiombikas <nuclear@mutantstargoat.com>
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.
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.
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/>.
29 static void txdraw(struct dtx_vertex *v, int vcount, struct dtx_pixmap *pixmap, void *cls);
31 int mouse_x, mouse_y, mouse_state[3];
33 int win_width, win_height;
39 struct app_screen *cur_scr;
43 /* available screens */
45 static struct app_screen *screens[MAX_SCREENS];
46 static int num_screens;
54 #if !defined(NDEBUG) && defined(DBG_FPEXCEPT)
55 printf("floating point exceptions enabled\n");
59 load_options("retroray.cfg");
60 app_resize(opt.xres, opt.yres);
66 dtx_target_user(txdraw, 0);
68 font = malloc_nf(sizeof *font);
69 if(load_font(uifont, "data/uifont.gmp") == -1) {
74 /* initialize screens */
75 screens[num_screens++] = &scr_model;
76 screens[num_screens++] = &scr_rend;
78 start_scr_name = getenv("START_SCREEN");
80 for(i=0; i<num_screens; i++) {
81 if(screens[i]->init() == -1) {
86 time_msec = app_getmsec();
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]);
101 void app_shutdown(void)
107 save_options(GAME_CFG_FILE);
109 for(i=0; i<num_screens; i++) {
110 if(screens[i]->destroy) {
111 screens[i]->destroy();
115 destroy_font(uifont);
119 void app_display(void)
121 static long nframes, interv, prev_msec;
123 time_msec = app_getmsec();
130 void app_reshape(int x, int y)
134 win_aspect = (float)x / (float)y;
135 gaw_viewport(0, 0, x, y);
137 if(cur_scr && cur_scr->reshape) {
138 cur_scr->reshape(x, y);
142 void app_keyboard(int key, int press)
154 if(modkeys & GKEY_MOD_ALT) {
163 if(cur_scr && cur_scr->keyboard) {
164 cur_scr->keyboard(key, press);
168 void app_mouse(int bn, int st, int x, int y)
173 mouse_state[bn] = st;
176 if(cur_scr && cur_scr->mouse) {
177 cur_scr->mouse(bn, st, x, y);
181 void app_motion(int x, int y)
183 if(cur_scr && cur_scr->motion) {
184 cur_scr->motion(x, y);
190 void app_sball_motion(int x, int y, int z)
192 if(cur_scr->sball_motion) {
193 cur_scr->sball_motion(x, y, z);
197 void app_sball_rotate(int x, int y, int z)
199 if(cur_scr->sball_rotate) {
200 cur_scr->sball_rotate(x, y, z);
204 void app_sball_button(int bn, int st)
206 if(cur_scr->sball_button) {
207 cur_scr->sball_button(bn, st);
211 void app_chscr(struct app_screen *scr)
213 struct app_screen *prev = cur_scr;
217 if(scr->start && scr->start() == -1) {
221 scr->reshape(win_width, win_height);
224 if(prev && prev->stop) {
230 static void txdraw(struct dtx_vertex *v, int vcount, struct dtx_pixmap *pixmap, void *cls)
233 unsigned char *src, *dest;
234 struct texture *tex = pixmap->udata;
237 struct img_pixmap *img = img_create();
238 img_set_pixels(img, pixmap->width, pixmap->height, IMG_FMT_RGBA32, 0);
240 npix = pixmap->width * pixmap->height;
241 src = pixmap->pixels;
243 for(i=0; i<npix; i++) {
244 dest[0] = dest[1] = dest[2] = 0xff;
249 if(!(tex = tex_image(img))) {
256 if(dtx_get(DTX_GL_BLEND)) {
257 gaw_enable(GAW_BLEND);
258 gaw_blend_func(GAW_SRC_ALPHA, GAW_ONE_MINUS_SRC_ALPHA);
260 gaw_disable(GAW_BLEND);
262 if((aref = dtx_get(DTX_GL_ALPHATEST))) {
263 gaw_enable(GAW_ALPHA_TEST);
264 gaw_alpha_func(GAW_GREATER, aref);
266 gaw_disable(GAW_ALPHA_TEST);
269 gaw_set_tex2d(tex->texid);
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);