Refactoring in SizeNotifiable.
Class is a template now and accepts a registraton policy.
This commit is contained in:
parent
dbf29f13d1
commit
d0a052ddb2
7 changed files with 122 additions and 22 deletions
|
@ -28,13 +28,10 @@ namespace cloonel {
|
|||
Character::Character (const std::string& parPath, SDLMain* parMain, float2 parSize) :
|
||||
Placeable(float2(0.0f)),
|
||||
Drawable(parSize),
|
||||
m_screenRatio(),
|
||||
m_screenRatioID(0),
|
||||
m_sdlmain(parMain),
|
||||
m_screenRatio(parMain),
|
||||
m_texture(new Texture(parPath, parMain, false))
|
||||
{
|
||||
assert(parMain);
|
||||
m_screenRatioID = parMain->RegisterForResChange(&m_screenRatio);
|
||||
}
|
||||
|
||||
///-------------------------------------------------------------------------
|
||||
|
@ -42,19 +39,15 @@ namespace cloonel {
|
|||
Character::Character (const std::string&& parPath, SDLMain* parMain, float2 parSize) :
|
||||
Placeable(float2(0.0f)),
|
||||
Drawable(parSize),
|
||||
m_screenRatio(),
|
||||
m_screenRatioID(0),
|
||||
m_sdlmain(parMain),
|
||||
m_screenRatio(parMain),
|
||||
m_texture(new Texture(parPath, parMain, false))
|
||||
{
|
||||
assert(parMain);
|
||||
m_screenRatioID = parMain->RegisterForResChange(&m_screenRatio);
|
||||
}
|
||||
|
||||
///-------------------------------------------------------------------------
|
||||
///-------------------------------------------------------------------------
|
||||
Character::~Character() noexcept {
|
||||
m_sdlmain->UnregisterForResChange(m_screenRatioID);
|
||||
}
|
||||
|
||||
///-------------------------------------------------------------------------
|
||||
|
|
|
@ -42,9 +42,7 @@ namespace cloonel {
|
|||
virtual void Draw ( void ) const;
|
||||
|
||||
private:
|
||||
SizeNotifiable m_screenRatio;
|
||||
size_t m_screenRatioID;
|
||||
SDLMain* const m_sdlmain;
|
||||
SizeNotifiable<regbehaviuors::AutoRegister> m_screenRatio;
|
||||
const std::unique_ptr<Texture> m_texture;
|
||||
};
|
||||
} //unnamed namespace
|
||||
|
|
|
@ -28,7 +28,7 @@ namespace cloonel {
|
|||
SDL_Window* window;
|
||||
SDL_Renderer* renderer;
|
||||
SizeRatio sizeratio;
|
||||
ObserversManager<SizeNotifiable*> resChangeNotifList;
|
||||
ObserversManager<SizeNotifiableBase*> resChangeNotifList;
|
||||
bool initialized;
|
||||
};
|
||||
|
||||
|
@ -108,7 +108,7 @@ namespace cloonel {
|
|||
|
||||
///------------------------------------------------------------------------
|
||||
///------------------------------------------------------------------------
|
||||
size_t SDLMain::RegisterForResChange (SizeNotifiable* parNotif) {
|
||||
size_t SDLMain::RegisterForResChange (SizeNotifiableBase* parNotif) {
|
||||
parNotif->NotifyResChanged(m_localData->sizeratio);
|
||||
return m_localData->resChangeNotifList.Add(parNotif);
|
||||
}
|
||||
|
|
|
@ -26,7 +26,7 @@
|
|||
struct SDL_Renderer;
|
||||
|
||||
namespace cloonel {
|
||||
class SizeNotifiable;
|
||||
class SizeNotifiableBase;
|
||||
|
||||
class SDLMain {
|
||||
public:
|
||||
|
@ -38,7 +38,7 @@ namespace cloonel {
|
|||
ushort2 WidthHeight ( void ) const noexcept;
|
||||
|
||||
void SetResolution ( ushort2 parRes );
|
||||
size_t RegisterForResChange ( SizeNotifiable* parNotif );
|
||||
size_t RegisterForResChange ( SizeNotifiableBase* parNotif );
|
||||
void UnregisterForResChange ( size_t parID ) noexcept;
|
||||
|
||||
private:
|
||||
|
|
|
@ -1,10 +1,40 @@
|
|||
#include "sizenotifiable.hpp"
|
||||
#include "sizeratio.hpp"
|
||||
#include "sdlmain.hpp"
|
||||
#include <cassert>
|
||||
#include <ciso646>
|
||||
|
||||
namespace cloonel {
|
||||
namespace implem {
|
||||
} //namespace implem
|
||||
|
||||
namespace regbehaviuors {
|
||||
///----------------------------------------------------------------------
|
||||
///----------------------------------------------------------------------
|
||||
void AutoRegister::Register (SizeNotifiableBase* parNotifiable) {
|
||||
assert(m_sdlmain);
|
||||
#if !defined(NDEBUG)
|
||||
assert(not m_registered);
|
||||
m_registered = true;
|
||||
#endif
|
||||
m_id = m_sdlmain->RegisterForResChange(parNotifiable);
|
||||
}
|
||||
|
||||
///----------------------------------------------------------------------
|
||||
///----------------------------------------------------------------------
|
||||
void AutoRegister::Unregister() noexcept {
|
||||
#if !defined(NDEBUG)
|
||||
assert(m_registered);
|
||||
m_registered = false;
|
||||
#endif
|
||||
assert(m_sdlmain);
|
||||
m_sdlmain->UnregisterForResChange(m_id);
|
||||
}
|
||||
} //namespace regbehaviuors
|
||||
|
||||
///--------------------------------------------------------------------------
|
||||
///--------------------------------------------------------------------------
|
||||
void SizeNotifiable::NotifyResChanged (const SizeRatio& parSize) {
|
||||
void SizeNotifiableBase::NotifyResChanged (const SizeRatio& parSize) {
|
||||
m_scaleRatio = parSize.Ratio();
|
||||
}
|
||||
} //namespace cloonel
|
||||
|
|
|
@ -21,21 +21,94 @@
|
|||
#define id78906DE4FB0D43219CD3F0D9C620FC06
|
||||
|
||||
#include "vector.hpp"
|
||||
#include <cstddef>
|
||||
|
||||
namespace cloonel {
|
||||
class SizeRatio;
|
||||
class SDLMain;
|
||||
class SizeNotifiableBase;
|
||||
|
||||
namespace regbehaviuors {
|
||||
class DontRegister {
|
||||
public:
|
||||
enum { SDLMAIN_NEEDED = false };
|
||||
|
||||
DontRegister ( void ) = default;
|
||||
~DontRegister ( void ) noexcept = default;
|
||||
|
||||
void Register ( SizeNotifiableBase* ) const noexcept { return; }
|
||||
void Unregister ( void ) const noexcept { return; }
|
||||
};
|
||||
|
||||
class AutoRegister {
|
||||
public:
|
||||
enum { SDLMAIN_NEEDED = true };
|
||||
|
||||
explicit AutoRegister ( SDLMain* parMain ) :
|
||||
m_sdlmain(parMain)
|
||||
#if !defined(NDEBUG)
|
||||
, m_registered(false)
|
||||
#endif
|
||||
{
|
||||
}
|
||||
~AutoRegister ( void ) noexcept = default;
|
||||
|
||||
void Register ( SizeNotifiableBase* parNotifiable );
|
||||
void Unregister ( void ) noexcept;
|
||||
|
||||
private:
|
||||
SDLMain* const m_sdlmain;
|
||||
size_t m_id;
|
||||
#if !defined(NDEBUG)
|
||||
bool m_registered;
|
||||
#endif
|
||||
};
|
||||
} //namespace regbehaviuors
|
||||
|
||||
namespace implem {
|
||||
} //namespace implem
|
||||
|
||||
class SizeNotifiableBase {
|
||||
protected:
|
||||
SizeNotifiableBase ( void ) = default;
|
||||
virtual ~SizeNotifiableBase ( void ) noexcept = default;
|
||||
|
||||
class SizeNotifiable {
|
||||
public:
|
||||
SizeNotifiable ( void ) = default;
|
||||
virtual ~SizeNotifiable ( void ) noexcept = default;
|
||||
|
||||
virtual void NotifyResChanged ( const SizeRatio& parSize );
|
||||
const float2& Ratio ( void ) const noexcept { return m_scaleRatio; }
|
||||
|
||||
private:
|
||||
float2 m_scaleRatio;
|
||||
};
|
||||
|
||||
template <class RegisterBehaviour, bool=RegisterBehaviour::SDLMAIN_NEEDED>
|
||||
class SizeNotifiable;
|
||||
|
||||
template <class RegisterBehaviour>
|
||||
class SizeNotifiable<RegisterBehaviour, false> : private RegisterBehaviour, public SizeNotifiableBase {
|
||||
static_assert(RegisterBehaviour::SDLMAIN_NEEDED == false, "SdlMainNeeded mismatches expected value");
|
||||
public:
|
||||
SizeNotifiable ( void ) {
|
||||
this->Register(this);
|
||||
}
|
||||
virtual ~SizeNotifiable ( void ) noexcept {
|
||||
this->Unregister();
|
||||
}
|
||||
};
|
||||
|
||||
template <class RegisterBehaviour>
|
||||
class SizeNotifiable<RegisterBehaviour, true> : private RegisterBehaviour, public SizeNotifiableBase {
|
||||
static_assert(RegisterBehaviour::SDLMAIN_NEEDED == true, "SdlMainNeeded mismatches expected value");
|
||||
public:
|
||||
explicit SizeNotifiable ( SDLMain* parSdlMain ) :
|
||||
RegisterBehaviour(parSdlMain)
|
||||
{
|
||||
this->Register(this);
|
||||
}
|
||||
virtual ~SizeNotifiable ( void ) noexcept {
|
||||
this->Unregister();
|
||||
}
|
||||
};
|
||||
} //namespace cloonel
|
||||
|
||||
#endif
|
||||
|
|
|
@ -20,8 +20,9 @@
|
|||
#ifndef idDDB250A761E9458899C0687B7C8C5B7D
|
||||
#define idDDB250A761E9458899C0687B7C8C5B7D
|
||||
|
||||
#include <memory>
|
||||
#include "drawable.hpp"
|
||||
#include "sizenotifiable.hpp"
|
||||
#include <memory>
|
||||
|
||||
namespace cloonel {
|
||||
class Texture;
|
||||
|
@ -37,7 +38,12 @@ namespace cloonel {
|
|||
virtual void Draw ( void ) const;
|
||||
|
||||
private:
|
||||
class TileCountNotifiable : public SizeNotifiable<regbehaviuors::DontRegister> {
|
||||
};
|
||||
|
||||
TileCountNotifiable m_tileCount;
|
||||
const std::unique_ptr<Texture> m_tile;
|
||||
size_t m_tileCountID;
|
||||
};
|
||||
} //namespace cloonel
|
||||
|
||||
|
|
Loading…
Reference in a new issue