OpenClonk
C4App.cpp
Go to the documentation of this file.
1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 2007, Alexander Post
5  * Copyright (c) 2010-2016, The OpenClonk Team and contributors
6  *
7  * Distributed under the terms of the ISC license; see accompanying file
8  * "COPYING" for details.
9  *
10  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
11  * See accompanying file "TRADEMARK" for details.
12  *
13  * To redistribute this file separately, substitute the full license texts
14  * for the above references.
15  */
16 
17 #include "C4Include.h"
18 #include "platform/C4App.h"
19 #include "platform/C4Window.h"
20 
21 #ifdef WITH_QT_EDITOR
22 #include "game/C4Application.h"
23 #include "editor/C4ConsoleQt.h"
24 #endif
25 
27 {
28 #ifdef WITH_QT_EDITOR
30  {
31  // Qt has its own event loop. Use a timer to call our own event handling whenever Qt is done
32  // with its events. The alternative (calling Qt's event handling from
33  // C4Console::Execute) is too slow, at least on Linux.
34  // FIXME: All of this should be happening in a background thread instead of on the UI thread
35  QTimer timer;
36  QObject::connect(&timer, &QTimer::timeout, [this, &timer]() {
37  ScheduleProcs(0);
38  if (fQuitMsgReceived)
39  {
40  QApplication::quit();
41  return;
42  }
43  auto now = C4TimeMilliseconds::Now();
44  auto next_tick = GetNextTick(now);
45  timer.setInterval(Clamp(next_tick - now, 0, 1000/36));
46  });
47  timer.setTimerType(Qt::PreciseTimer);
48  timer.start();
49  QApplication::exec();
50  return;
51  }
52 #endif
53 
54  // Main message loop
55  while (!fQuitMsgReceived)
56  ScheduleProcs();
57 }
58 
60 {
61  // Always fail after quit message
63  return false;
64 #if defined(USE_SDL_MAINLOOP) || defined(USE_COCOA)
65  // Unfortunately, the SDL event loop needs to be polled
66  FlushMessages();
67 #endif
68 #ifdef WITH_QT_EDITOR
69  // Some places, for example the lobby, have a nested event processing loop.
70  // To prevent the editor from freezing, call the Qt event loop here.
71  if (iTimeout)
72  ProcessQtEvents();
73 #endif
74  return StdScheduler::DoScheduleProcs(iTimeout);
75 }
76 
77 #ifdef WITH_QT_EDITOR
78 void C4AbstractApp::ProcessQtEvents()
79 {
81  QApplication::processEvents();
82 }
83 #endif
84 
86 {
87 }
88 
90 {
91 #ifdef _WIN32
92  if (!Active && pWindow) pWindow->FlashWindow();
93 #else
94  if (pWindow) pWindow->FlashWindow();
95 #endif
96 }
C4Application Application
Definition: C4Globals.cpp:44
T Clamp(T bval, T lbound, T rbound)
Definition: Standard.h:44
bool fQuitMsgReceived
Definition: C4App.h:81
void Run()
Definition: C4App.cpp:26
bool Active
Definition: C4App.h:63
bool FlushMessages()
Definition: C4AppSDL.cpp:115
void NotifyUserIfInactive()
Definition: C4App.cpp:89
bool DoScheduleProcs(int iTimeout) override
Definition: C4App.cpp:59
C4Window * pWindow
Definition: C4App.h:80
static C4TimeMilliseconds Now()
virtual void PerformUpdate()
Definition: C4App.cpp:85
void FlashWindow()
Definition: C4AppMac.mm:75
bool ScheduleProcs(int iTimeout=1000/36)
virtual bool DoScheduleProcs(int iTimeout)
C4TimeMilliseconds GetNextTick(C4TimeMilliseconds tNow)