tavli

view src/game.cc @ 16:d6209903454b

opengl capabilities
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 28 Jun 2015 08:48:25 +0300
parents b1a195c3ee16
children 16a420432aa3
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 int sdr_phong, sdr_phong_notex;
12 bool wireframe;
14 static Board board;
16 static float cam_theta, cam_phi = 45, cam_dist = 3;
17 static bool bnstate[8];
18 static int prev_x, prev_y;
20 static bool dbg_busyloop;
22 bool game_init()
23 {
24 if(init_opengl() == -1) {
25 return false;
26 }
28 glEnable(GL_DEPTH_TEST);
29 glEnable(GL_CULL_FACE);
30 glEnable(GL_NORMALIZE);
31 glEnable(GL_LIGHTING);
32 glEnable(GL_LIGHT0);
34 if(glcaps.sep_spec) {
35 glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
36 }
38 if(glcaps.fsaa) {
39 glEnable(GL_MULTISAMPLE);
40 }
42 if(glcaps.shaders) {
43 Mesh::use_custom_sdr_attr = false;
44 if(!(sdr_phong = create_program_load("sdr/phong.v.glsl", "sdr/phong.p.glsl"))) {
45 return false;
46 }
47 if(!(sdr_phong_notex = create_program_load("sdr/phong.v.glsl", "sdr/phong-notex.p.glsl"))) {
48 return false;
49 }
50 }
52 if(!board.init()) {
53 return false;
54 }
56 if(!init_scenery()) {
57 return false;
58 }
60 return true;
61 }
63 void game_cleanup()
64 {
65 board.destroy();
66 destroy_scenery();
67 }
69 void game_update(unsigned long time_msec)
70 {
71 }
73 void game_display()
74 {
75 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
77 glMatrixMode(GL_MODELVIEW);
78 glLoadIdentity();
79 glTranslatef(0, 0, -cam_dist);
80 glRotatef(cam_phi, 1, 0, 0);
81 glRotatef(cam_theta, 0, 1, 0);
83 float ldir[] = {-10, 20, 10, 1};
84 glLightfv(GL_LIGHT0, GL_POSITION, ldir);
86 draw_backdrop();
87 draw_scenery();
88 board.draw();
90 if(dbg_busyloop) {
91 redisplay();
92 }
93 }
95 static void draw_backdrop()
96 {
97 glPushAttrib(GL_ENABLE_BIT);
98 glDisable(GL_LIGHTING);
99 glDisable(GL_DEPTH_TEST);
101 glMatrixMode(GL_PROJECTION);
102 glPushMatrix();
103 glLoadIdentity();
104 glMatrixMode(GL_MODELVIEW);
105 glPushMatrix();
106 glLoadIdentity();
108 glBegin(GL_QUADS);
109 glColor3f(0.9, 0.8, 0.6);
110 glVertex2f(-1, -1);
111 glVertex2f(1, -1);
112 glColor3f(0.4, 0.5, 0.8);
113 glVertex2f(1, 1);
114 glVertex2f(-1, 1);
115 glEnd();
117 glMatrixMode(GL_PROJECTION);
118 glPopMatrix();
119 glMatrixMode(GL_MODELVIEW);
120 glPopMatrix();
122 glPopAttrib();
123 }
125 void game_reshape(int x, int y)
126 {
127 glMatrixMode(GL_PROJECTION);
128 glLoadIdentity();
129 gluPerspective(45, (float)x / (float)y, 0.2, 200.0);
131 glViewport(0, 0, x, y);
132 }
134 void game_keyboard(int bn, bool press)
135 {
136 if(press) {
137 switch(bn) {
138 case 27:
139 quit();
141 case 'w':
142 wireframe = !wireframe;
143 redisplay();
144 break;
146 case 'd':
147 dbg_busyloop = !dbg_busyloop;
148 redisplay();
149 break;
150 }
151 }
152 }
154 void game_mbutton(int bn, bool press, int x, int y)
155 {
156 bnstate[bn] = press;
157 prev_x = x;
158 prev_y = y;
159 }
161 void game_mmotion(int x, int y)
162 {
163 int dx = x - prev_x;
164 int dy = y - prev_y;
165 prev_x = x;
166 prev_y = y;
168 if(bnstate[0]) {
169 cam_theta += dx * 0.5;
170 cam_phi += dy * 0.5;
172 if(cam_phi < -90) cam_phi = -90;
173 if(cam_phi > 90) cam_phi = 90;
175 redisplay();
176 }
177 if(bnstate[2]) {
178 cam_dist += dy * 0.1;
179 if(cam_dist < 0.0) cam_dist = 0.0;
181 redisplay();
182 }
183 }