From c641f5602b1a680ff7cc6e2fdf29e2391ead4c95 Mon Sep 17 00:00:00 2001 From: Vraiment Date: Sun, 23 Jul 2017 01:08:34 -0700 Subject: [PATCH] Added EventWait code (definitions/logic) --- SDL2pp/EventWait.cc | 26 ++++++++++++++++++++++++++ SDL2pp/EventWait.hh | 5 +++++ 2 files changed, 31 insertions(+) diff --git a/SDL2pp/EventWait.cc b/SDL2pp/EventWait.cc index 4bfe084..6d59407 100644 --- a/SDL2pp/EventWait.cc +++ b/SDL2pp/EventWait.cc @@ -21,7 +21,33 @@ #include +#include + namespace SDL2pp { namespace Event { + SDL_Event WaitEvent() { + SDL_Event result; + if (SDL_WaitEvent(&result) == 0) { + throw Exception("SDL_WaitEvent"); + } + return result; + } + + Optional WaitEvent(int timeout) { + SDL_Event result; + SDL_ClearError(); // Necessary to ensure SDL_WaitEventTimeout + // returned with no errors + if (SDL_WaitEventTimeout(&result, timeout) == 0) { + auto error = SDL_GetError(); + // Check if the error message is empty + if (*error == 0) { + return Optional(); + } else { + throw Exception("SDL_WaitEventTimeout"); + } + } + + return result; + } } } diff --git a/SDL2pp/EventWait.hh b/SDL2pp/EventWait.hh index d29154e..bbc8338 100644 --- a/SDL2pp/EventWait.hh +++ b/SDL2pp/EventWait.hh @@ -24,8 +24,13 @@ #include +#include + namespace SDL2pp { namespace Event { + SDL_Event WaitEvent(); + + Optional WaitEvent(int timeout); } }