From eb02cc4b6abfb9aa0048b76d00fe6c5d03301a53 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Tue, 11 Feb 2014 21:56:45 +1100 Subject: [PATCH 01/10] Removed the old map system from the editor --- common/map/region.cpp | 123 ------------------------------ common/map/region.hpp | 112 --------------------------- common/map/region_pager.cpp | 121 ----------------------------- common/map/region_pager.hpp | 69 ----------------- common/map/tile.cpp | 44 ----------- common/map/tile.hpp | 50 ------------ common/map/tile_sheet.cpp | 59 -------------- common/map/tile_sheet.hpp | 70 ----------------- common/map/tile_sheet_manager.cpp | 73 ------------------ common/map/tile_sheet_manager.hpp | 51 ------------- editor/editor_scene.cpp | 56 +------------- editor/editor_scene.hpp | 9 --- editor/testificate_scene.cpp | 20 +---- editor/testificate_scene.hpp | 6 -- 14 files changed, 3 insertions(+), 860 deletions(-) delete mode 100644 common/map/region.cpp delete mode 100644 common/map/region.hpp delete mode 100644 common/map/region_pager.cpp delete mode 100644 common/map/region_pager.hpp delete mode 100644 common/map/tile.cpp delete mode 100644 common/map/tile.hpp delete mode 100644 common/map/tile_sheet.cpp delete mode 100644 common/map/tile_sheet.hpp delete mode 100644 common/map/tile_sheet_manager.cpp delete mode 100644 common/map/tile_sheet_manager.hpp diff --git a/common/map/region.cpp b/common/map/region.cpp deleted file mode 100644 index 190f310..0000000 --- a/common/map/region.cpp +++ /dev/null @@ -1,123 +0,0 @@ -/* Copyright: (c) Kayne Ruse 2013 - * - * 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 "region.hpp" - -#include "utility.hpp" - -#include -#include - -Region::Region(int _x, int _y, int _w, int _h): - x(_x), - y(_y), - width(_w), - height(_h) -{ - //make sure that the region's position lines up - if (x != snapToBase(width, x) || y != snapToBase(height, y)) { - std::ostringstream os; - os << "Region is unaligned; x: " << x << ", y: " << y << ", width: " << width << ", height: " << height; - throw(std::runtime_error(os.str())); - } -} - -int Region::NewTileR(Tile const& tile) { - //return 1 for overwrite, 0 for insert - if (!InBoundsR(tile.x, tile.y)) { - std::ostringstream os; - os << "New tile location out of bounds: " << - "(" << x << "," << y << ")->" << - "(" << tile.x << "," << tile.y << ")" - ; - throw(std::runtime_error(os.str())); - } - - int ret = tiles.erase(tile); - tiles.insert(tile); - return ret; -} - -Tile Region::GetTileR(int tx, int ty, int minDepth) { - std::set::iterator ptr = tiles.begin(); - - //skip the tiles that are too deep - while(ptr != tiles.end()) { - if (ptr->depth >= minDepth) { - break; - } - ptr++; - } - - //find the first tile here - while(ptr != tiles.end()) { - //bounds - if ((ptr->x <= tx) && (ptr->y <= ty) && (ptr->x + ptr->width > tx) && (ptr->y + ptr->height > ty)) { - break; - } - ptr++; - } - - //found it - if (ptr != tiles.end()) { - return *ptr; - } - - //a tileIndex of -1 is an error code, the rest is for show - return {0,0,0,-1,-1,-1}; -} - -int Region::DeleteTileR(Tile const& tile) { - if (!InBoundsR(tile.x, tile.y)) { - throw(std::runtime_error("Deleted tile location out of bounds")); - - std::ostringstream os; - os << "Deleted tile location out of bounds: " << - "(" << x << "," << y << ")->" << - "(" << tile.x << "," << tile.y << ")" - ; - throw(std::runtime_error(os.str())); - } - //sentinel/error code - if (tile.tileIndex == -1) { - return 0; - } - return tiles.erase(tile); -} - -bool operator<(Region const& lhs, Region const& rhs) { - //sort by y -> x - if (lhs.y == rhs.y) { - return lhs.x < rhs.x; - } - return lhs.y < rhs.y; -} - -inline bool operator>(Region const& lhs, Region const& rhs) { - //wrap the other operator - return rhs < lhs; -} - -inline bool operator==(Region const& lhs, Region const& rhs) { - //comparisons work on the location ONLY - //this function is redundant as far as the std::set object is concerned - return (lhs.x == rhs.x) && (lhs.y == rhs.y); -} diff --git a/common/map/region.hpp b/common/map/region.hpp deleted file mode 100644 index eac450f..0000000 --- a/common/map/region.hpp +++ /dev/null @@ -1,112 +0,0 @@ -/* Copyright: (c) Kayne Ruse 2013 - * - * 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 REGION_HPP_ -#define REGION_HPP_ - -#include "tile.hpp" - -#include - -/* A single section of the map. - * This class stores the tiles relative to it's own position, but - * there are functions for referencing the tiles' absolute position. - * These functions simply wrap the normal functions. -*/ -class Region { -public: - Region() = delete; - Region(int x, int y, int width, int height); - ~Region() = default; - - //create and insert a new tile, overwriting an existing tile at that location - int NewTileR(Tile const& tile); - int NewTileA(Tile const& tile) { - //these can change, if the Tile class is changed - return NewTileR({ - tile.x - x, - tile.y - y, - tile.depth, - tile.width, - tile.height, - tile.tileIndex - }); - } - - //find the first tile at this location, with the specified minimum depth - Tile GetTileR(int tx, int ty, int minDepth); - Tile GetTileA(int tx, int ty, int minDepth) { - return GetTileR(tx - x, ty - y, minDepth); - } - - //wrap the other delete functions - int DeleteTileR(int tx, int ty, int minDepth) { - return DeleteTileR(GetTileR(tx, ty, minDepth)); - } - int DeleteTileA(int tx, int ty, int minDepth) { - //explicitly skip one function call by adjusting from A to R - return DeleteTileR(GetTileR(tx - x, ty - y, minDepth)); - } - - //delete the specified tile - int DeleteTileR(Tile const& tile); - int DeleteTileA(Tile const& tile) { - //these can change, if the Tile class is changed - return DeleteTileR({ - tile.x - x, - tile.y - y, - tile.depth, - tile.width, - tile.height, - tile.tileIndex - }); - } - - //find if the specified location exists within the region's bounds - bool InBoundsR(int i, int j) { - return (i >= 0) && (j >= 0) && (i < width) && (j < height); - } - bool InBoundsA(int i, int j) { - return InBoundsR(i - x, j - y); - } - - //Raw accessors & mutators - int GetX() const { return x; } - int GetY() const { return y; } - int GetWidth() const { return width; } - int GetHeight() const { return height; } - - std::set* GetTiles() { return &tiles; } - - //sorting the regions by the locations - friend bool operator<(Region const& lhs, Region const& rhs); - friend bool operator>(Region const& lhs, Region const& rhs); - friend bool operator==(Region const& lhs, Region const& rhs); - -private: - int const x; - int const y; - int const width; - int const height; - std::set tiles; -}; - -#endif diff --git a/common/map/region_pager.cpp b/common/map/region_pager.cpp deleted file mode 100644 index 7e6ed7b..0000000 --- a/common/map/region_pager.cpp +++ /dev/null @@ -1,121 +0,0 @@ -/* Copyright: (c) Kayne Ruse 2013 - * - * 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 "region_pager.hpp" - -#include "utility.hpp" - -#include -#include - -RegionPager::RegionPager() { - // -} - -RegionPager::~RegionPager() { - if (onDelete) { - for (auto& i : regionList) { - onDelete(&i); - } - } -} - -Region* RegionPager::NewRegion(int x, int y) { - for (auto& i : regionList) { - if (i.GetX() == x && i.GetY() == y) { - throw(std::runtime_error("Duplicate Regions detected")); - } - } - - regionList.push_front({x, y, regionWidth, regionHeight}); - if (onNew) { - onNew(®ionList.front()); - } - return ®ionList.front(); -} - -Region* RegionPager::GetRegion(int x, int y) { - for (auto& i : regionList) { - if (i.GetX() == x && i.GetY() == y) { - return &i; - } - } - //create, insert and return - regionList.push_front({x, y, regionWidth, regionHeight}); - if (onNew) { - onNew(®ionList.front()); - } - return ®ionList.front(); -} - -void RegionPager::DeleteRegion(int x, int y) { - for (std::list::iterator i = regionList.begin(); i != regionList.end(); i++) { - if (i->GetX() == x && i->GetY() == y) { - if (onDelete) { - onDelete(&(*i)); - } - regionList.erase(i); - break; - } - } -} - -void RegionPager::DrawTo(SDL_Surface* const dest, TileSheetManager* const sheetMgr, int camX, int camY) { - for (auto& regionIter : regionList) { - -#ifdef DEBUG - //draw the region's location - SDL_Rect box = { - Sint16(regionIter.GetX() - camX), - Sint16(regionIter.GetY() - camY), - Uint16(regionIter.GetWidth()), - Uint16(regionIter.GetHeight()) - }; - SDL_FillRect(dest, &box, SDL_MapRGB(dest->format, 10, 10, 20)); -#endif - - //draw each tile - for (auto& tileIter : *regionIter.GetTiles()) { - sheetMgr->DrawTo( - dest, - tileIter.x + regionIter.GetX() - camX, - tileIter.y + regionIter.GetY() - camY, - tileIter.tileIndex - ); - } - } -} - -void RegionPager::Prune(int left, int top, int right, int bottom) { - std::list::iterator it = regionList.begin(); - - while(it != regionList.end()) { - if (it->GetX() >= right || it->GetY() >= bottom || it->GetX() + it->GetWidth() < left || it->GetY() + it->GetHeight() < top) { - if (onDelete) { - onDelete(&(*it)); - } - regionList.erase(it); - it = regionList.begin(); - continue; - } - it++; - } -} diff --git a/common/map/region_pager.hpp b/common/map/region_pager.hpp deleted file mode 100644 index 54cba33..0000000 --- a/common/map/region_pager.hpp +++ /dev/null @@ -1,69 +0,0 @@ -/* Copyright: (c) Kayne Ruse 2013 - * - * 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 REGIONPAGER_HPP_ -#define REGIONPAGER_HPP_ - -#include "region.hpp" -#include "tile_sheet_manager.hpp" - -#include - -class RegionPager { -public: - //for simplicity and consistency - typedef void (*regionCallback_t)(Region* const); - - RegionPager(); - ~RegionPager(); - - //these parameters MUST be multiples of the width & height - Region* NewRegion(int x, int y); - Region* GetRegion(int x, int y); - void DeleteRegion(int x, int y); - - //call this to draw to the screen - void DrawTo(SDL_Surface* const, TileSheetManager* const, int camX, int camY); - - //callback hooks - void SetOnNew(regionCallback_t f) { onNew = f; } - void SetOnDelete(regionCallback_t f) { onDelete = f; } - - //params: Absolute values - void Prune(int left, int top, int right, int bottom); - - //accessors and mutators - int SetWidth(int i) { return regionWidth = i; } - int SetHeight(int i) { return regionHeight = i; } - - int GetWidth() const { return regionWidth; } - int GetHeight() const { return regionHeight; } - - std::list* GetRegions() { return ®ionList; } -private: - std::list regionList; - int regionWidth = 0, regionHeight = 0; - - regionCallback_t onNew = nullptr; - regionCallback_t onDelete = nullptr; -}; - -#endif diff --git a/common/map/tile.cpp b/common/map/tile.cpp deleted file mode 100644 index fcb2373..0000000 --- a/common/map/tile.cpp +++ /dev/null @@ -1,44 +0,0 @@ -/* Copyright: (c) Kayne Ruse 2013 - * - * 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 "tile.hpp" - -bool operator<(Tile const& lhs, Tile const& rhs) { - //sort by depth -> y -> x - if (lhs.depth == rhs.depth) { - if (lhs.y == rhs.y) { - return lhs.x < rhs.x; - } - return lhs.y < rhs.y; - } - return lhs.depth < rhs.depth; -} - -inline bool operator>(Tile const& lhs, Tile const& rhs) { - //wrap the other operator - return rhs < lhs; -} - -inline bool operator==(Tile const& lhs, Tile const& rhs) { - //comparisons work on the location ONLY - //this function is redundant as far as the std::set object is concerned - return (lhs.x == rhs.x) && (lhs.y == rhs.y) && (lhs.depth == rhs.depth); -} diff --git a/common/map/tile.hpp b/common/map/tile.hpp deleted file mode 100644 index 6e74942..0000000 --- a/common/map/tile.hpp +++ /dev/null @@ -1,50 +0,0 @@ -/* Copyright: (c) Kayne Ruse 2013 - * - * 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 TILE_HPP_ -#define TILE_HPP_ - -//explicitly a POD -struct Tile { - //position relative to the Region - int x, y, depth; - - //graphics - int width, height; - int tileIndex; - - Tile() = default; - Tile(int _x, int _y, int _depth, int _width, int _height, int _tileIndex) { - //The order of the arguments should be explicit - x = _x; - y = _y; - depth = _depth; - width = _width; - height = _height; - tileIndex = _tileIndex; - } -}; - -bool operator<(Tile const& lhs, Tile const& rhs); -bool operator>(Tile const& lhs, Tile const& rhs); -bool operator==(Tile const& lhs, Tile const& rhs); - -#endif diff --git a/common/map/tile_sheet.cpp b/common/map/tile_sheet.cpp deleted file mode 100644 index 1747998..0000000 --- a/common/map/tile_sheet.cpp +++ /dev/null @@ -1,59 +0,0 @@ -/* Copyright: (c) Kayne Ruse 2013 - * - * 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 "tile_sheet.hpp" - -#include - -void TileSheet::Load(std::string fname, Uint16 w, Uint16 h) { - //setup the image - image.LoadSurface(fname); - image.SetClipW(w); - image.SetClipH(h); - - //get the tile counts - xCount = image.GetSurface()->w / w; - yCount = image.GetSurface()->h / h; - totalCount = xCount * yCount; - - //set begin & end (usually temporary) - begin = 0; - end = totalCount; -} - -void TileSheet::Unload() { - image.FreeSurface(); - totalCount = xCount = yCount = 0; - begin = end = -1; -} - -void TileSheet::DrawTo(SDL_Surface* const dest, int x, int y, int tileIndex) { - if (!InRange(tileIndex)) { - throw(std::invalid_argument("Tile index out of range")); - } - Sint16 clipX = (tileIndex-begin) % xCount * image.GetClipW(); - Sint16 clipY = (tileIndex-begin) / xCount * image.GetClipH(); - - image.SetClipX(clipX); - image.SetClipY(clipY); - - image.DrawTo(dest, x, y); -} diff --git a/common/map/tile_sheet.hpp b/common/map/tile_sheet.hpp deleted file mode 100644 index c127407..0000000 --- a/common/map/tile_sheet.hpp +++ /dev/null @@ -1,70 +0,0 @@ -/* Copyright: (c) Kayne Ruse 2013 - * - * 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 TILESHEET_HPP_ -#define TILESHEET_HPP_ - -#include "image.hpp" - -#include - -/* The TileSheet class is used for drawing tiles of the map to the screen. - * This class also tracks the range of the tile images. -*/ -class TileSheet { -public: - TileSheet() = default; - ~TileSheet() = default; - - //these load/set functions need to be followed by bookkeeping code - //w & h are the width & height of individual tiles - //TODO: rename these - void Load(std::string fname, Uint16 w, Uint16 h); - void Unload(); - - void DrawTo(SDL_Surface* const dest, int x, int y, int tileIndex); - - bool InRange(int i) { return i >= begin && i < end; } - - //accessors and mutators - Image* GetImage() { return ℑ } - - int GetTileW() const { return image.GetClipW(); } - int GetTileH() const { return image.GetClipH(); } - - int GetTotalCount() const { return totalCount; } - int GetXCount() const { return xCount; } - int GetYCount() const { return yCount; } - - int SetBegin(int i) { return begin = i; } - int SetEnd(int i) { return end = i; } - - int GetBegin() const { return begin; } - int GetEnd() const { return end; } -private: - Image image; - - //these are generated and used by internal processes - int totalCount = 0, xCount = 0, yCount = 0; - int begin = -1, end = -1; -}; - -#endif diff --git a/common/map/tile_sheet_manager.cpp b/common/map/tile_sheet_manager.cpp deleted file mode 100644 index 531f8c4..0000000 --- a/common/map/tile_sheet_manager.cpp +++ /dev/null @@ -1,73 +0,0 @@ -/* Copyright: (c) Kayne Ruse 2013 - * - * 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 "tile_sheet_manager.hpp" - -#include "utility.hpp" - -#include - -TileSheet* TileSheetManager::LoadSheet(std::string fname, Uint16 w, Uint16 h) { - //get the key - std::string key = truncatePath(fname); - - //don't override what's already here - if (sheetMap.find(key) != sheetMap.end()) { - throw(std::runtime_error("Cannot load duplicate tile sheets")); - } - - //load & setup the sheet object - sheetMap[key].Load(fname, w, h); - sheetMap[key].SetBegin(rangeEnd); - rangeEnd += sheetMap[key].GetTotalCount(); - sheetMap[key].SetEnd(rangeEnd); - - //you can modify the object, say, during the save & load routines... - return &sheetMap[key]; -} - -TileSheet* TileSheetManager::GetSheet(std::string name) { - return &sheetMap.at(name); -} - -TileSheet* TileSheetManager::GetSheetByIndex(int tileIndex) { - for (auto& it : sheetMap) { - if (it.second.InRange(tileIndex)) { - return &it.second; - } - } - return nullptr; -} - -void TileSheetManager::UnloadSheet(std::string name) { - sheetMap.erase(name); -} - -void TileSheetManager::DrawTo(SDL_Surface* const dest, int x, int y, int tileIndex) { - for (auto& it : sheetMap) { - if (it.second.InRange(tileIndex)) { - it.second.DrawTo(dest, x, y, tileIndex); - return; - } - } - //No matching tile index - throw(std::invalid_argument("Tile index is out of range of all tile sheets")); -} diff --git a/common/map/tile_sheet_manager.hpp b/common/map/tile_sheet_manager.hpp deleted file mode 100644 index 5e81b64..0000000 --- a/common/map/tile_sheet_manager.hpp +++ /dev/null @@ -1,51 +0,0 @@ -/* Copyright: (c) Kayne Ruse 2013 - * - * 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 TILESHEETMANAGER_HPP_ -#define TILESHEETMANAGER_HPP_ - -#include "tile_sheet.hpp" - -#include -#include - -class TileSheetManager { -public: - TileSheetManager() = default; - ~TileSheetManager() = default; - - TileSheet* LoadSheet(std::string fname, Uint16 w, Uint16 h); - TileSheet* GetSheet(std::string name); - TileSheet* GetSheetByIndex(int tileIndex); - void UnloadSheet(std::string name); - - void DrawTo(SDL_Surface* const, int x, int y, int tileIndex); - - int SetRangeEnd(int i) { return rangeEnd += i; } - int GetRangeEnd() const { return rangeEnd; } - - std::map* GetSheetMap() { return &sheetMap; } -private: - std::map sheetMap; - int rangeEnd = 0; -}; - -#endif diff --git a/editor/editor_scene.cpp b/editor/editor_scene.cpp index 2d15e55..8d27d6b 100644 --- a/editor/editor_scene.cpp +++ b/editor/editor_scene.cpp @@ -54,21 +54,6 @@ EditorScene::EditorScene(ConfigUtility* const arg1): {"Edit", "-Set Tile", "-Load Sheet", "-Delete Sheet", "-Metadata", "-Run Script"}, {"Debugging", "Debug On", "Debug Off", "Toggle Debug", "Testificate"} }); - - //setup the pager - pager.SetOnNew([](Region* const ptr){ - printf("New Region: %d, %d\n", ptr->GetX(), ptr->GetY()); - }); - - pager.SetOnDelete([](Region* const ptr){ - printf("Delete Region: %d, %d\n", ptr->GetX(), ptr->GetY()); - }); - - //Set a resonable size for the regions - pager.SetWidth(32*4); - pager.SetHeight(32*4); - - sheetMgr.LoadSheet(config["dir.tilesets"] + "terrain.bmp", 32, 32); } EditorScene::~EditorScene() { @@ -84,7 +69,7 @@ void EditorScene::FrameStart() { } void EditorScene::Update(double delta) { - pager.Prune(camera.x, camera.y, camera.x + GetScreen()->w, camera.y + GetScreen()->h); + // } void EditorScene::FrameEnd() { @@ -92,9 +77,6 @@ void EditorScene::FrameEnd() { } void EditorScene::Render(SDL_Surface* const screen) { - //draw the map - pager.DrawTo(screen, &sheetMgr, camera.x, camera.y); - //draw a big bar across the top buttonImage.SetClipY(0); for (int i = 0; i < screen->w; i += buttonImage.GetClipW()) { @@ -130,24 +112,6 @@ void EditorScene::DrawToDebugInfo(std::string str, int line) { void EditorScene::MouseMotion(SDL_MouseMotionEvent const& motion) { menuBar.MouseMotion(motion); - if (motion.state & SDL_BUTTON_LMASK && motion.y >= buttonImage.GetClipH()) { - Region* regionPtr = pager.GetRegion( - snapToBase(pager.GetWidth(), motion.x + camera.x), - snapToBase(pager.GetHeight(), motion.y + camera.y) - ); - - TileSheet* sheetPtr = sheetMgr.GetSheetByIndex(tileCounter); - - regionPtr->NewTileA({ - snapToBase(sheetPtr->GetTileW(), motion.x + camera.x), //x - snapToBase(sheetPtr->GetTileH(), motion.y + camera.y), //y - 0, //depth - sheetPtr->GetTileW(), //width - sheetPtr->GetTileH(), //height - tileCounter++ //value - }); - } - if (motion.state & SDL_BUTTON_RMASK) { camera.x -= motion.xrel; camera.y -= motion.yrel; @@ -156,24 +120,6 @@ void EditorScene::MouseMotion(SDL_MouseMotionEvent const& motion) { void EditorScene::MouseButtonDown(SDL_MouseButtonEvent const& button) { menuBar.MouseButtonDown(button); - - if (button.button == SDL_BUTTON_LEFT && button.y >= buttonImage.GetClipH()) { - Region* regionPtr = pager.GetRegion( - snapToBase(pager.GetWidth(), button.x + camera.x), - snapToBase(pager.GetHeight(), button.y + camera.y) - ); - - TileSheet* sheetPtr = sheetMgr.GetSheetByIndex(tileCounter); - - regionPtr->NewTileA({ - snapToBase(sheetPtr->GetTileW(), button.x + camera.x), //x - snapToBase(sheetPtr->GetTileH(), button.y + camera.y), //y - 0, //depth - sheetPtr->GetTileW(), //width - sheetPtr->GetTileH(), //height - tileCounter++ //value - }); - } } void EditorScene::MouseButtonUp(SDL_MouseButtonEvent const& button) { diff --git a/editor/editor_scene.hpp b/editor/editor_scene.hpp index 1cc872f..a01722b 100644 --- a/editor/editor_scene.hpp +++ b/editor/editor_scene.hpp @@ -24,9 +24,6 @@ #include "base_scene.hpp" -#include "region_pager.hpp" -#include "tile_sheet_manager.hpp" - #include "config_utility.hpp" #include "image.hpp" #include "raster_font.hpp" @@ -62,19 +59,13 @@ protected: Image debugInfo; bool debugOpen = true; - RegionPager pager; - TileSheetManager sheetMgr; - RasterFont font; Image buttonImage; - MenuBar menuBar; struct { int x = 0, y = 0; } camera; - - int tileCounter = 0; }; #endif diff --git a/editor/testificate_scene.cpp b/editor/testificate_scene.cpp index 3baf500..9f451f2 100644 --- a/editor/testificate_scene.cpp +++ b/editor/testificate_scene.cpp @@ -33,18 +33,7 @@ using std::endl; TestificateScene::TestificateScene(ConfigUtility* const arg1): config(*arg1) { - sheetMgr.LoadSheet(config["dir.tilesets"] + "grass.bmp", 32, 32); - sheetMgr.LoadSheet(config["dir.tilesets"] + "longgrass.bmp", 32, 32); - sheetMgr.LoadSheet(config["dir.tilesets"] + "sand.bmp", 32, 32); - sheetMgr.LoadSheet(config["dir.tilesets"] + "dirt.bmp", 32, 32); - sheetMgr.LoadSheet(config["dir.tilesets"] + "water.bmp", 32, 32); - - cout << "Range End: " << sheetMgr.GetRangeEnd() << endl; - - pager.SetWidth(128); - pager.SetHeight(128); - - pager.GetRegion(0, 0)->NewTileR({0, 0, 0, 32, 32, 0}); + // } TestificateScene::~TestificateScene() { @@ -68,12 +57,7 @@ void TestificateScene::FrameEnd() { } void TestificateScene::Render(SDL_Surface* const screen) { - //dump all tile graphics to the screen - for (int i = 0; i < sheetMgr.GetRangeEnd(); i++) { - sheetMgr.DrawTo(screen, i * 32 % screen->w, i * 32 / screen->w * 32, i); - } - -// pager.DrawTo(screen, &sheetMgr, 0, 0); + // } //------------------------- diff --git a/editor/testificate_scene.hpp b/editor/testificate_scene.hpp index 069fca0..dd0a76b 100644 --- a/editor/testificate_scene.hpp +++ b/editor/testificate_scene.hpp @@ -25,8 +25,6 @@ #include "base_scene.hpp" #include "config_utility.hpp" -#include "tile_Sheet_manager.hpp" -#include "region_pager.hpp" class TestificateScene : public BaseScene { public: @@ -50,10 +48,6 @@ protected: //globals ConfigUtility& config; - - //members - TileSheetManager sheetMgr; - RegionPager pager; }; #endif From e706cc9d13ada991fdf3557bb39e7c06b013929a Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Sat, 15 Feb 2014 20:06:20 +1100 Subject: [PATCH 02/10] Completed a simple region class --- common/map/region.cpp | 46 +++++++++++++++++++++++++++++++++++++++ common/map/region.hpp | 50 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 common/map/region.cpp create mode 100644 common/map/region.hpp diff --git a/common/map/region.cpp b/common/map/region.cpp new file mode 100644 index 0000000..b4314cf --- /dev/null +++ b/common/map/region.cpp @@ -0,0 +1,46 @@ +/* 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 "region.hpp" + +#include + +Region::Region(int argWidth, int argHeight, int argDepth, int argX, int argY): + width(argWidth), + height(argHeight), + depth(argDepth), + x(argX), + y(argY) +{ + tiles = static_cast(calloc(width * height * depth, sizeof(int))); +} + +Region::~Region() { + free(tiles); +} + +int Region::SetTile(int x, int y, int z, int v) { + return *(tiles + x*width + y*height + z*depth) = v; +} + +int Region::GetTile(int x, int y, int z) { + return *(tiles + x*width + y*height + z*depth); +} diff --git a/common/map/region.hpp b/common/map/region.hpp new file mode 100644 index 0000000..435f271 --- /dev/null +++ b/common/map/region.hpp @@ -0,0 +1,50 @@ +/* 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 REGION_HPP_ +#define REGION_HPP_ + +class Region { +public: + Region() = delete; + Region(int width, int height, int depth, int x, int y); + ~Region(); + + int SetTile(int x, int y, int z, int v); + int GetTile(int x, int y, int z); + + //accessors + int GetWidth() { return width; } + int GetHeight() { return height; } + int GetDepth() { return depth; } + int GetX() { return width; } + int GetY() { return width; } +private: + const int width; + const int height; + const int depth; + const int x; + const int y; + + int* tiles = nullptr; +}; + +#endif From 808fe570a34b4909a59972be7ee82b03c65fa34b Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Tue, 18 Feb 2014 01:18:04 +1100 Subject: [PATCH 03/10] Added empty RegionPager --- common/map/region_pager.cpp | 62 +++++++++++++++++++++++++++++++++++++ common/map/region_pager.hpp | 56 +++++++++++++++++++++++++++++++++ 2 files changed, 118 insertions(+) create mode 100644 common/map/region_pager.cpp create mode 100644 common/map/region_pager.hpp diff --git a/common/map/region_pager.cpp b/common/map/region_pager.cpp new file mode 100644 index 0000000..af4ad0a --- /dev/null +++ b/common/map/region_pager.cpp @@ -0,0 +1,62 @@ +/* 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 "region_pager.hpp" + +RegionPager::RegionPager(int argWidth, int argHeight, int argDepth): + regionWidth(argWidth), + regionHeight(argHeight), + regionDepth(argDepth) +{ + // +} + +RegionPager::~RegionPager() { + // +} + +int RegionPager::SetTile(int x, int y, int z, int v) { + // +} + +int RegionPager::GetTile(int x, int y, int z) { + // +} + +Region* RegionPager::GetRegion(int x, int y) { + // +} + +void RegionPager::LoadRegion(int x, int y) { + // +} + +void RegionPager::SaveRegion(int x, int y) { + // +} + +void RegionPager::CreateRegion(int x, int y) { + // +} + +void RegionPager::UnloadRegion(int x, int y) { + // +} diff --git a/common/map/region_pager.hpp b/common/map/region_pager.hpp new file mode 100644 index 0000000..fa0576a --- /dev/null +++ b/common/map/region_pager.hpp @@ -0,0 +1,56 @@ +/* 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 REGIONPAGER_HPP_ +#define REGIONPAGER_HPP_ + +#include "region.hpp" + +#include + +class RegionPager { +public: + RegionPager() = delete; + RegionPager(int regionWidth, int regionHeight, int regionDepth); + ~RegionPager(); + + int SetTile(int x, int y, int z, int v); + int GetTile(int x, int y, int z); + + Region* GetRegion(int x, int y); + + void LoadRegion(int x, int y); + void SaveRegion(int x, int y); + void CreateRegion(int x, int y); + void UnloadRegion(int x, int y); + + //accessors + int GetRegionWidth() { return regionWidth; } + int GetRegionHeight() { return regionHeight; } + int GetRegionDepth() { return regionDepth; } +private: + const int regionWidth; + const int regionHeight; + const int regionDepth; + std::list regionList; +}; + +#endif From 31c8bd7fd22ddfd267d1a971aefb7b8a25be3c1c Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Fri, 21 Feb 2014 03:42:02 +1100 Subject: [PATCH 04/10] Split the pager class into two I've also made the base class abstract, and the derived class a template. Hopefully this'll let me work on different parts of the class without major issues. This code compiles, but since it's incomplete, I can't test it properly. --- common/map/region_pager.cpp | 68 +++++++++++++++++++++++-------------- common/map/region_pager.hpp | 54 ++++++++++++++++++++++++----- 2 files changed, 88 insertions(+), 34 deletions(-) diff --git a/common/map/region_pager.cpp b/common/map/region_pager.cpp index af4ad0a..d0a9249 100644 --- a/common/map/region_pager.cpp +++ b/common/map/region_pager.cpp @@ -21,42 +21,60 @@ */ #include "region_pager.hpp" -RegionPager::RegionPager(int argWidth, int argHeight, int argDepth): +#include "utility.hpp" + +#include + +RegionPagerBase::RegionPagerBase(int argWidth, int argHeight, int argDepth): regionWidth(argWidth), regionHeight(argHeight), regionDepth(argDepth) { - // + //EMPTY } -RegionPager::~RegionPager() { - // +RegionPagerBase::~RegionPagerBase() { + //EMPTY } -int RegionPager::SetTile(int x, int y, int z, int v) { - // +int RegionPagerBase::SetTile(int x, int y, int z, int v) { + Region* ptr = GetRegion(snapToBase(regionWidth, x), snapToBase(regionHeight, y)); + return ptr->SetTile(x - ptr->GetX(), y = ptr->GetY(), z, v); } -int RegionPager::GetTile(int x, int y, int z) { - // +int RegionPagerBase::GetTile(int x, int y, int z) { + Region* ptr = GetRegion(snapToBase(regionWidth, x), snapToBase(regionHeight, y)); + return ptr->GetTile(x - ptr->GetX(), y = ptr->GetY(), z); } -Region* RegionPager::GetRegion(int x, int y) { - // +Region* RegionPagerBase::GetRegion(int x, int y) { + //TODO: clean this up + Region* ptr = nullptr; + + //find the loaded region + auto iter = std::find_if(regionList.begin(), regionList.end(), [x,y](Region& it) { + if (it.GetX() == x && it.GetY() == y) + return true; + else + return false; + }); + if (iter != regionList.end()) { //ugly hack + ptr = &(*iter); + } + + //or load the region + if (!ptr) { + ptr = LoadRegion(x, y); + } + + //or create the region + if (!ptr) { + ptr = CreateRegion(x, y); + } + + return ptr; } -void RegionPager::LoadRegion(int x, int y) { - // -} - -void RegionPager::SaveRegion(int x, int y) { - // -} - -void RegionPager::CreateRegion(int x, int y) { - // -} - -void RegionPager::UnloadRegion(int x, int 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 fa0576a..3130922 100644 --- a/common/map/region_pager.hpp +++ b/common/map/region_pager.hpp @@ -26,31 +26,67 @@ #include -class RegionPager { +class RegionPagerBase { public: - RegionPager() = delete; - RegionPager(int regionWidth, int regionHeight, int regionDepth); - ~RegionPager(); + RegionPagerBase() = delete; + RegionPagerBase(int regionWidth, int regionHeight, int regionDepth); + ~RegionPagerBase(); int SetTile(int x, int y, int z, int v); int GetTile(int x, int y, int z); + void Update(); + Region* GetRegion(int x, int y); - void LoadRegion(int x, int y); - void SaveRegion(int x, int y); - void CreateRegion(int x, int y); - void UnloadRegion(int x, int y); + //interface + virtual Region* LoadRegion(int x, int y) = 0; + virtual Region* SaveRegion(int x, int y) = 0; + virtual Region* CreateRegion(int x, int y) = 0; + virtual void UnloadRegion(int x, int y) = 0; //accessors int GetRegionWidth() { return regionWidth; } int GetRegionHeight() { return regionHeight; } int GetRegionDepth() { return regionDepth; } -private: +protected: const int regionWidth; const int regionHeight; const int regionDepth; std::list regionList; }; +template +class RegionPager : public RegionPagerBase { +public: + Region* LoadRegion(int x, int y) { + //TODO + //if there's no region to load, return null + } + + Region* SaveRegion(int x, int y) { + //TODO + //save the region using the functor + } + + Region* CreateRegion(int x, int y) { + //TODO + //create the region + //DON'T call this on a non-zero region, + //or if there's a region saved to the disk + } + + void UnloadRegion(int x, int y) { + //TODO + //free the region, possibly saving it + } + + //accessors + MapGenerator* GetGenerator() { return &generator; } + MapFileFormat* GetFormat() { return &format; } +protected: + MapGenerator generator; + MapFileFormat format; +}; + #endif From 37b02352e2f619a259a75460508917fbc6be47b5 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Sun, 23 Feb 2014 03:08:37 +1100 Subject: [PATCH 05/10] Fleshed out inherited functions TODO: * RegionPagerBase::Update() is a stub * MapFileFormat * MapGenerator * Can replace the calls to std::find_if() with a utility function --- common/map/region_pager.cpp | 14 ++++---- common/map/region_pager.hpp | 69 +++++++++++++++++++++++++++++++------ 2 files changed, 66 insertions(+), 17 deletions(-) diff --git a/common/map/region_pager.cpp b/common/map/region_pager.cpp index d0a9249..39558c0 100644 --- a/common/map/region_pager.cpp +++ b/common/map/region_pager.cpp @@ -38,25 +38,25 @@ RegionPagerBase::~RegionPagerBase() { } int RegionPagerBase::SetTile(int x, int y, int z, int v) { - Region* ptr = GetRegion(snapToBase(regionWidth, x), snapToBase(regionHeight, y)); + Region* ptr = GetRegion(x, y); return ptr->SetTile(x - ptr->GetX(), y = ptr->GetY(), z, v); } int RegionPagerBase::GetTile(int x, int y, int z) { - Region* ptr = GetRegion(snapToBase(regionWidth, x), snapToBase(regionHeight, y)); + Region* ptr = GetRegion(x, y); return ptr->GetTile(x - ptr->GetX(), y = ptr->GetY(), z); } Region* RegionPagerBase::GetRegion(int x, int y) { - //TODO: clean this up + //snap the coords + 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) { - if (it.GetX() == x && it.GetY() == y) - return true; - else - return false; + return it.GetX() == x && it.GetY() == y; }); if (iter != regionList.end()) { //ugly hack ptr = &(*iter); diff --git a/common/map/region_pager.hpp b/common/map/region_pager.hpp index 3130922..9321011 100644 --- a/common/map/region_pager.hpp +++ b/common/map/region_pager.hpp @@ -23,7 +23,9 @@ #define REGIONPAGER_HPP_ #include "region.hpp" +#include "utility.hpp" +#include #include class RegionPagerBase { @@ -59,26 +61,73 @@ protected: template class RegionPager : public RegionPagerBase { public: + RegionPager() = delete; + RegionPager(int w, int h, int d): + RegionPagerBase(w, h, d) + { + //EMPTY + } + ~RegionPager() = default; + Region* LoadRegion(int x, int y) { - //TODO - //if there's no region to load, return null + //snap the coords + x = snapToBase(regionWidth, x); + 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(); + } + return nullptr; } Region* SaveRegion(int x, int y) { - //TODO - //save the region using the functor + //snap the coords + 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); + } + return nullptr; } Region* CreateRegion(int x, int y) { - //TODO - //create the region - //DON'T call this on a non-zero region, - //or if there's a region saved to the disk + //snap the coords + x = snapToBase(regionWidth, x); + y = snapToBase(regionHeight, y); + + //create and push the object + Region* region = nullptr; + generator.Create(®ion); + regionList.push_back(std::move(*region)); + return regionList.back(); } void UnloadRegion(int x, int y) { - //TODO - //free the region, possibly saving it + //snap the coords + 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; + }); + + //pass it to the generator for unloading + if (iter != regionList.end()) { + generator.Unload(&(*iter)); + } } //accessors From 639c0c70e38112d8723b4cb99649449082d64d89 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Mon, 24 Feb 2014 00:27:23 +1100 Subject: [PATCH 06/10] 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 From 91c9cef56de3937859898723d6c7b0bb0c0bb758 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Fri, 28 Feb 2014 03:40:18 +1100 Subject: [PATCH 07/10] Working on the TileSheet class --- common/graphics/tile_sheet.cpp | 41 +++++++++++++++++++++++++++ common/graphics/tile_sheet.hpp | 52 ++++++++++++++++++++++++++++++++++ editor/main.cpp | 18 +----------- 3 files changed, 94 insertions(+), 17 deletions(-) create mode 100644 common/graphics/tile_sheet.cpp create mode 100644 common/graphics/tile_sheet.hpp diff --git a/common/graphics/tile_sheet.cpp b/common/graphics/tile_sheet.cpp new file mode 100644 index 0000000..ef3153d --- /dev/null +++ b/common/graphics/tile_sheet.cpp @@ -0,0 +1,41 @@ +/* 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 "tile_sheet.hpp" + +void TileSheet::Load(std::string fname, int xc, int yc) { + XCount = xc; + YCount = yc; + image.LoadSurface(fname); + image.SetClipW(image.GetClipW()/XCount); + image.SetClipH(image.GetClipH()/YCount); +} + +void TileSheet::Unload() { + image.FreeSurface(); + XCount = YCount = 0; +} + +void TileSheet::DrawTo(SDL_Surface* const dest, int x, int y, int tile) { + image.SetClipX(tile % XCount * image.GetClipW()); + image.SetClipY(tile / XCount * image.GetClipH()); + image.DrawTo(dest, x, y); +} diff --git a/common/graphics/tile_sheet.hpp b/common/graphics/tile_sheet.hpp new file mode 100644 index 0000000..c0ee836 --- /dev/null +++ b/common/graphics/tile_sheet.hpp @@ -0,0 +1,52 @@ +/* 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 TILESHEET_HPP_ +#define TILESHEET_HPP_ + +#include "image.hpp" + +#include + +class TileSheet { +public: + TileSheet() = default; + TileSheet(std::string f, int x, int y) { Load(f, x, y); } + ~TileSheet() = default; + + void Load(std::string fname, int XCount, int YCount); + void Unload(); + + void DrawTo(SDL_Surface* const dest, int x, int y, int tile); + + //accessors + Image* GetImage() { return ℑ } + int GetXCount() { return XCount; } + int GetYCount() { return YCount; } + int GetTileW() { return image.GetClipW(); } + int GetTileH() { return image.GetClipH(); } +private: + Image image; + int XCount = 0, YCount = 0; +}; + +#endif + \ No newline at end of file diff --git a/editor/main.cpp b/editor/main.cpp index f8dc556..3348ec0 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 @@ -39,20 +39,4 @@ 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 From 02d83d1f162e76f03d92def2845252e66843e841 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Fri, 28 Feb 2014 03:41:03 +1100 Subject: [PATCH 08/10] BUG: Region's memory isn't being zeroed by the looks of it --- editor/editor_scene.cpp | 19 ++++++++++++++++++- editor/editor_scene.hpp | 6 +++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/editor/editor_scene.cpp b/editor/editor_scene.cpp index 8d27d6b..4a4c9b4 100644 --- a/editor/editor_scene.cpp +++ b/editor/editor_scene.cpp @@ -34,7 +34,8 @@ using namespace std; //------------------------- EditorScene::EditorScene(ConfigUtility* const arg1): - config(*arg1) + config(*arg1), + region(20, 20, 1, 0, 0) { //create the debugging "window" debugInfo.CreateSurface(256, 256); @@ -54,6 +55,12 @@ EditorScene::EditorScene(ConfigUtility* const arg1): {"Edit", "-Set Tile", "-Load Sheet", "-Delete Sheet", "-Metadata", "-Run Script"}, {"Debugging", "Debug On", "Debug Off", "Toggle Debug", "Testificate"} }); + + //debug + tsheet.Load("rsc\\graphics\\tilesets\\sand.bmp", 12, 3); + cout << region.GetWidth() << endl; + cout << region.GetHeight() << endl; + cout << region.GetDepth() << endl; } EditorScene::~EditorScene() { @@ -77,6 +84,16 @@ void EditorScene::FrameEnd() { } void EditorScene::Render(SDL_Surface* const screen) { + //debug + for (int i = 0; i < region.GetWidth(); i++) { + for (int j = 0; j < region.GetHeight(); j++) { +// for (int k = 0; k < region.GetDepth(); k++) { + cout << region.GetTile(i,j,0) << endl; + tsheet.DrawTo(screen, i*tsheet.GetTileW(), j*tsheet.GetTileH(), region.GetTile(i,j,0)); +// } + } + } + //draw a big bar across the top buttonImage.SetClipY(0); for (int i = 0; i < screen->w; i += buttonImage.GetClipW()) { diff --git a/editor/editor_scene.hpp b/editor/editor_scene.hpp index a01722b..15f4b05 100644 --- a/editor/editor_scene.hpp +++ b/editor/editor_scene.hpp @@ -29,7 +29,8 @@ #include "raster_font.hpp" #include "menu_bar.hpp" -//#include "map_loader.hpp" +#include "region_pager.hpp" +#include "tile_sheet.hpp" class EditorScene : public BaseScene { public: @@ -66,6 +67,9 @@ protected: struct { int x = 0, y = 0; } camera; + + Region region; + TileSheet tsheet; }; #endif From 4629b7302b245ebb23bfbb678ad091d78d1ea6a1 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Fri, 28 Feb 2014 23:14:22 +1100 Subject: [PATCH 09/10] Fixed Region's internals --- common/graphics/tile_sheet.hpp | 1 - common/map/region.cpp | 23 +++++++++++++++++++---- common/map/region.hpp | 2 +- editor/editor_scene.cpp | 20 ++++++++++++-------- 4 files changed, 32 insertions(+), 14 deletions(-) diff --git a/common/graphics/tile_sheet.hpp b/common/graphics/tile_sheet.hpp index c0ee836..ce60989 100644 --- a/common/graphics/tile_sheet.hpp +++ b/common/graphics/tile_sheet.hpp @@ -49,4 +49,3 @@ private: }; #endif - \ No newline at end of file diff --git a/common/map/region.cpp b/common/map/region.cpp index b4314cf..e1d614d 100644 --- a/common/map/region.cpp +++ b/common/map/region.cpp @@ -30,17 +30,32 @@ Region::Region(int argWidth, int argHeight, int argDepth, int argX, int argY): x(argX), y(argY) { - tiles = static_cast(calloc(width * height * depth, sizeof(int))); + tiles = new int**[width]; + for (int i = 0; i < width; ++i) { + tiles[i] = new int*[height]; + for (int j = 0; j < height; ++j) { + tiles[i][j] = new int[depth]; + for (int k = 0; k < depth; ++k) { + tiles[i][j][k] = 0; + } + } + } } Region::~Region() { - free(tiles); + for (int i = 0; i < width; ++i) { + for (int j = 0; j < height; j++) { + delete tiles[i][j]; + } + delete tiles[i]; + } + delete tiles; } int Region::SetTile(int x, int y, int z, int v) { - return *(tiles + x*width + y*height + z*depth) = v; + return tiles[x][y][z] = v; } int Region::GetTile(int x, int y, int z) { - return *(tiles + x*width + y*height + z*depth); + return tiles[x][y][z]; } diff --git a/common/map/region.hpp b/common/map/region.hpp index 7193fed..4d083ad 100644 --- a/common/map/region.hpp +++ b/common/map/region.hpp @@ -44,7 +44,7 @@ private: const int x; const int y; - int* tiles = nullptr; + int*** tiles = nullptr; }; #endif diff --git a/editor/editor_scene.cpp b/editor/editor_scene.cpp index 4a4c9b4..4865b2a 100644 --- a/editor/editor_scene.cpp +++ b/editor/editor_scene.cpp @@ -35,7 +35,7 @@ using namespace std; EditorScene::EditorScene(ConfigUtility* const arg1): config(*arg1), - region(20, 20, 1, 0, 0) + region(20, 20, 3, 0, 0) { //create the debugging "window" debugInfo.CreateSurface(256, 256); @@ -51,8 +51,8 @@ EditorScene::EditorScene(ConfigUtility* const arg1): menuBar.SetImage(&buttonImage); menuBar.SetEntries({ - {"File", "-New", "-Open", "-Save", "-Save As...", "-Close", "Exit"}, - {"Edit", "-Set Tile", "-Load Sheet", "-Delete Sheet", "-Metadata", "-Run Script"}, + {"File", "New", "Open", "Save", "Save As...", "Close", "Exit"}, + {"Edit", "Set Tile", "Load Sheet", "Delete Sheet", "Metadata", "Run Script"}, {"Debugging", "Debug On", "Debug Off", "Toggle Debug", "Testificate"} }); @@ -87,10 +87,14 @@ void EditorScene::Render(SDL_Surface* const screen) { //debug for (int i = 0; i < region.GetWidth(); i++) { for (int j = 0; j < region.GetHeight(); j++) { -// for (int k = 0; k < region.GetDepth(); k++) { - cout << region.GetTile(i,j,0) << endl; - tsheet.DrawTo(screen, i*tsheet.GetTileW(), j*tsheet.GetTileH(), region.GetTile(i,j,0)); -// } + for (int k = 0; k < region.GetDepth(); k++) { + tsheet.DrawTo( + screen, + i*tsheet.GetTileW()+region.GetX()-camera.x, + j*tsheet.GetTileH()+region.GetY()-camera.y, + region.GetTile(i,j,k) + ); + } } } @@ -168,7 +172,7 @@ void EditorScene::MouseButtonUp(SDL_MouseButtonEvent const& button) { break; case 5: { - //Quit + //EXIT SDL_Event e; e.type = SDL_QUIT; SDL_PushEvent(&e); From dc8f594eece14f7fd388e34b1ee953fa09716e41 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Sat, 1 Mar 2014 00:39:39 +1100 Subject: [PATCH 10/10] Tested RegionPager, removed trace statements, fixed logic errors --- common/map/map_file_format.cpp | 4 ---- common/map/map_generator.cpp | 4 ---- common/map/region.cpp | 2 -- common/map/region_pager.cpp | 7 ++----- common/utility.cpp | 4 ++-- editor/editor_scene.cpp | 17 +++++++---------- editor/editor_scene.hpp | 4 +++- 7 files changed, 14 insertions(+), 28 deletions(-) diff --git a/common/map/map_file_format.cpp b/common/map/map_file_format.cpp index ed9f589..ee7f58d 100644 --- a/common/map/map_file_format.cpp +++ b/common/map/map_file_format.cpp @@ -21,14 +21,10 @@ */ #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_generator.cpp b/common/map/map_generator.cpp index 4f5b40b..2668ca5 100644 --- a/common/map/map_generator.cpp +++ b/common/map/map_generator.cpp @@ -21,14 +21,10 @@ */ #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/region.cpp b/common/map/region.cpp index e1d614d..9f4f68e 100644 --- a/common/map/region.cpp +++ b/common/map/region.cpp @@ -21,8 +21,6 @@ */ #include "region.hpp" -#include - Region::Region(int argWidth, int argHeight, int argDepth, int argX, int argY): width(argWidth), height(argHeight), diff --git a/common/map/region_pager.cpp b/common/map/region_pager.cpp index 32d925a..a251ee8 100644 --- a/common/map/region_pager.cpp +++ b/common/map/region_pager.cpp @@ -23,8 +23,6 @@ #include "utility.hpp" -#include - RegionPagerBase::RegionPagerBase(int argWidth, int argHeight, int argDepth): regionWidth(argWidth), regionHeight(argHeight), @@ -34,18 +32,17 @@ RegionPagerBase::RegionPagerBase(int argWidth, int argHeight, int argDepth): } RegionPagerBase::~RegionPagerBase() { - std::cout << "final size: " << regionList.size() << std::endl; //EMPTY } int RegionPagerBase::SetTile(int x, int y, int z, int v) { Region* ptr = GetRegion(x, y); - return ptr->SetTile(x - ptr->GetX(), y = ptr->GetY(), z, v); + return ptr->SetTile(x - ptr->GetX(), y - ptr->GetY(), z, v); } int RegionPagerBase::GetTile(int x, int y, int z) { Region* ptr = GetRegion(x, y); - return ptr->GetTile(x - ptr->GetX(), y = ptr->GetY(), z); + return ptr->GetTile(x - ptr->GetX(), y - ptr->GetY(), z); } Region* RegionPagerBase::GetRegion(int x, int y) { diff --git a/common/utility.cpp b/common/utility.cpp index 4ba5fc1..38c79ef 100644 --- a/common/utility.cpp +++ b/common/utility.cpp @@ -27,9 +27,9 @@ int snapToBase(int base, int x) { //snap to a grid if (x < 0) { x++; - return x - x % base - base; + return x / base * base - base; } - return x - x % base; + return x / base * base; } std::string truncatePath(std::string pathname) { diff --git a/editor/editor_scene.cpp b/editor/editor_scene.cpp index 4865b2a..2709419 100644 --- a/editor/editor_scene.cpp +++ b/editor/editor_scene.cpp @@ -35,7 +35,7 @@ using namespace std; EditorScene::EditorScene(ConfigUtility* const arg1): config(*arg1), - region(20, 20, 3, 0, 0) + pager(20, 20, 3) { //create the debugging "window" debugInfo.CreateSurface(256, 256); @@ -58,9 +58,6 @@ EditorScene::EditorScene(ConfigUtility* const arg1): //debug tsheet.Load("rsc\\graphics\\tilesets\\sand.bmp", 12, 3); - cout << region.GetWidth() << endl; - cout << region.GetHeight() << endl; - cout << region.GetDepth() << endl; } EditorScene::~EditorScene() { @@ -85,14 +82,14 @@ void EditorScene::FrameEnd() { void EditorScene::Render(SDL_Surface* const screen) { //debug - for (int i = 0; i < region.GetWidth(); i++) { - for (int j = 0; j < region.GetHeight(); j++) { - for (int k = 0; k < region.GetDepth(); k++) { + for (int i = 0; i < pager.GetRegionWidth()*2; i++) { + for (int j = 0; j < pager.GetRegionHeight()*2; j++) { + for (int k = 0; k < pager.GetRegionDepth(); k++) { tsheet.DrawTo( screen, - i*tsheet.GetTileW()+region.GetX()-camera.x, - j*tsheet.GetTileH()+region.GetY()-camera.y, - region.GetTile(i,j,k) + i*tsheet.GetTileW()-camera.x, + j*tsheet.GetTileH()-camera.y, + pager.GetTile(i,j,k) ); } } diff --git a/editor/editor_scene.hpp b/editor/editor_scene.hpp index 15f4b05..d8052bd 100644 --- a/editor/editor_scene.hpp +++ b/editor/editor_scene.hpp @@ -30,6 +30,8 @@ #include "menu_bar.hpp" #include "region_pager.hpp" +#include "map_generator.hpp" +#include "map_file_format.hpp" #include "tile_sheet.hpp" class EditorScene : public BaseScene { @@ -68,7 +70,7 @@ protected: int x = 0, y = 0; } camera; - Region region; + RegionPager pager; TileSheet tsheet; };