2011-12-03 22:41:37 +01:00
|
|
|
/*
|
2013-01-08 19:21:54 +01:00
|
|
|
* Copyright (c) 2010-2013 OTClient <https://github.com/edubart/otclient>
|
2011-12-03 22:41:37 +01:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2012-07-14 03:10:24 +02:00
|
|
|
|
|
|
|
#include "graphicalapplication.h"
|
2011-12-03 22:41:37 +01:00
|
|
|
#include <framework/core/clock.h>
|
|
|
|
#include <framework/core/eventdispatcher.h>
|
|
|
|
#include <framework/platform/platformwindow.h>
|
|
|
|
#include <framework/ui/uimanager.h>
|
|
|
|
#include <framework/graphics/graphics.h>
|
2011-12-15 19:20:09 +01:00
|
|
|
#include <framework/graphics/particlemanager.h>
|
2013-01-08 21:45:27 +01:00
|
|
|
#include <framework/graphics/texturemanager.h>
|
2011-12-07 01:31:55 +01:00
|
|
|
#include <framework/graphics/painter.h>
|
2011-12-03 22:41:37 +01:00
|
|
|
|
2012-07-14 03:10:24 +02:00
|
|
|
#ifdef FW_SOUND
|
|
|
|
#include <framework/sound/soundmanager.h>
|
2013-01-25 14:17:51 +01:00
|
|
|
#include <framework/input/mouse.h>
|
2011-12-29 19:18:12 +01:00
|
|
|
#endif
|
|
|
|
|
2012-07-14 03:10:24 +02:00
|
|
|
GraphicalApplication g_app;
|
2011-12-03 22:41:37 +01:00
|
|
|
|
2013-01-08 21:45:27 +01:00
|
|
|
void GraphicalApplication::init(std::vector<std::string>& args)
|
2012-07-14 03:10:24 +02:00
|
|
|
{
|
|
|
|
Application::init(args);
|
2011-12-05 07:44:03 +01:00
|
|
|
|
2012-06-20 02:15:56 +02:00
|
|
|
// setup platform window
|
2012-04-13 21:54:08 +02:00
|
|
|
g_window.init();
|
|
|
|
g_window.hide();
|
2012-07-14 03:10:24 +02:00
|
|
|
g_window.setOnResize(std::bind(&GraphicalApplication::resize, this, std::placeholders::_1));
|
|
|
|
g_window.setOnInputEvent(std::bind(&GraphicalApplication::inputEvent, this, std::placeholders::_1));
|
|
|
|
g_window.setOnClose(std::bind(&GraphicalApplication::close, this));
|
|
|
|
|
2013-01-25 14:17:51 +01:00
|
|
|
g_mouse.init();
|
|
|
|
|
2012-07-14 03:10:24 +02:00
|
|
|
// initialize ui
|
|
|
|
g_ui.init();
|
2011-12-05 07:44:03 +01:00
|
|
|
|
2012-04-13 21:54:08 +02:00
|
|
|
// initialize graphics
|
|
|
|
g_graphics.init();
|
2011-12-03 22:41:37 +01:00
|
|
|
|
2012-06-20 02:15:56 +02:00
|
|
|
// fire first resize event
|
2012-04-13 21:54:08 +02:00
|
|
|
resize(g_window.getSize());
|
|
|
|
|
2012-07-14 03:10:24 +02:00
|
|
|
#ifdef FW_SOUND
|
|
|
|
// initialize sound
|
|
|
|
g_sounds.init();
|
|
|
|
#endif
|
2011-12-03 22:41:37 +01:00
|
|
|
}
|
|
|
|
|
2012-07-14 03:10:24 +02:00
|
|
|
void GraphicalApplication::deinit()
|
2011-12-03 22:41:37 +01:00
|
|
|
{
|
|
|
|
// hide the window because there is no render anymore
|
2012-04-13 21:54:08 +02:00
|
|
|
g_window.hide();
|
2011-12-03 22:41:37 +01:00
|
|
|
|
2012-07-14 03:10:24 +02:00
|
|
|
Application::deinit();
|
2012-06-18 10:13:52 +02:00
|
|
|
}
|
|
|
|
|
2012-07-14 03:10:24 +02:00
|
|
|
void GraphicalApplication::terminate()
|
2012-06-18 10:13:52 +02:00
|
|
|
{
|
2012-07-06 07:00:49 +02:00
|
|
|
// destroy particles
|
|
|
|
g_particles.terminate();
|
|
|
|
|
2013-01-17 15:23:01 +01:00
|
|
|
// destroy any remaining widget
|
2012-06-20 02:15:56 +02:00
|
|
|
g_ui.terminate();
|
|
|
|
|
2013-01-08 21:45:27 +01:00
|
|
|
Application::terminate();
|
|
|
|
m_terminated = false;
|
|
|
|
|
2012-07-14 03:10:24 +02:00
|
|
|
#ifdef FW_SOUND
|
2012-04-13 21:54:08 +02:00
|
|
|
// terminate sound
|
|
|
|
g_sounds.terminate();
|
2012-07-14 03:10:24 +02:00
|
|
|
#endif
|
2011-12-05 07:44:03 +01:00
|
|
|
|
2013-01-25 14:17:51 +01:00
|
|
|
g_mouse.terminate();
|
|
|
|
|
2012-06-18 10:13:52 +02:00
|
|
|
// terminate graphics
|
2012-06-01 21:39:09 +02:00
|
|
|
m_foreground = nullptr;
|
2012-06-18 10:13:52 +02:00
|
|
|
g_graphics.terminate();
|
|
|
|
g_window.terminate();
|
2012-06-01 21:39:09 +02:00
|
|
|
|
2012-06-18 10:13:52 +02:00
|
|
|
|
|
|
|
m_terminated = true;
|
2011-12-03 22:41:37 +01:00
|
|
|
}
|
|
|
|
|
2012-07-14 03:10:24 +02:00
|
|
|
void GraphicalApplication::run()
|
2011-12-03 22:41:37 +01:00
|
|
|
{
|
|
|
|
m_running = true;
|
|
|
|
|
2012-07-14 03:10:24 +02:00
|
|
|
// first clock update
|
|
|
|
g_clock.update();
|
|
|
|
|
2011-12-03 22:41:37 +01:00
|
|
|
// run the first poll
|
|
|
|
poll();
|
2012-07-14 03:10:24 +02:00
|
|
|
g_clock.update();
|
2011-12-03 22:41:37 +01:00
|
|
|
|
2013-01-08 21:45:27 +01:00
|
|
|
// show window
|
2012-08-20 23:53:06 +02:00
|
|
|
g_window.show();
|
2012-06-20 09:05:50 +02:00
|
|
|
|
2013-01-08 21:45:27 +01:00
|
|
|
// run the second poll
|
|
|
|
poll();
|
|
|
|
g_clock.update();
|
|
|
|
|
|
|
|
g_lua.callGlobalField("g_app", "onRun");
|
|
|
|
|
2012-06-02 16:43:27 +02:00
|
|
|
while(!m_stopping) {
|
2012-03-28 13:46:15 +02:00
|
|
|
// poll all events before rendering
|
|
|
|
poll();
|
2011-12-03 22:41:37 +01:00
|
|
|
|
2012-04-13 21:54:08 +02:00
|
|
|
if(g_window.isVisible()) {
|
2013-01-08 21:45:27 +01:00
|
|
|
// the screen consists of two panes
|
2012-06-01 21:39:09 +02:00
|
|
|
// background pane - high updated and animated pane (where the game are stuff happens)
|
|
|
|
// foreground pane - steady pane with few animated stuff (UI)
|
|
|
|
bool redraw = false;
|
|
|
|
bool updateForeground = false;
|
|
|
|
|
2012-06-09 02:40:22 +02:00
|
|
|
bool cacheForeground = g_graphics.canCacheBackbuffer() && m_foregroundFrameCounter.getMaxFps() != 0;
|
2012-06-08 18:58:08 +02:00
|
|
|
|
2012-06-01 21:39:09 +02:00
|
|
|
if(m_backgroundFrameCounter.shouldProcessNextFrame()) {
|
|
|
|
redraw = true;
|
|
|
|
|
|
|
|
if(m_mustRepaint || m_foregroundFrameCounter.shouldProcessNextFrame()) {
|
|
|
|
m_mustRepaint = false;
|
|
|
|
updateForeground = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(redraw) {
|
2012-06-08 18:58:08 +02:00
|
|
|
if(cacheForeground) {
|
2012-06-14 20:26:55 +02:00
|
|
|
Rect viewportRect(0, 0, g_painter->getResolution());
|
2012-06-01 21:39:09 +02:00
|
|
|
|
2012-06-08 18:58:08 +02:00
|
|
|
// draw the foreground into a texture
|
|
|
|
if(updateForeground) {
|
|
|
|
m_foregroundFrameCounter.processNextFrame();
|
2012-06-01 21:39:09 +02:00
|
|
|
|
2012-06-08 18:58:08 +02:00
|
|
|
// draw foreground
|
2012-06-09 02:40:22 +02:00
|
|
|
g_painter->setAlphaWriting(true);
|
|
|
|
g_painter->clear(Color::alpha);
|
2012-06-08 18:58:08 +02:00
|
|
|
g_ui.render(Fw::ForegroundPane);
|
2012-06-01 21:39:09 +02:00
|
|
|
|
2012-06-08 18:58:08 +02:00
|
|
|
// copy the foreground to a texture
|
|
|
|
m_foreground->copyFromScreen(viewportRect);
|
2012-06-09 02:40:22 +02:00
|
|
|
|
|
|
|
g_painter->clear(Color::black);
|
|
|
|
g_painter->setAlphaWriting(false);
|
2012-06-08 18:58:08 +02:00
|
|
|
}
|
2012-06-01 21:39:09 +02:00
|
|
|
|
2012-06-08 18:58:08 +02:00
|
|
|
// draw background (animated stuff)
|
|
|
|
m_backgroundFrameCounter.processNextFrame();
|
|
|
|
g_ui.render(Fw::BackgroundPane);
|
2012-06-01 21:39:09 +02:00
|
|
|
|
2012-06-08 18:58:08 +02:00
|
|
|
// draw the foreground (steady stuff)
|
|
|
|
g_painter->setColor(Color::white);
|
|
|
|
g_painter->setOpacity(1.0);
|
|
|
|
g_painter->drawTexturedRect(viewportRect, m_foreground, viewportRect);
|
|
|
|
} else {
|
|
|
|
m_foregroundFrameCounter.processNextFrame();
|
|
|
|
m_backgroundFrameCounter.processNextFrame();
|
|
|
|
g_ui.render(Fw::BothPanes);
|
|
|
|
}
|
2012-06-01 21:39:09 +02:00
|
|
|
|
|
|
|
// update screen pixels
|
|
|
|
g_window.swapBuffers();
|
|
|
|
}
|
|
|
|
|
2012-06-02 16:43:27 +02:00
|
|
|
// only update the current time once per frame to gain performance
|
|
|
|
g_clock.update();
|
|
|
|
|
2013-02-24 21:26:19 +01:00
|
|
|
if(m_backgroundFrameCounter.update())
|
|
|
|
g_lua.callGlobalField("g_app", "onFps", m_backgroundFrameCounter.getLastFps());
|
2012-06-01 21:39:09 +02:00
|
|
|
m_foregroundFrameCounter.update();
|
|
|
|
|
2012-06-15 13:46:55 +02:00
|
|
|
int sleepMicros = m_backgroundFrameCounter.getMaximumSleepMicros();
|
2012-06-08 18:58:08 +02:00
|
|
|
if(sleepMicros >= AdaptativeFrameCounter::MINIMUM_MICROS_SLEEP)
|
2012-06-15 13:46:55 +02:00
|
|
|
stdext::microsleep(sleepMicros);
|
2011-12-03 22:41:37 +01:00
|
|
|
|
|
|
|
} else {
|
|
|
|
// sleeps until next poll to avoid massive cpu usage
|
2012-06-02 16:43:27 +02:00
|
|
|
stdext::millisleep(POLL_CYCLE_DELAY+1);
|
|
|
|
g_clock.update();
|
2011-12-03 22:41:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m_stopping = false;
|
|
|
|
m_running = false;
|
|
|
|
}
|
|
|
|
|
2012-07-14 03:10:24 +02:00
|
|
|
void GraphicalApplication::poll()
|
2011-12-03 22:41:37 +01:00
|
|
|
{
|
2012-07-14 03:10:24 +02:00
|
|
|
#ifdef FW_SOUND
|
2012-04-14 11:53:32 +02:00
|
|
|
g_sounds.poll();
|
2012-07-14 03:10:24 +02:00
|
|
|
#endif
|
2012-04-14 11:53:32 +02:00
|
|
|
|
2012-07-14 03:10:24 +02:00
|
|
|
// poll window input events
|
2012-04-13 21:54:08 +02:00
|
|
|
g_window.poll();
|
2013-01-19 21:24:42 +01:00
|
|
|
g_particles.poll();
|
2013-01-16 15:42:06 +01:00
|
|
|
g_textures.poll();
|
2011-12-03 22:41:37 +01:00
|
|
|
|
2012-07-14 03:10:24 +02:00
|
|
|
Application::poll();
|
2011-12-03 22:41:37 +01:00
|
|
|
}
|
|
|
|
|
2012-07-14 03:10:24 +02:00
|
|
|
void GraphicalApplication::close()
|
2012-01-07 06:35:50 +01:00
|
|
|
{
|
2012-05-14 23:36:54 +02:00
|
|
|
m_onInputEvent = true;
|
2012-07-14 03:10:24 +02:00
|
|
|
Application::close();
|
2012-05-14 23:36:54 +02:00
|
|
|
m_onInputEvent = false;
|
2012-01-07 06:35:50 +01:00
|
|
|
}
|
|
|
|
|
2012-07-14 03:10:24 +02:00
|
|
|
void GraphicalApplication::resize(const Size& size)
|
2011-12-03 22:41:37 +01:00
|
|
|
{
|
2012-05-14 23:36:54 +02:00
|
|
|
m_onInputEvent = true;
|
2011-12-07 01:31:55 +01:00
|
|
|
g_graphics.resize(size);
|
2011-12-25 00:14:12 +01:00
|
|
|
g_ui.resize(size);
|
2012-05-14 23:36:54 +02:00
|
|
|
m_onInputEvent = false;
|
2012-06-01 21:39:09 +02:00
|
|
|
|
2012-06-08 18:58:08 +02:00
|
|
|
if(g_graphics.canCacheBackbuffer()) {
|
|
|
|
m_foreground = TexturePtr(new Texture(size));
|
|
|
|
m_foreground->setUpsideDown(true);
|
|
|
|
}
|
2012-06-01 21:39:09 +02:00
|
|
|
m_mustRepaint = true;
|
2011-12-03 22:41:37 +01:00
|
|
|
}
|
|
|
|
|
2012-07-14 03:10:24 +02:00
|
|
|
void GraphicalApplication::inputEvent(const InputEvent& event)
|
2011-12-03 22:41:37 +01:00
|
|
|
{
|
2012-05-14 23:36:54 +02:00
|
|
|
m_onInputEvent = true;
|
2011-12-03 22:41:37 +01:00
|
|
|
g_ui.inputEvent(event);
|
2012-05-14 23:36:54 +02:00
|
|
|
m_onInputEvent = false;
|
2011-12-03 22:41:37 +01:00
|
|
|
}
|