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