tavli

annotate src/game.cc @ 14:283eb6e9f0a3

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