removed debug rect
[retroray] / src / modern / 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 <assert.h>
21 #include "miniglut.h"
22 #include "app.h"
23 #include "options.h"
24 #include "rtk.h"
25 #include "logger.h"
26
27 static void display(void);
28 static void reshape(int x, int y);
29 static void keydown(unsigned char key, int x, int y);
30 static void keyup(unsigned char key, int x, int y);
31 static void skeydown(int key, int x, int y);
32 static void skeyup(int key, int x, int y);
33 static void mouse(int bn, int st, int x, int y);
34 static void motion(int x, int y);
35 static int translate_skey(int key);
36
37 #if defined(__unix__) || defined(unix)
38 #include <GL/glx.h>
39 static Display *xdpy;
40 static Window xwin;
41
42 static void (*glx_swap_interval_ext)();
43 static void (*glx_swap_interval_sgi)();
44 #endif
45 #ifdef _WIN32
46 #include <windows.h>
47 static PROC wgl_swap_interval_ext;
48 #endif
49
50 static rtk_rect rband;
51
52
53 int main(int argc, char **argv)
54 {
55         glutInit(&argc, argv);
56
57         load_options(CFGFILE);
58
59         glutInitWindowSize(opt.xres, opt.yres);
60         glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
61         glutCreateWindow("RetroRay");
62
63         glutDisplayFunc(display);
64         glutReshapeFunc(reshape);
65         glutKeyboardFunc(keydown);
66         glutKeyboardUpFunc(keyup);
67         glutSpecialFunc(skeydown);
68         glutSpecialUpFunc(skeyup);
69         glutMouseFunc(mouse);
70         glutMotionFunc(motion);
71         glutPassiveMotionFunc(motion);
72         glutSpaceballMotionFunc(app_sball_motion);
73         glutSpaceballRotateFunc(app_sball_rotate);
74         glutSpaceballButtonFunc(app_sball_button);
75
76 #if defined(__unix__) || defined(unix)
77         xdpy = glXGetCurrentDisplay();
78         xwin = glXGetCurrentDrawable();
79
80         if(!(glx_swap_interval_ext = glXGetProcAddress((unsigned char*)"glXSwapIntervalEXT"))) {
81                 glx_swap_interval_sgi = glXGetProcAddress((unsigned char*)"glXSwapIntervalSGI");
82         }
83 #endif
84 #ifdef _WIN32
85         wgl_swap_interval_ext = wglGetProcAddress("wglSwapIntervalEXT");
86 #endif
87
88         win_width = glutGet(GLUT_WINDOW_WIDTH);
89         win_height = glutGet(GLUT_WINDOW_HEIGHT);
90         win_aspect = (float)win_width / win_height;
91
92         init_logger();
93         add_log_stream(stdout);
94
95         if(app_init() == -1) {
96                 return 1;
97         }
98         atexit(app_shutdown);
99         glutMainLoop();
100         return 0;
101 }
102
103 unsigned long get_msec(void)
104 {
105         return (unsigned long)glutGet(GLUT_ELAPSED_TIME);
106 }
107
108 void app_redisplay(int x, int y, int w, int h)
109 {
110         /*dbgmsg("fakeupd: %d,%d (%dx%d)\n", x, y, w, h);*/
111         glutPostRedisplay();
112 }
113
114 void app_swap_buffers(void)
115 {
116         glViewport(0, 0, win_width, win_height);
117
118         glMatrixMode(GL_MODELVIEW);
119         glLoadIdentity();
120         glMatrixMode(GL_PROJECTION);
121         glPushMatrix();
122         glLoadIdentity();
123         glOrtho(0, win_width, win_height, 0, -1, 1);
124
125         glRasterPos2i(0, 0);
126         glPixelZoom(1, -1);
127         glEnable(GL_ALPHA_TEST);
128         glAlphaFunc(GL_GREATER, 0.5f);
129         glDrawPixels(win_width, win_height, GL_BGRA, GL_UNSIGNED_BYTE, framebuf);
130         glDisable(GL_ALPHA_TEST);
131
132         if(rband.width | rband.height) {
133                 glPushAttrib(GL_ENABLE_BIT);
134                 glDisable(GL_DEPTH_TEST);
135                 glDisable(GL_LIGHTING);
136
137                 glEnable(GL_COLOR_LOGIC_OP);
138                 glLogicOp(GL_XOR);
139
140                 glBegin(GL_LINE_LOOP);
141                 glColor3f(1, 1, 1);
142                 glVertex2f(rband.x, rband.y);
143                 glVertex2f(rband.x + rband.width, rband.y);
144                 glVertex2f(rband.x + rband.width, rband.y + rband.height);
145                 glVertex2f(rband.x, rband.y + rband.height);
146                 glEnd();
147
148                 glPopAttrib();
149         }
150
151         glPopMatrix();
152         glMatrixMode(GL_MODELVIEW);
153
154         glutSwapBuffers();
155         assert(glGetError() == GL_NO_ERROR);
156 }
157
158 void app_quit(void)
159 {
160         exit(0);
161 }
162
163 void app_resize(int x, int y)
164 {
165         if(x == win_width && y == win_height) return;
166
167         glutReshapeWindow(x, y);
168 }
169
170 void app_fullscreen(int fs)
171 {
172         static int prev_w, prev_h;
173
174         if(fs == -1) {
175                 fs = !fullscr;
176         }
177
178         if(fs == fullscr) return;
179
180         if(fs) {
181                 prev_w = glutGet(GLUT_WINDOW_WIDTH);
182                 prev_h = glutGet(GLUT_WINDOW_HEIGHT);
183                 glutFullScreen();
184         } else {
185                 glutReshapeWindow(prev_w, prev_h);
186         }
187         fullscr = fs;
188 }
189
190 #if defined(__unix__) || defined(unix)
191 void app_vsync(int vsync)
192 {
193         vsync = vsync ? 1 : 0;
194         if(glx_swap_interval_ext) {
195                 glx_swap_interval_ext(xdpy, xwin, vsync);
196         } else if(glx_swap_interval_sgi) {
197                 glx_swap_interval_sgi(vsync);
198         }
199 }
200 #endif
201 #ifdef WIN32
202 void app_vsync(int vsync)
203 {
204         if(wgl_swap_interval_ext) {
205                 wgl_swap_interval_ext(vsync ? 1 : 0);
206         }
207 }
208 #endif
209
210 void app_rband(int x, int y, int w, int h)
211 {
212         rband.x = x;
213         rband.y = y;
214         rband.width = w;
215         rband.height = h;
216
217         glutPostRedisplay();
218 }
219
220
221 static void display(void)
222 {
223         app_display();
224         app_swap_buffers();
225 }
226
227 static void reshape(int x, int y)
228 {
229         app_reshape(x, y);
230 }
231
232 static void keydown(unsigned char key, int x, int y)
233 {
234         modkeys = glutGetModifiers();
235         app_keyboard(key, 1);
236 }
237
238 static void keyup(unsigned char key, int x, int y)
239 {
240         app_keyboard(key, 0);
241 }
242
243 static void skeydown(int key, int x, int y)
244 {
245         int k;
246         modkeys = glutGetModifiers();
247         if((k = translate_skey(key)) >= 0) {
248                 app_keyboard(k, 1);
249         }
250 }
251
252 static void skeyup(int key, int x, int y)
253 {
254         int k = translate_skey(key);
255         if(k >= 0) {
256                 app_keyboard(k, 0);
257         }
258 }
259
260 static void mouse(int bn, int st, int x, int y)
261 {
262         modkeys = glutGetModifiers();
263         app_mouse(bn - GLUT_LEFT_BUTTON, st == GLUT_DOWN, x, y);
264 }
265
266 static void motion(int x, int y)
267 {
268         app_motion(x, y);
269 }
270
271 static int translate_skey(int key)
272 {
273         switch(key) {
274         case GLUT_KEY_LEFT:
275                 return KEY_LEFT;
276         case GLUT_KEY_UP:
277                 return KEY_UP;
278         case GLUT_KEY_RIGHT:
279                 return KEY_RIGHT;
280         case GLUT_KEY_DOWN:
281                 return KEY_DOWN;
282         case GLUT_KEY_PAGE_UP:
283                 return KEY_PGUP;
284         case GLUT_KEY_PAGE_DOWN:
285                 return KEY_PGDN;
286         case GLUT_KEY_HOME:
287                 return KEY_HOME;
288         case GLUT_KEY_END:
289                 return KEY_END;
290         case GLUT_KEY_INSERT:
291                 return KEY_INS;
292         default:
293                 if(key >= GLUT_KEY_F1 && key <= GLUT_KEY_F12) {
294                         return key - GLUT_KEY_F1 + KEY_F1;
295                 }
296         }
297
298         return -1;
299 }