conworlds
changeset 20:782ff06817fb
merged ...
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Tue, 26 Aug 2014 18:42:53 +0300 |
parents | c5054e0cd564 c814f77d177e |
children | 2da585428507 |
files | src/game.cc src/glut/main.cc |
diffstat | 7 files changed, 201 insertions(+), 220 deletions(-) [+] |
line diff
1.1 --- a/Makefile Tue Aug 26 18:40:23 2014 +0300 1.2 +++ b/Makefile Tue Aug 26 18:42:53 2014 +0300 1.3 @@ -11,13 +11,13 @@ 1.4 ohmd_cflags = -DUSE_OPENHMD 1.5 ohmd_libs = -lopenhmd 1.6 1.7 -CFLAGS = -pedantic -Wall -g $(ovr_cflags) 1.8 +CFLAGS = -pedantic -Wall -g $(ovr_cflags) `pkg-config --cflags sdl2` 1.9 CXXFLAGS = -std=c++11 $(CFLAGS) 1.10 -LDFLAGS = $(libgl_$(sys)) -lm -lvmath -limago -lanim $(ovr_libs) 1.11 +LDFLAGS = $(libgl_$(sys)) -lm -lvmath -limago -lanim $(ovr_libs) `pkg-config --libs sdl2` 1.12 1.13 -libgl_unix = -lGL -lGLU -lglut -lGLEW 1.14 -libgl_mac = -framework OpenGL -framework GLUT -lGLEW 1.15 -libgl_win = -lopengl32 -lglu32 -lglut32 -lglew32 1.16 +libgl_unix = -lGL -lGLU -lGLEW 1.17 +libgl_mac = -framework OpenGL -lGLEW 1.18 +libgl_win = -lopengl32 -lglu32 -lglew32 1.19 1.20 $(bin): $(obj) 1.21 $(CXX) -o $@ $(obj) $(LDFLAGS)
2.1 --- a/src/game.cc Tue Aug 26 18:40:23 2014 +0300 2.2 +++ b/src/game.cc Tue Aug 26 18:42:53 2014 +0300 2.3 @@ -176,7 +176,7 @@ 2.4 setup_rtarg(rtwidth, rtheight); 2.5 } 2.6 2.7 -void game_keyboard(int key, bool pressed, int x, int y) 2.8 +void game_keyboard(int key, bool pressed) 2.9 { 2.10 if(pressed) { 2.11 switch(key) { 2.12 @@ -338,12 +338,12 @@ 2.13 } 2.14 2.15 static void draw_pyramid(float basesz, float height) 2.16 -{ 2.17 - float hsz = basesz / 2.0; 2.18 - float theta = atan(hsz / height); 2.19 - float nx = cos(theta); 2.20 - float ny = sin(theta); 2.21 - 2.22 +{ 2.23 + float hsz = basesz / 2.0; 2.24 + float theta = atan(hsz / height); 2.25 + float nx = cos(theta); 2.26 + float ny = sin(theta); 2.27 + 2.28 glBegin(GL_TRIANGLES); 2.29 glNormal3f(0, ny, nx); 2.30 glVertex3f(-hsz, 0, hsz); 2.31 @@ -362,4 +362,4 @@ 2.32 glVertex3f(-hsz, 0, hsz); 2.33 glVertex3f(0, height, 0); 2.34 glEnd(); 2.35 -} 2.36 \ No newline at end of file 2.37 +}
3.1 --- a/src/game.h Tue Aug 26 18:40:23 2014 +0300 3.2 +++ b/src/game.h Tue Aug 26 18:42:53 2014 +0300 3.3 @@ -8,7 +8,7 @@ 3.4 void game_render(); 3.5 3.6 void game_reshape(int x, int y); 3.7 -void game_keyboard(int key, bool pressed, int x, int y); 3.8 +void game_keyboard(int key, bool pressed); 3.9 void game_mouse(int bn, bool pressed, int x, int y); 3.10 void game_motion(int x, int y); 3.11 void game_mwheel(int dir);
4.1 --- a/src/glut/main.cc Tue Aug 26 18:40:23 2014 +0300 4.2 +++ b/src/glut/main.cc Tue Aug 26 18:42:53 2014 +0300 4.3 @@ -1,53 +1,64 @@ 4.4 #include <stdio.h> 4.5 #include <stdlib.h> 4.6 +#include <SDL2/SDL.h> 4.7 #include "opengl.h" 4.8 #include "game.h" 4.9 #include "gameopt.h" 4.10 #include "vr/vr.h" 4.11 4.12 +#define ANYPOS SDL_WINDOWPOS_UNDEFINED 4.13 + 4.14 static bool init(); 4.15 static void cleanup(); 4.16 +static void handle_event(SDL_Event *ev); 4.17 4.18 -static void display(); 4.19 -static void idle(); 4.20 -static void reshape(int x, int y); 4.21 -static void keyb(unsigned char key, int x, int y); 4.22 -static void keyb_up(unsigned char key, int x, int y); 4.23 -static void mouse(int bn, int st, int x, int y); 4.24 -static void motion(int x, int y); 4.25 -static void sball_motion(int x, int y, int z); 4.26 -static void sball_rotate(int x, int y, int z); 4.27 - 4.28 -static bool fullscreen_pending; 4.29 +static SDL_Window *win; 4.30 +static SDL_GLContext ctx; 4.31 4.32 int main(int argc, char **argv) 4.33 { 4.34 - glutInitWindowSize(1024, 600); 4.35 - glutInit(&argc, argv); 4.36 - 4.37 if(!parse_args(argc, argv)) { 4.38 return 1; 4.39 } 4.40 4.41 - glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | (opt.stereo ? GLUT_STEREO : 0)); 4.42 - glutCreateWindow("LD48 #30 - connected worlds"); 4.43 + SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER); 4.44 4.45 - glutDisplayFunc(display); 4.46 - glutIdleFunc(idle); 4.47 - glutReshapeFunc(reshape); 4.48 - glutKeyboardFunc(keyb); 4.49 - glutKeyboardUpFunc(keyb_up); 4.50 - glutMouseFunc(mouse); 4.51 - glutMotionFunc(motion); 4.52 - glutSpaceballMotionFunc(sball_motion); 4.53 - glutSpaceballRotateFunc(sball_rotate); 4.54 + unsigned int flags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE; 4.55 + if(!(win = SDL_CreateWindow("oculus test", ANYPOS, ANYPOS, 1024, 600, flags))) { 4.56 + fprintf(stderr, "failed to create window\n"); 4.57 + return 1; 4.58 + } 4.59 + 4.60 + SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1); 4.61 + SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4); 4.62 + 4.63 + if(!(ctx = SDL_GL_CreateContext(win))) { 4.64 + fprintf(stderr, "failed to create OpenGL context\n"); 4.65 + return 1; 4.66 + } 4.67 4.68 if(!init()) { 4.69 return 1; 4.70 } 4.71 atexit(cleanup); 4.72 4.73 - glutMainLoop(); 4.74 + int xsz, ysz; 4.75 + SDL_GetWindowSize(win, &xsz, &ysz); 4.76 + game_reshape(xsz, ysz); 4.77 + 4.78 + for(;;) { 4.79 + SDL_Event ev; 4.80 + 4.81 + while(SDL_PollEvent(&ev)) { 4.82 + handle_event(&ev); 4.83 + } 4.84 + 4.85 + unsigned int msec = SDL_GetTicks(); 4.86 + 4.87 + game_update(msec); 4.88 + game_render(); 4.89 + } 4.90 + 4.91 return 0; 4.92 } 4.93 4.94 @@ -63,7 +74,7 @@ 4.95 int win_xsz = vr_get_opti(VR_OPT_DISPLAY_WIDTH); 4.96 int win_ysz = vr_get_opti(VR_OPT_DISPLAY_HEIGHT); 4.97 if(win_xsz && win_ysz) { 4.98 - glutReshapeWindow(win_xsz, win_ysz); 4.99 + SDL_SetWindowSize(win, win_xsz, win_ysz); 4.100 } 4.101 } 4.102 return true; 4.103 @@ -74,101 +85,66 @@ 4.104 game_cleanup(); 4.105 } 4.106 4.107 -static void display() 4.108 -{ 4.109 - unsigned int msec = glutGet(GLUT_ELAPSED_TIME); 4.110 - 4.111 - game_update(msec); 4.112 - game_render(); 4.113 -} 4.114 - 4.115 -static void idle() 4.116 -{ 4.117 - if(fullscreen_pending) { 4.118 - glutFullScreen(); 4.119 - } 4.120 - 4.121 - glutPostRedisplay(); 4.122 -} 4.123 - 4.124 -static void reshape(int x, int y) 4.125 -{ 4.126 - game_reshape(x, y); 4.127 -} 4.128 - 4.129 -static void keyb(unsigned char key, int x, int y) 4.130 +static void handle_event(SDL_Event *ev) 4.131 { 4.132 static bool fullscr; 4.133 static int prev_xpos, prev_ypos; 4.134 static int prev_xsz, prev_ysz; 4.135 4.136 - switch(key) { 4.137 - case 'f': 4.138 - fullscr = !fullscr; 4.139 - if(fullscr) { 4.140 - int xoffs, yoffs; 4.141 + switch(ev->type) { 4.142 + case SDL_KEYDOWN: 4.143 + case SDL_KEYUP: 4.144 + if(ev->key.keysym.sym == 'f' && ev->key.state == SDL_PRESSED) { 4.145 + fullscr = !fullscr; 4.146 + if(fullscr) { 4.147 + int xoffs, yoffs; 4.148 4.149 - prev_xpos = glutGet(GLUT_WINDOW_X); 4.150 - prev_ypos = glutGet(GLUT_WINDOW_Y); 4.151 - prev_xsz = glutGet(GLUT_WINDOW_WIDTH); 4.152 - prev_ysz = glutGet(GLUT_WINDOW_HEIGHT); 4.153 + SDL_GetWindowPosition(win, &prev_xpos, &prev_ypos); 4.154 + SDL_GetWindowSize(win, &prev_xsz, &prev_ysz); 4.155 4.156 - xoffs = vr_get_opti(VR_OPT_WIN_XOFFS); 4.157 - yoffs = vr_get_opti(VR_OPT_WIN_YOFFS); 4.158 - if(xoffs || yoffs) { 4.159 - printf("repositioning: %d,%d\n", xoffs, yoffs); 4.160 - glutPositionWindow(xoffs, yoffs); 4.161 + xoffs = vr_get_opti(VR_OPT_WIN_XOFFS); 4.162 + yoffs = vr_get_opti(VR_OPT_WIN_YOFFS); 4.163 + if(xoffs || yoffs) { 4.164 + printf("repositioning: %d,%d\n", xoffs, yoffs); 4.165 + SDL_SetWindowPosition(win, xoffs, yoffs); 4.166 + } 4.167 + SDL_SetWindowFullscreen(win, SDL_WINDOW_FULLSCREEN_DESKTOP); 4.168 + } else { 4.169 + SDL_SetWindowFullscreen(win, 0); 4.170 + /*SDL_SetWindowPosition(win, prev_xpos, prev_ypos); 4.171 + SDL_SetWindowSize(win, prev_xsz, prev_ysz);*/ 4.172 } 4.173 - fullscreen_pending = true; 4.174 - } else { 4.175 - fullscreen_pending = false; 4.176 - glutPositionWindow(prev_xpos, prev_ypos); 4.177 - glutReshapeWindow(prev_xsz, prev_ysz); 4.178 + break; 4.179 } 4.180 + 4.181 + game_keyboard(ev->key.keysym.sym, ev->key.state == SDL_PRESSED); 4.182 + break; 4.183 + 4.184 + case SDL_MOUSEBUTTONDOWN: 4.185 + case SDL_MOUSEBUTTONUP: 4.186 + game_mouse(ev->button.button - SDL_BUTTON_LEFT, ev->button.state == SDL_PRESSED, 4.187 + ev->button.x, ev->button.y); 4.188 + break; 4.189 + 4.190 + case SDL_MOUSEWHEEL: 4.191 + game_mwheel(ev->wheel.y); 4.192 + break; 4.193 + 4.194 + case SDL_MOUSEMOTION: 4.195 + game_motion(ev->motion.x, ev->motion.y); 4.196 + break; 4.197 + 4.198 + case SDL_WINDOWEVENT: 4.199 + switch(ev->window.event) { 4.200 + case SDL_WINDOWEVENT_RESIZED: 4.201 + game_reshape(ev->window.data1, ev->window.data2); 4.202 + break; 4.203 + 4.204 + default: 4.205 + break; 4.206 + } 4.207 + 4.208 + default: 4.209 break; 4.210 } 4.211 - game_keyboard(key, true, x, y); 4.212 } 4.213 - 4.214 -static void keyb_up(unsigned char key, int x, int y) 4.215 -{ 4.216 - game_keyboard(key, false, x, y); 4.217 -} 4.218 - 4.219 -static void mouse(int bn, int st, int x, int y) 4.220 -{ 4.221 - switch(bn) { 4.222 - case GLUT_RIGHT_BUTTON + 1: 4.223 - if(st == GLUT_DOWN) { 4.224 - game_mwheel(1); 4.225 - } 4.226 - break; 4.227 - 4.228 - case GLUT_RIGHT_BUTTON + 2: 4.229 - if(st == GLUT_DOWN) { 4.230 - game_mwheel(-1); 4.231 - } 4.232 - break; 4.233 - 4.234 - default: 4.235 - game_mouse(bn - GLUT_LEFT_BUTTON, st == GLUT_DOWN, x, y); 4.236 - } 4.237 -} 4.238 - 4.239 -static void motion(int x, int y) 4.240 -{ 4.241 - game_motion(x, y); 4.242 -} 4.243 - 4.244 -#define SBALL_MOVE_SCALE 0.00025 4.245 -#define SBALL_ROT_SCALE 0.01 4.246 - 4.247 -static void sball_motion(int x, int y, int z) 4.248 -{ 4.249 - game_6dof_move(x * SBALL_MOVE_SCALE, y * SBALL_MOVE_SCALE, z * SBALL_MOVE_SCALE); 4.250 -} 4.251 - 4.252 -static void sball_rotate(int x, int y, int z) 4.253 -{ 4.254 - game_6dof_rotate(x * SBALL_ROT_SCALE, y * SBALL_ROT_SCALE, z * SBALL_ROT_SCALE); 4.255 -}
5.1 --- a/src/opengl.h Tue Aug 26 18:40:23 2014 +0300 5.2 +++ b/src/opengl.h Tue Aug 26 18:42:53 2014 +0300 5.3 @@ -4,9 +4,14 @@ 5.4 #include <GL/glew.h> 5.5 5.6 #ifndef __APPLE__ 5.7 -#include <GL/glut.h> 5.8 + 5.9 +#ifdef WIN32 5.10 +#include <windows.h> 5.11 +#endif 5.12 + 5.13 +#include <GL/gl.h> 5.14 #else 5.15 -#include <GLUT/glut.h> 5.16 +#include <OpenGL/gl.h> 5.17 #endif 5.18 5.19 #define CHECKGLERR \
6.1 --- a/src/vr/mathutil.c Tue Aug 26 18:40:23 2014 +0300 6.2 +++ b/src/vr/mathutil.c Tue Aug 26 18:42:53 2014 +0300 6.3 @@ -1,78 +1,78 @@ 6.4 -#include <stdio.h> 6.5 -#include <string.h> 6.6 -#include "mathutil.h" 6.7 - 6.8 -#define M(i, j) ((i) * 4 + (j)) 6.9 - 6.10 -static const float idmat[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}; 6.11 - 6.12 -void rotation_matrix(const float *quat, float *matrix) 6.13 -{ 6.14 - matrix[0] = 1.0 - 2.0 * quat[1] * quat[1] - 2.0 * quat[2] * quat[2]; 6.15 - matrix[1] = 2.0 * quat[0] * quat[1] + 2.0 * quat[3] * quat[2]; 6.16 - matrix[2] = 2.0 * quat[2] * quat[0] - 2.0 * quat[3] * quat[1]; 6.17 - matrix[3] = 0.0f; 6.18 - 6.19 - matrix[4] = 2.0 * quat[0] * quat[1] - 2.0 * quat[3] * quat[2]; 6.20 - matrix[5] = 1.0 - 2.0 * quat[0]*quat[0] - 2.0 * quat[2]*quat[2]; 6.21 - matrix[6] = 2.0 * quat[1] * quat[2] + 2.0 * quat[3] * quat[0]; 6.22 - matrix[7] = 0.0f; 6.23 - 6.24 - matrix[8] = 2.0 * quat[2] * quat[0] + 2.0 * quat[3] * quat[1]; 6.25 - matrix[9] = 2.0 * quat[1] * quat[2] - 2.0 * quat[3] * quat[0]; 6.26 - matrix[10] = 1.0 - 2.0 * quat[0]*quat[0] - 2.0 * quat[1]*quat[1]; 6.27 - matrix[11] = 0.0f; 6.28 - 6.29 - matrix[12] = matrix[13] = matrix[14] = 0.0f; 6.30 - matrix[15] = 1.0f; 6.31 - 6.32 - transpose_matrix(matrix); 6.33 -} 6.34 - 6.35 -void translation_matrix(const float *vec, float *matrix) 6.36 -{ 6.37 - memcpy(matrix, idmat, sizeof idmat); 6.38 - matrix[12] = vec[0]; 6.39 - matrix[13] = vec[1]; 6.40 - matrix[14] = vec[2]; 6.41 -} 6.42 - 6.43 -void mult_matrix(float *dest, const float *m1, const float *m2) 6.44 -{ 6.45 - int i, j; 6.46 - float tmp[16]; 6.47 - 6.48 - for(i=0; i<4; i++) { 6.49 - for(j=0; j<4; j++) { 6.50 - tmp[M(i, j)] = m1[M(i, 0)] * m2[M(0, j)] + m1[M(i, 1)] * m2[M(1, j)] + 6.51 - m1[M(i, 2)] * m2[M(2, j)] + m1[M(i, 3)] * m2[M(3, j)]; 6.52 - } 6.53 - } 6.54 - memcpy(dest, tmp, sizeof tmp); 6.55 -} 6.56 - 6.57 -void transpose_matrix(float *matrix) 6.58 -{ 6.59 - int i, j; 6.60 - float tmp[16]; 6.61 - 6.62 - for(i=0; i<4; i++) { 6.63 - for(j=0; j<4; j++) { 6.64 - tmp[M(i, j)] = matrix[M(j, i)]; 6.65 - } 6.66 - } 6.67 - memcpy(matrix, tmp, sizeof tmp); 6.68 -} 6.69 - 6.70 -void print_matrix(const float *matrix) 6.71 -{ 6.72 - int i, j; 6.73 - 6.74 - for(i=0; i<4; i++) { 6.75 - putchar('['); 6.76 - for(j=0; j<4; j++) { 6.77 - printf("%6.3f ", matrix[M(i, j)]); 6.78 - } 6.79 - printf("]\n"); 6.80 - } 6.81 -} 6.82 \ No newline at end of file 6.83 +#include <stdio.h> 6.84 +#include <string.h> 6.85 +#include "mathutil.h" 6.86 + 6.87 +#define M(i, j) ((i) * 4 + (j)) 6.88 + 6.89 +static const float idmat[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}; 6.90 + 6.91 +void rotation_matrix(const float *quat, float *matrix) 6.92 +{ 6.93 + matrix[0] = 1.0 - 2.0 * quat[1] * quat[1] - 2.0 * quat[2] * quat[2]; 6.94 + matrix[1] = 2.0 * quat[0] * quat[1] + 2.0 * quat[3] * quat[2]; 6.95 + matrix[2] = 2.0 * quat[2] * quat[0] - 2.0 * quat[3] * quat[1]; 6.96 + matrix[3] = 0.0f; 6.97 + 6.98 + matrix[4] = 2.0 * quat[0] * quat[1] - 2.0 * quat[3] * quat[2]; 6.99 + matrix[5] = 1.0 - 2.0 * quat[0]*quat[0] - 2.0 * quat[2]*quat[2]; 6.100 + matrix[6] = 2.0 * quat[1] * quat[2] + 2.0 * quat[3] * quat[0]; 6.101 + matrix[7] = 0.0f; 6.102 + 6.103 + matrix[8] = 2.0 * quat[2] * quat[0] + 2.0 * quat[3] * quat[1]; 6.104 + matrix[9] = 2.0 * quat[1] * quat[2] - 2.0 * quat[3] * quat[0]; 6.105 + matrix[10] = 1.0 - 2.0 * quat[0]*quat[0] - 2.0 * quat[1]*quat[1]; 6.106 + matrix[11] = 0.0f; 6.107 + 6.108 + matrix[12] = matrix[13] = matrix[14] = 0.0f; 6.109 + matrix[15] = 1.0f; 6.110 + 6.111 + transpose_matrix(matrix); 6.112 +} 6.113 + 6.114 +void translation_matrix(const float *vec, float *matrix) 6.115 +{ 6.116 + memcpy(matrix, idmat, sizeof idmat); 6.117 + matrix[12] = vec[0]; 6.118 + matrix[13] = vec[1]; 6.119 + matrix[14] = vec[2]; 6.120 +} 6.121 + 6.122 +void mult_matrix(float *dest, const float *m1, const float *m2) 6.123 +{ 6.124 + int i, j; 6.125 + float tmp[16]; 6.126 + 6.127 + for(i=0; i<4; i++) { 6.128 + for(j=0; j<4; j++) { 6.129 + tmp[M(i, j)] = m1[M(i, 0)] * m2[M(0, j)] + m1[M(i, 1)] * m2[M(1, j)] + 6.130 + m1[M(i, 2)] * m2[M(2, j)] + m1[M(i, 3)] * m2[M(3, j)]; 6.131 + } 6.132 + } 6.133 + memcpy(dest, tmp, sizeof tmp); 6.134 +} 6.135 + 6.136 +void transpose_matrix(float *matrix) 6.137 +{ 6.138 + int i, j; 6.139 + float tmp[16]; 6.140 + 6.141 + for(i=0; i<4; i++) { 6.142 + for(j=0; j<4; j++) { 6.143 + tmp[M(i, j)] = matrix[M(j, i)]; 6.144 + } 6.145 + } 6.146 + memcpy(matrix, tmp, sizeof tmp); 6.147 +} 6.148 + 6.149 +void print_matrix(const float *matrix) 6.150 +{ 6.151 + int i, j; 6.152 + 6.153 + for(i=0; i<4; i++) { 6.154 + putchar('['); 6.155 + for(j=0; j<4; j++) { 6.156 + printf("%6.3f ", matrix[M(i, j)]); 6.157 + } 6.158 + printf("]\n"); 6.159 + } 6.160 +}
7.1 --- a/src/vr/mathutil.h Tue Aug 26 18:40:23 2014 +0300 7.2 +++ b/src/vr/mathutil.h Tue Aug 26 18:42:53 2014 +0300 7.3 @@ -1,13 +1,13 @@ 7.4 -#ifndef MATHUTIL_H_ 7.5 -#define MATHUTIL_H_ 7.6 - 7.7 -void rotation_matrix(const float *quat, float *matrix); 7.8 -void translation_matrix(const float *vec, float *matrix); 7.9 - 7.10 -void mult_matrix(float *dest, const float *m1, const float *m2); 7.11 - 7.12 -void transpose_matrix(float *matrix); 7.13 - 7.14 -void print_matrix(const float *matrix); 7.15 - 7.16 -#endif /* MATHUTIL_H_ */ 7.17 \ No newline at end of file 7.18 +#ifndef MATHUTIL_H_ 7.19 +#define MATHUTIL_H_ 7.20 + 7.21 +void rotation_matrix(const float *quat, float *matrix); 7.22 +void translation_matrix(const float *vec, float *matrix); 7.23 + 7.24 +void mult_matrix(float *dest, const float *m1, const float *m2); 7.25 + 7.26 +void transpose_matrix(float *matrix); 7.27 + 7.28 +void print_matrix(const float *matrix); 7.29 + 7.30 +#endif /* MATHUTIL_H_ */