stop appending repeat keys to the input buffer
[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 "timer.h"
25 #include "keyb.h"
26 #include "vidsys.h"
27 #include "cdpmi.h"
28 #include "mouse.h"
29 #include "logger.h"
30 #include "options.h"
31 #include "cpuid.h"
32 #include "util.h"
33 #include "rtk.h"
34
35 static INLINE int clamp(int x, int a, int b)
36 {
37         if(x < a) return a;
38         if(x > b) return b;
39         return x;
40 }
41
42 static void draw_cursor(int x, int y);
43 static void draw_rband(rtk_rect *r);
44
45 static uint32_t *vmem;
46 static int quit, dirty_valid;
47 static rtk_rect dirty;
48 static int mx, my, prev_mx, prev_my;
49 static rtk_rect rband, prev_rband;
50
51
52 int main(int argc, char **argv)
53 {
54         int i;
55         int vmidx;
56         int mdx, mdy, bnstate, bndiff;
57         static int prev_bnstate;
58         char *env;
59
60 #ifdef __DJGPP__
61         __djgpp_nearptr_enable();
62 #endif
63
64         if(!have_mouse()) {
65                 fprintf(stderr, "No mouse detected. Make sure the mouse driver is installed\n");
66                 return 1;
67         }
68
69         init_logger();
70
71         if(read_cpuid(&cpuid) == 0) {
72                 print_cpuid(&cpuid);
73         }
74
75         init_timer(0);
76         kb_init();
77
78         load_options(CFGFILE);
79
80         if((env = getenv("RRLOG"))) {
81                 if(tolower(env[0]) == 'c' && tolower(env[1]) == 'o' && tolower(env[2]) == 'm'
82                                 && isdigit(env[3])) {
83                         add_log_console(env);
84                 } else {
85                         add_log_file(env);
86                 }
87         }
88
89         if(vid_init() == -1) {
90                 return 1;
91         }
92
93         if((vmidx = vid_findmode(opt.xres, opt.yres, opt.bpp)) == -1) {
94                 return 1;
95         }
96         if(!(vmem = vid_setmode(vmidx))) {
97                 return 1;
98         }
99
100         win_width = opt.xres;
101         win_height = opt.yres;
102         win_aspect = (float)win_width / (float)win_height;
103
104         if(app_init() == -1) {
105                 goto break_evloop;
106         }
107         app_redisplay(0, 0, 0, 0);
108
109         app_reshape(win_width, win_height);
110         mx = win_width / 2;
111         my = win_height / 2;
112         prev_mx = prev_my = -1;
113
114         for(;;) {
115                 int key;
116
117                 modkeys = 0;
118                 if(kb_isdown(KEY_ALT)) {
119                         modkeys |= KEY_MOD_ALT;
120                 }
121                 if(kb_isdown(KEY_CTRL)) {
122                         modkeys |= KEY_MOD_CTRL;
123                 }
124                 if(kb_isdown(KEY_SHIFT)) {
125                         modkeys |= KEY_MOD_SHIFT;
126                 }
127
128                 while((key = kb_getkey()) != -1) {
129                         if(key == 'r' && (modkeys & KEY_MOD_CTRL)) {
130                                 app_redisplay(0, 0, 0, 0);
131                         } else {
132                                 app_keyboard(key, 1);
133                         }
134                         if(quit) goto break_evloop;
135                 }
136
137                 bnstate = read_mouse_bn();
138                 bndiff = bnstate ^ prev_bnstate;
139                 prev_bnstate = bnstate;
140
141                 read_mouse_rel(&mdx, &mdy);
142                 mx = clamp(mx + mdx, 0, win_width - 1);
143                 my = clamp(my + mdy, 0, win_height - 1);
144                 mdx = mx - prev_mx;
145                 mdy = my - prev_my;
146
147                 if(bndiff & 1) app_mouse(0, bnstate & 1, mx, my);
148                 if(bndiff & 2) app_mouse(1, bnstate & 2, mx, my);
149                 if(bndiff & 4) app_mouse(3, bnstate & 4, mx, my);
150
151                 if((mdx | mdy) != 0) {
152                         app_motion(mx, my);
153                 }
154
155                 app_display();
156                 app_swap_buffers();
157         }
158
159 break_evloop:
160         app_shutdown();
161         vid_cleanup();
162         kb_shutdown();
163         return 0;
164 }
165
166 void app_redisplay(int x, int y, int w, int h)
167 {
168         rtk_rect r;
169
170         if((w | h) == 0) {
171                 r.x = r.y = 0;
172                 r.width = win_width;
173                 r.height = win_height;
174         } else {
175                 r.x = x;
176                 r.y = y;
177                 r.width = w;
178                 r.height = h;
179         }
180
181         if(dirty_valid) {
182                 rtk_rect_union(&dirty, &r);
183         } else {
184                 dirty = r;
185         }
186         dirty_valid = 1;
187 }
188
189 void app_swap_buffers(void)
190 {
191         if(opt.vsync) {
192                 vid_vsync();
193         }
194         if(dirty_valid) {
195                 if(dirty.width < win_width || dirty.height < win_height) {
196                         uint32_t *src = framebuf + dirty.y * win_width + dirty.x;
197                         vid_blit32(dirty.x, dirty.y, dirty.width, dirty.height, src, 0);
198                 } else {
199                         vid_blitfb32(framebuf, 0);
200                 }
201                 dirty_valid = 0;
202         }
203         if(prev_mx >= 0) {
204                 draw_cursor(prev_mx, prev_my);
205         }
206         draw_cursor(mx, my);
207         prev_mx = mx;
208         prev_my = my;
209
210         if(prev_rband.width) {
211                 draw_rband(&prev_rband);
212         }
213         if(rband.width) {
214                 draw_rband(&rband);
215         }
216         prev_rband = rband;
217 }
218
219 void app_quit(void)
220 {
221         quit = 1;
222 }
223
224 void app_resize(int x, int y)
225 {
226 }
227
228 void app_fullscreen(int fs)
229 {
230 }
231
232 void app_vsync(int vsync)
233 {
234 }
235
236 void app_rband(int x, int y, int w, int h)
237 {
238         if(!(w | h)) {
239                 w = h = 0;
240         }
241
242         rband.x = x;
243         rband.y = y;
244         rband.width = w;
245         rband.height = h;
246 }
247
248 static void draw_cursor(int x, int y)
249 {
250         int i;
251         uint32_t *fbptr = vmem + y * win_width + x;
252
253         for(i=0; i<3; i++) {
254                 int offs = i + 1;
255                 if(y > offs) fbptr[-win_width * offs] ^= 0xffffff;
256                 if(y < win_height - offs - 1) fbptr[win_width * offs] ^= 0xffffff;
257                 if(x > offs) fbptr[-offs] ^= 0xffffff;
258                 if(x < win_width - offs - 1) fbptr[offs] ^= 0xffffff;
259         }
260 }
261
262 static void draw_rband(rtk_rect *r)
263 {
264         int i;
265         rtk_rect rect;
266         uint32_t *fbptr, *bptr;
267
268         rect = *r;
269         rtk_fix_rect(&rect);
270
271         if(rect.width <= 0 || rect.height <= 0) {
272                 return;
273         }
274
275         fbptr = vmem + rect.y * win_width + rect.x;
276         bptr = fbptr + win_width * (rect.height - 1);
277
278         for(i=0; i<rect.width; i++) {
279                 fbptr[i] ^= 0xffffff;
280                 bptr[i] ^= 0xffffff;
281         }
282         fbptr += win_width;
283         for(i=0; i<rect.height-2; i++) {
284                 fbptr[0] ^= 0xffffff;
285                 fbptr[rect.width - 1] ^= 0xffffff;
286                 fbptr += win_width;
287         }
288 }