mirror of
https://github.com/edubart/otclient.git
synced 2025-10-18 21:43:26 +02:00
remove game state classes
scripting improvements
This commit is contained in:
@@ -42,14 +42,23 @@ void Engine::init()
|
||||
|
||||
void Engine::terminate()
|
||||
{
|
||||
// force last state exit
|
||||
changeState(NULL);
|
||||
|
||||
// terminate stuff
|
||||
g_fonts.terminate();
|
||||
g_graphics.terminate();
|
||||
}
|
||||
|
||||
void Engine::poll()
|
||||
{
|
||||
// poll platform events
|
||||
Platform::poll();
|
||||
|
||||
// poll network events
|
||||
Connection::poll();
|
||||
|
||||
// poll diaptcher tasks
|
||||
g_dispatcher.poll();
|
||||
}
|
||||
|
||||
void Engine::run()
|
||||
{
|
||||
std::string fpsText;
|
||||
@@ -65,14 +74,7 @@ void Engine::run()
|
||||
while(!m_stopping) {
|
||||
m_lastFrameTicks = Platform::getTicks();
|
||||
|
||||
// poll platform events
|
||||
Platform::poll();
|
||||
|
||||
// poll network events
|
||||
Connection::poll();
|
||||
|
||||
// poll diaptcher tasks
|
||||
g_dispatcher.poll();
|
||||
poll();
|
||||
|
||||
// render only when visible
|
||||
if(Platform::isWindowVisible()) {
|
||||
@@ -93,8 +95,6 @@ void Engine::run()
|
||||
// render
|
||||
g_graphics.beginRender();
|
||||
|
||||
if(m_currentState)
|
||||
m_currentState->render();
|
||||
UIContainer::getRootContainer()->render();
|
||||
|
||||
// render fps
|
||||
@@ -117,39 +117,19 @@ void Engine::stop()
|
||||
m_stopping = true;
|
||||
}
|
||||
|
||||
void Engine::changeState(GameState* newState)
|
||||
{
|
||||
if(m_currentState)
|
||||
m_currentState->onLeave();
|
||||
m_currentState = newState;
|
||||
if(m_currentState)
|
||||
m_currentState->onEnter();
|
||||
}
|
||||
|
||||
void Engine::onClose()
|
||||
{
|
||||
if(m_currentState)
|
||||
m_currentState->onClose();
|
||||
if(m_onCloseCallback)
|
||||
g_dispatcher.addTask(m_onCloseCallback);
|
||||
}
|
||||
|
||||
void Engine::onResize(const Size& size)
|
||||
{
|
||||
g_graphics.resize(size);
|
||||
UIContainer::getRootContainer()->setSize(size);
|
||||
|
||||
if(m_currentState)
|
||||
m_currentState->onResize(size);
|
||||
}
|
||||
|
||||
void Engine::onInputEvent(const InputEvent& event)
|
||||
{
|
||||
bool eventCaptured = false;
|
||||
|
||||
// events goes to the state first
|
||||
if(m_currentState)
|
||||
eventCaptured = m_currentState->onInputEvent(event);
|
||||
|
||||
// if the state didn't capture the input then goes to the gui
|
||||
if(!eventCaptured)
|
||||
UIContainer::getRootContainer()->onInputEvent(event);
|
||||
UIContainer::getRootContainer()->onInputEvent(event);
|
||||
}
|
||||
|
@@ -26,19 +26,20 @@
|
||||
#define ENGINE_H
|
||||
|
||||
#include <prerequisites.h>
|
||||
#include <core/gamestate.h>
|
||||
#include <core/input.h>
|
||||
|
||||
class Engine
|
||||
{
|
||||
public:
|
||||
Engine() : m_stopping(false),
|
||||
m_running(false),
|
||||
m_calculateFps(false),
|
||||
m_currentState(NULL) { }
|
||||
m_calculateFps(false) { }
|
||||
|
||||
void init();
|
||||
void terminate();
|
||||
|
||||
/// Poll events
|
||||
void poll();
|
||||
/// Main loop
|
||||
void run();
|
||||
|
||||
@@ -46,8 +47,6 @@ public:
|
||||
void stop();
|
||||
|
||||
/// Change current game state
|
||||
void changeState(GameState *newState);
|
||||
|
||||
bool isRunning() const { return m_running; }
|
||||
bool isStopping() const { return m_stopping; }
|
||||
|
||||
@@ -64,13 +63,16 @@ public:
|
||||
/// Return the current ticks on this frame
|
||||
int getCurrentFrameTicks() const { return m_lastFrameTicks; }
|
||||
|
||||
void setOnClose(Callback onCloseCallback) { m_onCloseCallback = onCloseCallback; }
|
||||
|
||||
private:
|
||||
bool m_stopping;
|
||||
bool m_running;
|
||||
bool m_calculateFps;
|
||||
|
||||
GameState *m_currentState;
|
||||
int m_lastFrameTicks;
|
||||
|
||||
Callback m_onCloseCallback;
|
||||
};
|
||||
|
||||
extern Engine g_engine;
|
||||
|
26
src/framework/core/modules.cpp
Normal file
26
src/framework/core/modules.cpp
Normal file
@@ -0,0 +1,26 @@
|
||||
/* The MIT License
|
||||
*
|
||||
* Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
|
||||
#include "modules.h"
|
||||
|
@@ -22,34 +22,11 @@
|
||||
*/
|
||||
|
||||
|
||||
#ifndef GAMESTATE_H
|
||||
#define GAMESTATE_H
|
||||
#ifndef MODULES_H
|
||||
#define MODULES_H
|
||||
|
||||
#include <prerequisites.h>
|
||||
#include <core/input.h>
|
||||
|
||||
struct InputEvent;
|
||||
|
||||
class GameState
|
||||
class Modules
|
||||
{
|
||||
public:
|
||||
GameState() { }
|
||||
virtual ~GameState() { }
|
||||
|
||||
/// Fired when enter the state
|
||||
virtual void onEnter() = 0;
|
||||
/// Fired when leaves the state
|
||||
virtual void onLeave() = 0;
|
||||
|
||||
/// Fired when user tries to close the window
|
||||
virtual void onClose() = 0;
|
||||
/// Fired for every user input event, this is called before processing UI input and if it returns false the input is not passed to the UI
|
||||
virtual bool onInputEvent(const InputEvent& event) = 0;
|
||||
/// Fired when main window is resized
|
||||
virtual void onResize(const Size& size) = 0;
|
||||
|
||||
/// Fired before redering the UI
|
||||
virtual void render() = 0;
|
||||
};
|
||||
|
||||
#endif // GAMESTATE_H
|
||||
#endif // MODULES_H
|
Reference in New Issue
Block a user