Use the new notifiable system to scale the character.

This commit is contained in:
King_DuckZ 2014-03-20 11:46:20 +01:00
parent 85d57fd681
commit dbf29f13d1
10 changed files with 28 additions and 16 deletions

View file

@ -20,6 +20,7 @@
#include "character.hpp"
#include "sdlmain.hpp"
#include "texture.hpp"
#include <cassert>
namespace cloonel {
///-------------------------------------------------------------------------
@ -27,8 +28,13 @@ 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_texture(new Texture(parPath, parMain, false))
{
assert(parMain);
m_screenRatioID = parMain->RegisterForResChange(&m_screenRatio);
}
///-------------------------------------------------------------------------
@ -36,13 +42,19 @@ 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_texture(new Texture(parPath, parMain, false))
{
assert(parMain);
m_screenRatioID = parMain->RegisterForResChange(&m_screenRatio);
}
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
Character::~Character() noexcept {
m_sdlmain->UnregisterForResChange(m_screenRatioID);
}
///-------------------------------------------------------------------------
@ -63,6 +75,6 @@ namespace cloonel {
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
void Character::Draw() const {
m_texture->Render(GetPos(), WidthHeight());
m_texture->Render(GetPos(), WidthHeight(), m_screenRatio.Ratio());
}
} //namespace cloonel

View file

@ -23,6 +23,7 @@
#include "placeable.hpp"
#include "drawable.hpp"
#include "vector.hpp"
#include "sizenotifiable.hpp"
#include <string>
#include <memory>
@ -41,6 +42,9 @@ namespace cloonel {
virtual void Draw ( void ) const;
private:
SizeNotifiable m_screenRatio;
size_t m_screenRatioID;
SDLMain* const m_sdlmain;
const std::unique_ptr<Texture> m_texture;
};
} //unnamed namespace

View file

@ -109,6 +109,7 @@ namespace cloonel {
///------------------------------------------------------------------------
///------------------------------------------------------------------------
size_t SDLMain::RegisterForResChange (SizeNotifiable* parNotif) {
parNotif->NotifyResChanged(m_localData->sizeratio);
return m_localData->resChangeNotifList.Add(parNotif);
}
@ -123,10 +124,4 @@ namespace cloonel {
ushort2 SDLMain::WidthHeight() const noexcept {
return static_cast<ushort2>(m_localData->sizeratio.Resolution());
}
///------------------------------------------------------------------------
///------------------------------------------------------------------------
const float2& SDLMain::WHScaling() const noexcept {
return m_localData->sizeratio.Ratio();
}
} //namespace cloonel

View file

@ -36,7 +36,6 @@ namespace cloonel {
void Init ( void );
SDL_Renderer* GetRenderer ( void );
ushort2 WidthHeight ( void ) const noexcept;
const float2& WHScaling ( void ) const noexcept;
void SetResolution ( ushort2 parRes );
size_t RegisterForResChange ( SizeNotifiable* parNotif );

View file

@ -1,8 +1,10 @@
#include "sizenotifiable.hpp"
#include "sizeratio.hpp"
namespace cloonel {
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
void SizeNotifiable::NotifyResChanged (const SizeRatio& parSize) {
m_scaleRatio = parSize.Ratio();
}
} //namespace cloonel

View file

@ -31,6 +31,7 @@ namespace cloonel {
virtual ~SizeNotifiable ( void ) noexcept = default;
virtual void NotifyResChanged ( const SizeRatio& parSize );
const float2& Ratio ( void ) const noexcept { return m_scaleRatio; }
private:
float2 m_scaleRatio;

View file

@ -7,7 +7,7 @@ namespace cloonel {
///----------------------------------------------------------------------
///----------------------------------------------------------------------
float2 CalculateRatio (float2 parOriginal, float2 parResolution) {
return parOriginal / parResolution;
return parResolution / parOriginal;
}
} //unnamed namespace

View file

@ -269,11 +269,10 @@ namespace cloonel {
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
void Texture::Render (const float2& parPos, const float2& parSize) const {
void Texture::Render (const float2& parPos, const float2& parSize, const float2& parScaling) const {
assert(IsLoaded());
const float2 scaling(m_sdlmain->WHScaling());
const ushort2 pos(static_cast<ushort2>(parPos * scaling + 0.5f));
const ushort2 siz(static_cast<ushort2>(parSize * scaling + 0.5f));
const ushort2 pos(static_cast<ushort2>(parPos * parScaling + 0.5f));
const ushort2 siz(static_cast<ushort2>(parSize * parScaling + 0.5f));
const int screenHeight = m_sdlmain->WidthHeight().y();
const SDL_Rect dest = { pos.x(), screenHeight - pos.y() - siz.y(), siz.x(), siz.y() };
SDL_RenderCopy(m_sdlmain->GetRenderer(), m_texture, nullptr, &dest);

View file

@ -36,8 +36,8 @@ namespace cloonel {
void Reload ( void );
void Destroy ( void ) noexcept;
bool IsLoaded ( void ) const { return nullptr != m_texture; }
void Render ( const float2& parPos ) const { Render(parPos, m_size); }
void Render ( const float2& parPos, const float2& parSize ) const;
void Render ( const float2& parPos, const float2& parScaling ) const { Render(parPos, m_size, parScaling); }
void Render ( const float2& parPos, const float2& parSize, const float2& parScaling ) const;
const SDLMain* SDLObject ( void ) const { return m_sdlmain; }
private:

View file

@ -50,6 +50,6 @@ namespace cloonel {
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
void TiledWallpaper::Draw() const {
m_tile->Render(float2(0.0f));
m_tile->Render(float2(0.0f), float2(1.0f));
}
} //namespace cloonel