| 1 |
{%MainUnit castlewindow.pas} |
| 2 |
{ |
| 3 |
Copyright 2001-2022 Michalis Kamburelis. |
| 4 |
|
| 5 |
This file is part of "Castle Game Engine". |
| 6 |
|
| 7 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 8 |
included in this distribution, for details about the copyright. |
| 9 |
|
| 10 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
|
| 14 |
---------------------------------------------------------------------------- |
| 15 |
} |
| 16 |
|
| 17 |
{ This is the common code for CASTLE_WINDOW_XLIB and CASTLE_WINDOW_WINAPI implementations. } |
| 18 |
|
| 19 |
{$ifdef read_window_interface} |
| 20 |
{$endif read_window_interface} |
| 21 |
|
| 22 |
{$ifdef read_application_interface} |
| 23 |
private |
| 24 |
{ Same as ProcessMessage(WaitForMessage, WaitToLimitFPS), |
| 25 |
but additionally returns under WasAnyMessage was there any message |
| 26 |
processed. |
| 27 |
|
| 28 |
This ProcessMessage2 is useful to implement ProcessAllMessages. |
| 29 |
ActuallyProcessMessage is implemented using ProcessMessage2 |
| 30 |
(and simply ignoring WasAnyMessage). } |
| 31 |
function ProcessMessage2(WaitForMessage, WaitToLimitFPS: boolean; |
| 32 |
var WasAnyMessage: boolean): boolean; |
| 33 |
{$endif read_application_interface} |
| 34 |
|
| 35 |
{$ifdef read_implementation} |
| 36 |
|
| 37 |
{ common things for winsystem CastleWindow implementations. |
| 38 |
Uwagi : |
| 39 |
- zawsze musimy w jakis sposob sprawdzic czy okienko dla ktorego |
| 40 |
dostalismy message jest aktualnie na liscie Application.OpenWindows. |
| 41 |
Pamietajmy bowiem ze do naszej kolejki komunikatow mogly |
| 42 |
trafic message'y dla okienek ktore juz zamknelismy - typowym |
| 43 |
przykladem jest gdy zamykamy okienko w reakcji na jakies OnPress. |
| 44 |
Przeciez w kolejce komunikatow moze nam wtedy zostac OnRelease |
| 45 |
(wyslany przez WindowManagera kiedy mysmy robili cos innego. |
| 46 |
User szybko zrobil key down, potem up, dopiero potem my zauwazylismy |
| 47 |
ze dostalismy KeyDown, zrobilismy sobie Close (moze nawet |
| 48 |
zwolnilismy z pamieci obiekt TCastleWindow !!) i potem wywolujemy |
| 49 |
ProcessMessage (bo np. sa jeszcze jakies inne okienka otwarte) |
| 50 |
i dostajemy messaga KeyUp dla okienka ktore juz jest Closed |
| 51 |
albo moze nawet ktorego juz nie ma w pamieci !! |
| 52 |
|
| 53 |
Dotyczy to zarowno WinAPI jak i Xlib. |
| 54 |
} |
| 55 |
|
| 56 |
{ TCastleApplication ---------------------------------------------------------- } |
| 57 |
|
| 58 |
function TCastleApplication.ProcessAllMessages: boolean; |
| 59 |
var |
| 60 |
WasAnyMessage: boolean; |
| 61 |
begin |
| 62 |
repeat |
| 63 |
Result := ProcessMessage2(false, false, WasAnyMessage); |
| 64 |
if (not Result) or (not WasAnyMessage) then break; |
| 65 |
until false; |
| 66 |
end; |
| 67 |
|
| 68 |
function TCastleApplication.ProcessMessage(WaitForMessage, WaitToLimitFPS: boolean): boolean; |
| 69 |
var |
| 70 |
Dummy: boolean; |
| 71 |
begin |
| 72 |
Result := ProcessMessage2(WaitForMessage, WaitToLimitFPS, Dummy); |
| 73 |
end; |
| 74 |
|
| 75 |
function TCastleApplication.ProcessMessage2(WaitForMessage, WaitToLimitFPS: boolean; |
| 76 |
var WasAnyMessage: boolean): boolean; |
| 77 |
|
| 78 |
{ CheckMessage sprawdza czy jest jakis message w naszej queue, |
| 79 |
jesli tak - laduje go do msg/event i wywoluje HandleEvent i zwraca true, |
| 80 |
jesli nie - zwraca false. |
| 81 |
WaitMessage czeka az nasza kolejka bedzie niepusta i |
| 82 |
wtedy laduje message do msg/event i wywoluje HandleEvent. |
| 83 |
W rezultacie WaitMessage ZAWSZE przetworzy jakiegos messaga - |
| 84 |
dziala jak CheckMessage ktore uparlo sie zeby zwrocic true. |
| 85 |
NoMessageProcessed - wywolanie tej proc. oznacza ze w tym |
| 86 |
wywolaniu ProcessMessage CheckMessage zwrocilo false |
| 87 |
i z jakichs zewnetrznych powodow nie mozemy wywolac WaitMessage. |
| 88 |
Wobec tego nie przetworzylismy zadnego messaga - moze implementacja wymaga |
| 89 |
by cos zrobic w tej sytuacji ? (np. pod X-ami nalezy wowczas |
| 90 |
zrobic XFlush). |
| 91 |
|
| 92 |
Pod X-ami robimy XFlush w NoMessageProcessed i jest implicit XFlush |
| 93 |
w WaitMessage wiec zawsze w wywolaniu ProcessMessage zrobimy jakos |
| 94 |
XFlush - co jest wazne. |
| 95 |
} |
| 96 |
|
| 97 |
{$ifdef CASTLE_WINDOW_WINAPI} var msg: TMsg; {$endif} |
| 98 |
{$ifdef CASTLE_WINDOW_XLIB} var event: TXEvent; {$endif} |
| 99 |
|
| 100 |
procedure HandleEvent; |
| 101 |
begin |
| 102 |
WasAnyMessage := true; |
| 103 |
|
| 104 |
{$ifdef CASTLE_WINDOW_WINAPI} |
| 105 |
if msg.message = WM_QUIT then |
| 106 |
Terminate { set Terminated := true } else |
| 107 |
begin |
| 108 |
TranslateMessage(msg); |
| 109 |
DispatchMessage(msg); |
| 110 |
end; |
| 111 |
{$endif} |
| 112 |
|
| 113 |
{$ifdef CASTLE_WINDOW_XLIB} |
| 114 |
HandleXEvent(event); |
| 115 |
{$endif} |
| 116 |
end; |
| 117 |
|
| 118 |
function CheckMessage: boolean; |
| 119 |
{$ifdef CASTLE_WINDOW_WINAPI} |
| 120 |
begin |
| 121 |
Result := true; |
| 122 |
{seek for message to any of windows of our thread} |
| 123 |
if PeekMessageW(msg, 0, 0, 0, PM_REMOVE) then |
| 124 |
HandleEvent else |
| 125 |
(*{seek for messages to our thread with hWnd = 0 (send with PostThreadMessage) |
| 126 |
(NOT NEEDED NOW)} |
| 127 |
if PeekMessageW(msg, HWND(-1), 0, 0, PM_REMOVE) then |
| 128 |
HandleEvent else *) |
| 129 |
Result := false; |
| 130 |
{$endif} |
| 131 |
|
| 132 |
{$ifdef CASTLE_WINDOW_XLIB} |
| 133 |
begin |
| 134 |
Result := true; |
| 135 |
if XBool(XCheckMaskEvent(XDisplay, AcceptedEventsMask, @event)) <> XBool_False then |
| 136 |
HandleEvent else |
| 137 |
if XBool(XCheckTypedEvent(XDisplay, ClientMessage, @event)) <> XBool_False then |
| 138 |
HandleEvent else |
| 139 |
Result := false; |
| 140 |
{$endif} |
| 141 |
end; |
| 142 |
|
| 143 |
procedure WaitMessage; |
| 144 |
begin |
| 145 |
{$ifdef CASTLE_WINDOW_WINAPI} |
| 146 |
Check( LongInt(GetMessageW(msg, 0, 0, 0)) <> -1, 'GetMessageW failed with Result -1.'); |
| 147 |
{$endif} |
| 148 |
{$ifdef CASTLE_WINDOW_XLIB} |
| 149 |
XNextEvent(XDisplay, @event); { implicit XFlush } |
| 150 |
{$endif} |
| 151 |
HandleEvent; |
| 152 |
end; |
| 153 |
|
| 154 |
procedure NoMessageProcessed; |
| 155 |
begin |
| 156 |
{$ifdef CASTLE_WINDOW_XLIB} |
| 157 |
XFlush(XDisplay); |
| 158 |
{$endif} |
| 159 |
end; |
| 160 |
|
| 161 |
var |
| 162 |
WasAnyRendering: boolean; |
| 163 |
begin |
| 164 |
WasAnyMessage := false; |
| 165 |
|
| 166 |
if not CheckMessage then |
| 167 |
begin |
| 168 |
{ No messages to us, so call update/timer/render on everything. } |
| 169 |
UpdateAndRenderEverything(WasAnyRendering); |
| 170 |
if Terminated then Exit(false); |
| 171 |
|
| 172 |
{ If we do not do anything in any Application Update/Timer |
| 173 |
(this is checked by AllowSuspendForInput), |
| 174 |
and there's no reason to redisplay, |
| 175 |
and we're not during quit process (started by Application.Terminate), |
| 176 |
=> then we can wait, giving OS/CPU some rest (as opposed to doing |
| 177 |
"busy waiting"). } |
| 178 |
WaitForMessage := WaitForMessage and Application.AllowSuspendForInput and |
| 179 |
not (WasAnyRendering or Terminated); |
| 180 |
if WaitForMessage then |
| 181 |
begin |
| 182 |
MarkSleeping; |
| 183 |
WaitMessage; |
| 184 |
end else |
| 185 |
NoMessageProcessed; |
| 186 |
end; |
| 187 |
|
| 188 |
if Terminated then Exit(false); |
| 189 |
|
| 190 |
if (not WasAnyMessage) and |
| 191 |
(not Terminated) and |
| 192 |
(not WaitForMessage) and |
| 193 |
WaitToLimitFPS then |
| 194 |
DoLimitFPS; |
| 195 |
|
| 196 |
Result := not Terminated; |
| 197 |
end; |
| 198 |
|
| 199 |
procedure TCastleApplication.Run; |
| 200 |
begin |
| 201 |
if OpenWindowsCount = 0 then Exit; |
| 202 |
inherited Initialize; // set Terminated := false |
| 203 |
inherited Run; // calls DoRun, surrounded by HandleException, in a loop |
| 204 |
end; |
| 205 |
|
| 206 |
procedure TCastleApplication.BackendTerminate; |
| 207 |
begin |
| 208 |
end; |
| 209 |
|
| 210 |
{$endif read_implementation} |