new vbe sortof works
[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 <time.h>
22 #include "app.h"
23 #include "keyb.h"
24 #include "vidsys.h"
25 #include "cdpmi.h"
26 #include "mouse.h"
27 #include "logger.h"
28 #include "options.h"
29 #include "cpuid.h"
30 #include "util.h"
31
32 static INLINE int clamp(int x, int a, int b)
33 {
34         if(x < a) return a;
35         if(x > b) return b;
36         return x;
37 }
38
39 static void draw_cursor(int x, int y);
40
41 static uint32_t *vmem;
42 static int quit, disp_pending;
43
44 int main(int argc, char **argv)
45 {
46         int i;
47         int vmidx;
48         int mx, my, mdx, mdy, prev_mx, prev_my, bnstate, bndiff;
49         static int prev_bnstate;
50
51 #ifdef __DJGPP__
52         __djgpp_nearptr_enable();
53 #endif
54
55         init_logger();
56
57         if(read_cpuid(&cpuid) == 0) {
58                 print_cpuid(&cpuid);
59         }
60
61         kb_init();
62
63         if(!have_mouse()) {
64                 fprintf(stderr, "No mouse detected. Make sure the mouse driver is installed\n");
65                 return 1;
66         }
67
68         /*add_log_file("retroray.log");*/
69
70         if(vid_init() == -1) {
71                 return 1;
72         }
73
74         if((vmidx = vid_findmode(640, 480, 32)) == -1) {
75                 return 1;
76         }
77         if(!(vmem = vid_setmode(vmidx))) {
78                 return 1;
79         }
80
81         win_width = 640;
82         win_height = 480;
83         win_aspect = (float)win_width / (float)win_height;
84
85         if(app_init() == -1) {
86                 goto break_evloop;
87         }
88         disp_pending = 1;
89
90         app_reshape(win_width, win_height);
91         mx = win_width / 2;
92         my = win_height / 2;
93
94         for(;;) {
95                 int key;
96
97                 modkeys = 0;
98                 if(kb_isdown(KEY_ALT)) {
99                         modkeys |= KEY_MOD_ALT;
100                 }
101                 if(kb_isdown(KEY_CTRL)) {
102                         modkeys |= KEY_MOD_CTRL;
103                 }
104                 if(kb_isdown(KEY_SHIFT)) {
105                         modkeys |= KEY_MOD_SHIFT;
106                 }
107
108                 while((key = kb_getkey()) != -1) {
109                         app_keyboard(key, 1);
110                         if(quit) goto break_evloop;
111                 }
112
113                 bnstate = read_mouse_bn();
114                 bndiff = bnstate ^ prev_bnstate;
115                 prev_bnstate = bnstate;
116
117                 read_mouse_rel(&mdx, &mdy);
118                 prev_mx = mx;
119                 prev_my = my;
120                 mx = clamp(mx + mdx, 0, win_width - 1);
121                 my = clamp(my + mdy, 0, win_height - 1);
122                 mdx = mx - prev_mx;
123                 mdy = my - prev_my;
124
125                 if(bndiff & 1) app_mouse(0, bnstate & 1, mx, my);
126                 if(bndiff & 2) app_mouse(1, bnstate & 2, mx, my);
127                 if(bndiff & 4) app_mouse(3, bnstate & 4, mx, my);
128
129                 if((mdx | mdy) != 0) {
130                         app_motion(mx, my);
131                 }
132
133                 if(disp_pending) {
134                         disp_pending = 0;
135                         app_display();
136                 }
137
138                 draw_cursor(prev_mx, prev_my);
139                 draw_cursor(mx, my);
140
141                 app_swap_buffers();
142         }
143
144 break_evloop:
145         app_shutdown();
146         vid_cleanup();
147         kb_shutdown();
148         return 0;
149 }
150
151 long app_getmsec(void)
152 {
153         return time(0) * 1000;  /* TODO */
154 }
155
156 void app_redisplay(void)
157 {
158         disp_pending = 1;
159 }
160
161 void app_swap_buffers(void)
162 {
163         if(opt.vsync) {
164                 vid_vsync();
165         }
166         vid_blitfb32(framebuf, 0);
167 }
168
169 void app_quit(void)
170 {
171         quit = 1;
172 }
173
174 void app_resize(int x, int y)
175 {
176 }
177
178 void app_fullscreen(int fs)
179 {
180 }
181
182 void app_vsync(int vsync)
183 {
184 }
185
186 static void draw_cursor(int x, int y)
187 {
188         int i;
189         uint32_t *fbptr = framebuf + y * win_width + x;
190
191         for(i=0; i<3; i++) {
192                 int offs = i + 1;
193                 if(y > offs) fbptr[-win_width * offs] ^= 0xffffff;
194                 if(y < win_height - offs - 1) fbptr[win_width * offs] ^= 0xffffff;
195                 if(x > offs) fbptr[-offs] ^= 0xffffff;
196                 if(x < win_width - offs - 1) fbptr[offs] ^= 0xffffff;
197         }
198 }