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