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/>.
34 static INLINE int clamp(int x, int a, int b)
41 static void draw_cursor(int x, int y);
43 static uint32_t *vmem;
44 static int quit, disp_pending, dirty_valid;
45 static rtk_rect dirty;
47 static rtk_rect rband, prev_rband;
50 int main(int argc, char **argv)
54 int mdx, mdy, prev_mx, prev_my, bnstate, bndiff;
55 static int prev_bnstate;
59 __djgpp_nearptr_enable();
64 if(read_cpuid(&cpuid) == 0) {
71 fprintf(stderr, "No mouse detected. Make sure the mouse driver is installed\n");
75 if((env = getenv("RRLOG"))) {
76 if(tolower(env[0]) == 'c' && tolower(env[1]) == 'o' && tolower(env[2]) == 'm'
84 if(vid_init() == -1) {
88 if((vmidx = vid_findmode(640, 480, 32)) == -1) {
91 if(!(vmem = vid_setmode(vmidx))) {
97 win_aspect = (float)win_width / (float)win_height;
99 if(app_init() == -1) {
102 app_redisplay(0, 0, 0, 0);
104 app_reshape(win_width, win_height);
112 if(kb_isdown(KEY_ALT)) {
113 modkeys |= KEY_MOD_ALT;
115 if(kb_isdown(KEY_CTRL)) {
116 modkeys |= KEY_MOD_CTRL;
118 if(kb_isdown(KEY_SHIFT)) {
119 modkeys |= KEY_MOD_SHIFT;
122 while((key = kb_getkey()) != -1) {
123 if(key == 'r' && (modkeys & KEY_MOD_CTRL)) {
124 app_redisplay(0, 0, 0, 0);
126 app_keyboard(key, 1);
128 if(quit) goto break_evloop;
131 bnstate = read_mouse_bn();
132 bndiff = bnstate ^ prev_bnstate;
133 prev_bnstate = bnstate;
135 read_mouse_rel(&mdx, &mdy);
138 mx = clamp(mx + mdx, 0, win_width - 1);
139 my = clamp(my + mdy, 0, win_height - 1);
143 if(bndiff & 1) app_mouse(0, bnstate & 1, mx, my);
144 if(bndiff & 2) app_mouse(1, bnstate & 2, mx, my);
145 if(bndiff & 4) app_mouse(3, bnstate & 4, mx, my);
147 if((mdx | mdy) != 0) {
158 draw_cursor(prev_mx, prev_my);
169 long app_getmsec(void)
171 return time(0) * 1000; /* TODO */
174 void app_redisplay(int x, int y, int w, int h)
181 r.height = win_height;
190 rtk_rect_union(&dirty, &r);
198 void app_swap_buffers(void)
204 if(dirty.width < win_width || dirty.height < win_height) {
205 uint32_t *src = framebuf + dirty.y * win_width + dirty.x;
206 vid_blit32(dirty.x, dirty.y, dirty.width, dirty.height, src, 0);
208 if(mx >= dirty.x && my >= dirty.y && mx < dirty.x + dirty.width && my < dirty.y + dirty.height) {
212 vid_blitfb32(framebuf, 0);
219 if(prev_rband.width) {
220 draw_rband(&prev_rband);
232 void app_resize(int x, int y)
236 void app_fullscreen(int fs)
240 void app_vsync(int vsync)
244 void app_rband(int x, int y, int w, int h)
248 prev_rband.width = 0;
257 static void draw_cursor(int x, int y)
260 uint32_t *fbptr = vmem + y * win_width + x;
264 if(y > offs) fbptr[-win_width * offs] ^= 0xffffff;
265 if(y < win_height - offs - 1) fbptr[win_width * offs] ^= 0xffffff;
266 if(x > offs) fbptr[-offs] ^= 0xffffff;
267 if(x < win_width - offs - 1) fbptr[offs] ^= 0xffffff;
271 static void draw_rband(rtk_rect *r)
275 uint32_t *fbptr, *bptr;
280 fbptr = vmem + rect.y * win_width + rect.x;
281 bptr = fbptr + win_width * (rect.height - 1);
283 for(i=0; i<rect.width; i++) {
284 fbptr[i] ^= 0xffffff;
288 for(i=0; i<rect.height-2; i++) {
289 fbptr[0] ^= 0xffffff;
290 fbptr[rect.width - 1] ^= 0xffffff;