bloboland
diff src/main.cc @ 1:cfe68befb7cc
some progress
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 15 Dec 2012 23:43:03 +0200 |
parents | e4818a3300b9 |
children | 1757973feaed |
line diff
1.1 --- a/src/main.cc Sat Dec 15 07:52:13 2012 +0200 1.2 +++ b/src/main.cc Sat Dec 15 23:43:03 2012 +0200 1.3 @@ -1,10 +1,12 @@ 1.4 #include <stdio.h> 1.5 #include <stdlib.h> 1.6 +#include <assert.h> 1.7 #include "opengl.h" 1.8 #include "game.h" 1.9 #include "opt.h" 1.10 1.11 static void disp(); 1.12 +static void idle(); 1.13 static void reshape(int x, int y); 1.14 static void keydown(unsigned char key, int x, int y); 1.15 static void keyup(unsigned char key, int x, int y); 1.16 @@ -16,6 +18,9 @@ 1.17 static void spacerot(int x, int y, int z); 1.18 static void spacebut(int bn, int state); 1.19 1.20 +static int win_xsz, win_ysz, centerx, centery; 1.21 +static unsigned int prev_msec; 1.22 + 1.23 int main(int argc, char **argv) 1.24 { 1.25 glutInit(&argc, argv); 1.26 @@ -25,10 +30,11 @@ 1.27 } 1.28 1.29 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | (opt.stereo ? GLUT_STEREO : 0)); 1.30 - glutInitWindowSize(800, 450); 1.31 + glutInitWindowSize(opt.xsz, opt.ysz); 1.32 glutCreateWindow("bloboland"); 1.33 1.34 glutDisplayFunc(disp); 1.35 + glutIdleFunc(idle); 1.36 glutReshapeFunc(reshape); 1.37 glutKeyboardFunc(keydown); 1.38 glutKeyboardUpFunc(keyup); 1.39 @@ -55,11 +61,17 @@ 1.40 game_iter((msec - prev_msec) / 1000.0); 1.41 1.42 1.43 - glClear(GL_COLOR_BUFFER_BIT); 1.44 + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 1.45 1.46 game_render(); 1.47 1.48 glutSwapBuffers(); 1.49 + assert(glGetError() == GL_NO_ERROR); 1.50 +} 1.51 + 1.52 +static void idle() 1.53 +{ 1.54 + glutPostRedisplay(); 1.55 } 1.56 1.57 static void reshape(int x, int y) 1.58 @@ -69,6 +81,12 @@ 1.59 glMatrixMode(GL_PROJECTION); 1.60 glLoadIdentity(); 1.61 gluPerspective(50.0, (float)x / (float)y, 0.5, 500.0); 1.62 + 1.63 + win_xsz = x; 1.64 + win_ysz = y; 1.65 + 1.66 + centerx = x / 2; 1.67 + centery = y / 2; 1.68 } 1.69 1.70 static void keydown(unsigned char key, int x, int y) 1.71 @@ -87,35 +105,73 @@ 1.72 exit(0); 1.73 } 1.74 1.75 - if(key < sizeof keystate / sizeof *keystate) { 1.76 + if(key < GAME_MAX_KEYS) { 1.77 keystate[key] = true; 1.78 } 1.79 } 1.80 1.81 static void skeyup(int key, int x, int y) 1.82 { 1.83 - if(key < sizeof keystate / sizeof *keystate) { 1.84 + if(key < GAME_MAX_KEYS) { 1.85 keystate[key] = false; 1.86 } 1.87 } 1.88 1.89 + 1.90 +static int prev_x, prev_y; 1.91 + 1.92 static void mouse(int bn, int state, int x, int y) 1.93 { 1.94 + int idx = bn - GLUT_LEFT_BUTTON; 1.95 + 1.96 + if(idx < GAME_MAX_BUTTONS) { 1.97 + bnstate[idx] = state == GLUT_DOWN; 1.98 + 1.99 + if(state == GLUT_DOWN) { 1.100 + game_input_shoot(idx); 1.101 + } 1.102 + } 1.103 + prev_x = x; 1.104 + prev_y = y; 1.105 } 1.106 1.107 + 1.108 static void motion(int x, int y) 1.109 { 1.110 + /* 1.111 + int dx = x - centerx; 1.112 + int dy = y - centery; 1.113 + 1.114 + if(!dx && !dy) { 1.115 + return; 1.116 + } 1.117 + game_input_rot(dx * 0.5, dy * 0.5); 1.118 + 1.119 + glutWarpPointer(centerx, centery); 1.120 + */ 1.121 + int dx = x - prev_x; 1.122 + int dy = y - prev_y; 1.123 + 1.124 + prev_x = x; 1.125 + prev_y = y; 1.126 + 1.127 + if(bnstate[0]) { 1.128 + game_input_rot((float)dx / win_xsz, (float)dy / win_ysz); 1.129 + } 1.130 } 1.131 1.132 static void spacemove(int x, int y, int z) 1.133 { 1.134 + game_input_move(x * 0.1, y * 0.1, z * 0.1); 1.135 } 1.136 1.137 static void spacerot(int x, int y, int z) 1.138 { 1.139 + game_input_rot(y * 0.1, x * 0.1); 1.140 } 1.141 1.142 static void spacebut(int bn, int state) 1.143 { 1.144 + game_input_shoot(bn); 1.145 } 1.146