From 77a90f9c0c0e58d12dc7339fc37b3853f2248fb0 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Tue, 22 Oct 2013 20:19:09 +1100 Subject: [PATCH] Slight refactoring --- common/mapsystem/tile_sheet.cpp | 8 ++------ common/mapsystem/tile_sheet.hpp | 8 +++++--- common/mapsystem/tile_sheet_manager.cpp | 22 +++++++++++++++++++++- common/mapsystem/tile_sheet_manager.hpp | 9 +++------ editor/editor_scene.cpp | 19 ++++++++++--------- editor/editor_scene.hpp | 2 ++ 6 files changed, 43 insertions(+), 25 deletions(-) diff --git a/common/mapsystem/tile_sheet.cpp b/common/mapsystem/tile_sheet.cpp index 434c259..1747998 100644 --- a/common/mapsystem/tile_sheet.cpp +++ b/common/mapsystem/tile_sheet.cpp @@ -23,7 +23,7 @@ #include -SDL_Surface* TileSheet::LoadSurface(std::string fname, Uint16 w, Uint16 h) { +void TileSheet::Load(std::string fname, Uint16 w, Uint16 h) { //setup the image image.LoadSurface(fname); image.SetClipW(w); @@ -39,11 +39,7 @@ SDL_Surface* TileSheet::LoadSurface(std::string fname, Uint16 w, Uint16 h) { end = totalCount; } -SDL_Surface* TileSheet::GetSurface() { - return image.GetSurface(); -} - -void TileSheet::FreeSurface() { +void TileSheet::Unload() { image.FreeSurface(); totalCount = xCount = yCount = 0; begin = end = -1; diff --git a/common/mapsystem/tile_sheet.hpp b/common/mapsystem/tile_sheet.hpp index c872b5f..c127407 100644 --- a/common/mapsystem/tile_sheet.hpp +++ b/common/mapsystem/tile_sheet.hpp @@ -37,9 +37,8 @@ public: //these load/set functions need to be followed by bookkeeping code //w & h are the width & height of individual tiles //TODO: rename these - SDL_Surface* LoadSurface(std::string fname, Uint16 w, Uint16 h); - SDL_Surface* GetSurface(); - void FreeSurface(); + void Load(std::string fname, Uint16 w, Uint16 h); + void Unload(); void DrawTo(SDL_Surface* const dest, int x, int y, int tileIndex); @@ -48,6 +47,9 @@ public: //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; } diff --git a/common/mapsystem/tile_sheet_manager.cpp b/common/mapsystem/tile_sheet_manager.cpp index 07e3bc8..531f8c4 100644 --- a/common/mapsystem/tile_sheet_manager.cpp +++ b/common/mapsystem/tile_sheet_manager.cpp @@ -35,10 +35,30 @@ TileSheet* TileSheetManager::LoadSheet(std::string fname, Uint16 w, Uint16 h) { } //load & setup the sheet object - sheetMap[key].LoadSurface(fname, w, h); + 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) { diff --git a/common/mapsystem/tile_sheet_manager.hpp b/common/mapsystem/tile_sheet_manager.hpp index 954da94..5e81b64 100644 --- a/common/mapsystem/tile_sheet_manager.hpp +++ b/common/mapsystem/tile_sheet_manager.hpp @@ -33,12 +33,9 @@ public: ~TileSheetManager() = default; TileSheet* LoadSheet(std::string fname, Uint16 w, Uint16 h); - TileSheet* GetSheet(std::string name) { - return &sheetMap.at(name); - } - void UnloadSheet(std::string name) { - sheetMap.erase(name); - } + 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); diff --git a/editor/editor_scene.cpp b/editor/editor_scene.cpp index c1e5650..64f4f01 100644 --- a/editor/editor_scene.cpp +++ b/editor/editor_scene.cpp @@ -63,7 +63,7 @@ EditorScene::EditorScene() { pager.SetHeight(32*4); sheetMgr.LoadSheet("rsc\\graphics\\tilesets\\grass.bmp", 32, 32); - sheetMgr.LoadSheet("rsc\\graphics\\tilesets\\longgrass.bmp", 32, 32); + sheetMgr.LoadSheet("rsc\\graphics\\tilesets\\longgrass.bmp", 16, 16); // loadGameMap("rsc\\maps\\mappy", &pager, &sheetList); // saveGameMap("rsc\\maps\\foo", &pager, &sheetList); @@ -145,19 +145,20 @@ void EditorScene::MouseButtonDown(SDL_MouseButtonEvent const& button) { menuBar.MouseButtonDown(button); if (button.button == SDL_BUTTON_LEFT && button.y >= buttonImage.GetClipH()) { - Region* ptr = pager.GetRegion( + Region* regionPtr = pager.GetRegion( snapToBase(pager.GetWidth(), button.x + camera.x), snapToBase(pager.GetHeight(), button.y + camera.y) ); - //TODO: find the tileset matching this value, and then use it's width & height for param 4 & 5 - ptr->NewTileA({ - snapToBase(32, button.x + camera.x), //x - snapToBase(32, button.y + camera.y), //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 - 32, //width (from tileset) - 32, //height (from tileset) - 0 //value + sheetPtr->GetTileW(), //width + sheetPtr->GetTileH(), //height + tileCounter++ //value }); } } diff --git a/editor/editor_scene.hpp b/editor/editor_scene.hpp index 7f3a1ce..6b2f6d2 100644 --- a/editor/editor_scene.hpp +++ b/editor/editor_scene.hpp @@ -71,6 +71,8 @@ protected: struct { int x = 0, y = 0; } camera; + + int tileCounter = 0; }; #endif