[Fix] Movement is now time based.

[Fix] Stuff was stretching on window resize.
This commit is contained in:
Tamir Atias 2012-04-14 21:51:20 +03:00
parent d7bef996e6
commit 8f06f70ad3
10 changed files with 37 additions and 33 deletions

View File

@ -26,9 +26,9 @@ Player::~Player(void) {
delete _player;
}
void Player::Update(void) {
void Player::Update(float dt) {
// Process events here.
ProcessEvents();
ProcessEvents(dt);
}
void Player::Render(void) {
@ -36,7 +36,7 @@ void Player::Render(void) {
_player->Draw();
}
void Player::ProcessEvents(void) {
void Player::ProcessEvents(float dt) {
float oldX = x = _player->GetX();
float oldY = y = _player->GetY();
if(KeyStillDown(SDLK_w) || KeyStillDown(SDLK_UP)) {
@ -58,11 +58,11 @@ void Player::ProcessEvents(void) {
if(KeyDown(SDLK_LSHIFT)) {
// Run!
PLAYER_SPEED += 3;
Debug::logger->message("Speed: %f", PLAYER_SPEED);
Debug::logger->message("Speed: %f", PLAYER_SPEED * 16 * dt);
}
if(KeyUp(SDLK_LSHIFT)) {
PLAYER_SPEED -= 3;
Debug::logger->message("Speed: %f", PLAYER_SPEED);
Debug::logger->message("Speed: %f", PLAYER_SPEED * 16 * dt);
}
if(x != oldX || y != oldY) {
if(!SoundEffect::IsPlaying(1)) {

View File

@ -13,12 +13,12 @@ public:
Player(void);
~Player(void);
void Update(void);
void Update(float dt);
void Render(void);
// --- Collision stuff.
void ProcessEvents(void);
void ProcessEvents(float dt);
int GetX(void) { return x; }
int GetY(void) { return y; }

View File

@ -1,7 +1,5 @@
#include "Constants.h"
const int WINDOW_WIDTH = 800;
const int WINDOW_HEIGHT = 600;
const int WINDOW_BPP = 16;
const int WINDOW_FULLSCREEN = 0;

View File

@ -1,8 +1,6 @@
#pragma once
#include <SDL/SDL.h>
extern const int WINDOW_WIDTH;
extern const int WINDOW_HEIGHT;
extern const int WINDOW_BPP;
extern const int WINDOW_FULLSCREEN;

View File

@ -3,3 +3,5 @@
SDL_Surface* screen = NULL;
SDL_Event event;
int windowWidth = 800;
int windowHeight = 600;

View File

@ -4,3 +4,5 @@
extern SDL_Surface* screen;
extern SDL_Event event;
extern int windowWidth;
extern int windowHeight;

View File

@ -1,6 +1,6 @@
#include "Layer.h"
#include "Tileset.h"
#include "../Global/Constants.h"
#include "../Global/Globals.h"
Layer::Layer(int width, int height, int tileWidth, int tileHeight) {
_width = width;
@ -26,9 +26,9 @@ void Layer::Draw(int xOffset, int yOffset) {
int yOffsetTiles = yOffset / _tileHeight;
int minX = std::max(xOffsetTiles, 0);
int maxX = std::min(_width, xOffsetTiles + WINDOW_WIDTH / (int)_tileWidth + 2);
int maxX = std::min(_width, xOffsetTiles + windowWidth / (int)_tileWidth + 2);
int minY = std::max(yOffsetTiles, 0);
int maxY = std::min(_height, yOffsetTiles + WINDOW_HEIGHT / (int)_tileHeight + 2);
int maxY = std::min(_height, yOffsetTiles + windowHeight / (int)_tileHeight + 2);
for(int x = minX; x < maxX; x++) {
for(int y = minY; y < maxY; y++) {

View File

@ -49,21 +49,17 @@ void Game::Prepare(float dt) {
void Game::Render(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 800.0, 600.0, 0.0, 0.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
float windowCenterX = ((float)WINDOW_WIDTH / 2.0f) - ((float)_player->GetWidth() / 2.0f);
float windowCenterY = ((float)WINDOW_HEIGHT / 2.0f) - ((float)_player->GetHeight() / 2.0f);
float windowCenterX = ((float)windowWidth / 2.0f) - ((float)_player->GetWidth() / 2.0f);
float windowCenterY = ((float)windowHeight / 2.0f) - ((float)_player->GetHeight() / 2.0f);
float xOffset = _player->GetX() - windowCenterX;
float yOffset = _player->GetY() - windowCenterY;
float maxXOffset = (_level->GetWidth() * _level->GetTileWidth()) - (float)WINDOW_WIDTH;
float maxYOffset = (_level->GetHeight() * _level->GetTileHeight()) - (float)WINDOW_HEIGHT;
float maxXOffset = (_level->GetWidth() * _level->GetTileWidth()) - (float)windowWidth;
float maxYOffset = (_level->GetHeight() * _level->GetTileHeight()) - (float)windowHeight;
if(xOffset < 0.0f) xOffset = 0.0f;
if(yOffset < 0.0f) yOffset = 0.0f;
@ -84,16 +80,19 @@ void Game::Shutdown(void) {
delete _level;
}
void Game::ProcessEvents(void) {
_player->ProcessEvents();
void Game::ProcessEvents(float dt) {
_player->ProcessEvents(dt);
}
void Game::OnResize(int width, int height) {
glViewport(0, 0, width, height);
windowWidth = width;
windowHeight = height;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 800.0, 600.0, 0.0, 0.0, 1.0);
glOrtho(0.0, (GLdouble)windowWidth, (GLdouble)windowHeight, 0.0, 0.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

View File

@ -16,7 +16,7 @@ public:
void Render(void);
void Shutdown(void);
void ProcessEvents(void);
void ProcessEvents(float dt);
void OnResize(int width, int height);

View File

@ -26,11 +26,9 @@ void Destroy(void) {
SDL_Quit();
}
static void ResizeWindow(int w, int h) {
static void ResizeWindow(Game& game, int w, int h) {
SDL_SetVideoMode(w, h, 32, SDL_OPENGL | SDL_RESIZABLE);
glViewport(0,0,w,h);
glLoadIdentity();
glOrtho(0.0, 800.0, 600.0, 0.0, 0.0, 1.0);
game.OnResize(w, h);
SDL_GL_SwapBuffers();
}
@ -60,7 +58,7 @@ int main(int argc, char** argv) {
flags = SDL_OPENGL | SDL_HWSURFACE | SDL_RESIZABLE;
screen = SDL_SetVideoMode(WINDOW_WIDTH, WINDOW_HEIGHT, 32, flags);
screen = SDL_SetVideoMode(windowWidth, windowHeight, 32, flags);
Debug::logger->message("Video mode set..");
if(Mix_OpenAudio(44100, AUDIO_S16, 2, 4096)) {
@ -86,6 +84,9 @@ int main(int argc, char** argv) {
game.Init();
CreateInput();
Uint32 timeStart = SDL_GetTicks();
float dt = 1.0f / 60.0f;
bool isRunning = true;
while(isRunning) {
@ -96,15 +97,19 @@ int main(int argc, char** argv) {
}
if(event.type == SDL_VIDEORESIZE) {
// Resize the window.
ResizeWindow(event.resize.w, event.resize.h);
ResizeWindow(game, event.resize.w, event.resize.h);
break;
}
}
UpdateInput();
game.ProcessEvents();
game.ProcessEvents(dt);
game.Render();
SDL_GL_SwapBuffers();
Uint32 timeEnd = SDL_GetTicks();
dt = (float)(timeEnd - timeStart) / 1000.0f;
timeStart = timeEnd;
}
game.Shutdown();