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 "region_pager.hpp"
#include "utility.hpp"
#include <stdexcept> #include <stdexcept>
#include <string>
RegionPager::RegionPager() { 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 //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: ; continueTile: ;
//continue with the next tile //continue with the next tile
+3
View File
@@ -47,6 +47,9 @@ public:
void SetOnNew(regionCallback_t f) { onNew = f; } void SetOnNew(regionCallback_t f) { onNew = f; }
void SetOnDelete(regionCallback_t f) { onDelete = f; } void SetOnDelete(regionCallback_t f) { onDelete = f; }
//TODO
//void Prune(int camX, int camY, int screenW, int screenH);
//accessors and mutators //accessors and mutators
int SetWidth(int i) { return regionWidth = i; } int SetWidth(int i) { return regionWidth = i; }
int SetHeight(int i) { return regionHeight = 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); snprintf(buffer, 20, "%d", i);
return std::string(buffer); 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); int snapToBase(int base, int x);
std::string truncatePath(std::string pathname); std::string truncatePath(std::string pathname);
//fixing a known bug in g++ //fixing known bugs in g++
std::string to_string_custom(int i); std::string to_string_custom(int i);
int to_integer_custom(std::string);
#endif #endif
+21 -11
View File
@@ -59,11 +59,20 @@ EditorScene::EditorScene() {
// }); // });
//32 * 32 sized tiles //32 * 32 sized tiles
pager.SetWidth(32*4); // pager.SetWidth(32*4);
pager.SetHeight(32*4); // pager.SetHeight(32*4);
sheetList.push_front(TileSheet()); // sheetList.push_front(TileSheet());
sheetList.front().LoadSurface("rsc\\graphics\\tilesets\\terrain.bmp", 32, 32); // 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() { EditorScene::~EditorScene() {
@@ -140,13 +149,14 @@ void EditorScene::MouseButtonDown(SDL_MouseButtonEvent const& button) {
snapToBase(pager.GetHeight(), button.y - camera.y) 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({ ptr->NewTileA({
snapToBase(32, button.x - camera.x), snapToBase(32, button.x - camera.x), //x
snapToBase(32, button.y - camera.y), snapToBase(32, button.y - camera.y), //y
0, 0, //depth
32, 32, //width (from tileset)
32, 32, //height (from tileset)
0 0 //value
}); });
} }
} }
@@ -156,7 +166,7 @@ void EditorScene::MouseButtonUp(SDL_MouseButtonEvent const& button) {
menuBar.MouseButtonUp(button, &entry, &drop); menuBar.MouseButtonUp(button, &entry, &drop);
#ifdef DEBUG #ifdef DEBUG
cout << "Menu: (" << entry << "," << drop << ")" << endl; cout << "Menu Bar: (" << entry << "," << drop << ")" << endl;
#endif #endif
//manage input from the menu bar //manage input from the menu bar
+2
View File
@@ -31,6 +31,8 @@
#include "raster_font.hpp" #include "raster_font.hpp"
#include "menu_bar.hpp" #include "menu_bar.hpp"
#include "map_loader.hpp"
#include <list> #include <list>
class EditorScene : public BaseScene { class EditorScene : public BaseScene {