tavli

annotate src/game.cc @ 7:f1ecc2439802

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