载入中...
搜索中...
未找到
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/config.h"
9
10#include <SDL2/SDL.h>
11
13{
14
16{
17 if (SDL_InitSubSystem(SDL_INIT_EVENTS) < 0)
18 throw eve::Exception("Could not initialize SDL events subsystem (%s)", SDL_GetError());
19 wakeEventType_ = SDL_RegisterEvents(1);
20 if (wakeEventType_ == static_cast<Uint32>(-1)) {
21 SDL_QuitSubSystem(SDL_INIT_EVENTS);
22 throw eve::Exception("Could not reserve SDL wake event (%s)", SDL_GetError());
23 }
24}
25
27{
28 SDL_QuitSubSystem(SDL_INIT_EVENTS);
29}
30
32{
33 SDL_Event e;
34
35 while (SDL_PollEvent(&e))
36 {
37 Message *msg = convert(e);
38 if (msg)
39 push(msg);
40 // Ownership stays in the queue until poll()/clear() — do not delete here.
41 }
42
43 // Per-frame work that modules hang off the pump (audio streaming, ...).
45}
46
48{
49 for (;;) {
50 if (Message *queued = poll())
51 return queued;
52
53 SDL_Event e;
54 if (SDL_WaitEvent(&e) != 1)
55 return nullptr;
56 if (e.type == wakeEventType_)
57 continue;
58
59 if (Message *message = convert(e))
60 return message;
61 }
62}
63
64void Event::wakeWaiters()
65{
66 SDL_Event e{};
67 e.type = wakeEventType_;
68 e.user.type = wakeEventType_;
69 e.user.data1 = this;
70 SDL_PushEvent(&e);
71}
72
74{
75 SDL_Event e;
76
77 while (SDL_PollEvent(&e))
78 {
79 // Do nothing with 'e' ...
80 }
81
83}
84
85Message *Event::convert(const SDL_Event &e)
86{
87 // Modules that own an event family translate it themselves; see
88 // event/PlatformEventSink.h. Only window-close and quit are handled here,
89 // because they are about the event loop rather than any one module.
90 Message *translated = nullptr;
91 const bool consumed = cap::forEachUntil<IPlatformEventSink>([&](IPlatformEventSink *sink) {
92 if (sink->observePlatformEvent(&e))
93 return true;
94 translated = sink->translatePlatformEvent(&e);
95 return translated != nullptr;
96 });
97 if (translated)
98 return translated;
99 if (consumed)
100 return nullptr;
101
102 switch (e.type) {
103 case SDL_QUIT:
104 return new Message("quit");
105 case SDL_WINDOWEVENT:
106 if (e.window.event == SDL_WINDOWEVENT_CLOSE)
107 return new Message("quit");
108 break;
109 default:
110 break;
111 }
112 return nullptr;
113}
114
115}
void push(Message *msg)
Queues a message. Thread-safe: workers may push completions for the main loop to poll.
Definition Event.cpp:31
virtual void clear()
Drops all queued messages and frees them.
Definition Event.cpp:93
Message * poll()
Pops the oldest message, or nullptr if the queue is empty (caller must delete).
Definition Event.cpp:49
virtual bool observePlatformEvent(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...
Definition Event.h:56
void pump() override
Pumps the event queue. This function gathers all the pending input information from devices and place...
Definition Event.cpp:31
void clear() override
Clears the event queue.
Definition Event.cpp:73
Message * wait() override
Waits for the next event (indefinitely). Useful for creating games where the screen and game state on...
Definition Event.cpp:47
I * query()
Definition Capability.h:77