rev |
line source |
nuclear@0
|
1 #include <stdio.h>
|
nuclear@0
|
2 #include <GL/glew.h>
|
nuclear@0
|
3 #include "game.h"
|
nuclear@1
|
4 #include "board.h"
|
nuclear@0
|
5
|
nuclear@0
|
6 static void draw_backdrop();
|
nuclear@0
|
7
|
nuclear@0
|
8 int win_width, win_height;
|
nuclear@0
|
9
|
nuclear@1
|
10 static Board board;
|
nuclear@1
|
11
|
nuclear@0
|
12 static float cam_theta, cam_phi = 25, cam_dist = 6;
|
nuclear@0
|
13 static bool bnstate[8];
|
nuclear@0
|
14 static int prev_x, prev_y;
|
nuclear@0
|
15
|
nuclear@0
|
16
|
nuclear@0
|
17 bool game_init()
|
nuclear@0
|
18 {
|
nuclear@0
|
19 glEnable(GL_DEPTH_TEST);
|
nuclear@0
|
20 glEnable(GL_CULL_FACE);
|
nuclear@0
|
21
|
nuclear@0
|
22 glEnable(GL_LIGHTING);
|
nuclear@0
|
23 glEnable(GL_LIGHT0);
|
nuclear@0
|
24
|
nuclear@1
|
25 if(!board.init()) {
|
nuclear@1
|
26 return false;
|
nuclear@1
|
27 }
|
nuclear@1
|
28
|
nuclear@0
|
29 return true;
|
nuclear@0
|
30 }
|
nuclear@0
|
31
|
nuclear@0
|
32 void game_cleanup()
|
nuclear@0
|
33 {
|
nuclear@1
|
34 board.destroy();
|
nuclear@0
|
35 }
|
nuclear@0
|
36
|
nuclear@0
|
37 void game_update(unsigned long time_msec)
|
nuclear@0
|
38 {
|
nuclear@0
|
39 }
|
nuclear@0
|
40
|
nuclear@0
|
41 void game_display()
|
nuclear@0
|
42 {
|
nuclear@0
|
43 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
nuclear@0
|
44
|
nuclear@0
|
45 glMatrixMode(GL_MODELVIEW);
|
nuclear@0
|
46 glLoadIdentity();
|
nuclear@0
|
47 glTranslatef(0, 0, -cam_dist);
|
nuclear@0
|
48 glRotatef(cam_phi, 1, 0, 0);
|
nuclear@0
|
49 glRotatef(cam_theta, 0, 1, 0);
|
nuclear@0
|
50
|
nuclear@0
|
51 draw_backdrop();
|
nuclear@0
|
52
|
nuclear@1
|
53 board.draw();
|
nuclear@1
|
54
|
nuclear@1
|
55 /*
|
nuclear@0
|
56 glBegin(GL_QUADS);
|
nuclear@0
|
57 glNormal3f(0, 1, 0);
|
nuclear@0
|
58 glVertex3f(-1, 0, 1);
|
nuclear@0
|
59 glVertex3f(1, 0, 1);
|
nuclear@0
|
60 glVertex3f(1, 0, -1);
|
nuclear@0
|
61 glVertex3f(-1, 0, -1);
|
nuclear@1
|
62 glEnd();*/
|
nuclear@0
|
63 }
|
nuclear@0
|
64
|
nuclear@0
|
65 static void draw_backdrop()
|
nuclear@0
|
66 {
|
nuclear@0
|
67 glPushAttrib(GL_ENABLE_BIT);
|
nuclear@0
|
68 glDisable(GL_LIGHTING);
|
nuclear@0
|
69 glDisable(GL_DEPTH_TEST);
|
nuclear@0
|
70
|
nuclear@0
|
71 glMatrixMode(GL_PROJECTION);
|
nuclear@0
|
72 glPushMatrix();
|
nuclear@0
|
73 glLoadIdentity();
|
nuclear@0
|
74 glMatrixMode(GL_MODELVIEW);
|
nuclear@0
|
75 glPushMatrix();
|
nuclear@0
|
76 glLoadIdentity();
|
nuclear@0
|
77
|
nuclear@0
|
78 glBegin(GL_QUADS);
|
nuclear@0
|
79 glColor3f(0.9, 0.8, 0.6);
|
nuclear@0
|
80 glVertex2f(-1, -1);
|
nuclear@0
|
81 glVertex2f(1, -1);
|
nuclear@0
|
82 glColor3f(0.4, 0.5, 0.8);
|
nuclear@0
|
83 glVertex2f(1, 1);
|
nuclear@0
|
84 glVertex2f(-1, 1);
|
nuclear@0
|
85 glEnd();
|
nuclear@0
|
86
|
nuclear@0
|
87 glMatrixMode(GL_PROJECTION);
|
nuclear@0
|
88 glPopMatrix();
|
nuclear@0
|
89 glMatrixMode(GL_MODELVIEW);
|
nuclear@0
|
90 glPopMatrix();
|
nuclear@0
|
91
|
nuclear@0
|
92 glPopAttrib();
|
nuclear@0
|
93 }
|
nuclear@0
|
94
|
nuclear@0
|
95 void game_reshape(int x, int y)
|
nuclear@0
|
96 {
|
nuclear@0
|
97 glMatrixMode(GL_PROJECTION);
|
nuclear@0
|
98 glLoadIdentity();
|
nuclear@1
|
99 gluPerspective(50, (float)x / (float)y, 0.2, 200.0);
|
nuclear@0
|
100
|
nuclear@0
|
101 glViewport(0, 0, x, y);
|
nuclear@0
|
102 }
|
nuclear@0
|
103
|
nuclear@0
|
104 void game_keyboard(int bn, bool press)
|
nuclear@0
|
105 {
|
nuclear@0
|
106 if(press) {
|
nuclear@0
|
107 switch(bn) {
|
nuclear@0
|
108 case 27:
|
nuclear@0
|
109 quit();
|
nuclear@0
|
110 }
|
nuclear@0
|
111 }
|
nuclear@0
|
112 }
|
nuclear@0
|
113
|
nuclear@0
|
114 void game_mbutton(int bn, bool press, int x, int y)
|
nuclear@0
|
115 {
|
nuclear@0
|
116 bnstate[bn] = press;
|
nuclear@0
|
117 prev_x = x;
|
nuclear@0
|
118 prev_y = y;
|
nuclear@0
|
119 }
|
nuclear@0
|
120
|
nuclear@0
|
121 void game_mmotion(int x, int y)
|
nuclear@0
|
122 {
|
nuclear@0
|
123 int dx = x - prev_x;
|
nuclear@0
|
124 int dy = y - prev_y;
|
nuclear@0
|
125 prev_x = x;
|
nuclear@0
|
126 prev_y = y;
|
nuclear@0
|
127
|
nuclear@0
|
128 if(bnstate[0]) {
|
nuclear@0
|
129 cam_theta += dx * 0.5;
|
nuclear@0
|
130 cam_phi += dy * 0.5;
|
nuclear@0
|
131
|
nuclear@0
|
132 if(cam_phi < -90) cam_phi = -90;
|
nuclear@0
|
133 if(cam_phi > 90) cam_phi = 90;
|
nuclear@0
|
134
|
nuclear@0
|
135 redisplay();
|
nuclear@0
|
136 }
|
nuclear@0
|
137 if(bnstate[2]) {
|
nuclear@0
|
138 cam_dist += dy * 0.1;
|
nuclear@0
|
139 if(cam_dist < 0.0) cam_dist = 0.0;
|
nuclear@0
|
140
|
nuclear@0
|
141 redisplay();
|
nuclear@0
|
142 }
|
nuclear@0
|
143 }
|