comparison Applications/Sdl/SdlEngine.cpp @ 56:9e3c2e75b870 wasm

extremely simplified SDL engine
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 28 Apr 2017 21:46:41 +0200
parents c2dc924f1a63
children ca644004d2ee
comparison
equal deleted inserted replaced
55:f0f354a97581 56:9e3c2e75b870
27 27
28 #include <SDL.h> 28 #include <SDL.h>
29 29
30 namespace OrthancStone 30 namespace OrthancStone
31 { 31 {
32 void SdlEngine::RenderFrame()
33 {
34 if (!viewportChanged_)
35 {
36 return;
37 }
38
39 viewportChanged_ = false;
40
41 bool updated;
42
43 {
44 BasicApplicationContext::ViewportLocker locker(context_);
45 updated = buffering_.RenderOffscreen(locker.GetViewport());
46 }
47
48 if (updated)
49 {
50 // Do not notify twice when a new frame was rendered, to avoid
51 // spoiling the SDL event queue
52 SDL_Event event;
53 SDL_memset(&event, 0, sizeof(event));
54 event.type = refreshEvent_;
55 event.user.code = 0;
56 event.user.data1 = 0;
57 event.user.data2 = 0;
58 SDL_PushEvent(&event);
59 }
60 }
61
62
63 void SdlEngine::RenderThread(SdlEngine* that)
64 {
65 for (;;)
66 {
67 that->renderFrame_.Wait();
68
69 if (that->continue_)
70 {
71 that->RenderFrame();
72 }
73 else
74 {
75 return;
76 }
77 }
78 }
79
80
81 KeyboardModifiers SdlEngine::GetKeyboardModifiers(const uint8_t* keyboardState,
82 const int scancodeCount)
83 {
84 int result = KeyboardModifiers_None;
85
86 if (keyboardState != NULL)
87 {
88 if (SDL_SCANCODE_LSHIFT < scancodeCount &&
89 keyboardState[SDL_SCANCODE_LSHIFT])
90 {
91 result |= KeyboardModifiers_Shift;
92 }
93
94 if (SDL_SCANCODE_RSHIFT < scancodeCount &&
95 keyboardState[SDL_SCANCODE_RSHIFT])
96 {
97 result |= KeyboardModifiers_Shift;
98 }
99
100 if (SDL_SCANCODE_LCTRL < scancodeCount &&
101 keyboardState[SDL_SCANCODE_LCTRL])
102 {
103 result |= KeyboardModifiers_Control;
104 }
105
106 if (SDL_SCANCODE_RCTRL < scancodeCount &&
107 keyboardState[SDL_SCANCODE_RCTRL])
108 {
109 result |= KeyboardModifiers_Control;
110 }
111
112 if (SDL_SCANCODE_LALT < scancodeCount &&
113 keyboardState[SDL_SCANCODE_LALT])
114 {
115 result |= KeyboardModifiers_Alt;
116 }
117
118 if (SDL_SCANCODE_RALT < scancodeCount &&
119 keyboardState[SDL_SCANCODE_RALT])
120 {
121 result |= KeyboardModifiers_Alt;
122 }
123 }
124
125 return static_cast<KeyboardModifiers>(result);
126 }
127
128
129 void SdlEngine::SetSize(BasicApplicationContext::ViewportLocker& locker, 32 void SdlEngine::SetSize(BasicApplicationContext::ViewportLocker& locker,
130 unsigned int width, 33 unsigned int width,
131 unsigned int height) 34 unsigned int height)
132 { 35 {
133 buffering_.SetSize(width, height, locker.GetViewport()); 36 locker.GetViewport().SetSize(width, height);
134 viewportChanged_ = true; 37 surface_.SetSize(width, height);
135 Refresh(); 38 }
136 } 39
137 40
138 41 void SdlEngine::RenderFrame()
139 void SdlEngine::Stop() 42 {
140 { 43 if (viewportChanged_)
141 if (continue_) 44 {
142 { 45 BasicApplicationContext::ViewportLocker locker(context_);
143 continue_ = false; 46 surface_.Render(locker.GetViewport());
144 renderFrame_.Signal(); // Unlock the render thread 47
145 renderThread_.join(); 48 viewportChanged_ = false;
146 } 49 }
147 } 50 }
148 51
149 52
150 void SdlEngine::Refresh() 53 KeyboardModifiers SdlEngine::GetKeyboardModifiers(const uint8_t* keyboardState,
151 { 54 const int scancodeCount)
152 renderFrame_.Signal(); 55 {
56 int result = KeyboardModifiers_None;
57
58 if (keyboardState != NULL)
59 {
60 if (SDL_SCANCODE_LSHIFT < scancodeCount &&
61 keyboardState[SDL_SCANCODE_LSHIFT])
62 {
63 result |= KeyboardModifiers_Shift;
64 }
65
66 if (SDL_SCANCODE_RSHIFT < scancodeCount &&
67 keyboardState[SDL_SCANCODE_RSHIFT])
68 {
69 result |= KeyboardModifiers_Shift;
70 }
71
72 if (SDL_SCANCODE_LCTRL < scancodeCount &&
73 keyboardState[SDL_SCANCODE_LCTRL])
74 {
75 result |= KeyboardModifiers_Control;
76 }
77
78 if (SDL_SCANCODE_RCTRL < scancodeCount &&
79 keyboardState[SDL_SCANCODE_RCTRL])
80 {
81 result |= KeyboardModifiers_Control;
82 }
83
84 if (SDL_SCANCODE_LALT < scancodeCount &&
85 keyboardState[SDL_SCANCODE_LALT])
86 {
87 result |= KeyboardModifiers_Alt;
88 }
89
90 if (SDL_SCANCODE_RALT < scancodeCount &&
91 keyboardState[SDL_SCANCODE_RALT])
92 {
93 result |= KeyboardModifiers_Alt;
94 }
95 }
96
97 return static_cast<KeyboardModifiers>(result);
153 } 98 }
154 99
155 100
156 SdlEngine::SdlEngine(SdlWindow& window, 101 SdlEngine::SdlEngine(SdlWindow& window,
157 BasicApplicationContext& context) : 102 BasicApplicationContext& context) :
158 window_(window), 103 window_(window),
159 context_(context), 104 context_(context),
160 continue_(true) 105 surface_(window),
161 { 106 viewportChanged_(true)
162 refreshEvent_ = SDL_RegisterEvents(1); 107 {
163
164 { 108 {
165 BasicApplicationContext::ViewportLocker locker(context_); 109 BasicApplicationContext::ViewportLocker locker(context_);
166 SetSize(locker, window_.GetWidth(), window_.GetHeight()); 110 SetSize(locker, window_.GetWidth(), window_.GetHeight());
167 locker.GetViewport().Register(*this); 111 locker.GetViewport().Register(*this);
168 } 112 }
169
170 renderThread_ = boost::thread(RenderThread, this);
171 } 113 }
172 114
173 115
174 SdlEngine::~SdlEngine() 116 SdlEngine::~SdlEngine()
175 { 117 {
176 Stop();
177
178 { 118 {
179 BasicApplicationContext::ViewportLocker locker(context_); 119 BasicApplicationContext::ViewportLocker locker(context_);
180 locker.GetViewport().Unregister(*this); 120 locker.GetViewport().Unregister(*this);
181 } 121 }
182 } 122 }
188 const uint8_t* keyboardState = SDL_GetKeyboardState(&scancodeCount); 128 const uint8_t* keyboardState = SDL_GetKeyboardState(&scancodeCount);
189 129
190 bool stop = false; 130 bool stop = false;
191 while (!stop) 131 while (!stop)
192 { 132 {
193 Refresh(); 133 RenderFrame();
194 134
195 SDL_Event event; 135 SDL_Event event;
196 136
197 while (SDL_PollEvent(&event)) 137 while (!stop &&
138 SDL_PollEvent(&event))
198 { 139 {
199 BasicApplicationContext::ViewportLocker locker(context_); 140 BasicApplicationContext::ViewportLocker locker(context_);
200 141
201 if (event.type == SDL_QUIT) 142 if (event.type == SDL_QUIT)
202 { 143 {
203 stop = true; 144 stop = true;
204 break; 145 break;
205 }
206 else if (event.type == refreshEvent_)
207 {
208 buffering_.SwapToScreen(window_);
209 } 146 }
210 else if (event.type == SDL_MOUSEBUTTONDOWN) 147 else if (event.type == SDL_MOUSEBUTTONDOWN)
211 { 148 {
212 KeyboardModifiers modifiers = GetKeyboardModifiers(keyboardState, scancodeCount); 149 KeyboardModifiers modifiers = GetKeyboardModifiers(keyboardState, scancodeCount);
213 150
271 else if (event.wheel.y < 0) 208 else if (event.wheel.y < 0)
272 { 209 {
273 locker.GetViewport().MouseWheel(MouseWheelDirection_Down, x, y, modifiers); 210 locker.GetViewport().MouseWheel(MouseWheelDirection_Down, x, y, modifiers);
274 } 211 }
275 } 212 }
276 else if (event.type == SDL_KEYDOWN) 213 else if (event.type == SDL_KEYDOWN &&
214 event.key.repeat == 0 /* Ignore key bounce */)
277 { 215 {
278 KeyboardModifiers modifiers = GetKeyboardModifiers(keyboardState, scancodeCount); 216 KeyboardModifiers modifiers = GetKeyboardModifiers(keyboardState, scancodeCount);
279 217
280 switch (event.key.keysym.sym) 218 switch (event.key.keysym.sym)
281 { 219 {
309 default: 247 default:
310 break; 248 break;
311 } 249 }
312 } 250 }
313 } 251 }
314 252 }
315 SDL_Delay(10); // Necessary for mouse wheel events to work
316 }
317
318 Stop();
319 } 253 }
320 254
321 255
322 void SdlEngine::GlobalInitialize() 256 void SdlEngine::GlobalInitialize()
323 { 257 {