2 RetroRay - integrated standalone vintage modeller/renderer
3 Copyright (C) 2023 John Tsiombikas <nuclear@mutantstargoat.com>
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.
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.
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/>.
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);
35 #if defined(__unix__) || defined(unix)
40 static void (*glx_swap_interval_ext)();
41 static void (*glx_swap_interval_sgi)();
45 static PROC wgl_swap_interval_ext;
50 int main(int argc, char **argv)
52 glutInit(&argc, argv);
53 glutInitWindowSize(640, 480);
54 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
55 glutCreateWindow("RetroRay");
57 glutDisplayFunc(display);
58 glutReshapeFunc(reshape);
59 glutKeyboardFunc(keydown);
60 glutKeyboardUpFunc(keyup);
61 glutSpecialFunc(skeydown);
62 glutSpecialUpFunc(skeyup);
64 glutMotionFunc(motion);
65 glutPassiveMotionFunc(motion);
66 glutSpaceballMotionFunc(app_sball_motion);
67 glutSpaceballRotateFunc(app_sball_rotate);
68 glutSpaceballButtonFunc(app_sball_button);
70 #if defined(__unix__) || defined(unix)
71 xdpy = glXGetCurrentDisplay();
72 xwin = glXGetCurrentDrawable();
74 if(!(glx_swap_interval_ext = glXGetProcAddress((unsigned char*)"glXSwapIntervalEXT"))) {
75 glx_swap_interval_sgi = glXGetProcAddress((unsigned char*)"glXSwapIntervalSGI");
79 wgl_swap_interval_ext = wglGetProcAddress("wglSwapIntervalEXT");
82 win_width = glutGet(GLUT_WINDOW_WIDTH);
83 win_height = glutGet(GLUT_WINDOW_HEIGHT);
84 win_aspect = (float)win_width / win_height;
88 if(app_init() == -1) {
96 long app_getmsec(void)
98 return glutGet(GLUT_ELAPSED_TIME);
101 void app_redisplay(void)
106 void app_swap_buffers(void)
108 glMatrixMode(GL_PROJECTION);
111 glMatrixMode(GL_MODELVIEW);
114 glRasterPos2i(-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);
121 glMatrixMode(GL_PROJECTION);
123 glMatrixMode(GL_MODELVIEW);
126 assert(glGetError() == GL_NO_ERROR);
134 void app_resize(int x, int y)
136 if(x == win_width && y == win_height) return;
138 glutReshapeWindow(x, y);
141 void app_fullscreen(int fs)
143 static int prev_w, prev_h;
149 if(fs == fullscr) return;
152 prev_w = glutGet(GLUT_WINDOW_WIDTH);
153 prev_h = glutGet(GLUT_WINDOW_HEIGHT);
156 glutReshapeWindow(prev_w, prev_h);
161 #if defined(__unix__) || defined(unix)
162 void app_vsync(int vsync)
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);
173 void app_vsync(int vsync)
175 if(wgl_swap_interval_ext) {
176 wgl_swap_interval_ext(vsync ? 1 : 0);
182 static void display(void)
188 static void reshape(int x, int y)
193 static void keydown(unsigned char key, int x, int y)
195 modkeys = glutGetModifiers();
196 app_keyboard(key, 1);
199 static void keyup(unsigned char key, int x, int y)
201 app_keyboard(key, 0);
204 static void skeydown(int key, int x, int y)
207 modkeys = glutGetModifiers();
208 if((k = translate_skey(key)) >= 0) {
213 static void skeyup(int key, int x, int y)
215 int k = translate_skey(key);
221 static void mouse(int bn, int st, int x, int y)
223 modkeys = glutGetModifiers();
224 app_mouse(bn - GLUT_LEFT_BUTTON, st == GLUT_DOWN, x, y);
227 static void motion(int x, int y)
232 static int translate_skey(int key)
243 case GLUT_KEY_PAGE_UP:
245 case GLUT_KEY_PAGE_DOWN:
251 case GLUT_KEY_INSERT:
254 if(key >= GLUT_KEY_F1 && key <= GLUT_KEY_F12) {
255 return key - GLUT_KEY_F1 + KEY_F1;