[Add] Initial map scrolling.

This commit is contained in:
Tamir Atias 2012-04-12 03:53:42 +03:00
parent 2de0088860
commit deb832f7e2
11 changed files with 44 additions and 12 deletions

View File

@ -124,6 +124,7 @@
<ItemGroup> <ItemGroup>
<ClCompile Include="..\..\src\Actor\Player.cpp" /> <ClCompile Include="..\..\src\Actor\Player.cpp" />
<ClCompile Include="..\..\src\Collision\AABB.cpp" /> <ClCompile Include="..\..\src\Collision\AABB.cpp" />
<ClCompile Include="..\..\src\Global\Constants.cpp" />
<ClCompile Include="..\..\src\Global\Globals.cpp" /> <ClCompile Include="..\..\src\Global\Globals.cpp" />
<ClCompile Include="..\..\src\IO\Input.cpp" /> <ClCompile Include="..\..\src\IO\Input.cpp" />
<ClCompile Include="..\..\src\Level\Layer.cpp" /> <ClCompile Include="..\..\src\Level\Layer.cpp" />

View File

@ -236,5 +236,8 @@
<ClCompile Include="..\..\src\Level\Tileset.cpp"> <ClCompile Include="..\..\src\Level\Tileset.cpp">
<Filter>Level</Filter> <Filter>Level</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\..\src\Global\Constants.cpp">
<Filter>Global</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -46,6 +46,7 @@ HEADERS += ../src/Actor/Player.h \
../src/TMXParser/TmxUtil.h ../src/TMXParser/TmxUtil.h
SOURCES += ../src/Actor/Player.cpp \ SOURCES += ../src/Actor/Player.cpp \
../src/Global/Globals.cpp \ ../src/Global/Globals.cpp \
../src/Global/Constants.cpp \
../src/IO/Input.cpp \ ../src/IO/Input.cpp \
../src/Main/main.cpp \ ../src/Main/main.cpp \
../src/Main/LGLXWindow.cpp \ ../src/Main/LGLXWindow.cpp \

View File

@ -16,6 +16,9 @@ public:
void Render(void); void Render(void);
void ProcessEvents(void); void ProcessEvents(void);
int GetX() { return x; }
int GetY() { return y; }
private: private:
float x; float x;
float y; float y;

10
src/Global/Constants.cpp Normal file
View File

@ -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;

View File

@ -1,11 +1,11 @@
#pragma once #pragma once
#include <SDL/SDL.h> #include <SDL/SDL.h>
const int WINDOW_WIDTH = 800; extern const int WINDOW_WIDTH;
const int WINDOW_HEIGHT = 600; extern const int WINDOW_HEIGHT;
const int WINDOW_BPP = 16; extern const int WINDOW_BPP;
const int WINDOW_FULLSCREEN = false; extern const int WINDOW_FULLSCREEN;
const SDL_VideoInfo* info = NULL; extern const SDL_VideoInfo* info;
int flags = 0; extern int flags;

View File

@ -1,5 +1,6 @@
#include "Layer.h" #include "Layer.h"
#include "Tileset.h" #include "Tileset.h"
#include "../Global/Constants.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;
@ -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 x = 0; x < _width; x++) {
for(int y = 0; y < _height; y++) { for(int y = 0; y < _height; y++) {
MapTile& tile = GetTile(x, y); MapTile& tile = GetTile(x, y);

View File

@ -8,7 +8,7 @@ public:
~Layer(); ~Layer();
void Update(float dt); void Update(float dt);
void Draw(); void Draw(int xOffset, int yOffset);
int GetWidth() const { return _width; } int GetWidth() const { return _width; }
int GetHeight() const { return _height; } int GetHeight() const { return _height; }

View File

@ -1,4 +1,5 @@
#include <map> #include <map>
#include <algorithm>
#include "Level.h" #include "Level.h"
#include "Layer.h" #include "Layer.h"
#include "Tileset.h" #include "Tileset.h"
@ -78,8 +79,8 @@ bool Level::Load(const std::string& filename) {
return true; return true;
} }
void Level::Draw() { void Level::Draw(int xOffset, int yOffset) {
for(std::list<Layer*>::iterator i = _layers.begin(); i != _layers.end(); ++i) { for(std::list<Layer*>::iterator i = _layers.begin(); i != _layers.end(); ++i) {
(*i)->Draw(); (*i)->Draw(xOffset, yOffset);
} }
} }

View File

@ -14,7 +14,7 @@ public:
bool Load(const std::string& filename); bool Load(const std::string& filename);
void Update(float dt); void Update(float dt);
void Draw(); void Draw(int xOffset, int yOffset);
int GetWidth() const { return _width; } int GetWidth() const { return _width; }
int GetHeight() const { return _height; } int GetHeight() const { return _height; }

View File

@ -52,9 +52,13 @@ void Game::Render(void) {
glMatrixMode(GL_MODELVIEW); glMatrixMode(GL_MODELVIEW);
glLoadIdentity(); glLoadIdentity();
glTranslatef(-(_player->GetX() - 256), -(_player->GetY() - 128), 0.0f);
// Render our shit.. // Render our shit..
_level->Draw(); _level->Draw(_player->GetX() - 256, _player->GetY() - 128);
_player->Render(); _player->Render();
glPopMatrix();
} }
void Game::Shutdown(void) { void Game::Shutdown(void) {