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 GetHeight() { return height; }
int GetDepth() { return depth; }
int GetX() { return width; }
int GetY() { return width; }
int GetX() { return x; }
int GetY() { return y; }
private:
const int width;
const int height;
+12 -21
View File
@@ -23,7 +23,7 @@
#include "utility.hpp"
#include <algorithm>
#include <iostream>
RegionPagerBase::RegionPagerBase(int argWidth, int argHeight, int argDepth):
regionWidth(argWidth),
@@ -34,6 +34,7 @@ RegionPagerBase::RegionPagerBase(int argWidth, int argHeight, int argDepth):
}
RegionPagerBase::~RegionPagerBase() {
std::cout << "final size: " << regionList.size() << std::endl;
//EMPTY
}
@@ -52,29 +53,19 @@ Region* RegionPagerBase::GetRegion(int x, int y) {
x = snapToBase(regionWidth, x);
y = snapToBase(regionHeight, y);
Region* ptr = nullptr;
//find the loaded region
auto iter = std::find_if(regionList.begin(), regionList.end(), [x,y](Region& it) {
return it.GetX() == x && it.GetY() == y;
});
if (iter != regionList.end()) { //ugly hack
ptr = &(*iter);
//find the region
for (std::list<Region*>::iterator it = regionList.begin(); it != regionList.end(); it++) {
if ((*it)->GetX() == x && (*it)->GetY() == y) {
return *it;
}
}
//or load the region
if (!ptr) {
ptr = LoadRegion(x, y);
}
//or create the region
if (!ptr) {
ptr = CreateRegion(x, y);
}
return ptr;
//get the region by other means
Region* ptr = LoadRegion(x, y);
if (ptr) return ptr;
return CreateRegion(x, y);
}
void RegionPagerBase::Update() {
//TODO
}
}
+26 -28
View File
@@ -25,14 +25,13 @@
#include "region.hpp"
#include "utility.hpp"
#include <algorithm>
#include <list>
class RegionPagerBase {
public:
RegionPagerBase() = delete;
RegionPagerBase(int regionWidth, int regionHeight, int regionDepth);
~RegionPagerBase();
virtual ~RegionPagerBase();
int SetTile(int x, int y, int z, int v);
int GetTile(int x, int y, int z);
@@ -55,7 +54,7 @@ protected:
const int regionWidth;
const int regionHeight;
const int regionDepth;
std::list<Region> regionList;
std::list<Region*> regionList;
};
template<typename MapGenerator, typename MapFileFormat>
@@ -75,11 +74,11 @@ public:
y = snapToBase(regionHeight, y);
//load the region if possible
Region* region = nullptr;
format.Load(&region, x, y);
if (region) {
regionList.push_back(std::move(*region));
return &regionList.back();
Region* ptr = nullptr;
format.Load(&ptr, x, y);
if (ptr) {
regionList.push_back(ptr);
return ptr;
}
return nullptr;
}
@@ -89,15 +88,12 @@ public:
x = snapToBase(regionWidth, x);
y = snapToBase(regionHeight, y);
//find the specified region
auto iter = std::find_if(regionList.begin(), regionList.end(), [x, y](Region& it){
return it.GetX() == x && it.GetY() == y;
});
//save the region if it's loaded
if (iter != regionList.end()) {
format.Save(&(*iter, x, y));
return &(*iter);
//find & save the region
for (std::list<Region*>::iterator it = regionList.begin(); it != regionList.end(); it++) {
if ((*it)->GetX() == x && (*it)->GetY() == y) {
format.Save(*it);
return *it;
}
}
return nullptr;
}
@@ -108,10 +104,10 @@ public:
y = snapToBase(regionHeight, y);
//create and push the object
Region* region = nullptr;
generator.Create(&region);
regionList.push_back(std::move(*region));
return regionList.back();
Region* ptr = nullptr;
generator.Create(&ptr, regionWidth, regionHeight, regionDepth, x, y);
regionList.push_back(ptr);
return ptr;
}
void UnloadRegion(int x, int y) {
@@ -119,14 +115,16 @@ public:
x = snapToBase(regionWidth, x);
y = snapToBase(regionHeight, y);
//find the specified region
auto iter = std::find_if(regionList.begin(), regionList.end(), [x, y](Region& it){
return it.GetX() == x && it.GetY() == y;
});
for (std::list<Region*>::iterator it = regionList.begin(); it != regionList.end(); /* EMPTY */) {
if ((*it)->GetX() == x && (*it)->GetY() == y) {
generator.Unload(*it);
regionList.erase(it);
//pass it to the generator for unloading
if (iter != regionList.end()) {
generator.Unload(&(*iter));
//reset the loop, because of reasons
it = regionList.begin();
continue;
}
++it;
}
}
+17 -1
View File
@@ -18,7 +18,7 @@
*
* 3. This notice may not be removed or altered from any source
* distribution.
*/
* /
#include "editor_application.hpp"
#include <stdexcept>
@@ -40,3 +40,19 @@ int main(int, char**) {
cout << "Clean exit" << endl;
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;
}