[Fix] Movement is now time based.
[Fix] Stuff was stretching on window resize.
This commit is contained in:
parent
d7bef996e6
commit
8f06f70ad3
@ -26,9 +26,9 @@ Player::~Player(void) {
|
|||||||
delete _player;
|
delete _player;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Player::Update(void) {
|
void Player::Update(float dt) {
|
||||||
// Process events here.
|
// Process events here.
|
||||||
ProcessEvents();
|
ProcessEvents(dt);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Player::Render(void) {
|
void Player::Render(void) {
|
||||||
@ -36,7 +36,7 @@ void Player::Render(void) {
|
|||||||
_player->Draw();
|
_player->Draw();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Player::ProcessEvents(void) {
|
void Player::ProcessEvents(float dt) {
|
||||||
float oldX = x = _player->GetX();
|
float oldX = x = _player->GetX();
|
||||||
float oldY = y = _player->GetY();
|
float oldY = y = _player->GetY();
|
||||||
if(KeyStillDown(SDLK_w) || KeyStillDown(SDLK_UP)) {
|
if(KeyStillDown(SDLK_w) || KeyStillDown(SDLK_UP)) {
|
||||||
@ -58,11 +58,11 @@ void Player::ProcessEvents(void) {
|
|||||||
if(KeyDown(SDLK_LSHIFT)) {
|
if(KeyDown(SDLK_LSHIFT)) {
|
||||||
// Run!
|
// Run!
|
||||||
PLAYER_SPEED += 3;
|
PLAYER_SPEED += 3;
|
||||||
Debug::logger->message("Speed: %f", PLAYER_SPEED);
|
Debug::logger->message("Speed: %f", PLAYER_SPEED * 16 * dt);
|
||||||
}
|
}
|
||||||
if(KeyUp(SDLK_LSHIFT)) {
|
if(KeyUp(SDLK_LSHIFT)) {
|
||||||
PLAYER_SPEED -= 3;
|
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(x != oldX || y != oldY) {
|
||||||
if(!SoundEffect::IsPlaying(1)) {
|
if(!SoundEffect::IsPlaying(1)) {
|
||||||
|
@ -13,12 +13,12 @@ public:
|
|||||||
Player(void);
|
Player(void);
|
||||||
~Player(void);
|
~Player(void);
|
||||||
|
|
||||||
void Update(void);
|
void Update(float dt);
|
||||||
void Render(void);
|
void Render(void);
|
||||||
|
|
||||||
// --- Collision stuff.
|
// --- Collision stuff.
|
||||||
|
|
||||||
void ProcessEvents(void);
|
void ProcessEvents(float dt);
|
||||||
|
|
||||||
int GetX(void) { return x; }
|
int GetX(void) { return x; }
|
||||||
int GetY(void) { return y; }
|
int GetY(void) { return y; }
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
#include "Constants.h"
|
#include "Constants.h"
|
||||||
|
|
||||||
const int WINDOW_WIDTH = 800;
|
|
||||||
const int WINDOW_HEIGHT = 600;
|
|
||||||
const int WINDOW_BPP = 16;
|
const int WINDOW_BPP = 16;
|
||||||
const int WINDOW_FULLSCREEN = 0;
|
const int WINDOW_FULLSCREEN = 0;
|
||||||
|
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <SDL/SDL.h>
|
#include <SDL/SDL.h>
|
||||||
|
|
||||||
extern const int WINDOW_WIDTH;
|
|
||||||
extern const int WINDOW_HEIGHT;
|
|
||||||
extern const int WINDOW_BPP;
|
extern const int WINDOW_BPP;
|
||||||
extern const int WINDOW_FULLSCREEN;
|
extern const int WINDOW_FULLSCREEN;
|
||||||
|
|
||||||
|
@ -3,3 +3,5 @@
|
|||||||
SDL_Surface* screen = NULL;
|
SDL_Surface* screen = NULL;
|
||||||
SDL_Event event;
|
SDL_Event event;
|
||||||
|
|
||||||
|
int windowWidth = 800;
|
||||||
|
int windowHeight = 600;
|
||||||
|
@ -4,3 +4,5 @@
|
|||||||
extern SDL_Surface* screen;
|
extern SDL_Surface* screen;
|
||||||
extern SDL_Event event;
|
extern SDL_Event event;
|
||||||
|
|
||||||
|
extern int windowWidth;
|
||||||
|
extern int windowHeight;
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
#include "Layer.h"
|
#include "Layer.h"
|
||||||
#include "Tileset.h"
|
#include "Tileset.h"
|
||||||
#include "../Global/Constants.h"
|
#include "../Global/Globals.h"
|
||||||
|
|
||||||
Layer::Layer(int width, int height, int tileWidth, int tileHeight) {
|
Layer::Layer(int width, int height, int tileWidth, int tileHeight) {
|
||||||
_width = width;
|
_width = width;
|
||||||
@ -26,9 +26,9 @@ void Layer::Draw(int xOffset, int yOffset) {
|
|||||||
int yOffsetTiles = yOffset / _tileHeight;
|
int yOffsetTiles = yOffset / _tileHeight;
|
||||||
|
|
||||||
int minX = std::max(xOffsetTiles, 0);
|
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 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 x = minX; x < maxX; x++) {
|
||||||
for(int y = minY; y < maxY; y++) {
|
for(int y = minY; y < maxY; y++) {
|
||||||
|
@ -49,21 +49,17 @@ void Game::Prepare(float dt) {
|
|||||||
void Game::Render(void) {
|
void Game::Render(void) {
|
||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
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);
|
glMatrixMode(GL_MODELVIEW);
|
||||||
glLoadIdentity();
|
glLoadIdentity();
|
||||||
|
|
||||||
float windowCenterX = ((float)WINDOW_WIDTH / 2.0f) - ((float)_player->GetWidth() / 2.0f);
|
float windowCenterX = ((float)windowWidth / 2.0f) - ((float)_player->GetWidth() / 2.0f);
|
||||||
float windowCenterY = ((float)WINDOW_HEIGHT / 2.0f) - ((float)_player->GetHeight() / 2.0f);
|
float windowCenterY = ((float)windowHeight / 2.0f) - ((float)_player->GetHeight() / 2.0f);
|
||||||
|
|
||||||
float xOffset = _player->GetX() - windowCenterX;
|
float xOffset = _player->GetX() - windowCenterX;
|
||||||
float yOffset = _player->GetY() - windowCenterY;
|
float yOffset = _player->GetY() - windowCenterY;
|
||||||
|
|
||||||
float maxXOffset = (_level->GetWidth() * _level->GetTileWidth()) - (float)WINDOW_WIDTH;
|
float maxXOffset = (_level->GetWidth() * _level->GetTileWidth()) - (float)windowWidth;
|
||||||
float maxYOffset = (_level->GetHeight() * _level->GetTileHeight()) - (float)WINDOW_HEIGHT;
|
float maxYOffset = (_level->GetHeight() * _level->GetTileHeight()) - (float)windowHeight;
|
||||||
|
|
||||||
if(xOffset < 0.0f) xOffset = 0.0f;
|
if(xOffset < 0.0f) xOffset = 0.0f;
|
||||||
if(yOffset < 0.0f) yOffset = 0.0f;
|
if(yOffset < 0.0f) yOffset = 0.0f;
|
||||||
@ -84,16 +80,19 @@ void Game::Shutdown(void) {
|
|||||||
delete _level;
|
delete _level;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Game::ProcessEvents(void) {
|
void Game::ProcessEvents(float dt) {
|
||||||
_player->ProcessEvents();
|
_player->ProcessEvents(dt);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Game::OnResize(int width, int height) {
|
void Game::OnResize(int width, int height) {
|
||||||
glViewport(0, 0, width, height);
|
glViewport(0, 0, width, height);
|
||||||
|
|
||||||
|
windowWidth = width;
|
||||||
|
windowHeight = height;
|
||||||
|
|
||||||
glMatrixMode(GL_PROJECTION);
|
glMatrixMode(GL_PROJECTION);
|
||||||
glLoadIdentity();
|
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);
|
glMatrixMode(GL_MODELVIEW);
|
||||||
glLoadIdentity();
|
glLoadIdentity();
|
||||||
|
@ -16,7 +16,7 @@ public:
|
|||||||
void Render(void);
|
void Render(void);
|
||||||
void Shutdown(void);
|
void Shutdown(void);
|
||||||
|
|
||||||
void ProcessEvents(void);
|
void ProcessEvents(float dt);
|
||||||
|
|
||||||
void OnResize(int width, int height);
|
void OnResize(int width, int height);
|
||||||
|
|
||||||
|
@ -26,11 +26,9 @@ void Destroy(void) {
|
|||||||
SDL_Quit();
|
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);
|
SDL_SetVideoMode(w, h, 32, SDL_OPENGL | SDL_RESIZABLE);
|
||||||
glViewport(0,0,w,h);
|
game.OnResize(w, h);
|
||||||
glLoadIdentity();
|
|
||||||
glOrtho(0.0, 800.0, 600.0, 0.0, 0.0, 1.0);
|
|
||||||
SDL_GL_SwapBuffers();
|
SDL_GL_SwapBuffers();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -60,7 +58,7 @@ int main(int argc, char** argv) {
|
|||||||
|
|
||||||
flags = SDL_OPENGL | SDL_HWSURFACE | SDL_RESIZABLE;
|
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..");
|
Debug::logger->message("Video mode set..");
|
||||||
|
|
||||||
if(Mix_OpenAudio(44100, AUDIO_S16, 2, 4096)) {
|
if(Mix_OpenAudio(44100, AUDIO_S16, 2, 4096)) {
|
||||||
@ -86,6 +84,9 @@ int main(int argc, char** argv) {
|
|||||||
game.Init();
|
game.Init();
|
||||||
CreateInput();
|
CreateInput();
|
||||||
|
|
||||||
|
Uint32 timeStart = SDL_GetTicks();
|
||||||
|
float dt = 1.0f / 60.0f;
|
||||||
|
|
||||||
bool isRunning = true;
|
bool isRunning = true;
|
||||||
while(isRunning) {
|
while(isRunning) {
|
||||||
|
|
||||||
@ -96,15 +97,19 @@ int main(int argc, char** argv) {
|
|||||||
}
|
}
|
||||||
if(event.type == SDL_VIDEORESIZE) {
|
if(event.type == SDL_VIDEORESIZE) {
|
||||||
// Resize the window.
|
// Resize the window.
|
||||||
ResizeWindow(event.resize.w, event.resize.h);
|
ResizeWindow(game, event.resize.w, event.resize.h);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
UpdateInput();
|
UpdateInput();
|
||||||
game.ProcessEvents();
|
game.ProcessEvents(dt);
|
||||||
game.Render();
|
game.Render();
|
||||||
SDL_GL_SwapBuffers();
|
SDL_GL_SwapBuffers();
|
||||||
|
|
||||||
|
Uint32 timeEnd = SDL_GetTicks();
|
||||||
|
dt = (float)(timeEnd - timeStart) / 1000.0f;
|
||||||
|
timeStart = timeEnd;
|
||||||
}
|
}
|
||||||
|
|
||||||
game.Shutdown();
|
game.Shutdown();
|
||||||
|
Loading…
Reference in New Issue
Block a user