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:
@@ -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
|
||||
}
|
||||
@@ -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
|
||||
@@ -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
|
||||
|
||||
@@ -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; }
|
||||
|
||||
@@ -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
@@ -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
|
||||
|
||||
+21
-11
@@ -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
|
||||
|
||||
@@ -31,6 +31,8 @@
|
||||
#include "raster_font.hpp"
|
||||
#include "menu_bar.hpp"
|
||||
|
||||
#include "map_loader.hpp"
|
||||
|
||||
#include <list>
|
||||
|
||||
class EditorScene : public BaseScene {
|
||||
|
||||
Reference in New Issue
Block a user