From a88a1f7cf7e10431fc536652c76581b70fc65f13 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Fri, 4 Oct 2013 00:34:57 +1000 Subject: [PATCH] Systems are working correctly, but needs cleaning --- editor/editor_scene.cpp | 73 +++++++++++++++++++++++++++++++++++++++-- editor/editor_scene.hpp | 12 +++++++ editor/tile_sheet.cpp | 19 ++++------- editor/tile_sheet.hpp | 1 - 4 files changed, 88 insertions(+), 17 deletions(-) diff --git a/editor/editor_scene.cpp b/editor/editor_scene.cpp index 53bfd65..0c11be8 100644 --- a/editor/editor_scene.cpp +++ b/editor/editor_scene.cpp @@ -21,12 +21,47 @@ */ #include "editor_scene.hpp" +#include "utility.hpp" + +#include +#include + +using namespace std; + //------------------------- //Public access members //------------------------- EditorScene::EditorScene() { - // + //32 * 32 sized tiles + pager.SetWidth(32*4); + pager.SetHeight(32*4); + + 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()); + }); + + sheetList.push_front(TileSheet()); + TileSheet* sheetOne = &sheetList.front(); + sheetList.push_front(TileSheet()); + TileSheet* sheetTwo = &sheetList.front(); + + sheetOne->LoadSurface("rsc\\terrain.bmp", 32, 32); + sheetTwo->LoadSurface("rsc\\terrain.bmp", 32, 32); + + cout << "--Sheet debug data--" << endl; + cout << "TileSheet::rangeEnd: " << TileSheet::GetRangeEnd() << endl; + cout << "sheetOne.totalCount: " << sheetOne->GetTotalCount() << endl; + cout << "sheetOne.begin: " << sheetOne->GetBegin() << endl; + cout << "sheetOne.end: " << sheetOne->GetEnd() << endl; + cout << "sheetTwo.totalCount: " << sheetTwo->GetTotalCount() << endl; + cout << "sheetTwo.begin: " << sheetTwo->GetBegin() << endl; + cout << "sheetTwo.end: " << sheetTwo->GetEnd() << endl; + cout << "--end debug data--" << endl; } EditorScene::~EditorScene() { @@ -50,7 +85,15 @@ void EditorScene::FrameEnd() { } void EditorScene::Render(SDL_Surface* const screen) { - // + for (auto& regionIter : *pager.GetRegions()) { + for (auto& tileIter : *regionIter.GetTiles()) { + for (auto& sheetIter : sheetList) { + if (sheetIter.InRange(tileIter.tileIndex)) { + sheetIter.DrawTo(screen, tileIter.x + regionIter.GetX(), tileIter.y + regionIter.GetY(), tileIter.tileIndex); + } + } + } + } } //------------------------- @@ -62,7 +105,31 @@ void EditorScene::MouseMotion(SDL_MouseMotionEvent const& motion) { } void EditorScene::MouseButtonDown(SDL_MouseButtonEvent const& button) { - // + switch(button.button) { + case SDL_BUTTON_LEFT: { + Region* ptr = pager.GetRegion( + snapToBase(pager.GetWidth(), button.x), + snapToBase(pager.GetHeight(), button.y) + ); + + cout << "New Tile: " << + ptr->NewTileA({ + snapToBase(32, button.x), + snapToBase(32, button.y), + 0, + 32, + 32, + incrementer + }) + << endl; + ; + + if (++incrementer >= 180) { + incrementer = 0; + } + } + + } } void EditorScene::MouseButtonUp(SDL_MouseButtonEvent const& button) { diff --git a/editor/editor_scene.hpp b/editor/editor_scene.hpp index 38ca16c..655dfdc 100644 --- a/editor/editor_scene.hpp +++ b/editor/editor_scene.hpp @@ -24,6 +24,11 @@ #include "base_scene.hpp" +#include "region_pager.hpp" +#include "tile_sheet.hpp" + +#include + class EditorScene : public BaseScene { public: //Public access members @@ -43,6 +48,13 @@ protected: void MouseButtonUp(SDL_MouseButtonEvent const&); void KeyDown(SDL_KeyboardEvent const&); void KeyUp(SDL_KeyboardEvent const&); + + //members + RegionPager pager; + std::list sheetList; + + //debugging + int incrementer = 0; }; #endif diff --git a/editor/tile_sheet.cpp b/editor/tile_sheet.cpp index 46cf8a3..3124cc0 100644 --- a/editor/tile_sheet.cpp +++ b/editor/tile_sheet.cpp @@ -40,18 +40,11 @@ SDL_Surface* TileSheet::LoadSurface(std::string fname, Uint16 w, Uint16 h) { //cache the name name = truncatePath(fname); -} -SDL_Surface* TileSheet::SetSurface(SDL_Surface* const ptr, Uint16 w, Uint16 h) { - //setup the image - image.SetSurface(ptr); - image.SetClipW(w); - image.SetClipH(h); - - //get the tile counts - xCount = image.GetSurface()->w / w; - yCount = image.GetSurface()->h / h; - totalCount = xCount * yCount; + //set the range + begin = rangeEnd; + end = begin + totalCount; + rangeEnd += totalCount; } SDL_Surface* TileSheet::GetSurface() { @@ -69,8 +62,8 @@ void TileSheet::DrawTo(SDL_Surface* const dest, int x, int y, int tileIndex) { if (!InRange(tileIndex)) { throw(std::runtime_error("Tile index out of range of Tile Sheet")); } - Sint16 clipX = tileIndex % xCount * image.GetClipW(); - Sint16 clipY = tileIndex / xCount * image.GetClipH(); + Sint16 clipX = (tileIndex-begin) % xCount * image.GetClipW(); + Sint16 clipY = (tileIndex-begin) / xCount * image.GetClipH(); image.SetClipX(clipX); image.SetClipY(clipY); diff --git a/editor/tile_sheet.hpp b/editor/tile_sheet.hpp index bcb47f1..9f55be2 100644 --- a/editor/tile_sheet.hpp +++ b/editor/tile_sheet.hpp @@ -37,7 +37,6 @@ public: //these load/set functions need to be followed by bookkeeping code //w & h are the width & height of individual tiles SDL_Surface* LoadSurface(std::string fname, Uint16 w, Uint16 h); - SDL_Surface* SetSurface(SDL_Surface* const, Uint16 w, Uint16 h); SDL_Surface* GetSurface(); void FreeSurface();