tavli

view src/game.cc @ 12:ae1c60726c41

better pieces
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 27 Jun 2015 13:53:43 +0300
parents 464c45a0bc24
children 283eb6e9f0a3
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 = 45, 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);
21 glEnable(GL_NORMALIZE);
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 float ldir[] = {-1, 2, 1, 0};
56 glLightfv(GL_LIGHT0, GL_POSITION, ldir);
58 draw_backdrop();
59 board.draw();
60 }
62 static void draw_backdrop()
63 {
64 glPushAttrib(GL_ENABLE_BIT);
65 glDisable(GL_LIGHTING);
66 glDisable(GL_DEPTH_TEST);
68 glMatrixMode(GL_PROJECTION);
69 glPushMatrix();
70 glLoadIdentity();
71 glMatrixMode(GL_MODELVIEW);
72 glPushMatrix();
73 glLoadIdentity();
75 glBegin(GL_QUADS);
76 glColor3f(0.9, 0.8, 0.6);
77 glVertex2f(-1, -1);
78 glVertex2f(1, -1);
79 glColor3f(0.4, 0.5, 0.8);
80 glVertex2f(1, 1);
81 glVertex2f(-1, 1);
82 glEnd();
84 glMatrixMode(GL_PROJECTION);
85 glPopMatrix();
86 glMatrixMode(GL_MODELVIEW);
87 glPopMatrix();
89 glPopAttrib();
90 }
92 void game_reshape(int x, int y)
93 {
94 glMatrixMode(GL_PROJECTION);
95 glLoadIdentity();
96 gluPerspective(45, (float)x / (float)y, 0.2, 200.0);
98 glViewport(0, 0, x, y);
99 }
101 void game_keyboard(int bn, bool press)
102 {
103 if(press) {
104 switch(bn) {
105 case 27:
106 quit();
107 }
108 }
109 }
111 void game_mbutton(int bn, bool press, int x, int y)
112 {
113 bnstate[bn] = press;
114 prev_x = x;
115 prev_y = y;
116 }
118 void game_mmotion(int x, int y)
119 {
120 int dx = x - prev_x;
121 int dy = y - prev_y;
122 prev_x = x;
123 prev_y = y;
125 if(bnstate[0]) {
126 cam_theta += dx * 0.5;
127 cam_phi += dy * 0.5;
129 if(cam_phi < -90) cam_phi = -90;
130 if(cam_phi > 90) cam_phi = 90;
132 redisplay();
133 }
134 if(bnstate[2]) {
135 cam_dist += dy * 0.1;
136 if(cam_dist < 0.0) cam_dist = 0.0;
138 redisplay();
139 }
140 }