bloboland

view 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 source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <assert.h>
4 #include "opengl.h"
5 #include "game.h"
6 #include "opt.h"
8 static void disp();
9 static void idle();
10 static void reshape(int x, int y);
11 static void keydown(unsigned char key, int x, int y);
12 static void keyup(unsigned char key, int x, int y);
13 static void skeydown(int key, int x, int y);
14 static void skeyup(int key, int x, int y);
15 static void mouse(int bn, int state, int x, int y);
16 static void motion(int x, int y);
17 static void spacemove(int x, int y, int z);
18 static void spacerot(int x, int y, int z);
19 static void spacebut(int bn, int state);
21 static int win_xsz, win_ysz, centerx, centery;
22 static unsigned int prev_msec;
24 int main(int argc, char **argv)
25 {
26 glutInit(&argc, argv);
28 if(!parse_opt(argc, argv)) {
29 return 1;
30 }
32 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | (opt.stereo ? GLUT_STEREO : 0));
33 glutInitWindowSize(opt.xsz, opt.ysz);
34 glutCreateWindow("bloboland");
36 glutDisplayFunc(disp);
37 glutIdleFunc(idle);
38 glutReshapeFunc(reshape);
39 glutKeyboardFunc(keydown);
40 glutKeyboardUpFunc(keyup);
41 glutSpecialFunc(skeydown);
42 glutSpecialUpFunc(skeyup);
43 glutMouseFunc(mouse);
44 glutMotionFunc(motion);
45 glutSpaceballMotionFunc(spacemove);
46 glutSpaceballRotateFunc(spacerot);
47 glutSpaceballButtonFunc(spacebut);
49 glewInit();
51 if(!game_init()) {
52 return 1;
53 }
55 glutMainLoop();
56 }
58 static void disp()
59 {
60 unsigned int msec = glutGet(GLUT_ELAPSED_TIME);
61 game_iter((msec - prev_msec) / 1000.0);
64 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
66 game_render();
68 glutSwapBuffers();
69 assert(glGetError() == GL_NO_ERROR);
70 }
72 static void idle()
73 {
74 glutPostRedisplay();
75 }
77 static void reshape(int x, int y)
78 {
79 glViewport(0, 0, x, y);
81 glMatrixMode(GL_PROJECTION);
82 glLoadIdentity();
83 gluPerspective(50.0, (float)x / (float)y, 0.5, 500.0);
85 win_xsz = x;
86 win_ysz = y;
88 centerx = x / 2;
89 centery = y / 2;
90 }
92 static void keydown(unsigned char key, int x, int y)
93 {
94 skeydown(key, x, y);
95 }
97 static void keyup(unsigned char key, int x, int y)
98 {
99 skeyup(key, x, y);
100 }
102 static void skeydown(int key, int x, int y)
103 {
104 if(key == 27) {
105 exit(0);
106 }
108 if(key < GAME_MAX_KEYS) {
109 keystate[key] = true;
110 }
111 }
113 static void skeyup(int key, int x, int y)
114 {
115 if(key < GAME_MAX_KEYS) {
116 keystate[key] = false;
117 }
118 }
121 static int prev_x, prev_y;
123 static void mouse(int bn, int state, int x, int y)
124 {
125 int idx = bn - GLUT_LEFT_BUTTON;
127 if(idx < GAME_MAX_BUTTONS) {
128 bnstate[idx] = state == GLUT_DOWN;
130 if(state == GLUT_DOWN) {
131 game_input_shoot(idx);
132 }
133 }
134 prev_x = x;
135 prev_y = y;
136 }
139 static void motion(int x, int y)
140 {
141 /*
142 int dx = x - centerx;
143 int dy = y - centery;
145 if(!dx && !dy) {
146 return;
147 }
148 game_input_rot(dx * 0.5, dy * 0.5);
150 glutWarpPointer(centerx, centery);
151 */
152 int dx = x - prev_x;
153 int dy = y - prev_y;
155 prev_x = x;
156 prev_y = y;
158 if(bnstate[0]) {
159 game_input_rot((float)dx / win_xsz, (float)dy / win_ysz);
160 }
161 }
163 static void spacemove(int x, int y, int z)
164 {
165 game_input_move(x * 0.1, y * 0.1, z * 0.1);
166 }
168 static void spacerot(int x, int y, int z)
169 {
170 game_input_rot(y * 0.1, x * 0.1);
171 }
173 static void spacebut(int bn, int state)
174 {
175 game_input_shoot(bn);
176 }