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 win_width = glutGet(GLUT_WINDOW_WIDTH);
81 win_height = glutGet(GLUT_WINDOW_HEIGHT);
82 win_aspect = (float)win_width / win_height;
84 if(app_init() == -1) {
92 long app_getmsec(void)
94 return glutGet(GLUT_ELAPSED_TIME);
97 void app_redisplay(void)
102 void app_swap_buffers(void)
104 glMatrixMode(GL_PROJECTION);
107 glMatrixMode(GL_MODELVIEW);
110 glRasterPos2i(-1, 1);
112 glEnable(GL_ALPHA_TEST);
113 glAlphaFunc(GL_GREATER, 0.5f);
114 glDrawPixels(win_width, win_height, GL_BGRA, GL_UNSIGNED_BYTE, framebuf);
115 glDisable(GL_ALPHA_TEST);
117 glMatrixMode(GL_PROJECTION);
119 glMatrixMode(GL_MODELVIEW);
122 assert(glGetError() == GL_NO_ERROR);
130 void app_resize(int x, int y)
132 if(x == win_width && y == win_height) return;
134 glutReshapeWindow(x, y);
137 void app_fullscreen(int fs)
139 static int prev_w, prev_h;
145 if(fs == fullscr) return;
148 prev_w = glutGet(GLUT_WINDOW_WIDTH);
149 prev_h = glutGet(GLUT_WINDOW_HEIGHT);
152 glutReshapeWindow(prev_w, prev_h);
157 #if defined(__unix__) || defined(unix)
158 void app_vsync(int vsync)
160 vsync = vsync ? 1 : 0;
161 if(glx_swap_interval_ext) {
162 glx_swap_interval_ext(xdpy, xwin, vsync);
163 } else if(glx_swap_interval_sgi) {
164 glx_swap_interval_sgi(vsync);
169 void app_vsync(int vsync)
171 if(wgl_swap_interval_ext) {
172 wgl_swap_interval_ext(vsync ? 1 : 0);
179 static void reshape(int x, int y)
184 static void keydown(unsigned char key, int x, int y)
186 modkeys = glutGetModifiers();
187 app_keyboard(key, 1);
190 static void keyup(unsigned char key, int x, int y)
192 app_keyboard(key, 0);
195 static void skeydown(int key, int x, int y)
198 modkeys = glutGetModifiers();
199 if((k = translate_skey(key)) >= 0) {
204 static void skeyup(int key, int x, int y)
206 int k = translate_skey(key);
212 static void mouse(int bn, int st, int x, int y)
214 modkeys = glutGetModifiers();
215 app_mouse(bn - GLUT_LEFT_BUTTON, st == GLUT_DOWN, x, y);
218 static void motion(int x, int y)
223 static int translate_skey(int key)
234 case GLUT_KEY_PAGE_UP:
236 case GLUT_KEY_PAGE_DOWN:
242 case GLUT_KEY_INSERT:
245 if(key >= GLUT_KEY_F1 && key <= GLUT_KEY_F12) {
246 return key - GLUT_KEY_F1 + KEY_F1;