From 639c0c70e38112d8723b4cb99649449082d64d89 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Mon, 24 Feb 2014 00:27:23 +1100 Subject: [PATCH] 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 --- common/map/map_file_format.cpp | 34 +++++++++++++++++++++ common/map/map_file_format.hpp | 35 ++++++++++++++++++++++ common/map/map_generator.cpp | 34 +++++++++++++++++++++ common/map/map_generator.hpp | 35 ++++++++++++++++++++++ common/map/region.hpp | 4 +-- common/map/region_pager.cpp | 33 ++++++++------------- common/map/region_pager.hpp | 54 ++++++++++++++++------------------ editor/main.cpp | 18 +++++++++++- 8 files changed, 195 insertions(+), 52 deletions(-) create mode 100644 common/map/map_file_format.cpp create mode 100644 common/map/map_file_format.hpp create mode 100644 common/map/map_generator.cpp create mode 100644 common/map/map_generator.hpp diff --git a/common/map/map_file_format.cpp b/common/map/map_file_format.cpp new file mode 100644 index 0000000..ed9f589 --- /dev/null +++ b/common/map/map_file_format.cpp @@ -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 + +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; +} diff --git a/common/map/map_file_format.hpp b/common/map/map_file_format.hpp new file mode 100644 index 0000000..546003d --- /dev/null +++ b/common/map/map_file_format.hpp @@ -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 diff --git a/common/map/map_generator.cpp b/common/map/map_generator.cpp new file mode 100644 index 0000000..4f5b40b --- /dev/null +++ b/common/map/map_generator.cpp @@ -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 + +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; +} diff --git a/common/map/map_generator.hpp b/common/map/map_generator.hpp new file mode 100644 index 0000000..85121b9 --- /dev/null +++ b/common/map/map_generator.hpp @@ -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 diff --git a/common/map/region.hpp b/common/map/region.hpp index 435f271..7193fed 100644 --- a/common/map/region.hpp +++ b/common/map/region.hpp @@ -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; diff --git a/common/map/region_pager.cpp b/common/map/region_pager.cpp index 39558c0..32d925a 100644 --- a/common/map/region_pager.cpp +++ b/common/map/region_pager.cpp @@ -23,7 +23,7 @@ #include "utility.hpp" -#include +#include 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::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 -} \ No newline at end of file +} diff --git a/common/map/region_pager.hpp b/common/map/region_pager.hpp index 9321011..30c6f64 100644 --- a/common/map/region_pager.hpp +++ b/common/map/region_pager.hpp @@ -25,14 +25,13 @@ #include "region.hpp" #include "utility.hpp" -#include #include 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 regionList; + std::list regionList; }; template @@ -75,11 +74,11 @@ public: y = snapToBase(regionHeight, y); //load the region if possible - Region* region = nullptr; - format.Load(®ion, x, y); - if (region) { - regionList.push_back(std::move(*region)); - return ®ionList.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::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(®ion); - 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::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; } } diff --git a/editor/main.cpp b/editor/main.cpp index c244c1e..f8dc556 100644 --- a/editor/main.cpp +++ b/editor/main.cpp @@ -18,7 +18,7 @@ * * 3. This notice may not be removed or altered from any source * distribution. -*/ +* / #include "editor_application.hpp" #include @@ -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 + +int main() { + RegionPager 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; +} \ No newline at end of file