Adding new files to register objects that require to be notified when resolution changes.

Also added a structure that holds the resolution and the current scaling ratio.
This commit is contained in:
King_DuckZ 2014-03-19 17:31:10 +01:00
parent b627b69221
commit f0a38a2f8a
7 changed files with 185 additions and 25 deletions

View file

@ -49,6 +49,8 @@ add_executable(${PROJECT_NAME}
src/moverleftright.cpp src/moverleftright.cpp
src/tiledwallpaper.cpp src/tiledwallpaper.cpp
src/drawable.cpp src/drawable.cpp
src/sizeratio.cpp
src/sizenotifiable.cpp
) )
target_link_libraries(${PROJECT_NAME} target_link_libraries(${PROJECT_NAME}

View file

@ -18,37 +18,27 @@
*/ */
#include "sdlmain.hpp" #include "sdlmain.hpp"
#include "observersmanager.hpp"
#include "sizenotifiable.hpp"
#include "sizeratio.hpp"
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
namespace cloonel { namespace cloonel {
struct SDLMain::LocalData { struct SDLMain::LocalData {
SDL_Window* window; SDL_Window* window;
SDL_Renderer* renderer; SDL_Renderer* renderer;
SizeRatio sizeratio;
ObserversManager<SizeNotifiable*> resChangeNotifList;
bool initialized; bool initialized;
}; };
namespace {
float2 GetScaling (ushort2 parRes, ushort2 parRef) __attribute__((pure));
///----------------------------------------------------------------------
///----------------------------------------------------------------------
float2 GetScaling (ushort2 parRes, ushort2 parRef) {
return float2(
static_cast<float>(parRes.x()) / static_cast<float>(parRef.x()),
static_cast<float>(parRes.y()) / static_cast<float>(parRef.y())
);
}
} //unnamed namespace
///------------------------------------------------------------------------ ///------------------------------------------------------------------------
///------------------------------------------------------------------------ ///------------------------------------------------------------------------
SDLMain::SDLMain (const char* parGameName, ushort2 parRes, ushort2 parReferenceRes) : SDLMain::SDLMain (const char* parGameName, ushort2 parRes, ushort2 parReferenceRes) :
m_gameName(parGameName), m_gameName(parGameName),
m_localData(new LocalData), m_localData(new LocalData)
m_WHScaling(GetScaling(parRes, parReferenceRes)),
m_WH(parRes),
m_WHRef(parReferenceRes)
{ {
m_localData->sizeratio.SetOriginal(static_cast<float2>(parReferenceRes), static_cast<float2>(parRes));
} }
///------------------------------------------------------------------------ ///------------------------------------------------------------------------
@ -75,7 +65,8 @@ namespace cloonel {
throw std::runtime_error(SDL_GetError()); throw std::runtime_error(SDL_GetError());
parInitSDL.initialized = true; parInitSDL.initialized = true;
SDL_Window* const win = SDL_CreateWindow(m_gameName.c_str(), 100, 100, m_WH.x(), m_WH.y(), SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE); const float2 wh(m_localData->sizeratio.Resolution());
SDL_Window* const win = SDL_CreateWindow(m_gameName.c_str(), 100, 100, static_cast<int>(wh.x()), static_cast<int>(wh.y()), SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
if (!win) if (!win)
throw std::runtime_error(SDL_GetError()); throw std::runtime_error(SDL_GetError());
parInitSDL.window = win; parInitSDL.window = win;
@ -109,7 +100,33 @@ namespace cloonel {
///------------------------------------------------------------------------ ///------------------------------------------------------------------------
///------------------------------------------------------------------------ ///------------------------------------------------------------------------
void SDLMain::SetResolution (ushort2 parRes) { void SDLMain::SetResolution (ushort2 parRes) {
m_WHScaling = GetScaling(parRes, m_WHRef); m_localData->sizeratio.UpdateResolution(static_cast<float2>(parRes));
m_WH = parRes; for (auto currNotifiable : m_localData->resChangeNotifList) {
currNotifiable->NotifyResChanged(m_localData->sizeratio);
}
}
///------------------------------------------------------------------------
///------------------------------------------------------------------------
size_t SDLMain::RegisterForResChange (SizeNotifiable* parNotif) {
return m_localData->resChangeNotifList.Add(parNotif);
}
///------------------------------------------------------------------------
///------------------------------------------------------------------------
void SDLMain::UnregisterForResChange (size_t parID) noexcept {
m_localData->resChangeNotifList.Remove(parID);
}
///------------------------------------------------------------------------
///------------------------------------------------------------------------
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 } //namespace cloonel

View file

@ -26,6 +26,8 @@
struct SDL_Renderer; struct SDL_Renderer;
namespace cloonel { namespace cloonel {
class SizeNotifiable;
class SDLMain { class SDLMain {
public: public:
SDLMain ( const char* parGameName, ushort2 parRes, ushort2 parReferenceRes ); SDLMain ( const char* parGameName, ushort2 parRes, ushort2 parReferenceRes );
@ -33,9 +35,12 @@ namespace cloonel {
void Init ( void ); void Init ( void );
SDL_Renderer* GetRenderer ( void ); SDL_Renderer* GetRenderer ( void );
const ushort2& WidthHeight ( void ) const { return m_WH; } ushort2 WidthHeight ( void ) const noexcept;
const float2& WHScaling ( void ) const { return m_WHScaling; } const float2& WHScaling ( void ) const noexcept;
void SetResolution ( ushort2 parRes ); void SetResolution ( ushort2 parRes );
size_t RegisterForResChange ( SizeNotifiable* parNotif );
void UnregisterForResChange ( size_t parID ) noexcept;
private: private:
struct LocalData; struct LocalData;
@ -45,9 +50,6 @@ namespace cloonel {
const std::string m_gameName; const std::string m_gameName;
std::unique_ptr<LocalData> m_localData; std::unique_ptr<LocalData> m_localData;
float2 m_WHScaling;
ushort2 m_WH;
const ushort2 m_WHRef;
}; };
} //namespace cloonel } //namespace cloonel

8
src/sizenotifiable.cpp Normal file
View file

@ -0,0 +1,8 @@
#include "sizenotifiable.hpp"
namespace cloonel {
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
void SizeNotifiable::NotifyResChanged (const SizeRatio& parSize) {
}
} //namespace cloonel

40
src/sizenotifiable.hpp Normal file
View file

@ -0,0 +1,40 @@
/*
Copyright 2014 Michele "King_DuckZ" Santullo
This file is part of CloonelJump.
CloonelJump is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
CloonelJump is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with CloonelJump. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef id78906DE4FB0D43219CD3F0D9C620FC06
#define id78906DE4FB0D43219CD3F0D9C620FC06
#include "vector.hpp"
namespace cloonel {
class SizeRatio;
class SizeNotifiable {
public:
SizeNotifiable ( void ) = default;
virtual ~SizeNotifiable ( void ) noexcept = default;
virtual void NotifyResChanged ( const SizeRatio& parSize );
private:
float2 m_scaleRatio;
};
} //namespace cloonel
#endif

46
src/sizeratio.cpp Normal file
View file

@ -0,0 +1,46 @@
#include "sizeratio.hpp"
namespace cloonel {
namespace {
float2 CalculateRatio ( float2 parOriginal, float2 parResolution ) __attribute__((pure));
///----------------------------------------------------------------------
///----------------------------------------------------------------------
float2 CalculateRatio (float2 parOriginal, float2 parResolution) {
return parOriginal / parResolution;
}
} //unnamed namespace
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
SizeRatio::SizeRatio (const float2& parOriginal) :
m_original(parOriginal),
m_size(parOriginal),
m_ratio(1.0f)
{
}
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
SizeRatio::SizeRatio (const float2& parOriginal, const float2& parSize) :
m_original(parOriginal),
m_size(parSize),
m_ratio(CalculateRatio(parOriginal, parSize))
{
}
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
void SizeRatio::SetOriginal (const float2& parOriginal, const float2& parRes) {
m_original = parOriginal;
m_size = parRes;
m_ratio = CalculateRatio(parOriginal, parRes);
}
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
void SizeRatio::UpdateResolution (const float2& parNewRes) {
m_size = parNewRes;
m_ratio = CalculateRatio(m_original, parNewRes);
}
} //namespace cloonel

45
src/sizeratio.hpp Normal file
View file

@ -0,0 +1,45 @@
/*
Copyright 2014 Michele "King_DuckZ" Santullo
This file is part of CloonelJump.
CloonelJump is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
CloonelJump is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with CloonelJump. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef id3098F08C14B84E3C8CE169CBA05C9C86
#define id3098F08C14B84E3C8CE169CBA05C9C86
#include "vector.hpp"
namespace cloonel {
class SizeRatio {
public:
SizeRatio ( void ) = default;
explicit SizeRatio ( const float2& parOriginal );
SizeRatio ( const float2& parOriginal, const float2& parSize );
~SizeRatio ( void ) noexcept = default;
const float2& Ratio ( void ) const noexcept { return m_ratio; }
const float2& Resolution ( void ) const noexcept { return m_size; }
void SetOriginal ( const float2& parOriginal, const float2& parRes );
void UpdateResolution ( const float2& parNewRes );
private:
float2 m_original;
float2 m_size;
float2 m_ratio;
};
} //namespace cloonel
#endif