tavli

view src/game.cc @ 3:94aff2ff1934

too much?
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 22 Jun 2015 21:46:57 +0300
parents 52e0dd47753b
children e48b40a3c82a
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 = 6;
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(!board.init()) {
26 return false;
27 }
29 return true;
30 }
32 void game_cleanup()
33 {
34 board.destroy();
35 }
37 void game_update(unsigned long time_msec)
38 {
39 }
41 void game_display()
42 {
43 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
45 glMatrixMode(GL_MODELVIEW);
46 glLoadIdentity();
47 glTranslatef(0, 0, -cam_dist);
48 glRotatef(cam_phi, 1, 0, 0);
49 glRotatef(cam_theta, 0, 1, 0);
51 draw_backdrop();
53 board.draw();
55 /*
56 glBegin(GL_QUADS);
57 glNormal3f(0, 1, 0);
58 glVertex3f(-1, 0, 1);
59 glVertex3f(1, 0, 1);
60 glVertex3f(1, 0, -1);
61 glVertex3f(-1, 0, -1);
62 glEnd();*/
63 }
65 static void draw_backdrop()
66 {
67 glPushAttrib(GL_ENABLE_BIT);
68 glDisable(GL_LIGHTING);
69 glDisable(GL_DEPTH_TEST);
71 glMatrixMode(GL_PROJECTION);
72 glPushMatrix();
73 glLoadIdentity();
74 glMatrixMode(GL_MODELVIEW);
75 glPushMatrix();
76 glLoadIdentity();
78 glBegin(GL_QUADS);
79 glColor3f(0.9, 0.8, 0.6);
80 glVertex2f(-1, -1);
81 glVertex2f(1, -1);
82 glColor3f(0.4, 0.5, 0.8);
83 glVertex2f(1, 1);
84 glVertex2f(-1, 1);
85 glEnd();
87 glMatrixMode(GL_PROJECTION);
88 glPopMatrix();
89 glMatrixMode(GL_MODELVIEW);
90 glPopMatrix();
92 glPopAttrib();
93 }
95 void game_reshape(int x, int y)
96 {
97 glMatrixMode(GL_PROJECTION);
98 glLoadIdentity();
99 gluPerspective(50, (float)x / (float)y, 0.2, 200.0);
101 glViewport(0, 0, x, y);
102 }
104 void game_keyboard(int bn, bool press)
105 {
106 if(press) {
107 switch(bn) {
108 case 27:
109 quit();
110 }
111 }
112 }
114 void game_mbutton(int bn, bool press, int x, int y)
115 {
116 bnstate[bn] = press;
117 prev_x = x;
118 prev_y = y;
119 }
121 void game_mmotion(int x, int y)
122 {
123 int dx = x - prev_x;
124 int dy = y - prev_y;
125 prev_x = x;
126 prev_y = y;
128 if(bnstate[0]) {
129 cam_theta += dx * 0.5;
130 cam_phi += dy * 0.5;
132 if(cam_phi < -90) cam_phi = -90;
133 if(cam_phi > 90) cam_phi = 90;
135 redisplay();
136 }
137 if(bnstate[2]) {
138 cam_dist += dy * 0.1;
139 if(cam_dist < 0.0) cam_dist = 0.0;
141 redisplay();
142 }
143 }