SDLFramework/src/sdlf/Window.hpp

65 lines
1.4 KiB
C++
Raw Normal View History

2020-06-15 20:03:08 +02:00
#pragma once
2020-06-15 21:36:57 +02:00
#include <string>
#include <thread>
#include <atomic>
2020-06-15 21:49:10 +02:00
#include <functional>
2020-06-15 21:36:57 +02:00
2020-06-15 20:03:08 +02:00
#include "SDL.h"
2020-06-15 21:36:57 +02:00
#include "util/Vector2.hpp"
2020-06-18 12:50:56 +02:00
#include "Screen.hpp"
2020-06-15 21:36:57 +02:00
#define SDLF_REQUIRED_SUBSYSTEMS SDL_INIT_VIDEO
2020-06-15 20:03:08 +02:00
namespace sf
{
2020-06-15 21:49:10 +02:00
typedef std::function<int(void*, SDL_Event*)> EventCallback;
2020-06-15 20:03:08 +02:00
class IWindow
{
public:
2020-06-15 21:36:57 +02:00
void Create(Vector2u size, Vector2i position, std::string title, Uint32 flags = SDL_WINDOW_RESIZABLE);
2020-06-15 20:03:08 +02:00
2020-06-15 21:36:57 +02:00
void Launch(bool threaded = false);
void Stop();
bool IsOpen() { return m_atomWindowOpen; }
2020-06-15 21:49:10 +02:00
void AddEventCallback(EventCallback callback, void* userdata);
2020-06-18 12:50:56 +02:00
void SwitchScreen(IScreen* screen);
2020-06-15 21:36:57 +02:00
protected:
IWindow(Vector2u size, Vector2i position, std::string title, Uint32 flags = SDL_WINDOW_RESIZABLE);
virtual bool OnCreate() { return true; }
virtual void OnClose() { }
2020-06-15 21:49:10 +02:00
virtual void OnEvent(const SDL_Event& event) { }
2020-06-15 21:36:57 +02:00
virtual bool OnUpdate(double frametime) { return true; }
2020-06-18 12:50:56 +02:00
virtual void OnRender(SDL_Renderer* renderer) { }
2020-06-15 21:36:57 +02:00
protected:
2020-06-15 20:03:08 +02:00
SDL_Window* m_pWindow;
SDL_Renderer* m_pRenderer;
2020-06-15 21:36:57 +02:00
SDL_Event m_oEvent;
private:
void MessageLoop();
void Destroy();
private:
Vector2u m_oSize;
Vector2i m_oPosition;
std::string m_strTitle;
Uint32 m_uFlags;
2020-06-18 12:50:56 +02:00
IScreen* m_pCurrentScreen;
2020-06-15 21:36:57 +02:00
std::thread m_oMsgLoopThread;
std::atomic_bool m_atomWindowOpen;
2020-06-18 12:50:56 +02:00
std::function<void( SDL_Event& )> m_oEventFunction;
std::function<bool( double )> m_oUpdateFunction;
std::function<void( SDL_Renderer* )> m_oRenderFunction;
2020-06-15 20:03:08 +02:00
};
}