tavli

view src/game.cc @ 15:b1a195c3ee16

added shaders
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 28 Jun 2015 08:34:24 +0300
parents 283eb6e9f0a3
children d6209903454b
line source
1 #include <stdio.h>
2 #include <GL/glew.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 glEnable(GL_DEPTH_TEST);
25 glEnable(GL_CULL_FACE);
26 glEnable(GL_NORMALIZE);
27 glEnable(GL_LIGHTING);
28 glEnable(GL_LIGHT0);
30 if(GLEW_ARB_multisample) {
31 glEnable(GL_MULTISAMPLE);
32 }
34 Mesh::use_custom_sdr_attr = false;
36 if(!(sdr_phong = create_program_load("sdr/phong.v.glsl", "sdr/phong.p.glsl"))) {
37 return false;
38 }
39 if(!(sdr_phong_notex = create_program_load("sdr/phong.v.glsl", "sdr/phong-notex.p.glsl"))) {
40 return false;
41 }
43 if(!board.init()) {
44 return false;
45 }
47 if(!init_scenery()) {
48 return false;
49 }
51 return true;
52 }
54 void game_cleanup()
55 {
56 board.destroy();
57 destroy_scenery();
58 }
60 void game_update(unsigned long time_msec)
61 {
62 }
64 void game_display()
65 {
66 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
68 glMatrixMode(GL_MODELVIEW);
69 glLoadIdentity();
70 glTranslatef(0, 0, -cam_dist);
71 glRotatef(cam_phi, 1, 0, 0);
72 glRotatef(cam_theta, 0, 1, 0);
74 float ldir[] = {-10, 20, 10, 1};
75 glLightfv(GL_LIGHT0, GL_POSITION, ldir);
77 draw_backdrop();
78 draw_scenery();
79 board.draw();
81 if(dbg_busyloop) {
82 redisplay();
83 }
84 }
86 static void draw_backdrop()
87 {
88 glPushAttrib(GL_ENABLE_BIT);
89 glDisable(GL_LIGHTING);
90 glDisable(GL_DEPTH_TEST);
92 glMatrixMode(GL_PROJECTION);
93 glPushMatrix();
94 glLoadIdentity();
95 glMatrixMode(GL_MODELVIEW);
96 glPushMatrix();
97 glLoadIdentity();
99 glBegin(GL_QUADS);
100 glColor3f(0.9, 0.8, 0.6);
101 glVertex2f(-1, -1);
102 glVertex2f(1, -1);
103 glColor3f(0.4, 0.5, 0.8);
104 glVertex2f(1, 1);
105 glVertex2f(-1, 1);
106 glEnd();
108 glMatrixMode(GL_PROJECTION);
109 glPopMatrix();
110 glMatrixMode(GL_MODELVIEW);
111 glPopMatrix();
113 glPopAttrib();
114 }
116 void game_reshape(int x, int y)
117 {
118 glMatrixMode(GL_PROJECTION);
119 glLoadIdentity();
120 gluPerspective(45, (float)x / (float)y, 0.2, 200.0);
122 glViewport(0, 0, x, y);
123 }
125 void game_keyboard(int bn, bool press)
126 {
127 if(press) {
128 switch(bn) {
129 case 27:
130 quit();
132 case 'w':
133 wireframe = !wireframe;
134 redisplay();
135 break;
137 case 'd':
138 dbg_busyloop = !dbg_busyloop;
139 redisplay();
140 break;
141 }
142 }
143 }
145 void game_mbutton(int bn, bool press, int x, int y)
146 {
147 bnstate[bn] = press;
148 prev_x = x;
149 prev_y = y;
150 }
152 void game_mmotion(int x, int y)
153 {
154 int dx = x - prev_x;
155 int dy = y - prev_y;
156 prev_x = x;
157 prev_y = y;
159 if(bnstate[0]) {
160 cam_theta += dx * 0.5;
161 cam_phi += dy * 0.5;
163 if(cam_phi < -90) cam_phi = -90;
164 if(cam_phi > 90) cam_phi = 90;
166 redisplay();
167 }
168 if(bnstate[2]) {
169 cam_dist += dy * 0.1;
170 if(cam_dist < 0.0) cam_dist = 0.0;
172 redisplay();
173 }
174 }