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