From 83cf85c4c2a56f34fa208e0287c0e71ca670323a Mon Sep 17 00:00:00 2001 From: Ben Date: Thu, 19 Aug 2021 00:12:06 +0100 Subject: [PATCH] events coming together --- Aeon/Aeon.cpp | 14 ++++++++-- Aeon/Aeon.hpp | 12 +++++++- Aeon/Core/Display.cpp | 7 ++++- Aeon/Core/Display.hpp | 6 +++- Aeon/Core/Events.cpp | 2 +- Aeon/Core/Events.hpp | 35 ++++++++++++++--------- Aeon/Input/Input.cpp | 65 +++++++++++++++++++++++++++++++++++++++++++ Aeon/Input/Input.hpp | 8 +++++- Game/ExampleGame.cpp | 50 +-------------------------------- 9 files changed, 130 insertions(+), 69 deletions(-) diff --git a/Aeon/Aeon.cpp b/Aeon/Aeon.cpp index b364522..a21aa26 100644 --- a/Aeon/Aeon.cpp +++ b/Aeon/Aeon.cpp @@ -4,18 +4,23 @@ using Aeon::Core::App; using Aeon::Core::Display; using Aeon::Core::DisplayProperties; +using Aeon::Input::Input; + App::App( const DisplayProperties& props ) : mDisplay() + , mInput() { + RegisterAsSink( "ENGINE_SYSTEM_CORE", 0 ); + mDisplay.Create( props ); } void App::Run() { - // while ( !mSIGTERM ) + while ( !mSIGTERM ) { - + mInput.PollInput(); } } @@ -23,3 +28,8 @@ const Display& App::GetDisplay() { return mDisplay; } + +bool App::EventRecieved( GenericEvent& e ) +{ + return false; +} diff --git a/Aeon/Aeon.hpp b/Aeon/Aeon.hpp index 2a463ce..4d349d2 100644 --- a/Aeon/Aeon.hpp +++ b/Aeon/Aeon.hpp @@ -4,10 +4,13 @@ #include #include "Aeon/Core/Display.hpp" +#include "Aeon/Core/Events.hpp" +#include "Aeon/Input/Input.hpp" namespace Aeon::Core { -class App { +class App : public EventListener +{ public: App( const DisplayProperties& props ); @@ -17,10 +20,17 @@ public: void PopLayer(); const Display& GetDisplay(); + + bool EventRecieved( GenericEvent& e ) override; + private: Display mDisplay; + Aeon::Input::Input mInput; + +private: + bool mSIGTERM = false; }; diff --git a/Aeon/Core/Display.cpp b/Aeon/Core/Display.cpp index a437bab..19491a2 100644 --- a/Aeon/Core/Display.cpp +++ b/Aeon/Core/Display.cpp @@ -10,7 +10,7 @@ Display::Display() : mWindow( nullptr ) , mContext( NULL ) { - + RegisterAsSink( "ENGINE_DISPLAY_CORE", 0 ); } Display::~Display() @@ -77,3 +77,8 @@ void Display::Destroy() mWidth = 0; mHeight = 0; } + +bool Display::EventRecieved( GenericEvent& e ) +{ + return false; +} diff --git a/Aeon/Core/Display.hpp b/Aeon/Core/Display.hpp index fae4866..fb90110 100644 --- a/Aeon/Core/Display.hpp +++ b/Aeon/Core/Display.hpp @@ -8,6 +8,8 @@ extern "C" { #include } +#include "Aeon/Core/Events.hpp" + namespace Aeon::Core { struct DisplayProperties @@ -23,7 +25,7 @@ struct DisplayProperties VSync( vSync ) { } }; -class Display +class Display : public EventListener { public: Display(); @@ -36,6 +38,8 @@ public: void Destroy(); + bool EventRecieved( GenericEvent& e ) override; + private: SDL_Window* mWindow; SDL_GLContext mContext; diff --git a/Aeon/Core/Events.cpp b/Aeon/Core/Events.cpp index 0e24664..2b61bc5 100644 --- a/Aeon/Core/Events.cpp +++ b/Aeon/Core/Events.cpp @@ -41,7 +41,7 @@ EventDispatcher::EventDispatcher() EventDispatcher::EventDispatcher( std::string system ) { - + RegisterAsSource( system ); } EventDispatcher::~EventDispatcher() diff --git a/Aeon/Core/Events.hpp b/Aeon/Core/Events.hpp index cf2a02f..579c849 100644 --- a/Aeon/Core/Events.hpp +++ b/Aeon/Core/Events.hpp @@ -22,28 +22,37 @@ namespace Aeon::Core { +/* +* Engine event systems / type +* ENGINE_SYSTEM_CORE - start, stop, pause, etc +* ENGINE_DISPLAY_CORE - window open, window close, etc +* DISPLAY_RESIZE (x, y) - rezise to x, y being new w, h +* DISPLAY_MOVE (x, y) - move to x, y +* DISPLAY_SHOW - no data +* DISPLAY_HIDE - no data +* DISPLAY_MINIMISED - no data +* DISPLAY_MAXIMISED - no data +* DISPLAY_CLOSED - no data +* DISPLAY_MOUSE_ENTER - no data +* DISPLAY_MOUSE_LEAVE - no data +* DISPLAY_FOCUS - no data +* DISPLAY_OUT_OF_FOCUS - no data +* +*/ + + struct GenericEvent { - std::string Source; - std::string Sink; + std::string System; std::string Type; + // can be empty std::string Data; + bool Handled = false; GenericEvent() { } }; -struct KeyboardEvent : public GenericEvent -{ - int KeyCode; - int KeyStatus; - - KeyboardEvent( int keyCode, int keyStatus ) - : GenericEvent() - , KeyCode(keyCode) - , KeyStatus(keyStatus) { } -}; - class EventListener { public: diff --git a/Aeon/Input/Input.cpp b/Aeon/Input/Input.cpp index 8a0936c..190110d 100644 --- a/Aeon/Input/Input.cpp +++ b/Aeon/Input/Input.cpp @@ -1,9 +1,13 @@ #include "Aeon/Input/Input.hpp" + +#include + #include "Aeon/Core/Events.hpp" using Aeon::Input::Input; Input::Input() + : mEvent() { } @@ -15,6 +19,67 @@ Input::~Input() void Input::PollInput() { + while ( SDL_PollEvent( &mEvent ) ) + { + switch ( mEvent.type ) + { + case SDL_WINDOWEVENT: + switch ( mEvent.window.event ) + { + case SDL_WINDOWEVENT_SHOWN: + SDL_Log( "Window %d shown", mEvent.window.windowID ); + break; + case SDL_WINDOWEVENT_HIDDEN: + SDL_Log( "Window %d hidden", mEvent.window.windowID ); + break; + case SDL_WINDOWEVENT_MOVED: + SDL_Log( "Window %d moved to %d,%d", + mEvent.window.windowID, mEvent.window.data1, + mEvent.window.data2 ); + break; + case SDL_WINDOWEVENT_RESIZED: + SDL_Log( "Window %d resized to %dx%d", + mEvent.window.windowID, mEvent.window.data1, + mEvent.window.data2 ); + break; + case SDL_WINDOWEVENT_SIZE_CHANGED: + SDL_Log( "Window %d size changed to %dx%d", + mEvent.window.windowID, mEvent.window.data1, + mEvent.window.data2 ); + break; + case SDL_WINDOWEVENT_MINIMIZED: + SDL_Log( "Window %d minimized", mEvent.window.windowID ); + break; + case SDL_WINDOWEVENT_MAXIMIZED: + SDL_Log( "Window %d maximized", mEvent.window.windowID ); + break; + case SDL_WINDOWEVENT_RESTORED: + SDL_Log( "Window %d restored", mEvent.window.windowID ); + break; + case SDL_WINDOWEVENT_ENTER: + SDL_Log( "Mouse entered window %d", + mEvent.window.windowID ); + break; + case SDL_WINDOWEVENT_LEAVE: + SDL_Log( "Mouse left window %d", mEvent.window.windowID ); + break; + case SDL_WINDOWEVENT_FOCUS_GAINED: + SDL_Log( "Window %d gained keyboard focus", + mEvent.window.windowID ); + break; + case SDL_WINDOWEVENT_FOCUS_LOST: + SDL_Log( "Window %d lost keyboard focus", + mEvent.window.windowID ); + break; + case SDL_WINDOWEVENT_CLOSE: + SDL_Log( "Window %d closed", mEvent.window.windowID ); + break; + } + } + } + //Uint8* state = (Uint8*)SDL_GetKeyboardState( NULL ); + //std::cout << state << std::endl; + //std::cout << std::endl; } diff --git a/Aeon/Input/Input.hpp b/Aeon/Input/Input.hpp index 9326975..a4578be 100644 --- a/Aeon/Input/Input.hpp +++ b/Aeon/Input/Input.hpp @@ -1,17 +1,23 @@ #ifndef AEON_INPUT_INPUT_H_ #define AEON_INPUT_INPUT_H_ +#include + #include "Aeon/Singleton.hpp" namespace Aeon::Input { -class Input : public Aeon::Helpers::Singleton + + +class Input { public: Input(); ~Input(); void PollInput(); +private: + SDL_Event mEvent; }; } diff --git a/Game/ExampleGame.cpp b/Game/ExampleGame.cpp index f117691..ae2711b 100644 --- a/Game/ExampleGame.cpp +++ b/Game/ExampleGame.cpp @@ -6,7 +6,7 @@ #include #include -class ExampleGame : public Aeon::Core::App +class ExampleGame : public Aeon::Core::App { public: @@ -24,58 +24,10 @@ public: }; -class SomeSystem : public Aeon::Core::EventListener -{ -public: - SomeSystem() - { - RegisterAsSink( "System1", 0 ); - } - - ~SomeSystem() override - { - DeRegisterAsSink( "System1" ); - } - - bool EventRecieved( Aeon::Core::GenericEvent& e ) override - { - std::cout << e.Source << ":" << e.Type << ":" << e.Sink << ":" << e.Data << std::endl; - return false; - } - -}; - -class SomeOtherSystem : public Aeon::Core::EventListener -{ -public: - SomeOtherSystem() - { - RegisterAsSink( "System1", 0 ); - } - - ~SomeOtherSystem() override - { - DeRegisterAsSink( "System1" ); - } - - bool EventRecieved( Aeon::Core::GenericEvent& e ) override - { - std::cout << e.Source << ":" << e.Type << ":" << e.Sink << ":" << e.Data << std::endl; - return false; - } - -}; int main( int argc, char** argv ) { ExampleGame game; - SomeSystem system1; - SomeOtherSystem system2; - - Aeon::Core::EventDispatcher eventDispatcher; - eventDispatcher.RegisterAsSource( "System1" ); - eventDispatcher.Dispatch( "bro" ); - return 0; }