diff --git a/common/map_loader.cpp b/common/map_loader.cpp new file mode 100644 index 0000000..34fae2a --- /dev/null +++ b/common/map_loader.cpp @@ -0,0 +1,79 @@ +#include "map_loader.hpp" + +#include "utility.hpp" + +#include +#include + +void loadGameMap(std::string mapPathName, RegionPager* pager, std::list* sheetList) { + //open the index file + std::ifstream indexFile(mapPathName + "\\index"); + if (!indexFile.is_open()) { + throw(std::runtime_error(std::string("Failed to open game map: ") + mapPathName)); + } + + //read each part of the metadata header + std::string buffer, knownName; + int sheetCount = -1, rangeEnd = -1, regionWidth = -1, regionHeight = -1; + + getline(indexFile, knownName); + + if (knownName != truncatePath(mapPathName)) { + //probably not needed, I'll losen this down the road + throw(std::runtime_error("Internal and external map names do not match")); + } + + getline(indexFile, buffer, ','); + sheetCount = to_integer_custom(buffer); + + getline(indexFile, buffer, ','); + rangeEnd = to_integer_custom(buffer); + + getline(indexFile, buffer, ','); + regionWidth = to_integer_custom(buffer); + + getline(indexFile, buffer); + regionHeight = to_integer_custom(buffer); + + //setup the pager + pager->GetRegions()->clear(); + pager->SetWidth(regionWidth); + pager->SetHeight(regionHeight); + pager->SetOnNew([](Region* const ptr) { + //TODO + }); + pager->SetOnDelete([](Region* const ptr) { + //TODO + }); + + //load all of the tile sheets + sheetList->clear(); + for (int i = 0; i < sheetCount; i++) { + sheetList->push_back(TileSheet()); + + //get the name, width & height + std::string sheetName; + getline(indexFile, sheetName, ','); + getline(indexFile, buffer, ','); + int w = to_integer_custom(buffer); + getline(indexFile, buffer, ','); + int h = to_integer_custom(buffer); + + //load + sheetList->back().LoadSurface(std::string("rsc\\graphics\\tilesets\\") + sheetName, w, h); + + //set the range + getline(indexFile, buffer, ','); + sheetList->back().SetBegin(to_integer_custom(buffer)); + getline(indexFile, buffer); + sheetList->back().SetEnd(to_integer_custom(buffer)); + } + TileSheet::SetRangeEnd(rangeEnd); + + //clean up + indexFile.close(); +} + +void saveGameMap(std::string mapPathName, RegionPager* pager, std::list* sheetList) { + //TODO +} \ No newline at end of file diff --git a/common/map_loader.hpp b/common/map_loader.hpp new file mode 100644 index 0000000..9dff832 --- /dev/null +++ b/common/map_loader.hpp @@ -0,0 +1,15 @@ +#ifndef MAPLOADER_HPP_ +#define MAPLOADER_HPP_ + +#include "region_pager.hpp" +#include "tile_sheet.hpp" + +#include +#include + +/* Given the map name and pointers to containers, this is a generic loading system +*/ +void loadGameMap(std::string mapPathName, RegionPager* pager, std::list* sheetList); +void saveGameMap(std::string mapPathName, RegionPager* pager, std::list* sheetList); + +#endif diff --git a/common/region_pager.cpp b/common/region_pager.cpp index a0cdd5a..ee56e48 100644 --- a/common/region_pager.cpp +++ b/common/region_pager.cpp @@ -21,7 +21,10 @@ */ #include "region_pager.hpp" +#include "utility.hpp" + #include +#include RegionPager::RegionPager() { // @@ -110,7 +113,7 @@ void RegionPager::DrawTo(SDL_Surface* const dest, std::list* const sh } //reaching this point without rendering means that the tile is invalid - throw(std::runtime_error("Undrawable tile encountered")); + throw(std::runtime_error(std::string("Undrawable tile encountered: ") + to_string_custom(tileIter.tileIndex))); continueTile: ; //continue with the next tile diff --git a/common/region_pager.hpp b/common/region_pager.hpp index 4e49d13..468a919 100644 --- a/common/region_pager.hpp +++ b/common/region_pager.hpp @@ -47,6 +47,9 @@ public: void SetOnNew(regionCallback_t f) { onNew = f; } void SetOnDelete(regionCallback_t f) { onDelete = f; } + //TODO + //void Prune(int camX, int camY, int screenW, int screenH); + //accessors and mutators int SetWidth(int i) { return regionWidth = i; } int SetHeight(int i) { return regionHeight = i; } diff --git a/common/utility.cpp b/common/utility.cpp index 784e807..4ba5fc1 100644 --- a/common/utility.cpp +++ b/common/utility.cpp @@ -51,3 +51,9 @@ std::string to_string_custom(int i) { snprintf(buffer, 20, "%d", i); return std::string(buffer); } + +int to_integer_custom(std::string s) { + int ret = 0; + sscanf(s.c_str(), "%d", &ret); + return ret; +} \ No newline at end of file diff --git a/common/utility.hpp b/common/utility.hpp index b119271..4bb9ac8 100644 --- a/common/utility.hpp +++ b/common/utility.hpp @@ -27,7 +27,9 @@ int snapToBase(int base, int x); std::string truncatePath(std::string pathname); -//fixing a known bug in g++ +//fixing known bugs in g++ std::string to_string_custom(int i); +int to_integer_custom(std::string); + #endif diff --git a/editor/editor_scene.cpp b/editor/editor_scene.cpp index 98ef772..a699bac 100644 --- a/editor/editor_scene.cpp +++ b/editor/editor_scene.cpp @@ -59,11 +59,20 @@ EditorScene::EditorScene() { // }); //32 * 32 sized tiles - pager.SetWidth(32*4); - pager.SetHeight(32*4); +// pager.SetWidth(32*4); +// pager.SetHeight(32*4); - sheetList.push_front(TileSheet()); - sheetList.front().LoadSurface("rsc\\graphics\\tilesets\\terrain.bmp", 32, 32); +// sheetList.push_front(TileSheet()); +// sheetList.front().LoadSurface("rsc\\graphics\\tilesets\\terrain.bmp", 32, 32); + + loadGameMap("rsc\\maps\\mappy", &pager, &sheetList); + + cout << "Region Width: " << pager.GetWidth() << endl; + cout << "Region Height: " << pager.GetHeight() << endl; + + for (auto& it : sheetList) { + cout << it.GetName() << ": " << it.GetBegin() << ", " << it.GetEnd() << endl; + } } EditorScene::~EditorScene() { @@ -140,13 +149,14 @@ void EditorScene::MouseButtonDown(SDL_MouseButtonEvent const& button) { snapToBase(pager.GetHeight(), button.y - camera.y) ); + //TODO: find the tileset matching this value, and then use it's width & height for param 4 & 5 ptr->NewTileA({ - snapToBase(32, button.x - camera.x), - snapToBase(32, button.y - camera.y), - 0, - 32, - 32, - 0 + snapToBase(32, button.x - camera.x), //x + snapToBase(32, button.y - camera.y), //y + 0, //depth + 32, //width (from tileset) + 32, //height (from tileset) + 0 //value }); } } @@ -156,7 +166,7 @@ void EditorScene::MouseButtonUp(SDL_MouseButtonEvent const& button) { menuBar.MouseButtonUp(button, &entry, &drop); #ifdef DEBUG - cout << "Menu: (" << entry << "," << drop << ")" << endl; + cout << "Menu Bar: (" << entry << "," << drop << ")" << endl; #endif //manage input from the menu bar diff --git a/editor/editor_scene.hpp b/editor/editor_scene.hpp index 7811fbd..95b7414 100644 --- a/editor/editor_scene.hpp +++ b/editor/editor_scene.hpp @@ -31,6 +31,8 @@ #include "raster_font.hpp" #include "menu_bar.hpp" +#include "map_loader.hpp" + #include class EditorScene : public BaseScene {