From 79b8fb11967aa1637e0a0bfaeafab2d77f83ca82 Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Fri, 21 May 2010 01:51:44 +0200 Subject: Added a basic player class with movements. --- application.cpp | 10 ++++++++-- application.h | 2 ++ player.cpp | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ player.h | 16 ++++++++++++++++ textures/player.png | Bin 0 -> 799 bytes 5 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 player.cpp create mode 100644 player.h create mode 100644 textures/player.png diff --git a/application.cpp b/application.cpp index f7ddaae..15b68de 100644 --- a/application.cpp +++ b/application.cpp @@ -146,6 +146,7 @@ void Application::run() { background = new TextureSDL("textures/background.png"); texture = new TextureSDL("textures/shot1.png"); shader = new GLShaderProgram(); + player = new Player(); patterns.push_back(new BulletPattern()); for(int i = 0; i < 8; i++) { @@ -191,6 +192,8 @@ void Application::main_loop(unsigned int tick, unsigned int step) { for(std::vector::iterator it = patterns.begin(); it < patterns.end(); it++) { (*it)->update(elapsed, step); } + + player->update(); } glClearColor(0, 0, 0, 0); @@ -243,7 +246,7 @@ void Application::main_loop(unsigned int tick, unsigned int step) { glClearColor(0.2, 0.2, 0.2, 0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - + glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45, (float)660 / (float)740, 1, 100); @@ -282,9 +285,12 @@ void Application::main_loop(unsigned int tick, unsigned int step) { glMatrixMode(GL_MODELVIEW); glLoadIdentity(); - + glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE); + + player->draw(); + shader->use(); glPointSize(32.0); glEnable(GL_TEXTURE_2D); diff --git a/application.h b/application.h index 1378be6..4bf9251 100644 --- a/application.h +++ b/application.h @@ -8,6 +8,7 @@ #include "bulletpattern.h" #include "shader.h" #include "texture.h" +#include "player.h" class Application { private: @@ -25,6 +26,7 @@ class Application { Texture* background; Texture* texture; GLShaderProgram* shader; + Player *player; std::vector patterns; diff --git a/player.cpp b/player.cpp new file mode 100644 index 0000000..0a7118a --- /dev/null +++ b/player.cpp @@ -0,0 +1,48 @@ +#ifndef __APPLE__ +#include +#else +#include +#endif +#include +#include + +#include "player.h" +#include "texturesdl.h" + +Player::Player() { + x = 0.5; + y = 0.1; + move_factor = 0.005; + focus_factor = 0.5; + texture = new TextureSDL("textures/player.png"); +} + +void Player::draw() { + glPointSize(32.0); + + glColor4f(1, 1, 1, 1); + glEnable(GL_TEXTURE_2D); + glBindTexture(GL_TEXTURE_2D, texture->tex()); + + glBegin(GL_POINTS); + glVertex2f(x, y); + glEnd(); + + glDisable(GL_TEXTURE_2D); +} + +void Player::update() { + Uint8 *keystate = SDL_GetKeyState(NULL); + float factor = move_factor * (SDL_GetModState() & (KMOD_LSHIFT | KMOD_RSHIFT) ? focus_factor : 1); + + float x_speed = factor * keystate[SDLK_RIGHT] - factor * keystate[SDLK_LEFT]; + float y_speed = factor * keystate[SDLK_UP] - factor * keystate[SDLK_DOWN]; + + if(x_speed && y_speed) { + x_speed /= sqrtf(2); + y_speed /= sqrtf(2); + } + + x += x_speed; + y += y_speed; +} diff --git a/player.h b/player.h new file mode 100644 index 0000000..0f00876 --- /dev/null +++ b/player.h @@ -0,0 +1,16 @@ +#ifndef _PLAYER_H_ +#define _PLAYER_H_ + +#include "texture.h" + +class Player { + public: + float x, y, move_factor, focus_factor; + Texture *texture; + + Player(); + void draw(); + void update(); +}; + +#endif diff --git a/textures/player.png b/textures/player.png new file mode 100644 index 0000000..2c21d21 Binary files /dev/null and b/textures/player.png differ -- cgit v1.2.3