-
Notifications
You must be signed in to change notification settings - Fork 1
/
Game.cpp
executable file
·167 lines (143 loc) · 3.88 KB
/
Game.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <GL/glfw.h>
#include <GL/glext.h>
#include <math.h>
#include <stdio.h>
#include <iostream>
#include <sstream>
#include "Game.h"
Game::Game(const std::string& file, int w, int h):
invalid(true),windowWidth(w), windowHeight(h),moves(0),
mapFile(file), map(file),box(0,0,0,1,this){
if (!glfwInit() || !glfwOpenWindow(windowWidth,windowHeight, 8,8,8,8,24,0,GLFW_WINDOW)){
glfwTerminate();
throw 1;
}
}
Game::~Game(){
glfwTerminate();
}
BloxorzMap& Game::getMap(){
return map;
}
void Game::init(){
float aspectRatio = double(windowWidth)/windowHeight;
moves = 0;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(50.0,aspectRatio,1,50.0);
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
glEnable(GL_LINE_SMOOTH);
glEnable(GL_POLYGON_SMOOTH);
glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
glShadeModel(GL_SMOOTH);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glFrontFace(GL_CW);
glMatrixMode(GL_MODELVIEW);
int r1,c1,r2,c2;
map.init();
map.getBox(r1,c1,r2,c2);
box = Box(r1,c1,r2,c2,this);
box.init();
load();
invalidate();
mustInit = false;
}
void Game::loop(){
while (glfwGetKey(GLFW_KEY_ESC) != GLFW_PRESS && glfwGetWindowParam(GLFW_OPENED)){
if (invalid || mustInit){
if (mustInit)
init();
while (invalid){
glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);
invalid = paint();
glfwSwapBuffers();
}
// //print the state!
// std::cout<<"---------STATE----------------\n";
// std::vector<std::vector<char> > state = map.state();
// int r1,c1,r2,c2;
// box.get(r1,c1,r2,c2);
// state[r1][c1] = '+';
// if (r2 != -1)
// state[r2][c2] = '+';
// for (int i=0; i<state.size(); i++){
// for (int j=0; j<state[i].size(); j++){
// std::cout<<state[i][j];
// }
// std::cout<<std::endl;
// }
// //invalid = true;
}else{
glfwSwapBuffers();
}
//glfwPollEvents();
processKeys();
}
}
void Game::invalidate(){
invalid = true;
}
bool Game::paint(){
bool stillInvalid = false;
glLoadIdentity();
//gluLookAt(box.getXLocation(), 18, 35, box.getXLocation()+5, 0, 10, 0, 1, 0);
gluLookAt(map.width()/2.0-18.0, 18, 40, map.width()/2.0, 0, 10, 0, 1, 0);
if (map.paint() && !stillInvalid){
stillInvalid = true;
}else{
if (box.paint() && !stillInvalid)
stillInvalid = true;
}
std::stringstream ss;
ss<<"Bloxorz [Moves: "<<moves<<"]";
glfwSetWindowTitle(ss.str().c_str());
return stillInvalid;
}
void Game::processKeys(){
if (glfwGetKey(GLFW_KEY_LEFT)==GLFW_PRESS){
invalid = true;
moves++;
box.moveLeft();
}
else if (glfwGetKey(GLFW_KEY_RIGHT)==GLFW_PRESS){
invalid = true;
moves++;
box.moveRight();
}
else if (glfwGetKey(GLFW_KEY_UP)==GLFW_PRESS){
invalid = true;
moves++;
box.moveUp();
}
else if (glfwGetKey(GLFW_KEY_DOWN)==GLFW_PRESS){
invalid = true;
moves++;
box.moveDown();
}
else if (glfwGetKey('R')==GLFW_PRESS){
init();
invalid = true;
}
}
void Game::load(){
map.load();
}
void Game::unload(){
map.unload();
}
void Game::over(int status){
if (status == 1){ //WIN!!
std::cout<<"WIN!!\n";
mustInit = true;
invalid = true;
//may be more stuff here.. like load next level???
}else{
std::cout<<"YOU FELL OFF!!\n";
init();
}
}
void Game::requestInit(){
mustInit = true;
}