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