diff --git a/.gitignore b/.gitignore index 625fde3..a529989 100644 --- a/.gitignore +++ b/.gitignore @@ -8,4 +8,5 @@ cmake_install.cmake *.dll Makefile saland +!src/saland nbproject diff --git a/src/sagotmx/tmx_struct.h b/src/sagotmx/tmx_struct.h index 2317c62..8bad658 100644 --- a/src/sagotmx/tmx_struct.h +++ b/src/sagotmx/tmx_struct.h @@ -104,7 +104,6 @@ inline std::string zlib_decompress(const char *source, unsigned int slength) { while (ret == Z_OK) { ret = inflate(&strm, Z_NO_FLUSH); - std::cout << "Read some\n"; if (ret == Z_OK || ret == Z_STREAM_END) { res.append(buffer, sizeof(buffer)); strm.next_out = (Bytef*)buffer; diff --git a/src/saland/Game.cpp b/src/saland/Game.cpp new file mode 100644 index 0000000..b79502f --- /dev/null +++ b/src/saland/Game.cpp @@ -0,0 +1,57 @@ +#include "Game.hpp" +#include "../sagotmx/tmx_struct.h" +#include "../sago/SagoMisc.hpp" +#include "globals.hpp" + +static void Draw(SDL_Renderer* target, SDL_Texture* t, int x, int y, const SDL_Rect& part) { + SDL_Rect pos = {}; + pos.x = x; + pos.y = y; + pos.w = 32; + pos.h = 32; + SDL_RenderCopy(target, t, &part, &pos); +} + +static void DrawLayer(SDL_Renderer* renderer, SDL_Texture* texture, const sago::tiled::TileMap& tm, size_t layer) { + for (int i = 0; i < tm.height; ++i) { + for (int j = 0; j < tm.width; ++j) { + uint32_t gid = sago::tiled::getTileFromLayer(tm, tm.layers.at(layer), i, j); + if (gid == 0) { + continue; + } + SDL_Rect part{}; + getTextureLocationFromGid(tm, gid, nullptr, &part.x, &part.y, &part.w, &part.h); + Draw(renderer, texture, 32*i, 32*j, part); + } + } +} + +Game::Game() { + std::string tsx_file = sago::GetFileContent("maps/terrain.tsx"); + std::string tmx_file = sago::GetFileContent("maps/sample1.tmx"); + ts = sago::tiled::string2tileset(tsx_file); + tm = sago::tiled::string2tilemap(tmx_file); + tm.tileset.alternativeSource = &ts; +} + +Game::~Game() { +} + +bool Game::IsActive() { + return true; +} + +void Game::Draw(SDL_Renderer* target) { + SDL_Texture* texture = globalData.spriteHolder->GetDataHolder().getTexturePtr("terrain"); + for (size_t i = 0; i < tm.layers.size(); ++i ) { + DrawLayer(target, texture, tm, i); + } +} + +void Game::ProcessInput(const SDL_Event& event, bool& processed) { + +} + +void Game::Update() { + +} diff --git a/src/saland/Game.hpp b/src/saland/Game.hpp new file mode 100644 index 0000000..f74db04 --- /dev/null +++ b/src/saland/Game.hpp @@ -0,0 +1,21 @@ +#ifndef GAME_HPP +#define GAME_HPP + +#include "../sago/GameStateInterface.hpp" +#include "../sagotmx/tmx_struct.h" + +class Game : public sago::GameStateInterface { +public: + Game(); + virtual bool IsActive() override; + virtual void Draw(SDL_Renderer* target) override; + virtual void ProcessInput(const SDL_Event& event, bool &processed) override; + virtual void Update() override; + virtual ~Game(); +private: + sago::tiled::TileSet ts; + sago::tiled::TileMap tm; +}; + +#endif /* GAME_HPP */ + diff --git a/src/saland/globals.hpp b/src/saland/globals.hpp new file mode 100644 index 0000000..d6338c1 --- /dev/null +++ b/src/saland/globals.hpp @@ -0,0 +1,18 @@ +#ifndef GLOBALS_HPP +#define GLOBALS_HPP + +#include "../Libs/NFont.h" +#include "../sago/SagoSpriteHolder.hpp" +#include + +struct GlobalData { + NFont nf_standard_font; + bool isShuttingDown = false; + SDL_Renderer* screen = nullptr; + std::unique_ptr spriteHolder; +}; + +extern GlobalData globalData; + +#endif /* GLOBALS_HPP */ +