From deb832f7e2ecd07c3b4e1d2ba5f9422cc8aa2656 Mon Sep 17 00:00:00 2001 From: Tamir Atias Date: Thu, 12 Apr 2012 03:53:42 +0300 Subject: [PATCH] [Add] Initial map scrolling. --- Bin/VC10/VC10.vcxproj | 1 + Bin/VC10/VC10.vcxproj.filters | 3 +++ LibDQt/LibDQt.pro | 1 + src/Actor/Player.h | 3 +++ src/Global/Constants.cpp | 10 ++++++++++ src/Global/Constants.h | 12 ++++++------ src/Level/Layer.cpp | 11 ++++++++++- src/Level/Layer.h | 2 +- src/Level/Level.cpp | 5 +++-- src/Level/Level.h | 2 +- src/Main/Game.cpp | 6 +++++- 11 files changed, 44 insertions(+), 12 deletions(-) create mode 100644 src/Global/Constants.cpp diff --git a/Bin/VC10/VC10.vcxproj b/Bin/VC10/VC10.vcxproj index c0ff780..a73715e 100644 --- a/Bin/VC10/VC10.vcxproj +++ b/Bin/VC10/VC10.vcxproj @@ -124,6 +124,7 @@ + diff --git a/Bin/VC10/VC10.vcxproj.filters b/Bin/VC10/VC10.vcxproj.filters index 88db6ab..df0868d 100644 --- a/Bin/VC10/VC10.vcxproj.filters +++ b/Bin/VC10/VC10.vcxproj.filters @@ -236,5 +236,8 @@ Level + + Global + \ No newline at end of file diff --git a/LibDQt/LibDQt.pro b/LibDQt/LibDQt.pro index c57aeed..bff708f 100644 --- a/LibDQt/LibDQt.pro +++ b/LibDQt/LibDQt.pro @@ -46,6 +46,7 @@ HEADERS += ../src/Actor/Player.h \ ../src/TMXParser/TmxUtil.h SOURCES += ../src/Actor/Player.cpp \ ../src/Global/Globals.cpp \ + ../src/Global/Constants.cpp \ ../src/IO/Input.cpp \ ../src/Main/main.cpp \ ../src/Main/LGLXWindow.cpp \ diff --git a/src/Actor/Player.h b/src/Actor/Player.h index 6f53d27..184e67d 100644 --- a/src/Actor/Player.h +++ b/src/Actor/Player.h @@ -16,6 +16,9 @@ public: void Render(void); void ProcessEvents(void); + int GetX() { return x; } + int GetY() { return y; } + private: float x; float y; diff --git a/src/Global/Constants.cpp b/src/Global/Constants.cpp new file mode 100644 index 0000000..fe1d368 --- /dev/null +++ b/src/Global/Constants.cpp @@ -0,0 +1,10 @@ +#include "Constants.h" + +const int WINDOW_WIDTH = 800; +const int WINDOW_HEIGHT = 600; +const int WINDOW_BPP = 16; +const int WINDOW_FULLSCREEN = 0; + +const SDL_VideoInfo* info = NULL; + +int flags = 0; diff --git a/src/Global/Constants.h b/src/Global/Constants.h index e0d1ac1..55130f2 100644 --- a/src/Global/Constants.h +++ b/src/Global/Constants.h @@ -1,11 +1,11 @@ #pragma once #include -const int WINDOW_WIDTH = 800; -const int WINDOW_HEIGHT = 600; -const int WINDOW_BPP = 16; -const int WINDOW_FULLSCREEN = false; +extern const int WINDOW_WIDTH; +extern const int WINDOW_HEIGHT; +extern const int WINDOW_BPP; +extern const int WINDOW_FULLSCREEN; -const SDL_VideoInfo* info = NULL; +extern const SDL_VideoInfo* info; -int flags = 0; +extern int flags; diff --git a/src/Level/Layer.cpp b/src/Level/Layer.cpp index 30eaa9d..fba8462 100644 --- a/src/Level/Layer.cpp +++ b/src/Level/Layer.cpp @@ -1,5 +1,6 @@ #include "Layer.h" #include "Tileset.h" +#include "../Global/Constants.h" Layer::Layer(int width, int height, int tileWidth, int tileHeight) { _width = width; @@ -20,7 +21,15 @@ void Layer::Update(float dt) { } } -void Layer::Draw() { +void Layer::Draw(int xOffset, int yOffset) { + int xOffsetTiles = xOffset / _width; + int yOffsetTiles = yOffset / _height; + + int minX = std::max(0, xOffsetTiles - 1); + int maxX = std::min(_width, xOffsetTiles + (WINDOW_WIDTH / (int)_tileWidth) + 1); + int minY = std::max(0, yOffsetTiles - 1); + int maxY = std::min(_height, yOffsetTiles + (WINDOW_HEIGHT / (int)_tileHeight) + 1); + for(int x = 0; x < _width; x++) { for(int y = 0; y < _height; y++) { MapTile& tile = GetTile(x, y); diff --git a/src/Level/Layer.h b/src/Level/Layer.h index 1b13508..96b9417 100644 --- a/src/Level/Layer.h +++ b/src/Level/Layer.h @@ -8,7 +8,7 @@ public: ~Layer(); void Update(float dt); - void Draw(); + void Draw(int xOffset, int yOffset); int GetWidth() const { return _width; } int GetHeight() const { return _height; } diff --git a/src/Level/Level.cpp b/src/Level/Level.cpp index fc72fec..057cb2f 100644 --- a/src/Level/Level.cpp +++ b/src/Level/Level.cpp @@ -1,4 +1,5 @@ #include +#include #include "Level.h" #include "Layer.h" #include "Tileset.h" @@ -78,8 +79,8 @@ bool Level::Load(const std::string& filename) { return true; } -void Level::Draw() { +void Level::Draw(int xOffset, int yOffset) { for(std::list::iterator i = _layers.begin(); i != _layers.end(); ++i) { - (*i)->Draw(); + (*i)->Draw(xOffset, yOffset); } } diff --git a/src/Level/Level.h b/src/Level/Level.h index 07c33e4..fe9d4e2 100644 --- a/src/Level/Level.h +++ b/src/Level/Level.h @@ -14,7 +14,7 @@ public: bool Load(const std::string& filename); void Update(float dt); - void Draw(); + void Draw(int xOffset, int yOffset); int GetWidth() const { return _width; } int GetHeight() const { return _height; } diff --git a/src/Main/Game.cpp b/src/Main/Game.cpp index 87a70c9..d0180ba 100644 --- a/src/Main/Game.cpp +++ b/src/Main/Game.cpp @@ -52,9 +52,13 @@ void Game::Render(void) { glMatrixMode(GL_MODELVIEW); glLoadIdentity(); + glTranslatef(-(_player->GetX() - 256), -(_player->GetY() - 128), 0.0f); + // Render our shit.. - _level->Draw(); + _level->Draw(_player->GetX() - 256, _player->GetY() - 128); _player->Render(); + + glPopMatrix(); } void Game::Shutdown(void) {