tavli

annotate src/game.cc @ 15:b1a195c3ee16

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