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);
42 static void draw_rband(rtk_rect *r);
44 static uint32_t *vmem;
45 static int quit, dirty_valid;
46 static rtk_rect dirty;
47 static int mx, my, prev_mx, prev_my;
48 static rtk_rect rband, prev_rband;
51 int main(int argc, char **argv)
55 int mdx, mdy, bnstate, bndiff;
56 static int prev_bnstate;
60 __djgpp_nearptr_enable();
65 if(read_cpuid(&cpuid) == 0) {
72 fprintf(stderr, "No mouse detected. Make sure the mouse driver is installed\n");
76 if((env = getenv("RRLOG"))) {
77 if(tolower(env[0]) == 'c' && tolower(env[1]) == 'o' && tolower(env[2]) == 'm'
85 if(vid_init() == -1) {
89 if((vmidx = vid_findmode(640, 480, 32)) == -1) {
92 if(!(vmem = vid_setmode(vmidx))) {
98 win_aspect = (float)win_width / (float)win_height;
100 if(app_init() == -1) {
103 app_redisplay(0, 0, 0, 0);
105 app_reshape(win_width, win_height);
108 prev_mx = prev_my = -1;
114 if(kb_isdown(KEY_ALT)) {
115 modkeys |= KEY_MOD_ALT;
117 if(kb_isdown(KEY_CTRL)) {
118 modkeys |= KEY_MOD_CTRL;
120 if(kb_isdown(KEY_SHIFT)) {
121 modkeys |= KEY_MOD_SHIFT;
124 while((key = kb_getkey()) != -1) {
125 if(key == 'r' && (modkeys & KEY_MOD_CTRL)) {
126 app_redisplay(0, 0, 0, 0);
128 app_keyboard(key, 1);
130 if(quit) goto break_evloop;
133 bnstate = read_mouse_bn();
134 bndiff = bnstate ^ prev_bnstate;
135 prev_bnstate = bnstate;
137 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) {
162 long app_getmsec(void)
164 return time(0) * 1000; /* TODO */
167 void app_redisplay(int x, int y, int w, int h)
174 r.height = win_height;
183 rtk_rect_union(&dirty, &r);
190 void app_swap_buffers(void)
196 if(dirty.width < win_width || dirty.height < win_height) {
197 uint32_t *src = framebuf + dirty.y * win_width + dirty.x;
198 vid_blit32(dirty.x, dirty.y, dirty.width, dirty.height, src, 0);
200 vid_blitfb32(framebuf, 0);
205 draw_cursor(prev_mx, prev_my);
211 if(prev_rband.width) {
212 draw_rband(&prev_rband);
225 void app_resize(int x, int y)
229 void app_fullscreen(int fs)
233 void app_vsync(int vsync)
237 void app_rband(int x, int y, int w, int h)
249 static void draw_cursor(int x, int y)
252 uint32_t *fbptr = vmem + y * win_width + x;
256 if(y > offs) fbptr[-win_width * offs] ^= 0xffffff;
257 if(y < win_height - offs - 1) fbptr[win_width * offs] ^= 0xffffff;
258 if(x > offs) fbptr[-offs] ^= 0xffffff;
259 if(x < win_width - offs - 1) fbptr[offs] ^= 0xffffff;
263 static void draw_rband(rtk_rect *r)
267 uint32_t *fbptr, *bptr;
272 if(rect.width <= 0 || rect.height <= 0) {
276 fbptr = vmem + rect.y * win_width + rect.x;
277 bptr = fbptr + win_width * (rect.height - 1);
279 for(i=0; i<rect.width; i++) {
280 fbptr[i] ^= 0xffffff;
284 for(i=0; i<rect.height-2; i++) {
285 fbptr[0] ^= 0xffffff;
286 fbptr[rect.width - 1] ^= 0xffffff;