RegionPager can now be instanciated with template parameters

* Fixed Region::GetX() and Region::GetY()
* Replaced calls to std::find_if with regular for loops
* Changed typing of RegionPager::regionList
** regionList now holds pointers rather than the objects themselves
* Added vestigial classes MapFileFormat and MapGenerator
** MapGenerator creates and destroys the region objects
* I'm leaving in the debugging code for now
** This includes trace statements and a basterdized main() function in
/editor/main.cpp
This commit is contained in:
Kayne Ruse
2014-02-24 00:27:23 +11:00
parent 37b02352e2
commit 639c0c70e3
8 changed files with 195 additions and 52 deletions
+34
View File
@@ -0,0 +1,34 @@
/* Copyright: (c) Kayne Ruse 2014
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source
* distribution.
*/
#include "map_file_format.hpp"
#include <iostream>
void MapFileFormat::Load(Region** const ptr, int x, int y) {
//TODO
std::cout << "Load" << std::endl;
}
void MapFileFormat::Save(Region* const ptr) {
//TODO
std::cout << "Save" << std::endl;
}
+35
View File
@@ -0,0 +1,35 @@
/* Copyright: (c) Kayne Ruse 2014
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source
* distribution.
*/
#ifndef MAPFILEFORMAT_HPP_
#define MAPFILEFORMAT_HPP_
#include "region.hpp"
class MapFileFormat {
public:
void Load(Region** const, int x, int y);
void Save(Region* const);
private:
//
};
#endif
+34
View File
@@ -0,0 +1,34 @@
/* Copyright: (c) Kayne Ruse 2014
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source
* distribution.
*/
#include "map_generator.hpp"
#include <iostream>
void MapGenerator::Create(Region** const ptr, int width, int height, int depth, int x, int y) {
(*ptr) = new Region(width, height, depth, x, y);
std::cout << "Create" << std::endl;
}
void MapGenerator::Unload(Region* const ptr) {
delete ptr;
std::cout << "Unload" << std::endl;
}
+35
View File
@@ -0,0 +1,35 @@
/* Copyright: (c) Kayne Ruse 2014
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source
* distribution.
*/
#ifndef MAPGENERATOR_HPP_
#define MAPGENERATOR_HPP_
#include "region.hpp"
class MapGenerator {
public:
void Create(Region** const, int width, int height, int depth, int x, int y);
void Unload(Region* const);
private:
//
};
#endif
+2 -2
View File
@@ -35,8 +35,8 @@ public:
int GetWidth() { return width; } int GetWidth() { return width; }
int GetHeight() { return height; } int GetHeight() { return height; }
int GetDepth() { return depth; } int GetDepth() { return depth; }
int GetX() { return width; } int GetX() { return x; }
int GetY() { return width; } int GetY() { return y; }
private: private:
const int width; const int width;
const int height; const int height;
+11 -20
View File
@@ -23,7 +23,7 @@
#include "utility.hpp" #include "utility.hpp"
#include <algorithm> #include <iostream>
RegionPagerBase::RegionPagerBase(int argWidth, int argHeight, int argDepth): RegionPagerBase::RegionPagerBase(int argWidth, int argHeight, int argDepth):
regionWidth(argWidth), regionWidth(argWidth),
@@ -34,6 +34,7 @@ RegionPagerBase::RegionPagerBase(int argWidth, int argHeight, int argDepth):
} }
RegionPagerBase::~RegionPagerBase() { RegionPagerBase::~RegionPagerBase() {
std::cout << "final size: " << regionList.size() << std::endl;
//EMPTY //EMPTY
} }
@@ -52,27 +53,17 @@ Region* RegionPagerBase::GetRegion(int x, int y) {
x = snapToBase(regionWidth, x); x = snapToBase(regionWidth, x);
y = snapToBase(regionHeight, y); y = snapToBase(regionHeight, y);
Region* ptr = nullptr; //find the region
for (std::list<Region*>::iterator it = regionList.begin(); it != regionList.end(); it++) {
//find the loaded region if ((*it)->GetX() == x && (*it)->GetY() == y) {
auto iter = std::find_if(regionList.begin(), regionList.end(), [x,y](Region& it) { return *it;
return it.GetX() == x && it.GetY() == y; }
});
if (iter != regionList.end()) { //ugly hack
ptr = &(*iter);
} }
//or load the region //get the region by other means
if (!ptr) { Region* ptr = LoadRegion(x, y);
ptr = LoadRegion(x, y); if (ptr) return ptr;
} return CreateRegion(x, y);
//or create the region
if (!ptr) {
ptr = CreateRegion(x, y);
}
return ptr;
} }
void RegionPagerBase::Update() { void RegionPagerBase::Update() {
+26 -28
View File
@@ -25,14 +25,13 @@
#include "region.hpp" #include "region.hpp"
#include "utility.hpp" #include "utility.hpp"
#include <algorithm>
#include <list> #include <list>
class RegionPagerBase { class RegionPagerBase {
public: public:
RegionPagerBase() = delete; RegionPagerBase() = delete;
RegionPagerBase(int regionWidth, int regionHeight, int regionDepth); RegionPagerBase(int regionWidth, int regionHeight, int regionDepth);
~RegionPagerBase(); virtual ~RegionPagerBase();
int SetTile(int x, int y, int z, int v); int SetTile(int x, int y, int z, int v);
int GetTile(int x, int y, int z); int GetTile(int x, int y, int z);
@@ -55,7 +54,7 @@ protected:
const int regionWidth; const int regionWidth;
const int regionHeight; const int regionHeight;
const int regionDepth; const int regionDepth;
std::list<Region> regionList; std::list<Region*> regionList;
}; };
template<typename MapGenerator, typename MapFileFormat> template<typename MapGenerator, typename MapFileFormat>
@@ -75,11 +74,11 @@ public:
y = snapToBase(regionHeight, y); y = snapToBase(regionHeight, y);
//load the region if possible //load the region if possible
Region* region = nullptr; Region* ptr = nullptr;
format.Load(&region, x, y); format.Load(&ptr, x, y);
if (region) { if (ptr) {
regionList.push_back(std::move(*region)); regionList.push_back(ptr);
return &regionList.back(); return ptr;
} }
return nullptr; return nullptr;
} }
@@ -89,15 +88,12 @@ public:
x = snapToBase(regionWidth, x); x = snapToBase(regionWidth, x);
y = snapToBase(regionHeight, y); y = snapToBase(regionHeight, y);
//find the specified region //find & save the region
auto iter = std::find_if(regionList.begin(), regionList.end(), [x, y](Region& it){ for (std::list<Region*>::iterator it = regionList.begin(); it != regionList.end(); it++) {
return it.GetX() == x && it.GetY() == y; if ((*it)->GetX() == x && (*it)->GetY() == y) {
}); format.Save(*it);
return *it;
//save the region if it's loaded }
if (iter != regionList.end()) {
format.Save(&(*iter, x, y));
return &(*iter);
} }
return nullptr; return nullptr;
} }
@@ -108,10 +104,10 @@ public:
y = snapToBase(regionHeight, y); y = snapToBase(regionHeight, y);
//create and push the object //create and push the object
Region* region = nullptr; Region* ptr = nullptr;
generator.Create(&region); generator.Create(&ptr, regionWidth, regionHeight, regionDepth, x, y);
regionList.push_back(std::move(*region)); regionList.push_back(ptr);
return regionList.back(); return ptr;
} }
void UnloadRegion(int x, int y) { void UnloadRegion(int x, int y) {
@@ -119,14 +115,16 @@ public:
x = snapToBase(regionWidth, x); x = snapToBase(regionWidth, x);
y = snapToBase(regionHeight, y); y = snapToBase(regionHeight, y);
//find the specified region for (std::list<Region*>::iterator it = regionList.begin(); it != regionList.end(); /* EMPTY */) {
auto iter = std::find_if(regionList.begin(), regionList.end(), [x, y](Region& it){ if ((*it)->GetX() == x && (*it)->GetY() == y) {
return it.GetX() == x && it.GetY() == y; generator.Unload(*it);
}); regionList.erase(it);
//pass it to the generator for unloading //reset the loop, because of reasons
if (iter != regionList.end()) { it = regionList.begin();
generator.Unload(&(*iter)); continue;
}
++it;
} }
} }
+16
View File
@@ -40,3 +40,19 @@ int main(int, char**) {
cout << "Clean exit" << endl; cout << "Clean exit" << endl;
return 0; return 0;
} }
*/
#include "region_pager.hpp"
#include "map_file_format.hpp"
#include "map_generator.hpp"
#include <iostream>
int main() {
RegionPager<MapGenerator, MapFileFormat> pager(40, 40, 3);
pager.CreateRegion(0, 0);
pager.SaveRegion(0, 0);
pager.LoadRegion(0, 0);
pager.UnloadRegion(0, 0);
std::cout << "Finishing program" << std::endl;
return 0;
}