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