Otclient  14/8/2020
graphicalapplication.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2020 OTClient <https://github.com/edubart/otclient>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20  * THE SOFTWARE.
21  */
22 
23 
24 #include "graphicalapplication.h"
25 #include <framework/core/clock.h>
28 #include <framework/ui/uimanager.h>
33 #include <framework/input/mouse.h>
34 
35 #ifdef FW_SOUND
37 #endif
38 
40 
41 void GraphicalApplication::init(std::vector<std::string>& args)
42 {
43  Application::init(args);
44 
45  // setup platform window
46  g_window.init();
47  g_window.hide();
48  g_window.setOnResize(std::bind(&GraphicalApplication::resize, this, std::placeholders::_1));
49  g_window.setOnInputEvent(std::bind(&GraphicalApplication::inputEvent, this, std::placeholders::_1));
51 
52  g_mouse.init();
53 
54  // initialize ui
55  g_ui.init();
56 
57  // initialize graphics
58  g_graphics.init();
59 
60  // fire first resize event
62 
63 #ifdef FW_SOUND
64  // initialize sound
65  g_sounds.init();
66 #endif
67 }
68 
70 {
71  // hide the window because there is no render anymore
72  g_window.hide();
73 
75 }
76 
78 {
79  // destroy particles
81 
82  // destroy any remaining widget
83  g_ui.terminate();
84 
86  m_terminated = false;
87 
88 #ifdef FW_SOUND
89  // terminate sound
91 #endif
92 
94 
95  // terminate graphics
96  m_foreground = nullptr;
99 
100 
101  m_terminated = true;
102 }
103 
105 {
106  m_running = true;
107 
108  // first clock update
109  g_clock.update();
110 
111  // run the first poll
112  poll();
113  g_clock.update();
114 
115  // show window
116  g_window.show();
117 
118  // run the second poll
119  poll();
120  g_clock.update();
121 
122  g_lua.callGlobalField("g_app", "onRun");
123 
124  while(!m_stopping) {
125  // poll all events before rendering
126  poll();
127 
128  if(g_window.isVisible()) {
129  // the screen consists of two panes
130  // background pane - high updated and animated pane (where the game are stuff happens)
131  // foreground pane - steady pane with few animated stuff (UI)
132  bool redraw = false;
133  bool updateForeground = false;
134 
135  bool cacheForeground = g_graphics.canCacheBackbuffer() && m_foregroundFrameCounter.getMaxFps() != 0;
136 
137  if(m_backgroundFrameCounter.shouldProcessNextFrame()) {
138  redraw = true;
139 
140  if(m_mustRepaint || m_foregroundFrameCounter.shouldProcessNextFrame()) {
141  m_mustRepaint = false;
142  updateForeground = true;
143  }
144  }
145 
146  if(redraw) {
147  if(cacheForeground) {
148  Rect viewportRect(0, 0, g_painter->getResolution());
149 
150  // draw the foreground into a texture
151  if(updateForeground) {
152  m_foregroundFrameCounter.processNextFrame();
153 
154  // draw foreground
155  g_painter->setAlphaWriting(true);
158 
159  // copy the foreground to a texture
160  m_foreground->copyFromScreen(viewportRect);
161 
163  g_painter->setAlphaWriting(false);
164  }
165 
166  // draw background (animated stuff)
167  m_backgroundFrameCounter.processNextFrame();
169 
170  // draw the foreground (steady stuff)
172  g_painter->setOpacity(1.0);
173  g_painter->drawTexturedRect(viewportRect, m_foreground, viewportRect);
174  } else {
175  m_foregroundFrameCounter.processNextFrame();
176  m_backgroundFrameCounter.processNextFrame();
178  }
179 
180  // update screen pixels
182  }
183 
184  // only update the current time once per frame to gain performance
185  g_clock.update();
186 
187  if(m_backgroundFrameCounter.update())
188  g_lua.callGlobalField("g_app", "onFps", m_backgroundFrameCounter.getLastFps());
189  m_foregroundFrameCounter.update();
190 
191  int sleepMicros = m_backgroundFrameCounter.getMaximumSleepMicros();
193  stdext::microsleep(sleepMicros);
194 
195  } else {
196  // sleeps until next poll to avoid massive cpu usage
197  stdext::millisleep(POLL_CYCLE_DELAY+1);
198  g_clock.update();
199  }
200  }
201 
202  m_stopping = false;
203  m_running = false;
204 }
205 
207 {
208 #ifdef FW_SOUND
209  g_sounds.poll();
210 #endif
211 
212  // poll window input events
213  g_window.poll();
214  g_particles.poll();
215  g_textures.poll();
216 
218 }
219 
221 {
222  m_onInputEvent = true;
224  m_onInputEvent = false;
225 }
226 
228 {
229  m_onInputEvent = true;
230  g_graphics.resize(size);
231  g_ui.resize(size);
232  m_onInputEvent = false;
233 
235  m_foreground = TexturePtr(new Texture(size));
236  m_foreground->setUpsideDown(true);
237  }
238  m_mustRepaint = true;
239 }
240 
242 {
243  m_onInputEvent = true;
244  g_ui.inputEvent(event);
245  m_onInputEvent = false;
246 }
stdext::microsleep
void microsleep(size_t us)
Definition: time.cpp:50
Painter::setAlphaWriting
virtual void setAlphaWriting(bool enable)=0
Painter::setColor
virtual void setColor(const Color &color)
Definition: painter.h:77
GraphicalApplication
Definition: graphicalapplication.h:31
eventdispatcher.h
graphics.h
Application::terminate
virtual void terminate()
Definition: application.cpp:124
UIManager::resize
void resize(const Size &size)
Definition: uimanager.cpp:64
PlatformWindow::setOnClose
void setOnClose(const std::function< void()> &onClose)
Definition: platformwindow.h:93
platformwindow.h
TRect< int >
AdaptativeFrameCounter::getMaxFps
int getMaxFps()
Definition: adaptativeframecounter.h:54
AdaptativeFrameCounter::processNextFrame
void processNextFrame()
Definition: adaptativeframecounter.cpp:54
GraphicalApplication::run
void run()
Definition: graphicalapplication.cpp:104
Texture::setUpsideDown
void setUpsideDown(bool upsideDown)
Definition: texture.cpp:150
Painter::clear
virtual void clear(const Color &color)=0
Texture
Definition: texture.h:28
PlatformWindow::getSize
Size getSize()
Definition: platformwindow.h:75
GraphicalApplication::close
void close()
Definition: graphicalapplication.cpp:220
LuaInterface::callGlobalField
void callGlobalField(const std::string &global, const std::string &field, const T &... args)
Definition: luainterface.h:445
AdaptativeFrameCounter::update
bool update()
Definition: adaptativeframecounter.cpp:63
Application::m_running
stdext::boolean< false > m_running
Definition: application.h:73
PlatformWindow::swapBuffers
virtual void swapBuffers()=0
texturemanager.h
Fw::BackgroundPane
@ BackgroundPane
Definition: const.h:287
particlemanager.h
PlatformWindow::setOnInputEvent
void setOnInputEvent(const OnInputEventCallback &onInputEvent)
Definition: platformwindow.h:95
Painter::getResolution
Size getResolution()
Definition: painter.h:94
Graphics::canCacheBackbuffer
bool canCacheBackbuffer()
Definition: graphics.cpp:401
PlatformWindow::show
virtual void show()=0
stdext::millisleep
void millisleep(size_t ms)
Definition: time.cpp:45
GraphicalApplication::terminate
void terminate()
Definition: graphicalapplication.cpp:77
clock.h
g_sounds
SoundManager g_sounds
Definition: soundmanager.cpp:36
Application::deinit
virtual void deinit()
Definition: application.cpp:104
painter.h
g_app
GraphicalApplication g_app
Definition: graphicalapplication.cpp:39
PlatformWindow::init
virtual void init()=0
Application::poll
virtual void poll()
Definition: application.cpp:146
InputEvent
Definition: inputevent.h:28
g_window
PlatformWindow & g_window
Definition: platformwindow.cpp:37
g_mouse
Mouse g_mouse
Definition: mouse.cpp:28
g_lua
LuaInterface g_lua
Definition: luainterface.cpp:31
PlatformWindow::isVisible
bool isVisible()
Definition: platformwindow.h:88
Fw::BothPanes
@ BothPanes
Definition: const.h:288
GraphicalApplication::init
void init(std::vector< std::string > &args)
Definition: graphicalapplication.cpp:41
mouse.h
AdaptativeFrameCounter::MINIMUM_MICROS_SLEEP
@ MINIMUM_MICROS_SLEEP
Definition: adaptativeframecounter.h:39
Graphics::resize
void resize(const Size &size)
Definition: graphics.cpp:253
g_particles
ParticleManager g_particles
Definition: particlemanager.cpp:27
Color::white
static const Color white
Definition: color.h:101
Graphics::terminate
void terminate()
Definition: graphics.cpp:112
SoundManager::init
void init()
Definition: soundmanager.cpp:38
g_graphics
Graphics g_graphics
Definition: graphics.cpp:44
AdaptativeFrameCounter::getLastFps
int getLastFps()
Definition: adaptativeframecounter.h:52
GraphicalApplication::resize
void resize(const Size &size)
Definition: graphicalapplication.cpp:227
GraphicalApplication::deinit
void deinit()
Definition: graphicalapplication.cpp:69
TexturePtr
stdext::shared_object_ptr< Texture > TexturePtr
Definition: declarations.h:49
soundmanager.h
Texture::copyFromScreen
void copyFromScreen(const Rect &screenRect)
Definition: texture.cpp:105
UIManager::render
void render(Fw::DrawPane drawPane)
Definition: uimanager.cpp:59
graphicalapplication.h
Clock::update
void update()
Definition: clock.cpp:34
PlatformWindow::hide
virtual void hide()=0
AdaptativeFrameCounter::shouldProcessNextFrame
bool shouldProcessNextFrame()
Definition: adaptativeframecounter.cpp:43
Application::close
virtual void close()
Definition: application.cpp:166
Color::black
static const Color black
Definition: color.h:102
Color::alpha
static const Color alpha
Definition: color.h:100
AdaptativeFrameCounter::getMaximumSleepMicros
int getMaximumSleepMicros()
Definition: adaptativeframecounter.cpp:103
SoundManager::poll
void poll()
Definition: soundmanager.cpp:87
g_ui
UIManager g_ui
Definition: uimanager.cpp:33
ParticleManager::poll
void poll()
Definition: particlemanager.cpp:74
Mouse::terminate
void terminate()
Definition: mouse.cpp:34
UIManager::init
void init()
Definition: uimanager.cpp:35
Mouse::init
void init()
Definition: mouse.cpp:30
uimanager.h
UIManager::inputEvent
void inputEvent(const InputEvent &event)
Definition: uimanager.cpp:69
Fw::ForegroundPane
@ ForegroundPane
Definition: const.h:286
UIManager::terminate
void terminate()
Definition: uimanager.cpp:44
g_painter
Painter * g_painter
Definition: painter.cpp:28
PlatformWindow::setOnResize
void setOnResize(const OnResizeCallback &onResize)
Definition: platformwindow.h:94
Application::init
virtual void init(std::vector< std::string > &args)
Definition: application.cpp:64
SoundManager::terminate
void terminate()
Definition: soundmanager.cpp:58
PlatformWindow::poll
virtual void poll()=0
g_textures
TextureManager g_textures
Definition: texturemanager.cpp:33
Application::m_stopping
stdext::boolean< false > m_stopping
Definition: application.h:74
Graphics::init
void init()
Definition: graphics.cpp:52
GraphicalApplication::poll
void poll()
Definition: graphicalapplication.cpp:206
GraphicalApplication::inputEvent
void inputEvent(const InputEvent &event)
Definition: graphicalapplication.cpp:241
TextureManager::poll
void poll()
Definition: texturemanager.cpp:51
Application::m_terminated
stdext::boolean< false > m_terminated
Definition: application.h:75
TSize< int >
g_clock
Clock g_clock
Definition: clock.cpp:25
ParticleManager::terminate
void terminate()
Definition: particlemanager.cpp:67
PlatformWindow::terminate
virtual void terminate()=0
Painter::setOpacity
virtual void setOpacity(float opacity)
Definition: painter.h:91
Painter::drawTexturedRect
virtual void drawTexturedRect(const Rect &dest, const TexturePtr &texture, const Rect &src)=0