tavli

diff 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 diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/game.cc	Sun Jun 21 06:30:39 2015 +0300
     1.3 @@ -0,0 +1,132 @@
     1.4 +#include <stdio.h>
     1.5 +#include <GL/glew.h>
     1.6 +#include "game.h"
     1.7 +
     1.8 +static void draw_backdrop();
     1.9 +
    1.10 +int win_width, win_height;
    1.11 +
    1.12 +static float cam_theta, cam_phi = 25, cam_dist = 6;
    1.13 +static bool bnstate[8];
    1.14 +static int prev_x, prev_y;
    1.15 +
    1.16 +
    1.17 +bool game_init()
    1.18 +{
    1.19 +	glEnable(GL_DEPTH_TEST);
    1.20 +	glEnable(GL_CULL_FACE);
    1.21 +
    1.22 +	glEnable(GL_LIGHTING);
    1.23 +	glEnable(GL_LIGHT0);
    1.24 +
    1.25 +	return true;
    1.26 +}
    1.27 +
    1.28 +void game_cleanup()
    1.29 +{
    1.30 +}
    1.31 +
    1.32 +void game_update(unsigned long time_msec)
    1.33 +{
    1.34 +}
    1.35 +
    1.36 +void game_display()
    1.37 +{
    1.38 +	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    1.39 +
    1.40 +	glMatrixMode(GL_MODELVIEW);
    1.41 +	glLoadIdentity();
    1.42 +	glTranslatef(0, 0, -cam_dist);
    1.43 +	glRotatef(cam_phi, 1, 0, 0);
    1.44 +	glRotatef(cam_theta, 0, 1, 0);
    1.45 +
    1.46 +	draw_backdrop();
    1.47 +
    1.48 +	glBegin(GL_QUADS);
    1.49 +	glNormal3f(0, 1, 0);
    1.50 +	glVertex3f(-1, 0, 1);
    1.51 +	glVertex3f(1, 0, 1);
    1.52 +	glVertex3f(1, 0, -1);
    1.53 +	glVertex3f(-1, 0, -1);
    1.54 +	glEnd();
    1.55 +}
    1.56 +
    1.57 +static void draw_backdrop()
    1.58 +{
    1.59 +	glPushAttrib(GL_ENABLE_BIT);
    1.60 +	glDisable(GL_LIGHTING);
    1.61 +	glDisable(GL_DEPTH_TEST);
    1.62 +
    1.63 +	glMatrixMode(GL_PROJECTION);
    1.64 +	glPushMatrix();
    1.65 +	glLoadIdentity();
    1.66 +	glMatrixMode(GL_MODELVIEW);
    1.67 +	glPushMatrix();
    1.68 +	glLoadIdentity();
    1.69 +
    1.70 +	glBegin(GL_QUADS);
    1.71 +	glColor3f(0.9, 0.8, 0.6);
    1.72 +	glVertex2f(-1, -1);
    1.73 +	glVertex2f(1, -1);
    1.74 +	glColor3f(0.4, 0.5, 0.8);
    1.75 +	glVertex2f(1, 1);
    1.76 +	glVertex2f(-1, 1);
    1.77 +	glEnd();
    1.78 +
    1.79 +	glMatrixMode(GL_PROJECTION);
    1.80 +	glPopMatrix();
    1.81 +	glMatrixMode(GL_MODELVIEW);
    1.82 +	glPopMatrix();
    1.83 +
    1.84 +	glPopAttrib();
    1.85 +}
    1.86 +
    1.87 +void game_reshape(int x, int y)
    1.88 +{
    1.89 +	glMatrixMode(GL_PROJECTION);
    1.90 +	glLoadIdentity();
    1.91 +	gluPerspective(50, (float)x / (float)y, 0.5, 500.0);
    1.92 +
    1.93 +	glViewport(0, 0, x, y);
    1.94 +}
    1.95 +
    1.96 +void game_keyboard(int bn, bool press)
    1.97 +{
    1.98 +	if(press) {
    1.99 +		switch(bn) {
   1.100 +		case 27:
   1.101 +			quit();
   1.102 +		}
   1.103 +	}
   1.104 +}
   1.105 +
   1.106 +void game_mbutton(int bn, bool press, int x, int y)
   1.107 +{
   1.108 +	bnstate[bn] = press;
   1.109 +	prev_x = x;
   1.110 +	prev_y = y;
   1.111 +}
   1.112 +
   1.113 +void game_mmotion(int x, int y)
   1.114 +{
   1.115 +	int dx = x - prev_x;
   1.116 +	int dy = y - prev_y;
   1.117 +	prev_x = x;
   1.118 +	prev_y = y;
   1.119 +
   1.120 +	if(bnstate[0]) {
   1.121 +		cam_theta += dx * 0.5;
   1.122 +		cam_phi += dy * 0.5;
   1.123 +
   1.124 +		if(cam_phi < -90) cam_phi = -90;
   1.125 +		if(cam_phi > 90) cam_phi = 90;
   1.126 +
   1.127 +		redisplay();
   1.128 +	}
   1.129 +	if(bnstate[2]) {
   1.130 +		cam_dist += dy * 0.1;
   1.131 +		if(cam_dist < 0.0) cam_dist = 0.0;
   1.132 +
   1.133 +		redisplay();
   1.134 +	}
   1.135 +}