载入中...
搜索中...
未找到
Event.cpp
浏览该文件的文档.
1
2#include "Event.h"
3
4#include "common/Capability.h"
5#include "common/Exception.h"
6#include "common/Module.h"
7#include "common/Profile.h"
8#include "common/config.h"
10
11#if __has_include(<SDL2/SDL.h>)
12#include <SDL2/SDL.h>
13#else
14#include <SDL.h>
15#endif
16
18namespace {
19
20void releaseOwnedEventPayload(SDL_Event &event) {
21 if ((event.type == SDL_DROPFILE || event.type == SDL_DROPTEXT) && event.drop.file) {
22 SDL_free(event.drop.file);
23 event.drop.file = nullptr;
24 }
25}
26
27} // namespace
28
30 if (SDL_InitSubSystem(SDL_INIT_EVENTS) < 0)
31 throw eve::Exception("Could not initialize SDL events subsystem (%s)", SDL_GetError());
32 wakeEventType_ = SDL_RegisterEvents(1);
33 if (wakeEventType_ == static_cast<Uint32>(-1)) {
34 SDL_QuitSubSystem(SDL_INIT_EVENTS);
35 throw eve::Exception("Could not reserve SDL wake event (%s)", SDL_GetError());
36 }
37}
38
39Event::~Event() { SDL_QuitSubSystem(SDL_INIT_EVENTS); }
40
42 EV_PROFILE_MODULE("event", "Event::pump");
43 SDL_Event e;
44
45 while (SDL_PollEvent(&e)) {
46 Message *msg = convert(e);
47 if (msg) push(msg);
48 releaseOwnedEventPayload(e);
49 // Ownership stays in the queue until poll()/clear() — do not delete here.
50 }
51
52 // Per-frame work that modules hang off the pump (audio streaming, ...).
53 cap::forEach<IPlatformEventSink>([](IPlatformEventSink *sink) { sink->onPumpFinished(); });
54}
55
57 for (;;) {
58 if (Message *queued = poll()) return queued;
59
60 SDL_Event e;
61 if (SDL_WaitEvent(&e) != 1) return nullptr;
62 if (e.type == wakeEventType_) continue;
63
64 Message *message = convert(e);
65 releaseOwnedEventPayload(e);
66 if (message) return message;
67 }
68}
69
70void Event::wakeWaiters() {
71 SDL_Event e{};
72 e.type = wakeEventType_;
73 e.user.type = wakeEventType_;
74 e.user.data1 = this;
75 SDL_PushEvent(&e);
76}
77
79 SDL_Event e;
80
81 while (SDL_PollEvent(&e)) {
82 releaseOwnedEventPayload(e);
83 }
84
86}
87
88Message *Event::convert(const SDL_Event &e) {
89 // Modules that own an event family translate it themselves; see
90 // event/PlatformEventSink.h. Only window-close and quit are handled here,
91 // because they are about the event loop rather than any one module.
92 Message *translated = nullptr;
93 const bool consumed = cap::forEachUntil<IPlatformEventSink>([&](IPlatformEventSink *sink) {
94 if (sink->shouldConsumePlatformEvent(&e)) return true;
95 translated = sink->translatePlatformEvent(&e);
96 return translated != nullptr;
97 });
98 if (translated) return translated;
99 if (consumed) return nullptr;
100
101 switch (e.type) {
102 case SDL_QUIT: return new Message("quit");
103 case SDL_WINDOWEVENT:
104 if (e.window.event == SDL_WINDOWEVENT_CLOSE) return new Message("quit");
105 break;
106 default: break;
107 }
108 return nullptr;
109}
110
111} // namespace eve::platform_event::sdl
std::string message
size_t queued
Definition OnnxGpgpu.cpp:60
#define EV_PROFILE_MODULE(module, name)
Profile the enclosing scope, tagged with a module for grouping.
Definition Profile.h:140
EVENGINE_API_FOUNDATION public API.
Definition Exception.h:13
EVENGINE_API_FOUNDATION public API.
virtual bool shouldConsumePlatformEvent(const void *nativeEvent)
virtual Message * translatePlatformEvent(const void *nativeEvent)
A named event carrying an ordered list of Variant payloads. Pushed messages are heap-allocated; the q...
virtual void clear()
Drops all queued messages and frees them.
Message * poll()
Pops the oldest message, or nullptr if the queue is empty. @ownership Ownership transfers to the call...
void push(Message *msg)
Queues a message. Thread-safe: workers may push completions for the main loop to poll.
Message * wait() override
Waits for the next event (indefinitely). Useful for creating games where the screen and game state on...
Definition Event.cpp:56
void clear() override
Clears the event queue.
Definition Event.cpp:78
void pump() override
Pumps the event queue. This function gathers all the pending input information from devices and place...
Definition Event.cpp:41
virtual ~Event()
Releases Event resources.
Definition Event.cpp:39
Event()
Constructs a Event.
Definition Event.cpp:29
bool consumed
Definition Graphics.cpp:184