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/>.
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);
33 #if defined(__unix__) || defined(unix)
38 static void (*glx_swap_interval_ext)();
39 static void (*glx_swap_interval_sgi)();
43 static PROC wgl_swap_interval_ext;
48 int main(int argc, char **argv)
50 glutInit(&argc, argv);
51 glutInitWindowSize(640, 480);
52 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
53 glutCreateWindow("RetroRay");
55 glutDisplayFunc(app_display);
56 glutReshapeFunc(reshape);
57 glutKeyboardFunc(keydown);
58 glutKeyboardUpFunc(keyup);
59 glutSpecialFunc(skeydown);
60 glutSpecialUpFunc(skeyup);
62 glutMotionFunc(motion);
63 glutPassiveMotionFunc(motion);
64 glutSpaceballMotionFunc(app_sball_motion);
65 glutSpaceballRotateFunc(app_sball_rotate);
66 glutSpaceballButtonFunc(app_sball_button);
68 #if defined(__unix__) || defined(unix)
69 xdpy = glXGetCurrentDisplay();
70 xwin = glXGetCurrentDrawable();
72 if(!(glx_swap_interval_ext = glXGetProcAddress((unsigned char*)"glXSwapIntervalEXT"))) {
73 glx_swap_interval_sgi = glXGetProcAddress((unsigned char*)"glXSwapIntervalSGI");
77 wgl_swap_interval_ext = wglGetProcAddress("wglSwapIntervalEXT");
80 app_reshape(glutGet(GLUT_WINDOW_WIDTH), glutGet(GLUT_WINDOW_HEIGHT));
82 if(app_init() == -1) {
90 long app_getmsec(void)
92 return glutGet(GLUT_ELAPSED_TIME);
95 void app_redisplay(void)
100 void app_swap_buffers(void)
102 glMatrixMode(GL_PROJECTION);
105 glMatrixMode(GL_MODELVIEW);
108 glRasterPos2i(-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);
115 glMatrixMode(GL_PROJECTION);
117 glMatrixMode(GL_MODELVIEW);
120 assert(glGetError() == GL_NO_ERROR);
128 void app_resize(int x, int y)
130 if(x == win_width && y == win_height) return;
132 glutReshapeWindow(x, y);
135 void app_fullscreen(int fs)
137 static int prev_w, prev_h;
143 if(fs == fullscr) return;
146 prev_w = glutGet(GLUT_WINDOW_WIDTH);
147 prev_h = glutGet(GLUT_WINDOW_HEIGHT);
150 glutReshapeWindow(prev_w, prev_h);
155 #if defined(__unix__) || defined(unix)
156 void app_vsync(int vsync)
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);
167 void app_vsync(int vsync)
169 if(wgl_swap_interval_ext) {
170 wgl_swap_interval_ext(vsync ? 1 : 0);
177 static void reshape(int x, int y)
182 static void keydown(unsigned char key, int x, int y)
184 modkeys = glutGetModifiers();
185 app_keyboard(key, 1);
188 static void keyup(unsigned char key, int x, int y)
190 app_keyboard(key, 0);
193 static void skeydown(int key, int x, int y)
196 modkeys = glutGetModifiers();
197 if((k = translate_skey(key)) >= 0) {
202 static void skeyup(int key, int x, int y)
204 int k = translate_skey(key);
210 static void mouse(int bn, int st, int x, int y)
212 modkeys = glutGetModifiers();
213 app_mouse(bn - GLUT_LEFT_BUTTON, st == GLUT_DOWN, x, y);
216 static void motion(int x, int y)
221 static int translate_skey(int key)
232 case GLUT_KEY_PAGE_UP:
234 case GLUT_KEY_PAGE_DOWN:
240 case GLUT_KEY_INSERT:
243 if(key >= GLUT_KEY_F1 && key <= GLUT_KEY_F12) {
244 return key - GLUT_KEY_F1 + KEY_F1;