tavli

annotate src/game.cc @ 17:16a420432aa3

pieces on the board
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 28 Jun 2015 23:04:37 +0300
parents d6209903454b
children 986c0b76513f
rev   line source
nuclear@0 1 #include <stdio.h>
nuclear@16 2 #include "opengl.h"
nuclear@0 3 #include "game.h"
nuclear@1 4 #include "board.h"
nuclear@14 5 #include "scenery.h"
nuclear@15 6 #include "sdr.h"
nuclear@0 7
nuclear@0 8 static void draw_backdrop();
nuclear@0 9
nuclear@0 10 int win_width, win_height;
nuclear@17 11 unsigned long cur_time;
nuclear@15 12 unsigned int sdr_phong, sdr_phong_notex;
nuclear@14 13 bool wireframe;
nuclear@0 14
nuclear@1 15 static Board board;
nuclear@1 16
nuclear@8 17 static float cam_theta, cam_phi = 45, cam_dist = 3;
nuclear@0 18 static bool bnstate[8];
nuclear@0 19 static int prev_x, prev_y;
nuclear@0 20
nuclear@15 21 static bool dbg_busyloop;
nuclear@0 22
nuclear@0 23 bool game_init()
nuclear@0 24 {
nuclear@16 25 if(init_opengl() == -1) {
nuclear@16 26 return false;
nuclear@16 27 }
nuclear@16 28
nuclear@0 29 glEnable(GL_DEPTH_TEST);
nuclear@0 30 glEnable(GL_CULL_FACE);
nuclear@11 31 glEnable(GL_NORMALIZE);
nuclear@0 32 glEnable(GL_LIGHTING);
nuclear@0 33 glEnable(GL_LIGHT0);
nuclear@0 34
nuclear@16 35 if(glcaps.sep_spec) {
nuclear@16 36 glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
nuclear@16 37 }
nuclear@16 38
nuclear@16 39 if(glcaps.fsaa) {
nuclear@5 40 glEnable(GL_MULTISAMPLE);
nuclear@5 41 }
nuclear@5 42
nuclear@16 43 if(glcaps.shaders) {
nuclear@16 44 Mesh::use_custom_sdr_attr = false;
nuclear@16 45 if(!(sdr_phong = create_program_load("sdr/phong.v.glsl", "sdr/phong.p.glsl"))) {
nuclear@16 46 return false;
nuclear@16 47 }
nuclear@16 48 if(!(sdr_phong_notex = create_program_load("sdr/phong.v.glsl", "sdr/phong-notex.p.glsl"))) {
nuclear@16 49 return false;
nuclear@16 50 }
nuclear@15 51 }
nuclear@15 52
nuclear@1 53 if(!board.init()) {
nuclear@1 54 return false;
nuclear@1 55 }
nuclear@17 56 board.setup();
nuclear@1 57
nuclear@14 58 if(!init_scenery()) {
nuclear@14 59 return false;
nuclear@14 60 }
nuclear@14 61
nuclear@0 62 return true;
nuclear@0 63 }
nuclear@0 64
nuclear@0 65 void game_cleanup()
nuclear@0 66 {
nuclear@1 67 board.destroy();
nuclear@14 68 destroy_scenery();
nuclear@0 69 }
nuclear@0 70
nuclear@0 71 void game_update(unsigned long time_msec)
nuclear@0 72 {
nuclear@17 73 cur_time = time_msec;
nuclear@0 74 }
nuclear@0 75
nuclear@0 76 void game_display()
nuclear@0 77 {
nuclear@0 78 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
nuclear@0 79
nuclear@0 80 glMatrixMode(GL_MODELVIEW);
nuclear@0 81 glLoadIdentity();
nuclear@0 82 glTranslatef(0, 0, -cam_dist);
nuclear@0 83 glRotatef(cam_phi, 1, 0, 0);
nuclear@0 84 glRotatef(cam_theta, 0, 1, 0);
nuclear@0 85
nuclear@15 86 float ldir[] = {-10, 20, 10, 1};
nuclear@6 87 glLightfv(GL_LIGHT0, GL_POSITION, ldir);
nuclear@6 88
nuclear@0 89 draw_backdrop();
nuclear@14 90 draw_scenery();
nuclear@1 91 board.draw();
nuclear@15 92
nuclear@15 93 if(dbg_busyloop) {
nuclear@15 94 redisplay();
nuclear@15 95 }
nuclear@0 96 }
nuclear@0 97
nuclear@0 98 static void draw_backdrop()
nuclear@0 99 {
nuclear@0 100 glPushAttrib(GL_ENABLE_BIT);
nuclear@0 101 glDisable(GL_LIGHTING);
nuclear@0 102 glDisable(GL_DEPTH_TEST);
nuclear@0 103
nuclear@0 104 glMatrixMode(GL_PROJECTION);
nuclear@0 105 glPushMatrix();
nuclear@0 106 glLoadIdentity();
nuclear@0 107 glMatrixMode(GL_MODELVIEW);
nuclear@0 108 glPushMatrix();
nuclear@0 109 glLoadIdentity();
nuclear@0 110
nuclear@0 111 glBegin(GL_QUADS);
nuclear@0 112 glColor3f(0.9, 0.8, 0.6);
nuclear@0 113 glVertex2f(-1, -1);
nuclear@0 114 glVertex2f(1, -1);
nuclear@0 115 glColor3f(0.4, 0.5, 0.8);
nuclear@0 116 glVertex2f(1, 1);
nuclear@0 117 glVertex2f(-1, 1);
nuclear@0 118 glEnd();
nuclear@0 119
nuclear@0 120 glMatrixMode(GL_PROJECTION);
nuclear@0 121 glPopMatrix();
nuclear@0 122 glMatrixMode(GL_MODELVIEW);
nuclear@0 123 glPopMatrix();
nuclear@0 124
nuclear@0 125 glPopAttrib();
nuclear@0 126 }
nuclear@0 127
nuclear@0 128 void game_reshape(int x, int y)
nuclear@0 129 {
nuclear@0 130 glMatrixMode(GL_PROJECTION);
nuclear@0 131 glLoadIdentity();
nuclear@5 132 gluPerspective(45, (float)x / (float)y, 0.2, 200.0);
nuclear@0 133
nuclear@0 134 glViewport(0, 0, x, y);
nuclear@0 135 }
nuclear@0 136
nuclear@0 137 void game_keyboard(int bn, bool press)
nuclear@0 138 {
nuclear@0 139 if(press) {
nuclear@0 140 switch(bn) {
nuclear@0 141 case 27:
nuclear@0 142 quit();
nuclear@14 143
nuclear@14 144 case 'w':
nuclear@14 145 wireframe = !wireframe;
nuclear@14 146 redisplay();
nuclear@14 147 break;
nuclear@15 148
nuclear@15 149 case 'd':
nuclear@15 150 dbg_busyloop = !dbg_busyloop;
nuclear@15 151 redisplay();
nuclear@15 152 break;
nuclear@0 153 }
nuclear@0 154 }
nuclear@0 155 }
nuclear@0 156
nuclear@0 157 void game_mbutton(int bn, bool press, int x, int y)
nuclear@0 158 {
nuclear@0 159 bnstate[bn] = press;
nuclear@0 160 prev_x = x;
nuclear@0 161 prev_y = y;
nuclear@0 162 }
nuclear@0 163
nuclear@0 164 void game_mmotion(int x, int y)
nuclear@0 165 {
nuclear@0 166 int dx = x - prev_x;
nuclear@0 167 int dy = y - prev_y;
nuclear@0 168 prev_x = x;
nuclear@0 169 prev_y = y;
nuclear@0 170
nuclear@0 171 if(bnstate[0]) {
nuclear@0 172 cam_theta += dx * 0.5;
nuclear@0 173 cam_phi += dy * 0.5;
nuclear@0 174
nuclear@0 175 if(cam_phi < -90) cam_phi = -90;
nuclear@0 176 if(cam_phi > 90) cam_phi = 90;
nuclear@0 177
nuclear@0 178 redisplay();
nuclear@0 179 }
nuclear@0 180 if(bnstate[2]) {
nuclear@0 181 cam_dist += dy * 0.1;
nuclear@0 182 if(cam_dist < 0.0) cam_dist = 0.0;
nuclear@0 183
nuclear@0 184 redisplay();
nuclear@0 185 }
nuclear@0 186 }