tavli

view src/game.cc @ 0:52e0dd47753b

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 21 Jun 2015 06:30:39 +0300
parents
children 3fcd7b4d631f
line source
1 #include <stdio.h>
2 #include <GL/glew.h>
3 #include "game.h"
5 static void draw_backdrop();
7 int win_width, win_height;
9 static float cam_theta, cam_phi = 25, cam_dist = 6;
10 static bool bnstate[8];
11 static int prev_x, prev_y;
14 bool game_init()
15 {
16 glEnable(GL_DEPTH_TEST);
17 glEnable(GL_CULL_FACE);
19 glEnable(GL_LIGHTING);
20 glEnable(GL_LIGHT0);
22 return true;
23 }
25 void game_cleanup()
26 {
27 }
29 void game_update(unsigned long time_msec)
30 {
31 }
33 void game_display()
34 {
35 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
37 glMatrixMode(GL_MODELVIEW);
38 glLoadIdentity();
39 glTranslatef(0, 0, -cam_dist);
40 glRotatef(cam_phi, 1, 0, 0);
41 glRotatef(cam_theta, 0, 1, 0);
43 draw_backdrop();
45 glBegin(GL_QUADS);
46 glNormal3f(0, 1, 0);
47 glVertex3f(-1, 0, 1);
48 glVertex3f(1, 0, 1);
49 glVertex3f(1, 0, -1);
50 glVertex3f(-1, 0, -1);
51 glEnd();
52 }
54 static void draw_backdrop()
55 {
56 glPushAttrib(GL_ENABLE_BIT);
57 glDisable(GL_LIGHTING);
58 glDisable(GL_DEPTH_TEST);
60 glMatrixMode(GL_PROJECTION);
61 glPushMatrix();
62 glLoadIdentity();
63 glMatrixMode(GL_MODELVIEW);
64 glPushMatrix();
65 glLoadIdentity();
67 glBegin(GL_QUADS);
68 glColor3f(0.9, 0.8, 0.6);
69 glVertex2f(-1, -1);
70 glVertex2f(1, -1);
71 glColor3f(0.4, 0.5, 0.8);
72 glVertex2f(1, 1);
73 glVertex2f(-1, 1);
74 glEnd();
76 glMatrixMode(GL_PROJECTION);
77 glPopMatrix();
78 glMatrixMode(GL_MODELVIEW);
79 glPopMatrix();
81 glPopAttrib();
82 }
84 void game_reshape(int x, int y)
85 {
86 glMatrixMode(GL_PROJECTION);
87 glLoadIdentity();
88 gluPerspective(50, (float)x / (float)y, 0.5, 500.0);
90 glViewport(0, 0, x, y);
91 }
93 void game_keyboard(int bn, bool press)
94 {
95 if(press) {
96 switch(bn) {
97 case 27:
98 quit();
99 }
100 }
101 }
103 void game_mbutton(int bn, bool press, int x, int y)
104 {
105 bnstate[bn] = press;
106 prev_x = x;
107 prev_y = y;
108 }
110 void game_mmotion(int x, int y)
111 {
112 int dx = x - prev_x;
113 int dy = y - prev_y;
114 prev_x = x;
115 prev_y = y;
117 if(bnstate[0]) {
118 cam_theta += dx * 0.5;
119 cam_phi += dy * 0.5;
121 if(cam_phi < -90) cam_phi = -90;
122 if(cam_phi > 90) cam_phi = 90;
124 redisplay();
125 }
126 if(bnstate[2]) {
127 cam_dist += dy * 0.1;
128 if(cam_dist < 0.0) cam_dist = 0.0;
130 redisplay();
131 }
132 }