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 reshape(int x, int y);
26 static void keydown(unsigned char key, int x, int y);
27 static void keyup(unsigned char key, int x, int y);
28 static void skeydown(int key, int x, int y);
29 static void skeyup(int key, int x, int y);
30 static void mouse(int bn, int st, int x, int y);
31 static void motion(int x, int y);
32 static int translate_skey(int key);
34 #if defined(__unix__) || defined(unix)
39 static void (*glx_swap_interval_ext)();
40 static void (*glx_swap_interval_sgi)();
44 static PROC wgl_swap_interval_ext;
49 int main(int argc, char **argv)
51 glutInit(&argc, argv);
52 glutInitWindowSize(640, 480);
53 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
54 glutCreateWindow("RetroRay");
56 glutDisplayFunc(app_display);
57 glutReshapeFunc(reshape);
58 glutKeyboardFunc(keydown);
59 glutKeyboardUpFunc(keyup);
60 glutSpecialFunc(skeydown);
61 glutSpecialUpFunc(skeyup);
63 glutMotionFunc(motion);
64 glutPassiveMotionFunc(motion);
65 glutSpaceballMotionFunc(app_sball_motion);
66 glutSpaceballRotateFunc(app_sball_rotate);
67 glutSpaceballButtonFunc(app_sball_button);
69 #if defined(__unix__) || defined(unix)
70 xdpy = glXGetCurrentDisplay();
71 xwin = glXGetCurrentDrawable();
73 if(!(glx_swap_interval_ext = glXGetProcAddress((unsigned char*)"glXSwapIntervalEXT"))) {
74 glx_swap_interval_sgi = glXGetProcAddress((unsigned char*)"glXSwapIntervalSGI");
78 wgl_swap_interval_ext = wglGetProcAddress("wglSwapIntervalEXT");
81 win_width = glutGet(GLUT_WINDOW_WIDTH);
82 win_height = glutGet(GLUT_WINDOW_HEIGHT);
83 win_aspect = (float)win_width / win_height;
87 if(app_init() == -1) {
95 long app_getmsec(void)
97 return glutGet(GLUT_ELAPSED_TIME);
100 void app_redisplay(void)
105 void app_swap_buffers(void)
107 glMatrixMode(GL_PROJECTION);
110 glMatrixMode(GL_MODELVIEW);
113 glRasterPos2i(-1, 1);
115 glEnable(GL_ALPHA_TEST);
116 glAlphaFunc(GL_GREATER, 0.5f);
117 glDrawPixels(win_width, win_height, GL_BGRA, GL_UNSIGNED_BYTE, framebuf);
118 glDisable(GL_ALPHA_TEST);
120 glMatrixMode(GL_PROJECTION);
122 glMatrixMode(GL_MODELVIEW);
125 assert(glGetError() == GL_NO_ERROR);
133 void app_resize(int x, int y)
135 if(x == win_width && y == win_height) return;
137 glutReshapeWindow(x, y);
140 void app_fullscreen(int fs)
142 static int prev_w, prev_h;
148 if(fs == fullscr) return;
151 prev_w = glutGet(GLUT_WINDOW_WIDTH);
152 prev_h = glutGet(GLUT_WINDOW_HEIGHT);
155 glutReshapeWindow(prev_w, prev_h);
160 #if defined(__unix__) || defined(unix)
161 void app_vsync(int vsync)
163 vsync = vsync ? 1 : 0;
164 if(glx_swap_interval_ext) {
165 glx_swap_interval_ext(xdpy, xwin, vsync);
166 } else if(glx_swap_interval_sgi) {
167 glx_swap_interval_sgi(vsync);
172 void app_vsync(int vsync)
174 if(wgl_swap_interval_ext) {
175 wgl_swap_interval_ext(vsync ? 1 : 0);
182 static void reshape(int x, int y)
187 static void keydown(unsigned char key, int x, int y)
189 modkeys = glutGetModifiers();
190 app_keyboard(key, 1);
193 static void keyup(unsigned char key, int x, int y)
195 app_keyboard(key, 0);
198 static void skeydown(int key, int x, int y)
201 modkeys = glutGetModifiers();
202 if((k = translate_skey(key)) >= 0) {
207 static void skeyup(int key, int x, int y)
209 int k = translate_skey(key);
215 static void mouse(int bn, int st, int x, int y)
217 modkeys = glutGetModifiers();
218 app_mouse(bn - GLUT_LEFT_BUTTON, st == GLUT_DOWN, x, y);
221 static void motion(int x, int y)
226 static int translate_skey(int key)
237 case GLUT_KEY_PAGE_UP:
239 case GLUT_KEY_PAGE_DOWN:
245 case GLUT_KEY_INSERT:
248 if(key >= GLUT_KEY_F1 && key <= GLUT_KEY_F12) {
249 return key - GLUT_KEY_F1 + KEY_F1;