invalidations and dirty bits, trying to minimize screen updates
[retroray] / src / dos / main.c
1 /*
2 RetroRay - integrated standalone vintage modeller/renderer
3 Copyright (C) 2023  John Tsiombikas <nuclear@mutantstargoat.com>
4
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.
9
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.
14
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/>.
17 */
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <ctype.h>
22 #include <time.h>
23 #include "app.h"
24 #include "keyb.h"
25 #include "vidsys.h"
26 #include "cdpmi.h"
27 #include "mouse.h"
28 #include "logger.h"
29 #include "options.h"
30 #include "cpuid.h"
31 #include "util.h"
32 #include "rtk.h"
33
34 static INLINE int clamp(int x, int a, int b)
35 {
36         if(x < a) return a;
37         if(x > b) return b;
38         return x;
39 }
40
41 static void draw_cursor(int x, int y);
42
43 static uint32_t *vmem;
44 static int quit, disp_pending, dirty_valid;
45 static rtk_rect dirty;
46 static int mx, my;
47
48 int main(int argc, char **argv)
49 {
50         int i;
51         int vmidx;
52         int mdx, mdy, prev_mx, prev_my, bnstate, bndiff;
53         static int prev_bnstate;
54         char *env;
55
56 #ifdef __DJGPP__
57         __djgpp_nearptr_enable();
58 #endif
59
60         init_logger();
61
62         if(read_cpuid(&cpuid) == 0) {
63                 print_cpuid(&cpuid);
64         }
65
66         kb_init();
67
68         if(!have_mouse()) {
69                 fprintf(stderr, "No mouse detected. Make sure the mouse driver is installed\n");
70                 return 1;
71         }
72
73         if((env = getenv("RRLOG"))) {
74                 if(tolower(env[0]) == 'c' && tolower(env[1]) == 'o' && tolower(env[2]) == 'm'
75                                 && isdigit(env[3])) {
76                         add_log_console(env);
77                 } else {
78                         add_log_file(env);
79                 }
80         }
81
82         if(vid_init() == -1) {
83                 return 1;
84         }
85
86         if((vmidx = vid_findmode(640, 480, 32)) == -1) {
87                 return 1;
88         }
89         if(!(vmem = vid_setmode(vmidx))) {
90                 return 1;
91         }
92
93         win_width = 640;
94         win_height = 480;
95         win_aspect = (float)win_width / (float)win_height;
96
97         if(app_init() == -1) {
98                 goto break_evloop;
99         }
100         app_redisplay(0, 0, 0, 0);
101
102         app_reshape(win_width, win_height);
103         mx = win_width / 2;
104         my = win_height / 2;
105
106         for(;;) {
107                 int key;
108
109                 modkeys = 0;
110                 if(kb_isdown(KEY_ALT)) {
111                         modkeys |= KEY_MOD_ALT;
112                 }
113                 if(kb_isdown(KEY_CTRL)) {
114                         modkeys |= KEY_MOD_CTRL;
115                 }
116                 if(kb_isdown(KEY_SHIFT)) {
117                         modkeys |= KEY_MOD_SHIFT;
118                 }
119
120                 while((key = kb_getkey()) != -1) {
121                         if(key == 'r' && (modkeys & KEY_MOD_CTRL)) {
122                                 app_redisplay(0, 0, 0, 0);
123                         } else {
124                                 app_keyboard(key, 1);
125                         }
126                         if(quit) goto break_evloop;
127                 }
128
129                 bnstate = read_mouse_bn();
130                 bndiff = bnstate ^ prev_bnstate;
131                 prev_bnstate = bnstate;
132
133                 read_mouse_rel(&mdx, &mdy);
134                 prev_mx = mx;
135                 prev_my = my;
136                 mx = clamp(mx + mdx, 0, win_width - 1);
137                 my = clamp(my + mdy, 0, win_height - 1);
138                 mdx = mx - prev_mx;
139                 mdy = my - prev_my;
140
141                 if(bndiff & 1) app_mouse(0, bnstate & 1, mx, my);
142                 if(bndiff & 2) app_mouse(1, bnstate & 2, mx, my);
143                 if(bndiff & 4) app_mouse(3, bnstate & 4, mx, my);
144
145                 if((mdx | mdy) != 0) {
146                         app_motion(mx, my);
147                 }
148
149                 if(disp_pending) {
150                         disp_pending = 0;
151                         app_display();
152                 }
153
154                 app_swap_buffers();
155
156                 draw_cursor(prev_mx, prev_my);
157                 draw_cursor(mx, my);
158         }
159
160 break_evloop:
161         app_shutdown();
162         vid_cleanup();
163         kb_shutdown();
164         return 0;
165 }
166
167 long app_getmsec(void)
168 {
169         return time(0) * 1000;  /* TODO */
170 }
171
172 void app_redisplay(int x, int y, int w, int h)
173 {
174         rtk_rect r;
175
176         if((w | h) == 0) {
177                 r.x = r.y = 0;
178                 r.width = win_width;
179                 r.height = win_height;
180         } else {
181                 r.x = x;
182                 r.y = y;
183                 r.width = w;
184                 r.height = h;
185         }
186
187         if(dirty_valid) {
188                 rtk_rect_union(&dirty, &r);
189         } else {
190                 dirty = r;
191         }
192         disp_pending = 1;
193         dirty_valid = 1;
194 }
195
196 void app_swap_buffers(void)
197 {
198         if(opt.vsync) {
199                 vid_vsync();
200         }
201         if(!dirty_valid) return;
202         if(dirty.width < win_width || dirty.height < win_height) {
203                 uint32_t *src = framebuf + dirty.y * win_width + dirty.x;
204                 vid_blit32(dirty.x, dirty.y, dirty.width, dirty.height, src, 0);
205
206                 if(mx >= dirty.x && my >= dirty.y && mx < dirty.x + dirty.width && my < dirty.y + dirty.height) {
207                         draw_cursor(mx, my);
208                 }
209         } else {
210                 vid_blitfb32(framebuf, 0);
211                 draw_cursor(mx, my);
212         }
213
214         dirty_valid = 0;
215 }
216
217 void app_quit(void)
218 {
219         quit = 1;
220 }
221
222 void app_resize(int x, int y)
223 {
224 }
225
226 void app_fullscreen(int fs)
227 {
228 }
229
230 void app_vsync(int vsync)
231 {
232 }
233
234 static void draw_cursor(int x, int y)
235 {
236         int i;
237         uint32_t *fbptr = vmem + y * win_width + x;
238
239         for(i=0; i<3; i++) {
240                 int offs = i + 1;
241                 if(y > offs) fbptr[-win_width * offs] ^= 0xffffff;
242                 if(y < win_height - offs - 1) fbptr[win_width * offs] ^= 0xffffff;
243                 if(x > offs) fbptr[-offs] ^= 0xffffff;
244                 if(x < win_width - offs - 1) fbptr[offs] ^= 0xffffff;
245         }
246 }