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