tavli

annotate src/game.cc @ 16:d6209903454b

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