tavli
view src/game.cc @ 5:e48b40a3c82a
foo
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Thu, 25 Jun 2015 20:43:34 +0300 |
parents | 3fcd7b4d631f |
children | a0d30f6f20d4 |
line source
1 #include <stdio.h>
2 #include <GL/glew.h>
3 #include "game.h"
4 #include "board.h"
6 static void draw_backdrop();
8 int win_width, win_height;
10 static Board board;
12 static float cam_theta, cam_phi = 25, cam_dist = 3;
13 static bool bnstate[8];
14 static int prev_x, prev_y;
17 bool game_init()
18 {
19 glEnable(GL_DEPTH_TEST);
20 glEnable(GL_CULL_FACE);
22 glEnable(GL_LIGHTING);
23 glEnable(GL_LIGHT0);
25 if(GLEW_ARB_multisample) {
26 glEnable(GL_MULTISAMPLE);
27 }
29 if(!board.init()) {
30 return false;
31 }
33 return true;
34 }
36 void game_cleanup()
37 {
38 board.destroy();
39 }
41 void game_update(unsigned long time_msec)
42 {
43 }
45 void game_display()
46 {
47 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
49 glMatrixMode(GL_MODELVIEW);
50 glLoadIdentity();
51 glTranslatef(0, 0, -cam_dist);
52 glRotatef(cam_phi, 1, 0, 0);
53 glRotatef(cam_theta, 0, 1, 0);
55 draw_backdrop();
57 board.draw();
58 }
60 static void draw_backdrop()
61 {
62 glPushAttrib(GL_ENABLE_BIT);
63 glDisable(GL_LIGHTING);
64 glDisable(GL_DEPTH_TEST);
66 glMatrixMode(GL_PROJECTION);
67 glPushMatrix();
68 glLoadIdentity();
69 glMatrixMode(GL_MODELVIEW);
70 glPushMatrix();
71 glLoadIdentity();
73 glBegin(GL_QUADS);
74 glColor3f(0.9, 0.8, 0.6);
75 glVertex2f(-1, -1);
76 glVertex2f(1, -1);
77 glColor3f(0.4, 0.5, 0.8);
78 glVertex2f(1, 1);
79 glVertex2f(-1, 1);
80 glEnd();
82 glMatrixMode(GL_PROJECTION);
83 glPopMatrix();
84 glMatrixMode(GL_MODELVIEW);
85 glPopMatrix();
87 glPopAttrib();
88 }
90 void game_reshape(int x, int y)
91 {
92 glMatrixMode(GL_PROJECTION);
93 glLoadIdentity();
94 gluPerspective(45, (float)x / (float)y, 0.2, 200.0);
96 glViewport(0, 0, x, y);
97 }
99 void game_keyboard(int bn, bool press)
100 {
101 if(press) {
102 switch(bn) {
103 case 27:
104 quit();
105 }
106 }
107 }
109 void game_mbutton(int bn, bool press, int x, int y)
110 {
111 bnstate[bn] = press;
112 prev_x = x;
113 prev_y = y;
114 }
116 void game_mmotion(int x, int y)
117 {
118 int dx = x - prev_x;
119 int dy = y - prev_y;
120 prev_x = x;
121 prev_y = y;
123 if(bnstate[0]) {
124 cam_theta += dx * 0.5;
125 cam_phi += dy * 0.5;
127 if(cam_phi < -90) cam_phi = -90;
128 if(cam_phi > 90) cam_phi = 90;
130 redisplay();
131 }
132 if(bnstate[2]) {
133 cam_dist += dy * 0.1;
134 if(cam_dist < 0.0) cam_dist = 0.0;
136 redisplay();
137 }
138 }