summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMatthias Schiffer <matthias@gamezock.de>2009-12-15 22:42:49 +0100
committerMatthias Schiffer <matthias@gamezock.de>2009-12-15 22:42:49 +0100
commita407f8b9edbc7a6e865b2daf65b2a6cd10edd2d7 (patch)
treec8b206f234b6a8f9070fc9b2a149aa5b9745883c /src
parentd9f44af7aee41a111a3d7427d8735bc821f1824f (diff)
downloadzoom++-a407f8b9edbc7a6e865b2daf65b2a6cd10edd2d7.tar
zoom++-a407f8b9edbc7a6e865b2daf65b2a6cd10edd2d7.zip
Added movement controls
Diffstat (limited to 'src')
-rw-r--r--src/Game.cpp44
-rw-r--r--src/Game.h18
-rw-r--r--src/zoom.cpp23
3 files changed, 62 insertions, 23 deletions
diff --git a/src/Game.cpp b/src/Game.cpp
index c738080..d4991ed 100644
--- a/src/Game.cpp
+++ b/src/Game.cpp
@@ -27,7 +27,8 @@
namespace Zoom {
-Game::Game(bool multisample) : angle(0), lightPos(0) {
+Game::Game(bool multisample) : playerPos(vmml::vec3f::ZERO), playerRot(vmml::mat4f::IDENTITY),
+ input(NONE), lightPos(0) {
glClearColor(0.0, 0.0, 0.0, 1.0);
glClearDepth(1.0);
glEnable(GL_DEPTH_TEST);
@@ -41,14 +42,12 @@ Game::Game(bool multisample) : angle(0), lightPos(0) {
glEnable(GL_MULTISAMPLE_ARB);
#endif
- glShadeModel(GL_SMOOTH);
-
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_AMBIENT, vmml::vec4f::ZERO.array);
glLightfv(GL_LIGHT0, GL_DIFFUSE, vmml::vec4f::ONE.array);
glLightfv(GL_LIGHT0, GL_SPECULAR, vmml::vec4f::ONE.array);
glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 0);
- glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0.3);
+ glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0.2);
glEnable(GL_LIGHT0);
glEnable(GL_COLOR_MATERIAL);
@@ -62,6 +61,8 @@ Game::Game(bool multisample) : angle(0), lightPos(0) {
Shader::loadProgram("default.vert", "default.frag");
+ playerRot.rotate_y(M_PI/6);
+
loadLevel("level.xml");
triangles.insert(triangles.end(), level->getRooms().front().walls.begin(), level->getRooms().front().walls.end());
std::sort(triangles.begin(), triangles.end(), Renderer::TextureSorter());
@@ -74,13 +75,21 @@ bool Game::loadLevel(const std::string &name) {
}
void Game::run(int delta) {
- angle += delta*0.01;
-
- if(angle >= 360)
- angle -= 360;
-
lightPos += delta;
lightPos %= 24000;
+
+ if(input & FORWARD) {
+ playerPos -= playerRot*vmml::vec3f::UNIT_Z*0.01*delta;
+ }
+ if(input & BACKWARD) {
+ playerPos += playerRot*vmml::vec3f::UNIT_Z*0.01*delta;
+ }
+ if(input & LEFT) {
+ playerPos -= playerRot*vmml::vec3f::UNIT_X*0.01*delta;
+ }
+ if(input & RIGHT) {
+ playerPos += playerRot*vmml::vec3f::UNIT_X*0.01*delta;
+ }
}
void Game::render() {
@@ -107,23 +116,16 @@ void Game::render() {
light[2] = -8.0;
}
- /*std::vector<BSPTree::TriangleRecord> triangles(level->getRooms().front().walls.begin(),
- level->getRooms().front().walls.end());
- std::sort(triangles.begin(), triangles.end(), Renderer::TextureSorter());*/
-
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glPushMatrix();
- glRotatef(15, 1, 2, 1);
- /*glRotatef(5*angle, 0, -1, 2);
- glRotatef(7*angle, 2, -1, 0);
- glRotatef(11*angle, 2, -1, 2);*/
+ vmml::mat4f transform(playerRot), inverse;
+ transform.set_translation(playerPos);
+ transform.inverse(inverse);
+
+ glLoadMatrixf(inverse.array);
glLightfv(GL_LIGHT0, GL_POSITION, light);
- //renderer.render(std::list<BSPTree::TriangleRecord>(triangles.begin(), triangles.end()));
renderer.render(triangles);
-
- glPopMatrix();
}
}
diff --git a/src/Game.h b/src/Game.h
index 0fe7233..a81c7f5 100644
--- a/src/Game.h
+++ b/src/Game.h
@@ -31,10 +31,22 @@ class Triangle;
class Game {
public:
+ enum Input {
+ NONE = 0,
+ FORWARD = (1 << 0),
+ BACKWARD = (1 << 1),
+ LEFT = (1 << 2),
+ RIGHT = (1 << 3)
+ };
+
Game(bool multisample);
bool loadLevel(const std::string &name);
+ void setInput(unsigned input) {
+ this->input = static_cast<Input>(input);
+ }
+
void run(int delta);
void render();
@@ -44,7 +56,11 @@ class Game {
boost::shared_ptr<Level> level;
std::vector<BSPTree::TriangleRecord> triangles;
- float angle;
+ vmml::vec3f playerPos;
+ vmml::mat4f playerRot;
+
+ Input input;
+
int lightPos;
};
diff --git a/src/zoom.cpp b/src/zoom.cpp
index 6fab527..d9df6d4 100644
--- a/src/zoom.cpp
+++ b/src/zoom.cpp
@@ -272,7 +272,7 @@ bool GLXinit(Display *disp, Atom windele, Window *wnd, GLXContext *gc, bool *mul
XSetWindowAttributes swa;
swa.colormap = cmap;
swa.border_pixel = 0;
- swa.event_mask = StructureNotifyMask/* | PointerMotionMask | KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask*/;
+ swa.event_mask = StructureNotifyMask | KeyPressMask | KeyReleaseMask/* | PointerMotionMask | ButtonPressMask | ButtonReleaseMask*/;
*wnd = XCreateWindow(disp, RootWindow(disp, vi->screen), 0, 0, DEFAULT_WIDTH, DEFAULT_HEIGHT, 0, vi->depth, InputOutput,
vi->visual, CWBorderPixel|CWColormap|CWEventMask, &swa);
@@ -336,6 +336,7 @@ int main() {
unsigned long delta = 0;
unsigned long frames = 0, tocks = 0;
+ unsigned input = 0;
struct timeval tv;
gettimeofday(&tv, NULL);
@@ -353,11 +354,31 @@ int main() {
case ClientMessage:
if(static_cast<Atom>(event.xclient.data.l[0]) == windele)
running = false;
+ break;
+
+ case KeyPress:
+ switch(XKeycodeToKeysym(disp, event.xkey.keycode, 0)) {
+ case XK_Up: case XK_w: input |= Zoom::Game::FORWARD; break;
+ case XK_Down: case XK_s: input |= Zoom::Game::BACKWARD; break;
+ case XK_Left: case XK_a: input |= Zoom::Game::LEFT; break;
+ case XK_Right: case XK_d: input |= Zoom::Game::RIGHT; break;
+ case XK_Escape: running = false; break;
+ }
+ break;
+
+ case KeyRelease:
+ switch(XKeycodeToKeysym(disp, event.xkey.keycode, 0)) {
+ case XK_Up: case XK_w: input &= ~Zoom::Game::FORWARD; break;
+ case XK_Down: case XK_s: input &= ~Zoom::Game::BACKWARD; break;
+ case XK_Left: case XK_a: input &= ~Zoom::Game::LEFT; break;
+ case XK_Right: case XK_d: input &= ~Zoom::Game::RIGHT; break;
+ }
}
}
if(!running) break;
+ game.setInput(input);
game.run(delta);
game.render();