fixed read_mouse_bn and start using relative mouse input in dos
[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 INLINE int clamp(int x, int a, int b)
32 {
33         if(x < a) return a;
34         if(x > b) return b;
35         return x;
36 }
37
38 static void draw_cursor(int x, int y);
39
40 static uint32_t *vmem;
41 static int quit, disp_pending;
42
43 int main(int argc, char **argv)
44 {
45         int i;
46         int vmidx;
47         int mx, my, mdx, mdy, prev_mx, prev_my, bnstate, bndiff;
48         static int prev_bnstate;
49
50 #ifdef __DJGPP__
51         __djgpp_nearptr_enable();
52 #endif
53
54         init_logger();
55
56         if(read_cpuid(&cpuid) == 0) {
57                 print_cpuid(&cpuid);
58         }
59
60         kb_init();
61
62         if(!have_mouse()) {
63                 fprintf(stderr, "No mouse detected. Make sure the mouse driver is installed\n");
64                 return 1;
65         }
66
67         add_log_file("retroray.log");
68
69         if(init_video() == -1) {
70                 return 1;
71         }
72
73         if((vmidx = match_video_mode(640, 480, 32)) == -1) {
74                 return 1;
75         }
76         if(!(vmem = set_video_mode(vmidx, 1))) {
77                 return 1;
78         }
79
80         win_width = 640;
81         win_height = 480;
82         win_aspect = (float)win_width / (float)win_height;
83
84         if(app_init() == -1) {
85                 goto break_evloop;
86         }
87         disp_pending = 1;
88
89         app_reshape(win_width, win_height);
90         mx = win_width / 2;
91         my = win_height / 2;
92
93         for(;;) {
94                 int key;
95
96                 modkeys = 0;
97                 if(kb_isdown(KEY_ALT)) {
98                         modkeys |= KEY_MOD_ALT;
99                 }
100                 if(kb_isdown(KEY_CTRL)) {
101                         modkeys |= KEY_MOD_CTRL;
102                 }
103                 if(kb_isdown(KEY_SHIFT)) {
104                         modkeys |= KEY_MOD_SHIFT;
105                 }
106
107                 while((key = kb_getkey()) != -1) {
108                         app_keyboard(key, 1);
109                         if(quit) goto break_evloop;
110                 }
111
112                 bnstate = read_mouse_bn();
113                 bndiff = bnstate ^ prev_bnstate;
114                 prev_bnstate = bnstate;
115
116                 read_mouse_rel(&mdx, &mdy);
117                 prev_mx = mx;
118                 prev_my = my;
119                 mx = clamp(mx + mdx, 0, win_width - 1);
120                 my = clamp(my + mdy, 0, win_height - 1);
121                 mdx = mx - prev_mx;
122                 mdy = my - prev_my;
123
124                 if(bndiff & 1) app_mouse(0, bnstate & 1, mx, my);
125                 if(bndiff & 2) app_mouse(1, bnstate & 2, mx, my);
126                 if(bndiff & 4) app_mouse(3, bnstate & 4, mx, my);
127
128                 if((mdx | mdy) != 0) {
129                         app_motion(mx, my);
130                 }
131
132                 if(disp_pending) {
133                         disp_pending = 0;
134                         app_display();
135                 }
136
137                 draw_cursor(prev_mx, prev_my);
138                 draw_cursor(mx, my);
139
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 }