From 5d0f9e1bb8d950b199ed8c2f24f9422f0c6e30d4 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Fri, 8 Apr 2016 10:48:01 +1000 Subject: [PATCH] Barriers are created in client-side BarrierManager Still need to hookup a barrier query, and get them drawn to the screen. --- client/barrier_manager.cpp | 81 ++++++++++++++++++++++++++++++++ client/barrier_manager.hpp | 55 ++++++++++++++++++++++ client/entities/base_barrier.cpp | 4 +- client/entities/base_barrier.hpp | 4 +- client/scenes/world.cpp | 77 ++++++++++++++++++------------ client/scenes/world.hpp | 3 +- 6 files changed, 190 insertions(+), 34 deletions(-) create mode 100644 client/barrier_manager.cpp create mode 100644 client/barrier_manager.hpp diff --git a/client/barrier_manager.cpp b/client/barrier_manager.cpp new file mode 100644 index 0000000..b0d4303 --- /dev/null +++ b/client/barrier_manager.cpp @@ -0,0 +1,81 @@ +/* Copyright: (c) Kayne Ruse 2013-2016 + * + * 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 "barrier_manager.hpp" + +#include +void BarrierManager::DrawTo(SDL_Renderer* const dest, Sint16 x, Sint16 y, double scaleX, double scaleY) { + for (auto& it : barrierMap) { + it.second.DrawTo(dest, x, y); + } +} + +void BarrierManager::LoadBaseImage(SDL_Renderer* renderer, std::string fname) { + baseImage.Load(renderer, fname); +} + +void BarrierManager::UnloadBaseImage() { + baseImage.Free(); +} + +void BarrierManager::LoadTemplateImages(SDL_Renderer* renderer, std::list names) { + for (auto& it : names) { + //TODO: check the state of the local flag + templateImages.emplace(it, Image(renderer, it)); + } +} + +void BarrierManager::UnloadTemplateImages() { + templateImages.clear(); +} + +BaseBarrier* BarrierManager::Create(int index) { + barrierMap.emplace( index, BaseBarrier(baseImage, templateImages) ); + + //DEBUG: debugging + return &barrierMap[index]; +} + +void BarrierManager::Unload(int i) { + barrierMap.erase(i); +} + +void BarrierManager::UnloadAll() { + barrierMap.clear(); +} + +int BarrierManager::Size() { + return barrierMap.size(); +} + +BaseBarrier* BarrierManager::Find(int i) { + std::map::iterator it = barrierMap.find(i); + + if (it == barrierMap.end()) { + return nullptr; + } + + return &it->second; +} + +std::map* BarrierManager::GetContainer() { + return &barrierMap; +} \ No newline at end of file diff --git a/client/barrier_manager.hpp b/client/barrier_manager.hpp new file mode 100644 index 0000000..39464ad --- /dev/null +++ b/client/barrier_manager.hpp @@ -0,0 +1,55 @@ +/* Copyright: (c) Kayne Ruse 2013-2016 + * + * 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. +*/ +#pragma once + +#include "base_barrier.hpp" + +#include +#include + +class BarrierManager { +public: + BarrierManager() = default; + ~BarrierManager() = default; + + void DrawTo(SDL_Renderer* const, Sint16 x, Sint16 y, double scaleX = 1.0, double scaleY = 1.0); + + //NOTE: don't use these while you have barriers loaded + void LoadBaseImage(SDL_Renderer* renderer, std::string fname); + void UnloadBaseImage(); + void LoadTemplateImages(SDL_Renderer* renderer, std::list names); + void UnloadTemplateImages(); + + BaseBarrier* Create(int index); + void Unload(int i); + void UnloadAll(); + + int Size(); + + BaseBarrier* Find(int i); + std::map* GetContainer(); + +private: + Image baseImage; + std::map templateImages; + std::map barrierMap; +}; \ No newline at end of file diff --git a/client/entities/base_barrier.cpp b/client/entities/base_barrier.cpp index 380a812..5cdd623 100644 --- a/client/entities/base_barrier.cpp +++ b/client/entities/base_barrier.cpp @@ -25,7 +25,7 @@ #include -BaseBarrier::BaseBarrier(Image argBaseImage, std::map& templateImages) { +BaseBarrier::BaseBarrier(Image& argBaseImage, std::map& templateImages) { baseImage = argBaseImage; composite.SetImageTextures(templateImages); } @@ -35,7 +35,7 @@ BaseBarrier::~BaseBarrier() { } void BaseBarrier::CorrectSprite() { - //TODO: link status to sprite + //TODO: (0) link status to sprite } int BaseBarrier::SetStatus(int k, int v) { diff --git a/client/entities/base_barrier.hpp b/client/entities/base_barrier.hpp index bbac84f..c04de00 100644 --- a/client/entities/base_barrier.hpp +++ b/client/entities/base_barrier.hpp @@ -27,8 +27,8 @@ class BaseBarrier: public Entity { public: - BaseBarrier() = delete; - BaseBarrier(Image baseImage, std::map& templateImages); + BaseBarrier() = default; + BaseBarrier(Image& baseImage, std::map& templateImages); virtual ~BaseBarrier(); void CorrectSprite(); diff --git a/client/scenes/world.cpp b/client/scenes/world.cpp index 599dc2e..ca79d4d 100644 --- a/client/scenes/world.cpp +++ b/client/scenes/world.cpp @@ -98,7 +98,18 @@ World::World(int* const argClientIndex, int* const argAccountIndex): SDL_RenderGetLogicalSize(GetRenderer(), &camera.width, &camera.height); //debug - // + barrierMgr.LoadTemplateImages(GetRenderer(), + std::list{ + config["dir.sprites"] + "/barrier/slot 1 green.png", + config["dir.sprites"] + "/barrier/slot 2 green.png", + config["dir.sprites"] + "/barrier/slot 3 green.png", + config["dir.sprites"] + "/barrier/slot 4 green.png", + config["dir.sprites"] + "/barrier/slot 5 green.png", + config["dir.sprites"] + "/barrier/slot 6 green.png", + config["dir.sprites"] + "/barrier/slot 7 green.png", + config["dir.sprites"] + "/barrier/slot 8 green.png" + } + ); } World::~World() { @@ -175,6 +186,8 @@ void World::Update() { } } + //TODO: cull barriers + //get the collidable boxes std::list boxList = GenerateCollisionGrid(localCharacter, tileSheet.GetTileW(), tileSheet.GetTileH()); @@ -751,7 +764,7 @@ void World::hCharacterUnload(CharacterPacket* const argPacket) { //clear/reset the room roomIndex = -1; regionPager.UnloadAll(); - barrierMap.clear(); + barrierMgr.UnloadAll(); characterMap.clear(); creatureMap.clear(); } @@ -930,26 +943,24 @@ void World::hBarrierUpdate(BarrierPacket* const argPacket) { } //check if this barrier exists - std::map::iterator barrierIt = barrierMap.find(argPacket->barrierIndex); - if (barrierIt != barrierMap.end()) { - //update the origin and motion, if there's a difference - if (barrierIt->second.GetOrigin() != argPacket->origin) { - barrierIt->second.SetOrigin(argPacket->origin); - } - } - else { + BaseBarrier* barrier = barrierMgr.Find(argPacket->barrierIndex); + + if (!barrier) { hBarrierCreate(argPacket); + return; } + + //update the origin and motion, if there's a difference + if (barrier->GetOrigin() != argPacket->origin) { + barrier->SetOrigin(argPacket->origin); + } + barrier->SetStatusArray(argPacket->status); + barrier->CorrectSprite(); + } void World::hBarrierCreate(BarrierPacket* const argPacket) { //check for logic errors - if (barrierMap.find(argPacket->barrierIndex) != barrierMap.end()) { - std::ostringstream msg; - msg << "Double barrier creation event; "; - msg << "Index: " << argPacket->barrierIndex; - throw(std::runtime_error(msg.str())); - } //ignore barriers from other rooms if (roomIndex != argPacket->roomIndex) { @@ -960,30 +971,33 @@ void World::hBarrierCreate(BarrierPacket* const argPacket) { throw(std::runtime_error(msg.str())); } - //implicitly create the element - BaseBarrier* barrier = &barrierMap[argPacket->barrierIndex]; + BaseBarrier* barrier = barrierMgr.Find(argPacket->barrierIndex); + + if (barrier) { + std::ostringstream msg; + msg << "Double barrier creation event; "; + msg << "Index: " << argPacket->barrierIndex; + throw(std::runtime_error(msg.str())); + } + + barrier = barrierMgr.Create(argPacket->barrierIndex); //fill the barrier's info barrier->SetBounds(argPacket->bounds); barrier->SetOrigin(argPacket->origin); barrier->SetStatusArray(argPacket->status); + barrier->CorrectSprite(); //debug - std::cout << "Barrier Create, total: " << barrierMap.size() << std::endl; + std::cout << "Barrier Create, total: " << barrierMgr.Size() << std::endl; } void World::hBarrierUnload(BarrierPacket* const argPacket) { //ignore if this barrier doesn't exist - std::map::iterator barrierIt = barrierMap.find(argPacket->barrierIndex); - if (barrierIt == barrierMap.end()) { - return; - } - - //remove this barrier - barrierMap.erase(barrierIt); + barrierMgr.Unload(argPacket->barrierIndex); //debug - std::cout << "Barrier Unload, total: " << barrierMap.size() << std::endl; + std::cout << "Barrier Unload, total: " << barrierMgr.Size() << std::endl; } void World::hQueryBarrierExists(BarrierPacket* const argPacket) { @@ -995,15 +1009,20 @@ void World::hQueryBarrierExists(BarrierPacket* const argPacket) { } //implicitly create the element - BaseBarrier* barrier = &barrierMap[argPacket->barrierIndex]; + BaseBarrier* barrier = barrierMgr.Find(argPacket->barrierIndex); + + if (!barrier) { + barrier = barrierMgr.Create(argPacket->barrierIndex); + } //fill the barrier's info barrier->SetBounds(argPacket->bounds); barrier->SetOrigin(argPacket->origin); barrier->SetStatusArray(argPacket->status); + barrier->CorrectSprite(); //debug - std::cout << "Barrier Query, total: " << barrierMap.size() << std::endl; + std::cout << "Barrier Query, total: " << barrierMgr.Size() << std::endl; } //------------------------- diff --git a/client/scenes/world.hpp b/client/scenes/world.hpp index ba0bfcb..311d1fc 100644 --- a/client/scenes/world.hpp +++ b/client/scenes/world.hpp @@ -39,6 +39,7 @@ #include "frame_rate.hpp" //client +#include "barrier_manager.hpp" #include "base_scene.hpp" #include "base_barrier.hpp" #include "base_creature.hpp" @@ -158,7 +159,7 @@ private: } camera; //entities - std::map barrierMap; + BarrierManager barrierMgr; std::map characterMap; std::map creatureMap; LocalCharacter* localCharacter = nullptr;