added dos timer and double-esc to quit
[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         init_logger();
65
66         if(read_cpuid(&cpuid) == 0) {
67                 print_cpuid(&cpuid);
68         }
69
70         init_timer(0);
71         kb_init();
72
73         if(!have_mouse()) {
74                 fprintf(stderr, "No mouse detected. Make sure the mouse driver is installed\n");
75                 return 1;
76         }
77
78         if((env = getenv("RRLOG"))) {
79                 if(tolower(env[0]) == 'c' && tolower(env[1]) == 'o' && tolower(env[2]) == 'm'
80                                 && isdigit(env[3])) {
81                         add_log_console(env);
82                 } else {
83                         add_log_file(env);
84                 }
85         }
86
87         if(vid_init() == -1) {
88                 return 1;
89         }
90
91         if((vmidx = vid_findmode(640, 480, 32)) == -1) {
92                 return 1;
93         }
94         if(!(vmem = vid_setmode(vmidx))) {
95                 return 1;
96         }
97
98         win_width = 640;
99         win_height = 480;
100         win_aspect = (float)win_width / (float)win_height;
101
102         if(app_init() == -1) {
103                 goto break_evloop;
104         }
105         app_redisplay(0, 0, 0, 0);
106
107         app_reshape(win_width, win_height);
108         mx = win_width / 2;
109         my = win_height / 2;
110         prev_mx = prev_my = -1;
111
112         for(;;) {
113                 int key;
114
115                 modkeys = 0;
116                 if(kb_isdown(KEY_ALT)) {
117                         modkeys |= KEY_MOD_ALT;
118                 }
119                 if(kb_isdown(KEY_CTRL)) {
120                         modkeys |= KEY_MOD_CTRL;
121                 }
122                 if(kb_isdown(KEY_SHIFT)) {
123                         modkeys |= KEY_MOD_SHIFT;
124                 }
125
126                 while((key = kb_getkey()) != -1) {
127                         if(key == 'r' && (modkeys & KEY_MOD_CTRL)) {
128                                 app_redisplay(0, 0, 0, 0);
129                         } else {
130                                 app_keyboard(key, 1);
131                         }
132                         if(quit) goto break_evloop;
133                 }
134
135                 bnstate = read_mouse_bn();
136                 bndiff = bnstate ^ prev_bnstate;
137                 prev_bnstate = bnstate;
138
139                 read_mouse_rel(&mdx, &mdy);
140                 mx = clamp(mx + mdx, 0, win_width - 1);
141                 my = clamp(my + mdy, 0, win_height - 1);
142                 mdx = mx - prev_mx;
143                 mdy = my - prev_my;
144
145                 if(bndiff & 1) app_mouse(0, bnstate & 1, mx, my);
146                 if(bndiff & 2) app_mouse(1, bnstate & 2, mx, my);
147                 if(bndiff & 4) app_mouse(3, bnstate & 4, mx, my);
148
149                 if((mdx | mdy) != 0) {
150                         app_motion(mx, my);
151                 }
152
153                 app_display();
154                 app_swap_buffers();
155         }
156
157 break_evloop:
158         app_shutdown();
159         vid_cleanup();
160         kb_shutdown();
161         return 0;
162 }
163
164 void app_redisplay(int x, int y, int w, int h)
165 {
166         rtk_rect r;
167
168         if((w | h) == 0) {
169                 r.x = r.y = 0;
170                 r.width = win_width;
171                 r.height = win_height;
172         } else {
173                 r.x = x;
174                 r.y = y;
175                 r.width = w;
176                 r.height = h;
177         }
178
179         if(dirty_valid) {
180                 rtk_rect_union(&dirty, &r);
181         } else {
182                 dirty = r;
183         }
184         dirty_valid = 1;
185 }
186
187 void app_swap_buffers(void)
188 {
189         if(opt.vsync) {
190                 vid_vsync();
191         }
192         if(dirty_valid) {
193                 if(dirty.width < win_width || dirty.height < win_height) {
194                         uint32_t *src = framebuf + dirty.y * win_width + dirty.x;
195                         vid_blit32(dirty.x, dirty.y, dirty.width, dirty.height, src, 0);
196                 } else {
197                         vid_blitfb32(framebuf, 0);
198                 }
199                 dirty_valid = 0;
200         }
201         if(prev_mx >= 0) {
202                 draw_cursor(prev_mx, prev_my);
203         }
204         draw_cursor(mx, my);
205         prev_mx = mx;
206         prev_my = my;
207
208         if(prev_rband.width) {
209                 draw_rband(&prev_rband);
210         }
211         if(rband.width) {
212                 draw_rband(&rband);
213         }
214         prev_rband = rband;
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 void app_rband(int x, int y, int w, int h)
235 {
236         if(!(w | h)) {
237                 w = h = 0;
238         }
239
240         rband.x = x;
241         rband.y = y;
242         rband.width = w;
243         rband.height = h;
244 }
245
246 static void draw_cursor(int x, int y)
247 {
248         int i;
249         uint32_t *fbptr = vmem + y * win_width + x;
250
251         for(i=0; i<3; i++) {
252                 int offs = i + 1;
253                 if(y > offs) fbptr[-win_width * offs] ^= 0xffffff;
254                 if(y < win_height - offs - 1) fbptr[win_width * offs] ^= 0xffffff;
255                 if(x > offs) fbptr[-offs] ^= 0xffffff;
256                 if(x < win_width - offs - 1) fbptr[offs] ^= 0xffffff;
257         }
258 }
259
260 static void draw_rband(rtk_rect *r)
261 {
262         int i;
263         rtk_rect rect;
264         uint32_t *fbptr, *bptr;
265
266         rect = *r;
267         rtk_fix_rect(&rect);
268
269         if(rect.width <= 0 || rect.height <= 0) {
270                 return;
271         }
272
273         fbptr = vmem + rect.y * win_width + rect.x;
274         bptr = fbptr + win_width * (rect.height - 1);
275
276         for(i=0; i<rect.width; i++) {
277                 fbptr[i] ^= 0xffffff;
278                 bptr[i] ^= 0xffffff;
279         }
280         fbptr += win_width;
281         for(i=0; i<rect.height-2; i++) {
282                 fbptr[0] ^= 0xffffff;
283                 fbptr[rect.width - 1] ^= 0xffffff;
284                 fbptr += win_width;
285         }
286 }