Created the loadGameMap() function, still incomplete

Although the overall logic of this function is finished, I still need to
write the callbacks for RegionPager's onNew and onDelete.

I've also tested this using a hand written save/index file. I've written
up a map file format by hand, and I'll be implementing it over the next
few commits.
This commit is contained in:
Kayne Ruse
2013-10-14 21:12:09 +11:00
parent 0d3c3243a0
commit 88aee0f4f5
8 changed files with 133 additions and 13 deletions
+79
View File
@@ -0,0 +1,79 @@
#include "map_loader.hpp"
#include "utility.hpp"
#include <fstream>
#include <stdexcept>
void loadGameMap(std::string mapPathName, RegionPager* pager, std::list<TileSheet>* 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<TileSheet>* sheetList) {
//TODO
}
+15
View File
@@ -0,0 +1,15 @@
#ifndef MAPLOADER_HPP_
#define MAPLOADER_HPP_
#include "region_pager.hpp"
#include "tile_sheet.hpp"
#include <list>
#include <string>
/* Given the map name and pointers to containers, this is a generic loading system
*/
void loadGameMap(std::string mapPathName, RegionPager* pager, std::list<TileSheet>* sheetList);
void saveGameMap(std::string mapPathName, RegionPager* pager, std::list<TileSheet>* sheetList);
#endif
+4 -1
View File
@@ -21,7 +21,10 @@
*/
#include "region_pager.hpp"
#include "utility.hpp"
#include <stdexcept>
#include <string>
RegionPager::RegionPager() {
//
@@ -110,7 +113,7 @@ void RegionPager::DrawTo(SDL_Surface* const dest, std::list<TileSheet>* 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
+3
View File
@@ -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; }
+6
View File
@@ -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;
}
+3 -1
View File
@@ -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