135 /////////////////////////////////////////////////////////////////////////////// |
135 /////////////////////////////////////////////////////////////////////////////// |
136 procedure MainLoop; |
136 procedure MainLoop; |
137 var event: TSDL_Event; |
137 var event: TSDL_Event; |
138 PrevTime, CurrTime: Longword; |
138 PrevTime, CurrTime: Longword; |
139 isTerminated: boolean; |
139 isTerminated: boolean; |
140 {$IFDEF SDL2} |
|
141 previousGameState: TGameState; |
140 previousGameState: TGameState; |
142 {$ELSE} |
|
143 prevFocusState: boolean; |
|
144 {$ENDIF} |
|
145 begin |
141 begin |
146 isTerminated:= false; |
142 isTerminated:= false; |
147 PrevTime:= SDL_GetTicks; |
143 PrevTime:= SDL_GetTicks; |
148 while isTerminated = false do |
144 while isTerminated = false do |
149 begin |
145 begin |
150 SDL_PumpEvents(); |
146 SDL_PumpEvents(); |
151 |
147 |
152 while SDL_PeepEvents(@event, 1, SDL_GETEVENT, {$IFDEF SDL2}SDL_FIRSTEVENT, SDL_LASTEVENT{$ELSE}SDL_ALLEVENTS{$ENDIF}) > 0 do |
148 while SDL_PeepEvents(@event, 1, SDL_GETEVENT, SDL_FIRSTEVENT, SDL_LASTEVENT) > 0 do |
153 begin |
149 begin |
154 case event.type_ of |
150 case event.type_ of |
155 {$IFDEF SDL2} |
|
156 SDL_KEYDOWN: |
151 SDL_KEYDOWN: |
157 if (GameState = gsChat) then |
152 if (GameState = gsChat) then |
158 KeyPressChat(event.key.keysym.sym) |
153 KeyPressChat(event.key.keysym.sym) |
159 else if (GameState >= gsGame) then |
154 else if (GameState >= gsGame) then |
160 ProcessKey(event.key); |
155 ProcessKey(event.key); |
209 onTouchDown(event.tfinger.x, event.tfinger.y, event.tfinger.fingerId); |
204 onTouchDown(event.tfinger.x, event.tfinger.y, event.tfinger.fingerId); |
210 |
205 |
211 SDL_FINGERUP: |
206 SDL_FINGERUP: |
212 onTouchUp(event.tfinger.x, event.tfinger.y, event.tfinger.fingerId); |
207 onTouchUp(event.tfinger.x, event.tfinger.y, event.tfinger.fingerId); |
213 {$ENDIF} |
208 {$ENDIF} |
214 {$ELSE} |
|
215 SDL_KEYDOWN: |
|
216 if GameState = gsChat then |
|
217 KeyPressChat(event.key.keysym.unicode, event.key.keysym.sym) |
|
218 else |
|
219 if GameState >= gsGame then ProcessKey(event.key); |
|
220 SDL_KEYUP: |
|
221 if (GameState <> gsChat) and (GameState >= gsGame) then |
|
222 ProcessKey(event.key); |
|
223 |
|
224 SDL_MOUSEBUTTONDOWN: |
|
225 if GameState = gsConfirm then |
|
226 ParseCommand('quit', true) |
|
227 else |
|
228 if (GameState >= gsGame) then ProcessMouse(event.button, true); |
|
229 |
|
230 SDL_MOUSEBUTTONUP: |
|
231 if (GameState >= gsGame) then ProcessMouse(event.button, false); |
|
232 |
|
233 SDL_ACTIVEEVENT: |
|
234 if (event.active.state and SDL_APPINPUTFOCUS) <> 0 then |
|
235 begin |
|
236 prevFocusState:= cHasFocus; |
|
237 cHasFocus:= event.active.gain = 1; |
|
238 if prevFocusState xor cHasFocus then |
|
239 onFocusStateChanged() |
|
240 end; |
|
241 |
|
242 SDL_VIDEORESIZE: |
|
243 begin |
|
244 // using lower values than cMinScreenWidth or cMinScreenHeight causes widget overlap and off-screen widget parts |
|
245 // Change by sheepluva: |
|
246 // Let's only use even numbers for custom width/height since I ran into scaling issues with odd width values. |
|
247 // Maybe just fixes the symptom not the actual cause(?), I'm too tired to find out :P |
|
248 cNewScreenWidth:= max(2 * (event.resize.w div 2), cMinScreenWidth); |
|
249 cNewScreenHeight:= max(2 * (event.resize.h div 2), cMinScreenHeight); |
|
250 cScreenResizeDelay:= RealTicks+500; |
|
251 end; |
|
252 {$ENDIF} |
|
253 SDL_JOYAXISMOTION: |
209 SDL_JOYAXISMOTION: |
254 ControllerAxisEvent(event.jaxis.which, event.jaxis.axis, event.jaxis.value); |
210 ControllerAxisEvent(event.jaxis.which, event.jaxis.axis, event.jaxis.value); |
255 SDL_JOYHATMOTION: |
211 SDL_JOYHATMOTION: |
256 ControllerHatEvent(event.jhat.which, event.jhat.hat, event.jhat.value); |
212 ControllerHatEvent(event.jhat.which, event.jhat.hat, event.jhat.value); |
257 SDL_JOYBUTTONDOWN: |
213 SDL_JOYBUTTONDOWN: |
348 |
304 |
349 WriteToConsole('Init SDL... '); |
305 WriteToConsole('Init SDL... '); |
350 if not cOnlyStats then SDLTry(SDL_Init(SDL_INIT_VIDEO or SDL_INIT_NOPARACHUTE) >= 0, 'SDL_Init', true); |
306 if not cOnlyStats then SDLTry(SDL_Init(SDL_INIT_VIDEO or SDL_INIT_NOPARACHUTE) >= 0, 'SDL_Init', true); |
351 WriteLnToConsole(msgOK); |
307 WriteLnToConsole(msgOK); |
352 |
308 |
353 {$IFDEF SDL2} |
|
354 //SDL_StartTextInput(); |
309 //SDL_StartTextInput(); |
355 {$ELSE} |
|
356 SDL_EnableUNICODE(1); |
|
357 {$ENDIF} |
|
358 SDL_ShowCursor(0); |
310 SDL_ShowCursor(0); |
359 |
311 |
360 WriteToConsole('Init SDL_ttf... '); |
312 WriteToConsole('Init SDL_ttf... '); |
361 SDLTry(TTF_Init() <> -1, 'TTF_Init', true); |
313 SDLTry(TTF_Init() <> -1, 'TTF_Init', true); |
362 WriteLnToConsole(msgOK); |
314 WriteLnToConsole(msgOK); |