Added Screen system

This commit is contained in:
Robert 2020-06-18 12:50:56 +02:00
parent 3d747bbff8
commit 802e725db2
5 changed files with 86 additions and 17 deletions

View file

@ -4,11 +4,35 @@
using namespace sf; using namespace sf;
int Callback(void* userdata, SDL_Event* event) class MyScreen : public IScreen
{ {
std::cout << event->type << std::endl; public:
return 0; virtual void OnFocus() override
} {
printf("Received Focus\n");
}
virtual void OnDefocus() override
{
printf("Lost Focus\n");
}
virtual void OnRender(SDL_Renderer* renderer) override
{
SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255);
SDL_RenderClear(renderer);
}
static MyScreen* Get()
{
static MyScreen* instance = new MyScreen;
return instance;
}
private:
MyScreen() = default;
};
class MyWindow : public IWindow class MyWindow : public IWindow
{ {
@ -22,6 +46,7 @@ public:
private: private:
virtual bool OnCreate() override virtual bool OnCreate() override
{ {
SwitchScreen(MyScreen::Get());
printf("On Create\n"); printf("On Create\n");
return true; return true;
} }
@ -38,12 +63,13 @@ private:
} }
}; };
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
SDL_Init(SDL_INIT_VIDEO); SDL_Init(SDL_INIT_VIDEO);
MyWindow window; MyWindow window;
window.AddEventCallback(Callback, nullptr);
try try
{ {

View file

@ -7,15 +7,15 @@ namespace sf
class IScreen class IScreen
{ {
public: public:
virtual void OnFocus() {} // Called when the screen is being switched to
virtual void OnDefocus() {} // Called when the screen is being switched from
virtual bool OnUpdate(double frametime) { return true; } // Called every frame
virtual void OnEvent(const SDL_Event& event) {} // Called after an event occurs
virtual void OnRender(SDL_Renderer* renderer) {} // Called for rendering
protected: protected:
IScreen(); IScreen() = default;
virtual void OnFocus() = 0; // Called when the screen is being switched to
virtual void OnDefocus() = 0; // Called when the screen is being switched from
virtual void OnUpdate(double frametime) = 0; // Called every frame
virtual void OnEvent(const SDL_Event& event) = 0; // Called after an event occurs
private: private:
}; };
} }

View file

