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;
87 add_log_stream(stdout);
89 if(app_init() == -1) {
97 long app_getmsec(void)
99 return glutGet(GLUT_ELAPSED_TIME);
102 void app_redisplay(int x, int y, int w, int h)
104 dbgmsg("fakeupd: %d,%d (%dx%d)\n", x, y, w, h);
108 void app_swap_buffers(void)
110 glMatrixMode(GL_PROJECTION);
113 glMatrixMode(GL_MODELVIEW);
116 glRasterPos2i(-1, 1);
118 glEnable(GL_ALPHA_TEST);
119 glAlphaFunc(GL_GREATER, 0.5f);
120 glDrawPixels(win_width, win_height, GL_BGRA, GL_UNSIGNED_BYTE, framebuf);
121 glDisable(GL_ALPHA_TEST);
123 glMatrixMode(GL_PROJECTION);
125 glMatrixMode(GL_MODELVIEW);
128 assert(glGetError() == GL_NO_ERROR);
136 void app_resize(int x, int y)
138 if(x == win_width && y == win_height) return;
140 glutReshapeWindow(x, y);
143 void app_fullscreen(int fs)
145 static int prev_w, prev_h;
151 if(fs == fullscr) return;
154 prev_w = glutGet(GLUT_WINDOW_WIDTH);
155 prev_h = glutGet(GLUT_WINDOW_HEIGHT);
158 glutReshapeWindow(prev_w, prev_h);
163 #if defined(__unix__) || defined(unix)
164 void app_vsync(int vsync)
166 vsync = vsync ? 1 : 0;
167 if(glx_swap_interval_ext) {
168 glx_swap_interval_ext(xdpy, xwin, vsync);
169 } else if(glx_swap_interval_sgi) {
170 glx_swap_interval_sgi(vsync);
175 void app_vsync(int vsync)
177 if(wgl_swap_interval_ext) {
178 wgl_swap_interval_ext(vsync ? 1 : 0);
184 static void display(void)
190 static void reshape(int x, int y)
195 static void keydown(unsigned char key, int x, int y)
197 modkeys = glutGetModifiers();
198 app_keyboard(key, 1);
201 static void keyup(unsigned char key, int x, int y)
203 app_keyboard(key, 0);
206 static void skeydown(int key, int x, int y)
209 modkeys = glutGetModifiers();
210 if((k = translate_skey(key)) >= 0) {
215 static void skeyup(int key, int x, int y)
217 int k = translate_skey(key);
223 static void mouse(int bn, int st, int x, int y)
225 modkeys = glutGetModifiers();
226 app_mouse(bn - GLUT_LEFT_BUTTON, st == GLUT_DOWN, x, y);
229 static void motion(int x, int y)
234 static int translate_skey(int key)
245 case GLUT_KEY_PAGE_UP:
247 case GLUT_KEY_PAGE_DOWN:
253 case GLUT_KEY_INSERT:
256 if(key >= GLUT_KEY_F1 && key <= GLUT_KEY_F12) {
257 return key - GLUT_KEY_F1 + KEY_F1;