Added events
This commit is contained in:
parent
9ce9cd3432
commit
fd43b8917c
|
@ -4,6 +4,12 @@
|
||||||
|
|
||||||
using namespace sf;
|
using namespace sf;
|
||||||
|
|
||||||
|
int Callback(void* userdata, SDL_Event* event)
|
||||||
|
{
|
||||||
|
std::cout << event->type << std::endl;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
class MyWindow : public IWindow
|
class MyWindow : public IWindow
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -27,7 +33,7 @@ private:
|
||||||
|
|
||||||
virtual bool OnUpdate(double frametime) override
|
virtual bool OnUpdate(double frametime) override
|
||||||
{
|
{
|
||||||
printf("Frame took %f seconds\n", frametime);
|
SDL_SetWindowTitle(m_pWindow, std::to_string(frametime).c_str());
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -37,6 +43,7 @@ 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
|
||||||
{
|
{
|
||||||
|
|
|
@ -62,6 +62,11 @@ namespace sf
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void IWindow::AddEventCallback(EventCallback callback, void* userdata)
|
||||||
|
{
|
||||||
|
SDL_AddEventWatch(*(callback.target<SDL_EventFilter>()), userdata);
|
||||||
|
}
|
||||||
|
|
||||||
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(),
|
||||||
|
@ -83,6 +88,8 @@ namespace sf
|
||||||
{
|
{
|
||||||
while (SDL_PollEvent(&m_oEvent))
|
while (SDL_PollEvent(&m_oEvent))
|
||||||
{
|
{
|
||||||
|
OnEvent(m_oEvent);
|
||||||
|
|
||||||
if (m_oEvent.type == SDL_QUIT)
|
if (m_oEvent.type == SDL_QUIT)
|
||||||
{
|
{
|
||||||
m_atomWindowOpen = false;
|
m_atomWindowOpen = false;
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
#include "SDL.h"
|
#include "SDL.h"
|
||||||
#include "util/Vector2.hpp"
|
#include "util/Vector2.hpp"
|
||||||
|
@ -11,6 +12,8 @@
|
||||||
|
|
||||||
namespace sf
|
namespace sf
|
||||||
{
|
{
|
||||||
|
typedef std::function<int(void*, SDL_Event*)> EventCallback;
|
||||||
|
|
||||||
class IWindow
|
class IWindow
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -21,11 +24,14 @@ namespace sf
|
||||||
|
|
||||||
bool IsOpen() { return m_atomWindowOpen; }
|
bool IsOpen() { return m_atomWindowOpen; }
|
||||||
|
|
||||||
|
void AddEventCallback(EventCallback callback, void* userdata);
|
||||||
|
|
||||||
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);
|
||||||
|
|
||||||
virtual bool OnCreate() { return true; }
|
virtual bool OnCreate() { return true; }
|
||||||
virtual void OnClose() { }
|
virtual void OnClose() { }
|
||||||
|
virtual void OnEvent(const SDL_Event& event) { }
|
||||||
virtual bool OnUpdate(double frametime) { return true; }
|
virtual bool OnUpdate(double frametime) { return true; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
Loading…
Reference in a new issue