From 8f06f70ad338304e8b670f27fe1bfe2435c15f2c Mon Sep 17 00:00:00 2001 From: Tamir Atias Date: Sat, 14 Apr 2012 21:51:20 +0300 Subject: [PATCH] [Fix] Movement is now time based. [Fix] Stuff was stretching on window resize. --- src/Actor/Player.cpp | 10 +++++----- src/Actor/Player.h | 4 ++-- src/Global/Constants.cpp | 2 -- src/Global/Constants.h | 2 -- src/Global/Globals.cpp | 2 ++ src/Global/Globals.h | 2 ++ src/Level/Layer.cpp | 6 +++--- src/Main/Game.cpp | 21 ++++++++++----------- src/Main/Game.h | 2 +- src/Main/main.cpp | 19 ++++++++++++------- 10 files changed, 37 insertions(+), 33 deletions(-) diff --git a/src/Actor/Player.cpp b/src/Actor/Player.cpp index 188c3b1..022c2ca 100644 --- a/src/Actor/Player.cpp +++ b/src/Actor/Player.cpp @@ -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)) { diff --git a/src/Actor/Player.h b/src/Actor/Player.h index 0054e4e..55b8e3c 100644 --- a/src/Actor/Player.h +++ b/src/Actor/Player.h @@ -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; } diff --git a/src/Global/Constants.cpp b/src/Global/Constants.cpp index fe1d368..faa27c1 100644 --- a/src/Global/Constants.cpp +++ b/src/Global/Constants.cpp @@ -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; diff --git a/src/Global/Constants.h b/src/Global/Constants.h index 55130f2..d9918c7 100644 --- a/src/Global/Constants.h +++ b/src/Global/Constants.h @@ -1,8 +1,6 @@ #pragma once #include -extern const int WINDOW_WIDTH; -extern const int WINDOW_HEIGHT; extern const int WINDOW_BPP; extern const int WINDOW_FULLSCREEN; diff --git a/src/Global/Globals.cpp b/src/Global/Globals.cpp index 5ddada9..ece8ddd 100644 --- a/src/Global/Globals.cpp +++ b/src/Global/Globals.cpp @@ -3,3 +3,5 @@ SDL_Surface* screen = NULL; SDL_Event event; +int windowWidth = 800; +int windowHeight = 600; diff --git a/src/Global/Globals.h b/src/Global/Globals.h index 969a8e3..affc12d 100644 --- a/src/Global/Globals.h +++ b/src/Global/Globals.h @@ -4,3 +4,5 @@ extern SDL_Surface* screen; extern SDL_Event event; +extern int windowWidth; +extern int windowHeight; diff --git a/src/Level/Layer.cpp b/src/Level/Layer.cpp index 43ec8ed..2935d5c 100644 --- a/src/Level/Layer.cpp +++ b/src/Level/Layer.cpp @@ -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++) { diff --git a/src/Main/Game.cpp b/src/Main/Game.cpp index 215a590..e36625f 100644 --- a/src/Main/Game.cpp +++ b/src/Main/Game.cpp @@ -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(); diff --git a/src/Main/Game.h b/src/Main/Game.h index b4445cb..d9d6e99 100644 --- a/src/Main/Game.h +++ b/src/Main/Game.h @@ -16,7 +16,7 @@ public: void Render(void); void Shutdown(void); - void ProcessEvents(void); + void ProcessEvents(float dt); void OnResize(int width, int height); diff --git a/src/Main/main.cpp b/src/Main/main.cpp index 2c22d39..d5b42c8 100644 --- a/src/Main/main.cpp +++ b/src/Main/main.cpp @@ -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();