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

@ -18,37 +18,27 @@
*/
#include "sdlmain.hpp"
#include "observersmanager.hpp"
#include "sizenotifiable.hpp"
#include "sizeratio.hpp"
#include <SDL2/SDL.h>
namespace cloonel {
struct SDLMain::LocalData {
SDL_Window* window;
SDL_Renderer* renderer;
SizeRatio sizeratio;
ObserversManager<SizeNotifiable*> resChangeNotifList;
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) :
m_gameName(parGameName),
m_localData(new LocalData),
m_WHScaling(GetScaling(parRes, parReferenceRes)),
m_WH(parRes),
m_WHRef(parReferenceRes)
m_localData(new LocalData)
{
m_localData->sizeratio.SetOriginal(static_cast<float2>(parReferenceRes), static_cast<float2>(parRes));
}
///------------------------------------------------------------------------
@ -75,7 +65,8 @@ namespace cloonel {
throw std::runtime_error(SDL_GetError());
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)
throw std::runtime_error(SDL_GetError());
parInitSDL.window = win;
@ -109,7 +100,33 @@ namespace cloonel {
///------------------------------------------------------------------------
///------------------------------------------------------------------------
void SDLMain::SetResolution (ushort2 parRes) {
m_WHScaling = GetScaling(parRes, m_WHRef);
m_WH = parRes;
m_localData->sizeratio.UpdateResolution(static_cast<float2>(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