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. --- player.cpp | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 player.cpp (limited to 'player.cpp') 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; +} -- cgit v1.2.3