fix watcom version of set_mouse_limits
[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 "gfx.h"
25 #include "cdpmi.h"
26 #include "mouse.h"
27 #include "logger.h"
28 #include "options.h"
29 #include "cpuid.h"
30
31 static void draw_cursor(int x, int y);
32
33 static uint32_t *vmem;
34 static int quit, disp_pending;
35
36 int main(int argc, char **argv)
37 {
38         int i;
39         int vmidx;
40         int mx, my, bnstate, bndiff;
41         static int prev_mx = -1, prev_my, prev_bnstate;
42
43 #ifdef __DJGPP__
44         __djgpp_nearptr_enable();
45 #endif
46
47         init_logger();
48
49         if(read_cpuid(&cpuid) == 0) {
50                 print_cpuid(&cpuid);
51         }
52
53         kb_init();
54
55         if(!have_mouse()) {
56                 fprintf(stderr, "No mouse detected. Make sure the mouse driver is installed\n");
57                 return 1;
58         }
59         set_mouse_limits(0, 0, 639, 479);
60         mx = 320;
61         my = 240;
62         set_mouse(mx, my);
63
64         add_log_file("retroray.log");
65
66         if(init_video() == -1) {
67                 return 1;
68         }
69
70         if((vmidx = match_video_mode(640, 480, 32)) == -1) {
71                 return 1;
72         }
73         if(!(vmem = set_video_mode(vmidx, 1))) {
74                 return 1;
75         }
76
77         win_width = 640;
78         win_height = 480;
79         win_aspect = (float)win_width / (float)win_height;
80
81         if(app_init() == -1) {
82                 goto break_evloop;
83         }
84         disp_pending = 1;
85
86         app_reshape(win_width, win_height);
87         mx = win_width / 2;
88         my = win_height / 2;
89         set_mouse(mx, my);
90
91         for(;;) {
92                 int key;
93
94                 modkeys = 0;
95                 if(kb_isdown(KEY_ALT)) {
96                         modkeys |= KEY_MOD_ALT;
97                 }
98                 if(kb_isdown(KEY_CTRL)) {
99                         modkeys |= KEY_MOD_CTRL;
100                 }
101                 if(kb_isdown(KEY_SHIFT)) {
102                         modkeys |= KEY_MOD_SHIFT;
103                 }
104
105                 while((key = kb_getkey()) != -1) {
106                         app_keyboard(key, 1);
107                         if(quit) goto break_evloop;
108                 }
109
110                 bnstate = read_mouse(&mx, &my);
111                 bndiff = bnstate ^ prev_bnstate;
112                 prev_bnstate = bnstate;
113
114                 if(bndiff) {
115                         dbgmsg("bndiff: %04x\n", bndiff);
116                 }
117
118                 if(bndiff & 1) app_mouse(0, bnstate & 1, mx, my);
119                 if(bndiff & 2) app_mouse(1, bnstate & 2, mx, my);
120                 if(bndiff & 4) app_mouse(3, bnstate & 4, mx, my);
121
122                 if(prev_my == -1) {
123                         prev_mx = mx;
124                         prev_my = my;
125                 } else {
126                         draw_cursor(prev_mx, prev_my);
127                 }
128                 if((mx ^ prev_mx) | (my ^ prev_my)) {
129                         app_motion(mx, my);
130                 }
131
132                 if(disp_pending) {
133                         disp_pending = 0;
134                         app_display();
135                 }
136
137                 draw_cursor(mx, my);
138                 prev_mx = mx;
139                 prev_my = my;
140                 app_swap_buffers();
141         }
142
143 break_evloop:
144         app_shutdown();
145         set_text_mode();
146         cleanup_video();
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         blit_frame(framebuf, opt.vsync);
164 }
165
166 void app_quit(void)
167 {
168         quit = 1;
169 }
170
171 void app_resize(int x, int y)
172 {
173 }
174
175 void app_fullscreen(int fs)
176 {
177 }
178
179 void app_vsync(int vsync)
180 {
181 }
182
183 static void draw_cursor(int x, int y)
184 {
185         int i;
186         uint32_t *fbptr = framebuf + y * win_width + x;
187
188         for(i=0; i<3; i++) {
189                 int offs = i + 1;
190                 if(y > offs) fbptr[-win_width * offs] ^= 0xffffff;
191                 if(y < win_height - offs - 1) fbptr[win_width * offs] ^= 0xffffff;
192                 if(x > offs) fbptr[-offs] ^= 0xffffff;
193                 if(x < win_width - offs - 1) fbptr[offs] ^= 0xffffff;
194         }
195 }