tavli

view src/game.cc @ 14:283eb6e9f0a3

scenery
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 28 Jun 2015 07:44:23 +0300
parents a8e26f163f99
children b1a195c3ee16
line source
1 #include <stdio.h>
2 #include <GL/glew.h>
3 #include "game.h"
4 #include "board.h"
5 #include "scenery.h"
7 static void draw_backdrop();
9 int win_width, win_height;
10 bool wireframe;
12 static Board board;
14 static float cam_theta, cam_phi = 45, cam_dist = 3;
15 static bool bnstate[8];
16 static int prev_x, prev_y;
19 bool game_init()
20 {
21 glEnable(GL_DEPTH_TEST);
22 glEnable(GL_CULL_FACE);
23 glEnable(GL_NORMALIZE);
24 glEnable(GL_LIGHTING);
25 glEnable(GL_LIGHT0);
27 if(GLEW_ARB_multisample) {
28 glEnable(GL_MULTISAMPLE);
29 }
31 if(!board.init()) {
32 return false;
33 }
35 if(!init_scenery()) {
36 return false;
37 }
39 return true;
40 }
42 void game_cleanup()
43 {
44 board.destroy();
45 destroy_scenery();
46 }
48 void game_update(unsigned long time_msec)
49 {
50 }
52 void game_display()
53 {
54 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
56 glMatrixMode(GL_MODELVIEW);
57 glLoadIdentity();
58 glTranslatef(0, 0, -cam_dist);
59 glRotatef(cam_phi, 1, 0, 0);
60 glRotatef(cam_theta, 0, 1, 0);
62 float ldir[] = {-1, 2, 1, 0};
63 glLightfv(GL_LIGHT0, GL_POSITION, ldir);
65 draw_backdrop();
66 draw_scenery();
67 board.draw();
68 }
70 static void draw_backdrop()
71 {
72 glPushAttrib(GL_ENABLE_BIT);
73 glDisable(GL_LIGHTING);
74 glDisable(GL_DEPTH_TEST);
76 glMatrixMode(GL_PROJECTION);
77 glPushMatrix();
78 glLoadIdentity();
79 glMatrixMode(GL_MODELVIEW);
80 glPushMatrix();
81 glLoadIdentity();
83 glBegin(GL_QUADS);
84 glColor3f(0.9, 0.8, 0.6);
85 glVertex2f(-1, -1);
86 glVertex2f(1, -1);
87 glColor3f(0.4, 0.5, 0.8);
88 glVertex2f(1, 1);
89 glVertex2f(-1, 1);
90 glEnd();
92 glMatrixMode(GL_PROJECTION);
93 glPopMatrix();
94 glMatrixMode(GL_MODELVIEW);
95 glPopMatrix();
97 glPopAttrib();
98 }
100 void game_reshape(int x, int y)
101 {
102 glMatrixMode(GL_PROJECTION);
103 glLoadIdentity();
104 gluPerspective(45, (float)x / (float)y, 0.2, 200.0);
106 glViewport(0, 0, x, y);
107 }
109 void game_keyboard(int bn, bool press)
110 {
111 if(press) {
112 switch(bn) {
113 case 27:
114 quit();
116 case 'w':
117 wireframe = !wireframe;
118 redisplay();
119 break;
120 }
121 }
122 }
124 void game_mbutton(int bn, bool press, int x, int y)
125 {
126 bnstate[bn] = press;
127 prev_x = x;
128 prev_y = y;
129 }
131 void game_mmotion(int x, int y)
132 {
133 int dx = x - prev_x;
134 int dy = y - prev_y;
135 prev_x = x;
136 prev_y = y;
138 if(bnstate[0]) {
139 cam_theta += dx * 0.5;
140 cam_phi += dy * 0.5;
142 if(cam_phi < -90) cam_phi = -90;
143 if(cam_phi > 90) cam_phi = 90;
145 redisplay();
146 }
147 if(bnstate[2]) {
148 cam_dist += dy * 0.1;
149 if(cam_dist < 0.0) cam_dist = 0.0;
151 redisplay();
152 }
153 }