@ -33,6 +33,10 @@ namespace sf
THROW_IF_NULLPTR(m_pRenderer); THROW_IF_NULLPTR(m_pRenderer);
} }
m_oEventFunction = std::bind(&IWindow::OnEvent, this, std::placeholders::_1);
m_oUpdateFunction = std::bind<bool>(&IWindow::OnUpdate, this, std::placeholders::_1);
m_oRenderFunction = std::bind(&IWindow::OnRender, this, std::placeholders::_1);
} }
void IWindow::Destroy() void IWindow::Destroy()
@ -67,10 +71,38 @@ namespace sf
SDL_AddEventWatch(*(callback.target<SDL_EventFilter>()), userdata); SDL_AddEventWatch(*(callback.target<SDL_EventFilter>()), userdata);
} }
void IWindow::SwitchScreen(IScreen* screen)
{
if (m_pCurrentScreen == screen)
return;
if (IS_NOT_NULLPTR(m_pCurrentScreen))
m_pCurrentScreen->OnDefocus();
m_pCurrentScreen = screen;
if (IS_NOT_NULLPTR(m_pCurrentScreen))
{
m_oEventFunction = std::bind(&IScreen::OnEvent, m_pCurrentScreen, std::placeholders::_1);
m_oUpdateFunction = std::bind<bool>(&IScreen::OnUpdate, m_pCurrentScreen, std::placeholders::_1);
m_oRenderFunction = std::bind(&IScreen::OnRender, m_pCurrentScreen, std::placeholders::_1);
m_pCurrentScreen->OnFocus();
}
else
{
m_oEventFunction = std::bind(&IWindow::OnEvent, this, std::placeholders::_1);
m_oUpdateFunction = std::bind<bool>(&IWindow::OnUpdate, this, std::placeholders::_1);
m_oRenderFunction = std::bind(&IWindow::OnRender, this, std::placeholders::_1);
}
}
IWindow::IWindow(Vector2u size, Vector2i position, std::string title, IWindow::IWindow(Vector2u size, Vector2i position, std::string title,
Uint32 flags /*= SDL_WINDOW_RESIZABLE*/) : Uint32 flags /*= SDL_WINDOW_RESIZABLE*/) :
m_pWindow(nullptr), m_pRenderer(nullptr), m_oEvent(), m_pWindow(nullptr), m_pRenderer(nullptr), m_oEvent(),
m_oSize(size), m_oPosition(position), m_strTitle(title), m_uFlags(flags) m_oSize(size), m_oPosition(position), m_strTitle(title), m_uFlags(flags),
m_pCurrentScreen(nullptr)
{ {
} }
@ -88,7 +120,7 @@ namespace sf
{ {
while (SDL_PollEvent(&m_oEvent)) while (SDL_PollEvent(&m_oEvent))
{ {
OnEvent(m_oEvent); m_oEventFunction(m_oEvent);
if (m_oEvent.type == SDL_QUIT) if (m_oEvent.type == SDL_QUIT)
{ {
@ -100,11 +132,10 @@ namespace sf
std::chrono::steady_clock::now() - pastTime std::chrono::steady_clock::now() - pastTime
).count(); ).count();
pastTime = std::chrono::steady_clock::now(); pastTime = std::chrono::steady_clock::now();
if (!OnUpdate(frametime)) if (!m_oUpdateFunction(frametime))
m_atomWindowOpen = false; m_atomWindowOpen = false;
SDL_SetRenderDrawColor(m_pRenderer, 0, 0, 0, 255); m_oRenderFunction(m_pRenderer);
SDL_RenderClear(m_pRenderer);
SDL_RenderPresent(m_pRenderer); SDL_RenderPresent(m_pRenderer);
} }

View file

@ -7,6 +7,7 @@
#include "SDL.h" #include "SDL.h"
#include "util/Vector2.hpp" #include "util/Vector2.hpp"
#include "Screen.hpp"
#define SDLF_REQUIRED_SUBSYSTEMS SDL_INIT_VIDEO #define SDLF_REQUIRED_SUBSYSTEMS SDL_INIT_VIDEO
@ -26,6 +27,8 @@ namespace sf
void AddEventCallback(EventCallback callback, void* userdata); void AddEventCallback(EventCallback callback, void* userdata);
void SwitchScreen(IScreen* screen);
protected: protected:
IWindow(Vector2u size, Vector2i position, std::string title, Uint32 flags = SDL_WINDOW_RESIZABLE); IWindow(Vector2u size, Vector2i position, std::string title, Uint32 flags = SDL_WINDOW_RESIZABLE);
@ -33,6 +36,7 @@ namespace sf
virtual void OnClose() { } virtual void OnClose() { }
virtual void OnEvent(const SDL_Event& event) { } virtual void OnEvent(const SDL_Event& event) { }
virtual bool OnUpdate(double frametime) { return true; } virtual bool OnUpdate(double frametime) { return true; }
virtual void OnRender(SDL_Renderer* renderer) { }
protected: protected:
SDL_Window* m_pWindow; SDL_Window* m_pWindow;
@ -49,7 +53,13 @@ namespace sf
std::string m_strTitle; std::string m_strTitle;
Uint32 m_uFlags; Uint32 m_uFlags;
IScreen* m_pCurrentScreen;
std::thread m_oMsgLoopThread; std::thread m_oMsgLoopThread;
std::atomic_bool m_atomWindowOpen; std::atomic_bool m_atomWindowOpen;
std::function<void( SDL_Event& )> m_oEventFunction;
std::function<bool( double )> m_oUpdateFunction;
std::function<void( SDL_Renderer* )> m_oRenderFunction;
}; };
} }

View file

@ -1,3 +1,5 @@
#include "Vector2.hpp" #include "Vector2.hpp"
#define THROW_IF_NULLPTR(x) { if(x == nullptr) throw SDL_GetError(); } #define THROW_IF_NULLPTR(x) { if(x == nullptr) throw SDL_GetError(); }
#define IS_NULLPTR(x) ( x == nullptr ? true : false )
#define IS_NOT_NULLPTR(x) !IS_NULLPTR(x)