foo
[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         glutSwapBuffers();
103         assert(glGetError() == GL_NO_ERROR);
104 }
105
106 void app_quit(void)
107 {
108         exit(0);
109 }
110
111 void app_resize(int x, int y)
112 {
113         if(x == win_width && y == win_height) return;
114
115         glutReshapeWindow(x, y);
116 }
117
118 void app_fullscreen(int fs)
119 {
120         static int prev_w, prev_h;
121
122         if(fs == -1) {
123                 fs = !fullscr;
124         }
125
126         if(fs == fullscr) return;
127
128         if(fs) {
129                 prev_w = glutGet(GLUT_WINDOW_WIDTH);
130                 prev_h = glutGet(GLUT_WINDOW_HEIGHT);
131                 glutFullScreen();
132         } else {
133                 glutReshapeWindow(prev_w, prev_h);
134         }
135         fullscr = fs;
136 }
137
138 #if defined(__unix__) || defined(unix)
139 void app_vsync(int vsync)
140 {
141         vsync = vsync ? 1 : 0;
142         if(glx_swap_interval_ext) {
143                 glx_swap_interval_ext(xdpy, xwin, vsync);
144         } else if(glx_swap_interval_sgi) {
145                 glx_swap_interval_sgi(vsync);
146         }
147 }
148 #endif
149 #ifdef WIN32
150 void app_vsync(int vsync)
151 {
152         if(wgl_swap_interval_ext) {
153                 wgl_swap_interval_ext(vsync ? 1 : 0);
154         }
155 }
156 #endif
157
158
159
160 static void reshape(int x, int y)
161 {
162         app_reshape(x, y);
163 }
164
165 static void keydown(unsigned char key, int x, int y)
166 {
167         modkeys = glutGetModifiers();
168         app_keyboard(key, 1);
169 }
170
171 static void keyup(unsigned char key, int x, int y)
172 {
173         app_keyboard(key, 0);
174 }
175
176 static void skeydown(int key, int x, int y)
177 {
178         int k;
179         modkeys = glutGetModifiers();
180         if((k = translate_skey(key)) >= 0) {
181                 app_keyboard(k, 1);
182         }
183 }
184
185 static void skeyup(int key, int x, int y)
186 {
187         int k = translate_skey(key);
188         if(k >= 0) {
189                 app_keyboard(k, 0);
190         }
191 }
192
193 static void mouse(int bn, int st, int x, int y)
194 {
195         modkeys = glutGetModifiers();
196         app_mouse(bn - GLUT_LEFT_BUTTON, st == GLUT_DOWN, x, y);
197 }
198
199 static void motion(int x, int y)
200 {
201         app_motion(x, y);
202 }
203
204 static int translate_skey(int key)
205 {
206         switch(key) {
207         case GLUT_KEY_LEFT:
208                 return KEY_LEFT;
209         case GLUT_KEY_UP:
210                 return KEY_UP;
211         case GLUT_KEY_RIGHT:
212                 return KEY_RIGHT;
213         case GLUT_KEY_DOWN:
214                 return KEY_DOWN;
215         case GLUT_KEY_PAGE_UP:
216                 return KEY_PGUP;
217         case GLUT_KEY_PAGE_DOWN:
218                 return KEY_PGDOWN;
219         case GLUT_KEY_HOME:
220                 return KEY_HOME;
221         case GLUT_KEY_END:
222                 return KEY_END;
223         case GLUT_KEY_INSERT:
224                 return KEY_INS;
225         default:
226                 if(key >= GLUT_KEY_F1 && key <= GLUT_KEY_F12) {
227                         return key - GLUT_KEY_F1 + KEY_F1;
228                 }
229         }
230
231         return -1;
232 }