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/>.
30 static void txdraw(struct dtx_vertex *v, int vcount, struct dtx_pixmap *pixmap, void *cls);
32 int mouse_x, mouse_y, mouse_state[3];
34 int win_width, win_height;
40 struct app_screen *cur_scr;
44 /* available screens */
46 static struct app_screen *screens[MAX_SCREENS];
47 static int num_screens;
55 #if !defined(NDEBUG) && defined(DBG_FPEXCEPT)
56 printf("floating point exceptions enabled\n");
60 load_options("retroray.cfg");
61 app_resize(opt.xres, opt.yres);
67 dtx_target_user(txdraw, 0);
69 uifont = malloc_nf(sizeof *uifont);
70 if(load_font(uifont, "data/uifont.gmp") == -1) {
75 /* initialize screens */
76 screens[num_screens++] = &scr_model;
77 screens[num_screens++] = &scr_rend;
79 start_scr_name = getenv("START_SCREEN");
81 for(i=0; i<num_screens; i++) {
82 if(screens[i]->init() == -1) {
87 time_msec = app_getmsec();
89 for(i=0; i<num_screens; i++) {
90 if(screens[i]->name && start_scr_name && strcmp(screens[i]->name, start_scr_name) == 0) {
91 app_chscr(screens[i]);
96 app_chscr(&scr_model);
102 void app_shutdown(void)
108 save_options("retroray.cfg");
110 for(i=0; i<num_screens; i++) {
111 if(screens[i]->destroy) {
112 screens[i]->destroy();
116 destroy_font(uifont);
120 void app_display(void)
122 time_msec = app_getmsec();
129 void app_reshape(int x, int y)
133 win_aspect = (float)x / (float)y;
134 gaw_viewport(0, 0, x, y);
136 if(cur_scr && cur_scr->reshape) {
137 cur_scr->reshape(x, y);
141 void app_keyboard(int key, int press)
153 if(modkeys & KEY_MOD_ALT) {
162 if(cur_scr && cur_scr->keyboard) {
163 cur_scr->keyboard(key, press);
167 void app_mouse(int bn, int st, int x, int y)
172 mouse_state[bn] = st;
175 if(cur_scr && cur_scr->mouse) {
176 cur_scr->mouse(bn, st, x, y);
180 void app_motion(int x, int y)
182 if(cur_scr && cur_scr->motion) {
183 cur_scr->motion(x, y);
189 void app_sball_motion(int x, int y, int z)
191 if(cur_scr->sball_motion) {
192 cur_scr->sball_motion(x, y, z);
196 void app_sball_rotate(int x, int y, int z)
198 if(cur_scr->sball_rotate) {
199 cur_scr->sball_rotate(x, y, z);
203 void app_sball_button(int bn, int st)
205 if(cur_scr->sball_button) {
206 cur_scr->sball_button(bn, st);
210 void app_chscr(struct app_screen *scr)
212 struct app_screen *prev = cur_scr;
216 if(scr->start && scr->start() == -1) {
220 scr->reshape(win_width, win_height);
223 if(prev && prev->stop) {
229 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);