fixed simple draw_cursor
[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, 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(32);
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         read_mouse(&mx, &my);
88
89         for(;;) {
90                 int key;
91
92                 if(kb_isdown(KEY_ALT)) {
93                         modkeys |= KEY_MOD_ALT;
94                 }
95                 if(kb_isdown(KEY_CTRL)) {
96                         modkeys |= KEY_MOD_CTRL;
97                 }
98                 if(kb_isdown(KEY_SHIFT)) {
99                         modkeys |= KEY_MOD_SHIFT;
100                 }
101
102                 while((key = kb_getkey()) != -1) {
103                         app_keyboard(key, 1);
104                         if(quit) goto break_evloop;
105                 }
106
107                 draw_cursor(mx, my);
108
109                 bnstate = read_mouse(&mx, &my);
110                 bndiff = bnstate ^ prev_bnstate;
111                 prev_bnstate = bnstate;
112
113                 if(bndiff & 1) app_mouse(0, bnstate & 1, mx, my);
114                 if(bndiff & 2) app_mouse(1, bnstate & 2, mx, my);
115                 if(bndiff & 4) app_mouse(3, bnstate & 4, mx, my);
116
117                 if(disp_pending) {
118                         disp_pending = 0;
119                         app_display();
120                 }
121
122                 draw_cursor(mx, my);
123                 app_swap_buffers();
124         }
125
126 break_evloop:
127         app_shutdown();
128         set_text_mode();
129         cleanup_video();
130         kb_shutdown();
131         return 0;
132 }
133
134 long app_getmsec(void)
135 {
136         return time(0) * 1000;  /* TODO */
137 }
138
139 void app_redisplay(void)
140 {
141         disp_pending = 1;
142 }
143
144 void app_swap_buffers(void)
145 {
146         blit_frame(framebuf, opt.vsync);
147 }
148
149 void app_quit(void)
150 {
151         quit = 1;
152 }
153
154 void app_resize(int x, int y)
155 {
156 }
157
158 void app_fullscreen(int fs)
159 {
160 }
161
162 void app_vsync(int vsync)
163 {
164 }
165
166 static void draw_cursor(int x, int y)
167 {
168         int i;
169         uint32_t *fbptr = framebuf + y * win_width + x;
170
171         for(i=0; i<3; i++) {
172                 int offs = i + 1;
173                 if(y > offs) fbptr[-win_width * offs] ^= 0xffffff;
174                 if(y < win_height - offs - 1) fbptr[win_width * offs] ^= 0xffffff;
175                 if(x > offs) fbptr[-offs] ^= 0xffffff;
176                 if(x < win_width - offs - 1) fbptr[offs] ^= 0xffffff;
177         }
178 }