[Add] Level loading!
This commit is contained in:
parent
48e08fa576
commit
89d50ea8fb
@ -93,6 +93,7 @@
|
|||||||
<ClInclude Include="..\..\src\Level\Layer.h" />
|
<ClInclude Include="..\..\src\Level\Layer.h" />
|
||||||
<ClInclude Include="..\..\src\Level\Level.h" />
|
<ClInclude Include="..\..\src\Level\Level.h" />
|
||||||
<ClInclude Include="..\..\src\Level\MapTile.h" />
|
<ClInclude Include="..\..\src\Level\MapTile.h" />
|
||||||
|
<ClInclude Include="..\..\src\Level\Tileset.h" />
|
||||||
<ClInclude Include="..\..\src\Main\Game.h" />
|
<ClInclude Include="..\..\src\Main\Game.h" />
|
||||||
<ClInclude Include="..\..\src\Main\GLWindow.h" />
|
<ClInclude Include="..\..\src\Main\GLWindow.h" />
|
||||||
<ClInclude Include="..\..\src\Math\FPS.h" />
|
<ClInclude Include="..\..\src\Math\FPS.h" />
|
||||||
@ -127,6 +128,7 @@
|
|||||||
<ClCompile Include="..\..\src\IO\Input.cpp" />
|
<ClCompile Include="..\..\src\IO\Input.cpp" />
|
||||||
<ClCompile Include="..\..\src\Level\Layer.cpp" />
|
<ClCompile Include="..\..\src\Level\Layer.cpp" />
|
||||||
<ClCompile Include="..\..\src\Level\Level.cpp" />
|
<ClCompile Include="..\..\src\Level\Level.cpp" />
|
||||||
|
<ClCompile Include="..\..\src\Level\Tileset.cpp" />
|
||||||
<ClCompile Include="..\..\src\Main\Game.cpp" />
|
<ClCompile Include="..\..\src\Main\Game.cpp" />
|
||||||
<ClCompile Include="..\..\src\Main\GLWindow.cpp" />
|
<ClCompile Include="..\..\src\Main\GLWindow.cpp" />
|
||||||
<ClCompile Include="..\..\src\Main\main.cpp" />
|
<ClCompile Include="..\..\src\Main\main.cpp" />
|
||||||
|
@ -147,6 +147,9 @@
|
|||||||
<ClInclude Include="..\..\src\System\ResourceManager.h">
|
<ClInclude Include="..\..\src\System\ResourceManager.h">
|
||||||
<Filter>System</Filter>
|
<Filter>System</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="..\..\src\Level\Tileset.h">
|
||||||
|
<Filter>Level</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClCompile Include="..\..\src\Main\main.cpp">
|
<ClCompile Include="..\..\src\Main\main.cpp">
|
||||||
@ -230,5 +233,8 @@
|
|||||||
<ClCompile Include="..\..\src\Level\Layer.cpp">
|
<ClCompile Include="..\..\src\Level\Layer.cpp">
|
||||||
<Filter>Level</Filter>
|
<Filter>Level</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="..\..\src\Level\Tileset.cpp">
|
||||||
|
<Filter>Level</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
Binary file not shown.
Before Width: | Height: | Size: 23 KiB After Width: | Height: | Size: 23 KiB |
@ -5,7 +5,9 @@ LIBS += -lGL \
|
|||||||
-lSDL_image \
|
-lSDL_image \
|
||||||
-lSDL_gfx \
|
-lSDL_gfx \
|
||||||
-ltinyxml \
|
-ltinyxml \
|
||||||
-lGLU
|
-lGLU \
|
||||||
|
-lz \
|
||||||
|
-ltinyxml
|
||||||
HEADERS += ../src/Actor/Player.h \
|
HEADERS += ../src/Actor/Player.h \
|
||||||
../src/Global/Globals.h \
|
../src/Global/Globals.h \
|
||||||
../src/IO/Input.h \
|
../src/IO/Input.h \
|
||||||
@ -20,7 +22,28 @@ HEADERS += ../src/Actor/Player.h \
|
|||||||
../src/System/ResourceManager.h \
|
../src/System/ResourceManager.h \
|
||||||
../src/Texture/Texture.h \
|
../src/Texture/Texture.h \
|
||||||
../src/Global/Constants.h \
|
../src/Global/Constants.h \
|
||||||
../src/Collision/AABB.h
|
../src/Collision/AABB.h \
|
||||||
|
../src/Level/Level.h \
|
||||||
|
../src/Level/MapTile.h \
|
||||||
|
../src/Level/Map.h \
|
||||||
|
../src/Level/Layer.h \
|
||||||
|
../src/Level/Tileset.h \
|
||||||
|
../src/Level/Layer.h \
|
||||||
|
../src/TMXParser/base64.h \
|
||||||
|
../src/TMXParser/Tmx.h \
|
||||||
|
../src/TMXParser/TmxImage.h \
|
||||||
|
../src/TMXParser/TmxLayer.h \
|
||||||
|
../src/TMXParser/TmxMap.h \
|
||||||
|
../src/TMXParser/TmxMapTile.h \
|
||||||
|
../src/TMXParser/TmxObject.h \
|
||||||
|
../src/TMXParser/TmxObjectGroup.h \
|
||||||
|
../src/TMXParser/TmxPoint.h \
|
||||||
|
../src/TMXParser/TmxPolygon.h \
|
||||||
|
../src/TMXParser/TmxPolyline.h \
|
||||||
|
../src/TMXParser/TmxPropertySet.h \
|
||||||
|
../src/TMXParser/TmxTile.h \
|
||||||
|
../src/TMXParser/TmxTileset.h \
|
||||||
|
../src/TMXParser/TmxUtil.h
|
||||||
SOURCES += ../src/Actor/Player.cpp \
|
SOURCES += ../src/Actor/Player.cpp \
|
||||||
../src/Global/Globals.cpp \
|
../src/Global/Globals.cpp \
|
||||||
../src/IO/Input.cpp \
|
../src/IO/Input.cpp \
|
||||||
@ -33,5 +56,23 @@ SOURCES += ../src/Actor/Player.cpp \
|
|||||||
../src/Sprite/Sprite.cpp \
|
../src/Sprite/Sprite.cpp \
|
||||||
../src/System/Debug.cpp \
|
../src/System/Debug.cpp \
|
||||||
../src/Texture/Texture.cpp \
|
../src/Texture/Texture.cpp \
|
||||||
../src/Collision/AABB.cpp
|
../src/Collision/AABB.cpp \
|
||||||
|
../src/Level/Level.h \
|
||||||
|
../src/Level/MapTile.h \
|
||||||
|
../src/Level/Map.h \
|
||||||
|
../src/Level/Layer.h \
|
||||||
|
../src/Level/Tileset.h \
|
||||||
|
../src/Level/Layer.h \
|
||||||
|
../src/TMXParser/base64.cpp \
|
||||||
|
../src/TMXParser/TmxImage.cpp \
|
||||||
|
../src/TMXParser/TmxLayer.cpp \
|
||||||
|
../src/TMXParser/TmxMap.cpp \
|
||||||
|
../src/TMXParser/TmxObject.cpp \
|
||||||
|
../src/TMXParser/TmxObjectGroup.cpp \
|
||||||
|
../src/TMXParser/TmxPolygon.cpp \
|
||||||
|
../src/TMXParser/TmxPolyline.cpp \
|
||||||
|
../src/TMXParser/TmxPropertySet.cpp \
|
||||||
|
../src/TMXParser/TmxTile.cpp \
|
||||||
|
../src/TMXParser/TmxTileset.cpp \
|
||||||
|
../src/TMXParser/TmxUtil.cpp
|
||||||
OTHER_FILES +=
|
OTHER_FILES +=
|
||||||
|
32
src/Level/Layer.cpp
Normal file
32
src/Level/Layer.cpp
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
#include "Layer.h"
|
||||||
|
#include "Tileset.h"
|
||||||
|
|
||||||
|
Layer::Layer(int width, int height, int tileWidth, int tileHeight) {
|
||||||
|
_width = width;
|
||||||
|
_height = height;
|
||||||
|
_tileWidth = tileWidth;
|
||||||
|
_tileHeight = tileHeight;
|
||||||
|
_tileMap = new MapTile[width * height];
|
||||||
|
}
|
||||||
|
|
||||||
|
Layer::~Layer() {
|
||||||
|
delete[] _tileMap;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Layer::Update(float dt) {
|
||||||
|
for(int x = 0; x < _width; x++) {
|
||||||
|
for(int y = 0; y < _height; y++) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Layer::Draw() {
|
||||||
|
for(int x = 0; x < _width; x++) {
|
||||||
|
for(int y = 0; y < _height; y++) {
|
||||||
|
MapTile& tile = GetTile(x, y);
|
||||||
|
tile.tileset->DrawTile(tile.id, Vec2((float)x * _tileWidth, (float)y * _tileHeight));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
27
src/Level/Layer.h
Normal file
27
src/Level/Layer.h
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "MapTile.h"
|
||||||
|
|
||||||
|
class Layer {
|
||||||
|
public:
|
||||||
|
Layer(int width, int height, int tileWidth, int tileHeight);
|
||||||
|
~Layer();
|
||||||
|
|
||||||
|
void Update(float dt);
|
||||||
|
void Draw();
|
||||||
|
|
||||||
|
int GetWidth() const { return _width; }
|
||||||
|
int GetHeight() const { return _height; }
|
||||||
|
int GetTileWidth() const { return _tileWidth; }
|
||||||
|
int GetTileHeight() const { return _tileHeight; }
|
||||||
|
|
||||||
|
MapTile& GetTile(int x, int y) { return _tileMap[y * _width + x]; }
|
||||||
|
void SetTile(int x, int y, const MapTile& tile) { _tileMap[y * _width + x] = tile; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
int _width;
|
||||||
|
int _height;
|
||||||
|
int _tileWidth;
|
||||||
|
int _tileHeight;
|
||||||
|
MapTile* _tileMap;
|
||||||
|
};
|
85
src/Level/Level.cpp
Normal file
85
src/Level/Level.cpp
Normal file
@ -0,0 +1,85 @@
|
|||||||
|
#include <map>
|
||||||
|
#include "Level.h"
|
||||||
|
#include "Layer.h"
|
||||||
|
#include "Tileset.h"
|
||||||
|
#include "../System/Debug.h"
|
||||||
|
#include "../TMXParser/Tmx.h"
|
||||||
|
|
||||||
|
Level::Level() {
|
||||||
|
_width = 0;
|
||||||
|
_height = 0;
|
||||||
|
_tileWidth = 0;
|
||||||
|
_tileHeight = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
Level::~Level() {
|
||||||
|
for(std::list<Layer*>::iterator i = _layers.begin(); i != _layers.end(); ++i) {
|
||||||
|
delete (*i);
|
||||||
|
}
|
||||||
|
_layers.clear();
|
||||||
|
|
||||||
|
for(std::list<Tileset*>::iterator i = _tilesets.begin(); i != _tilesets.end(); ++i) {
|
||||||
|
delete (*i);
|
||||||
|
}
|
||||||
|
_tilesets.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Level::Load(const std::string& filename) {
|
||||||
|
Tmx::Map map;
|
||||||
|
map.ParseFile(filename);
|
||||||
|
|
||||||
|
if(map.HasError()) {
|
||||||
|
Debug::logger->message("Error while loading level %s: %s\n", filename.c_str(), map.GetErrorText().c_str());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
_width = map.GetWidth();
|
||||||
|
_height = map.GetHeight();
|
||||||
|
_tileWidth = map.GetTileWidth();
|
||||||
|
_tileHeight = map.GetTileHeight();
|
||||||
|
|
||||||
|
std::map<const Tmx::Tileset*, Tileset*> tilesetMap;
|
||||||
|
|
||||||
|
for(int i = 0; i < map.GetNumTilesets(); i++) {
|
||||||
|
const Tmx::Tileset* tmxTileset = map.GetTileset(i);
|
||||||
|
|
||||||
|
Tileset* tileset = new Tileset(_tileWidth, _tileHeight);
|
||||||
|
tileset->LoadImage(map.GetFilepath() + tmxTileset->GetImage()->GetSource());
|
||||||
|
|
||||||
|
_tilesets.push_back(tileset);
|
||||||
|
|
||||||
|
tilesetMap.insert(std::pair<const Tmx::Tileset*, Tileset*>(tmxTileset, tileset));
|
||||||
|
}
|
||||||
|
|
||||||
|
for(int i = 0; i < map.GetNumLayers(); i++) {
|
||||||
|
const Tmx::Layer* tmxLayer = map.GetLayer(i);
|
||||||
|
|
||||||
|
Layer* layer = new Layer(
|
||||||
|
tmxLayer->GetWidth(), tmxLayer->GetHeight(),
|
||||||
|
_tileWidth, _tileHeight);
|
||||||
|
|
||||||
|
for(int x = 0; x < layer->GetWidth(); x++) {
|
||||||
|
for(int y = 0; y < layer->GetHeight(); y++) {
|
||||||
|
Tmx::MapTile tmxTile = tmxLayer->GetTile(x, y);
|
||||||
|
|
||||||
|
const Tmx::Tileset* tmxTileset = map.FindTileset(tmxTile.gid);
|
||||||
|
|
||||||
|
MapTile tile;
|
||||||
|
tile.id = tmxTile.gid - tmxTileset->GetFirstGid();
|
||||||
|
tile.tileset = tilesetMap.find(tmxTileset)->second;
|
||||||
|
|
||||||
|
layer->SetTile(x, y, tile);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_layers.push_back(layer);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Level::Draw() {
|
||||||
|
for(std::list<Layer*>::iterator i = _layers.begin(); i != _layers.end(); ++i) {
|
||||||
|
(*i)->Draw();
|
||||||
|
}
|
||||||
|
}
|
29
src/Level/Level.h
Normal file
29
src/Level/Level.h
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <list>
|
||||||
|
|
||||||
|
class Layer;
|
||||||
|
class Tileset;
|
||||||
|
|
||||||
|
class Level {
|
||||||
|
public:
|
||||||
|
Level();
|
||||||
|
~Level();
|
||||||
|
|
||||||
|
bool Load(const std::string& filename);
|
||||||
|
|
||||||
|
void Update(float dt);
|
||||||
|
void Draw();
|
||||||
|
|
||||||
|
int GetWidth() const { return _width; }
|
||||||
|
int GetHeight() const { return _height; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
int _width;
|
||||||
|
int _height;
|
||||||
|
int _tileWidth;
|
||||||
|
int _tileHeight;
|
||||||
|
std::list<Layer*> _layers;
|
||||||
|
std::list<Tileset*> _tilesets;
|
||||||
|
};
|
8
src/Level/MapTile.h
Normal file
8
src/Level/MapTile.h
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
class Tileset;
|
||||||
|
|
||||||
|
struct MapTile {
|
||||||
|
Tileset* tileset;
|
||||||
|
int id;
|
||||||
|
};
|
41
src/Level/Tileset.cpp
Normal file
41
src/Level/Tileset.cpp
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
#include "Tileset.h"
|
||||||
|
#include "../Sprite/Sprite.h"
|
||||||
|
|
||||||
|
Tileset::Tileset(int tileWidth, int tileHeight) {
|
||||||
|
_tileWidth = tileWidth;
|
||||||
|
_tileHeight = tileHeight;
|
||||||
|
_sprite = NULL;
|
||||||
|
_widthInTiles = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
Tileset::~Tileset() {
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Tileset::LoadImage(const std::string& filename) {
|
||||||
|
if(_sprite) {
|
||||||
|
delete _sprite;
|
||||||
|
}
|
||||||
|
|
||||||
|
_sprite = new Sprite();
|
||||||
|
|
||||||
|
if (!_sprite->LoadSprite(filename)) {
|
||||||
|
delete _sprite;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
_widthInTiles = (int)_sprite->GetWidth() / _tileWidth;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Tileset::DrawTile(int id, const Vec2& position) {
|
||||||
|
_sprite->SetPosition(position);
|
||||||
|
|
||||||
|
Rect sourceRect(
|
||||||
|
float(id % _widthInTiles) * _tileWidth,
|
||||||
|
float(id / _widthInTiles) * _tileHeight,
|
||||||
|
(float)_tileWidth,
|
||||||
|
(float)_tileHeight);
|
||||||
|
|
||||||
|
_sprite->DrawRegion(sourceRect);
|
||||||
|
}
|
22
src/Level/Tileset.h
Normal file
22
src/Level/Tileset.h
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include "../Math/Vec2.h"
|
||||||
|
|
||||||
|
class Sprite;
|
||||||
|
|
||||||
|
class Tileset {
|
||||||
|
public:
|
||||||
|
Tileset(int tileWidth, int tileHeight);
|
||||||
|
~Tileset();
|
||||||
|
|
||||||
|
bool LoadImage(const std::string& filename);
|
||||||
|
|
||||||
|
void DrawTile(int id, const Vec2& position);
|
||||||
|
|
||||||
|
private:
|
||||||
|
int _tileWidth;
|
||||||
|
int _tileHeight;
|
||||||
|
Sprite* _sprite;
|
||||||
|
int _widthInTiles;
|
||||||
|
};
|
@ -9,23 +9,31 @@
|
|||||||
#include "../System/Debug.h"
|
#include "../System/Debug.h"
|
||||||
#include "../Sprite/Sprite.h"
|
#include "../Sprite/Sprite.h"
|
||||||
#include "../Texture/Texture.h"
|
#include "../Texture/Texture.h"
|
||||||
//#include "../Level/Level.h"
|
#include "../Level/Level.h"
|
||||||
#include "Game.h"
|
#include "Game.h"
|
||||||
|
|
||||||
Game::Game(void) {
|
Game::Game(void) {
|
||||||
_player = new Player();
|
_player = new Player();
|
||||||
|
_level = new Level();
|
||||||
//_rotationAngle = 0.0f;
|
//_rotationAngle = 0.0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
Game::~Game(void) {
|
Game::~Game(void) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Game::Init(void) {
|
bool Game::Init(void) {
|
||||||
glEnable(GL_DEPTH_TEST);
|
glEnable(GL_DEPTH_TEST);
|
||||||
glDepthFunc(GL_LEQUAL);
|
glDepthFunc(GL_LEQUAL);
|
||||||
|
|
||||||
|
glEnable(GL_BLEND);
|
||||||
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||||
|
|
||||||
|
glEnable(GL_ALPHA_TEST);
|
||||||
|
glAlphaFunc(GL_GREATER, 0.1f);
|
||||||
|
|
||||||
|
_level->Load("../Data/Map/Ugly.tmx");
|
||||||
_player->Prepare();
|
_player->Prepare();
|
||||||
|
|
||||||
// Return success.
|
// Return success.
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -45,12 +53,14 @@ void Game::Render(void) {
|
|||||||
glLoadIdentity();
|
glLoadIdentity();
|
||||||
|
|
||||||
// Render our shit..
|
// Render our shit..
|
||||||
|
_level->Draw();
|
||||||
_player->Render();
|
_player->Render();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Game::Shutdown(void) {
|
void Game::Shutdown(void) {
|
||||||
Debug::logger->message("\n ----- Cleaning Engine -----");
|
Debug::logger->message("\n ----- Cleaning Engine -----");
|
||||||
delete _player;
|
delete _player;
|
||||||
|
delete _level;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Game::ProcessEvents(void) {
|
void Game::ProcessEvents(void) {
|
||||||
|
@ -36,7 +36,7 @@ void Sprite::DrawRegion(const Rect& src) const {
|
|||||||
// . .
|
// . .
|
||||||
// 3---------2
|
// 3---------2
|
||||||
|
|
||||||
Vec2 scaledSize(size.x*scale.x, size.y*scale.y);
|
Vec2 scaledSize(src.w*scale.x, src.h*scale.y);
|
||||||
|
|
||||||
Vec2 vertices[4] = {
|
Vec2 vertices[4] = {
|
||||||
Vec2(0.0f, 0.0f),
|
Vec2(0.0f, 0.0f),
|
||||||
|
Loading…
Reference in New Issue
Block a user