tavli

annotate src/game.cc @ 5:e48b40a3c82a

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 25 Jun 2015 20:43:34 +0300
parents 3fcd7b4d631f
children a0d30f6f20d4
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@0 55 draw_backdrop();
nuclear@0 56
nuclear@1 57 board.draw();
nuclear@0 58 }
nuclear@0 59
nuclear@0 60 static void draw_backdrop()
nuclear@0 61 {
nuclear@0 62 glPushAttrib(GL_ENABLE_BIT);
nuclear@0 63 glDisable(GL_LIGHTING);
nuclear@0 64 glDisable(GL_DEPTH_TEST);
nuclear@0 65
nuclear@0 66 glMatrixMode(GL_PROJECTION);
nuclear@0 67 glPushMatrix();
nuclear@0 68 glLoadIdentity();
nuclear@0 69 glMatrixMode(GL_MODELVIEW);
nuclear@0 70 glPushMatrix();
nuclear@0 71 glLoadIdentity();
nuclear@0 72
nuclear@0 73 glBegin(GL_QUADS);
nuclear@0 74 glColor3f(0.9, 0.8, 0.6);
nuclear@0 75 glVertex2f(-1, -1);
nuclear@0 76 glVertex2f(1, -1);
nuclear@0 77 glColor3f(0.4, 0.5, 0.8);
nuclear@0 78 glVertex2f(1, 1);
nuclear@0 79 glVertex2f(-1, 1);
nuclear@0 80 glEnd();
nuclear@0 81
nuclear@0 82 glMatrixMode(GL_PROJECTION);
nuclear@0 83 glPopMatrix();
nuclear@0 84 glMatrixMode(GL_MODELVIEW);
nuclear@0 85 glPopMatrix();
nuclear@0 86
nuclear@0 87 glPopAttrib();
nuclear@0 88 }
nuclear@0 89
nuclear@0 90 void game_reshape(int x, int y)
nuclear@0 91 {
nuclear@0 92 glMatrixMode(GL_PROJECTION);
nuclear@0 93 glLoadIdentity();
nuclear@5 94 gluPerspective(45, (float)x / (float)y, 0.2, 200.0);
nuclear@0 95
nuclear@0 96 glViewport(0, 0, x, y);
nuclear@0 97 }
nuclear@0 98
nuclear@0 99 void game_keyboard(int bn, bool press)
nuclear@0 100 {
nuclear@0 101 if(press) {
nuclear@0 102 switch(bn) {
nuclear@0 103 case 27:
nuclear@0 104 quit();
nuclear@0 105 }
nuclear@0 106 }
nuclear@0 107 }
nuclear@0 108
nuclear@0 109 void game_mbutton(int bn, bool press, int x, int y)
nuclear@0 110 {
nuclear@0 111 bnstate[bn] = press;
nuclear@0 112 prev_x = x;
nuclear@0 113 prev_y = y;
nuclear@0 114 }
nuclear@0 115
nuclear@0 116 void game_mmotion(int x, int y)
nuclear@0 117 {
nuclear@0 118 int dx = x - prev_x;
nuclear@0 119 int dy = y - prev_y;
nuclear@0 120 prev_x = x;
nuclear@0 121 prev_y = y;
nuclear@0 122
nuclear@0 123 if(bnstate[0]) {
nuclear@0 124 cam_theta += dx * 0.5;
nuclear@0 125 cam_phi += dy * 0.5;
nuclear@0 126
nuclear@0 127 if(cam_phi < -90) cam_phi = -90;
nuclear@0 128 if(cam_phi > 90) cam_phi = 90;
nuclear@0 129
nuclear@0 130 redisplay();
nuclear@0 131 }
nuclear@0 132 if(bnstate[2]) {
nuclear@0 133 cam_dist += dy * 0.1;
nuclear@0 134 if(cam_dist < 0.0) cam_dist = 0.0;
nuclear@0 135
nuclear@0 136 redisplay();
nuclear@0 137 }
nuclear@0 138 }