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/>.
32 #include "gaw/gaw_sw.h"
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);
41 int mouse_x, mouse_y, mouse_state[3];
43 int win_width, win_height;
49 struct app_screen *cur_scr;
57 /* available screens */
59 static struct app_screen *screens[MAX_SCREENS];
60 static int num_screens;
67 static rtk_draw_ops guigfx = {gui_fill, gui_blit, gui_drawtext, gui_textrect};
69 #if !defined(NDEBUG) && defined(DBG_FPEXCEPT)
70 printf("floating point exceptions enabled\n");
78 load_options("retroray.cfg");
79 app_resize(opt.xres, opt.yres);
85 dtx_target_user(txdraw, 0);
87 uifont = malloc_nf(sizeof *uifont);
88 if(load_font(uifont, "data/uifont12.gmp") == -1) {
95 if(!(scn = create_scene())) {
99 /* initialize screens */
100 screens[num_screens++] = &scr_model;
101 screens[num_screens++] = &scr_rend;
103 start_scr_name = getenv("START_SCREEN");
105 for(i=0; i<num_screens; i++) {
106 if(screens[i]->init() == -1) {
111 time_msec = app_getmsec();
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]);
120 app_chscr(&scr_model);
126 void app_shutdown(void)
132 save_options("retroray.cfg");
134 for(i=0; i<num_screens; i++) {
135 if(screens[i]->destroy) {
136 screens[i]->destroy();
140 destroy_font(uifont);
152 void app_display(void)
154 time_msec = app_getmsec();
161 void app_reshape(int x, int y)
164 int prev_numpix = win_width * win_height;
166 printf("reshape(%d, %d)\n", x, y);
168 if(!framebuf || numpix > prev_numpix) {
170 if(!(tmp = realloc(framebuf, numpix * sizeof *framebuf))) {
171 errormsg("failed to resize framebuffer to %dx%d\n", x, y);
177 gaw_sw_framebuffer(x, y, framebuf);
182 win_aspect = (float)x / (float)y;
183 gaw_viewport(0, 0, x, y);
185 if(cur_scr && cur_scr->reshape) {
186 cur_scr->reshape(x, y);
190 void app_keyboard(int key, int press)
202 if(modkeys & KEY_MOD_ALT) {
211 if(cur_scr && cur_scr->keyboard) {
212 cur_scr->keyboard(key, press);
216 void app_mouse(int bn, int st, int x, int y)
221 mouse_state[bn] = st;
224 if(cur_scr && cur_scr->mouse) {
225 cur_scr->mouse(bn, st, x, y);
229 void app_motion(int x, int y)
231 if(cur_scr && cur_scr->motion) {
232 cur_scr->motion(x, y);
238 void app_sball_motion(int x, int y, int z)
240 if(cur_scr->sball_motion) {
241 cur_scr->sball_motion(x, y, z);
245 void app_sball_rotate(int x, int y, int z)
247 if(cur_scr->sball_rotate) {
248 cur_scr->sball_rotate(x, y, z);
252 void app_sball_button(int bn, int st)
254 if(cur_scr->sball_button) {
255 cur_scr->sball_button(bn, st);
259 void app_chscr(struct app_screen *scr)
261 struct app_screen *prev = cur_scr;
265 if(scr->start && scr->start() == -1) {
269 scr->reshape(win_width, win_height);
272 if(prev && prev->stop) {
278 static void gui_fill(rtk_rect *rect, uint32_t color)
281 uint32_t *fb = framebuf + rect->y * win_width + rect->x;
283 for(i=0; i<rect->height; i++) {
284 for(j=0; j<rect->width; j++) {
291 static void gui_blit(int x, int y, rtk_icon *icon)
294 uint32_t *dest, *src;
296 dest = framebuf + y * win_width + x;
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;
307 src += icon->scanlen;
311 static void gui_drawtext(int x, int y, const char *str)
315 static void gui_textrect(const char *str, rtk_rect *rect)
317 rect->x = rect->y = 0;
319 rect->height = 10;/* TODO */
322 static void txdraw(struct dtx_vertex *v, int vcount, struct dtx_pixmap *pixmap, void *cls)