From 0bfd916df42f03208f45126f0fede9bc66939a31 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Thu, 17 Oct 2013 22:18:42 +1100 Subject: [PATCH] Fixed inverted camera, and added RegionPager::Prune() --- common/region_pager.cpp | 26 +++++++++++++++++++++----- common/region_pager.hpp | 4 ++-- editor/editor_scene.cpp | 18 +++++++----------- 3 files changed, 30 insertions(+), 18 deletions(-) diff --git a/common/region_pager.cpp b/common/region_pager.cpp index ee56e48..3394d0f 100644 --- a/common/region_pager.cpp +++ b/common/region_pager.cpp @@ -90,8 +90,8 @@ void RegionPager::DrawTo(SDL_Surface* const dest, std::list* const sh for (auto& regionIter : regionList) { //draw the region's location SDL_Rect box = { - Sint16(regionIter.GetX() + camX), - Sint16(regionIter.GetY() + camY), + Sint16(regionIter.GetX() - camX), + Sint16(regionIter.GetY() - camY), Uint16(regionIter.GetWidth()), Uint16(regionIter.GetHeight()) }; @@ -103,8 +103,8 @@ void RegionPager::DrawTo(SDL_Surface* const dest, std::list* const sh if (sheetIter.InRange(tileIter.tileIndex)) { sheetIter.DrawTo( dest, - tileIter.x + regionIter.GetX() + camX, - tileIter.y + regionIter.GetY() + camY, + tileIter.x + regionIter.GetX() - camX, + tileIter.y + regionIter.GetY() - camY, tileIter.tileIndex ); //skip to the next tile @@ -119,4 +119,20 @@ void RegionPager::DrawTo(SDL_Surface* const dest, std::list* const sh //continue with the next tile } } -} \ No newline at end of file +} + +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/region_pager.hpp b/common/region_pager.hpp index 468a919..c75f58b 100644 --- a/common/region_pager.hpp +++ b/common/region_pager.hpp @@ -47,8 +47,8 @@ public: void SetOnNew(regionCallback_t f) { onNew = f; } void SetOnDelete(regionCallback_t f) { onDelete = f; } - //TODO - //void Prune(int camX, int camY, int screenW, int screenH); + //params: Absolute values + void Prune(int left, int top, int right, int bottom); //accessors and mutators int SetWidth(int i) { return regionWidth = i; } diff --git a/editor/editor_scene.cpp b/editor/editor_scene.cpp index a699bac..1d0fb64 100644 --- a/editor/editor_scene.cpp +++ b/editor/editor_scene.cpp @@ -88,7 +88,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() { @@ -135,8 +135,8 @@ void EditorScene::MouseMotion(SDL_MouseMotionEvent const& motion) { menuBar.MouseMotion(motion); if (motion.state & SDL_BUTTON_RMASK) { - camera.x += motion.xrel; - camera.y += motion.yrel; + camera.x -= motion.xrel; + camera.y -= motion.yrel; } } @@ -145,14 +145,14 @@ void EditorScene::MouseButtonDown(SDL_MouseButtonEvent const& button) { if (button.button == SDL_BUTTON_LEFT && button.y >= buttonImage.GetClipH()) { Region* ptr = pager.GetRegion( - snapToBase(pager.GetWidth(), button.x - camera.x), - snapToBase(pager.GetHeight(), button.y - camera.y) + 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 + snapToBase(32, button.x + camera.x), //x + snapToBase(32, button.y + camera.y), //y 0, //depth 32, //width (from tileset) 32, //height (from tileset) @@ -165,10 +165,6 @@ void EditorScene::MouseButtonUp(SDL_MouseButtonEvent const& button) { int entry, drop; menuBar.MouseButtonUp(button, &entry, &drop); -#ifdef DEBUG - cout << "Menu Bar: (" << entry << "," << drop << ")" << endl; -#endif - //manage input from the menu bar switch(entry) { case 0: //File