tavli
view src/game.cc @ 17:16a420432aa3
pieces on the board
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 28 Jun 2015 23:04:37 +0300 |
parents | d6209903454b |
children | 986c0b76513f |
line source
1 #include <stdio.h>
2 #include "opengl.h"
3 #include "game.h"
4 #include "board.h"
5 #include "scenery.h"
6 #include "sdr.h"
8 static void draw_backdrop();
10 int win_width, win_height;
11 unsigned long cur_time;
12 unsigned int sdr_phong, sdr_phong_notex;
13 bool wireframe;
15 static Board board;
17 static float cam_theta, cam_phi = 45, cam_dist = 3;
18 static bool bnstate[8];
19 static int prev_x, prev_y;
21 static bool dbg_busyloop;
23 bool game_init()
24 {
25 if(init_opengl() == -1) {
26 return false;
27 }
29 glEnable(GL_DEPTH_TEST);
30 glEnable(GL_CULL_FACE);
31 glEnable(GL_NORMALIZE);
32 glEnable(GL_LIGHTING);
33 glEnable(GL_LIGHT0);
35 if(glcaps.sep_spec) {
36 glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
37 }
39 if(glcaps.fsaa) {
40 glEnable(GL_MULTISAMPLE);
41 }
43 if(glcaps.shaders) {
44 Mesh::use_custom_sdr_attr = false;
45 if(!(sdr_phong = create_program_load("sdr/phong.v.glsl", "sdr/phong.p.glsl"))) {
46 return false;
47 }
48 if(!(sdr_phong_notex = create_program_load("sdr/phong.v.glsl", "sdr/phong-notex.p.glsl"))) {
49 return false;
50 }
51 }
53 if(!board.init()) {
54 return false;
55 }
56 board.setup();
58 if(!init_scenery()) {
59 return false;
60 }
62 return true;
63 }
65 void game_cleanup()
66 {
67 board.destroy();
68 destroy_scenery();
69 }
71 void game_update(unsigned long time_msec)
72 {
73 cur_time = time_msec;
74 }
76 void game_display()
77 {
78 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
80 glMatrixMode(GL_MODELVIEW);
81 glLoadIdentity();
82 glTranslatef(0, 0, -cam_dist);
83 glRotatef(cam_phi, 1, 0, 0);
84 glRotatef(cam_theta, 0, 1, 0);
86 float ldir[] = {-10, 20, 10, 1};
87 glLightfv(GL_LIGHT0, GL_POSITION, ldir);
89 draw_backdrop();
90 draw_scenery();
91 board.draw();
93 if(dbg_busyloop) {
94 redisplay();
95 }
96 }
98 static void draw_backdrop()
99 {
100 glPushAttrib(GL_ENABLE_BIT);
101 glDisable(GL_LIGHTING);
102 glDisable(GL_DEPTH_TEST);
104 glMatrixMode(GL_PROJECTION);
105 glPushMatrix();
106 glLoadIdentity();
107 glMatrixMode(GL_MODELVIEW);
108 glPushMatrix();
109 glLoadIdentity();
111 glBegin(GL_QUADS);
112 glColor3f(0.9, 0.8, 0.6);
113 glVertex2f(-1, -1);
114 glVertex2f(1, -1);
115 glColor3f(0.4, 0.5, 0.8);
116 glVertex2f(1, 1);
117 glVertex2f(-1, 1);
118 glEnd();
120 glMatrixMode(GL_PROJECTION);
121 glPopMatrix();
122 glMatrixMode(GL_MODELVIEW);
123 glPopMatrix();
125 glPopAttrib();
126 }
128 void game_reshape(int x, int y)
129 {
130 glMatrixMode(GL_PROJECTION);
131 glLoadIdentity();
132 gluPerspective(45, (float)x / (float)y, 0.2, 200.0);
134 glViewport(0, 0, x, y);
135 }
137 void game_keyboard(int bn, bool press)
138 {
139 if(press) {
140 switch(bn) {
141 case 27:
142 quit();
144 case 'w':
145 wireframe = !wireframe;
146 redisplay();
147 break;
149 case 'd':
150 dbg_busyloop = !dbg_busyloop;
151 redisplay();
152 break;
153 }
154 }
155 }
157 void game_mbutton(int bn, bool press, int x, int y)
158 {
159 bnstate[bn] = press;
160 prev_x = x;
161 prev_y = y;
162 }
164 void game_mmotion(int x, int y)
165 {
166 int dx = x - prev_x;
167 int dy = y - prev_y;
168 prev_x = x;
169 prev_y = y;
171 if(bnstate[0]) {
172 cam_theta += dx * 0.5;
173 cam_phi += dy * 0.5;
175 if(cam_phi < -90) cam_phi = -90;
176 if(cam_phi > 90) cam_phi = 90;
178 redisplay();
179 }
180 if(bnstate[2]) {
181 cam_dist += dy * 0.1;
182 if(cam_dist < 0.0) cam_dist = 0.0;
184 redisplay();
185 }
186 }