From 758b4b5dfba5d47cdeda4a2dd605672b78125391 Mon Sep 17 00:00:00 2001 From: Eduardo Bart Date: Tue, 16 Aug 2011 09:47:30 -0300 Subject: [PATCH] display motd message only once, remove update loop, use g_platform.getTicks() instead --- CMakeLists.txt | 2 +- modules/mainmenu/entergame.lua | 7 +++- src/framework/core/configmanager.cpp | 35 ------------------ src/framework/core/configmanager.h | 33 ----------------- src/framework/luascript/luafunctions.cpp | 9 +++++ src/framework/platform/platform.h | 8 +++- src/framework/platform/x11platform.cpp | 7 ++-- src/framework/util/tools.h | 17 +++++++-- src/otclient/core/effect.cpp | 27 +++++--------- src/otclient/core/effect.h | 3 +- src/otclient/core/map.cpp | 29 +++++---------- src/otclient/core/map.h | 2 +- src/otclient/core/tile.cpp | 10 ++--- src/otclient/otclient.cpp | 47 ++++++++++++------------ 14 files changed, 86 insertions(+), 150 deletions(-) delete mode 100644 src/framework/core/configmanager.cpp delete mode 100644 src/framework/core/configmanager.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 2f41c782..d87c9b5a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -89,7 +89,7 @@ SET(SOURCES src/framework/util/translator.cpp # framework core - src/framework/core/configmanager.cpp + src/framework/core/configs.cpp src/framework/core/resourcemanager.cpp src/framework/core/eventdispatcher.cpp src/framework/core/modulemanager.cpp diff --git a/modules/mainmenu/entergame.lua b/modules/mainmenu/entergame.lua index f85f8572..4b63f105 100644 --- a/modules/mainmenu/entergame.lua +++ b/modules/mainmenu/entergame.lua @@ -23,8 +23,11 @@ function EnterGame_connectToLoginServer() loadBox:destroy() local motdNumber = string.sub(motd, 0, string.find(motd, "\n")) local motdText = string.sub(motd, string.find(motd, "\n") + 1, string.len(motd)) - displayInfoBox("Message of the day", motdText) - mainMenu.visible = false + if motdNumber ~= Configs.get("motd") then + displayInfoBox("Message of the day", motdText) + Configs.set("motd", motdNumber) + end + mainMenu:hide() end local enterGameWindow = rootWidget:getChild("enterGameWindow") diff --git a/src/framework/core/configmanager.cpp b/src/framework/core/configmanager.cpp deleted file mode 100644 index 42d24dd3..00000000 --- a/src/framework/core/configmanager.cpp +++ /dev/null @@ -1,35 +0,0 @@ -#include "configmanager.h" -#include "resourcemanager.h" - -#include - -ConfigManager g_configs; - -bool ConfigManager::load(const std::string& fileName) -{ - m_fileName = fileName; - - if(!g_resources.fileExists(fileName)) - return false; - - try { - OTMLDocumentPtr doc = OTMLDocument::parse(fileName); - for(const OTMLNodePtr& child : doc->childNodes()) - m_confsMap[child->tag()] = child->value(); - } catch(std::exception& e) { - logError("ERROR: could not load configurations: ", e.what()); - return false; - } - - return true; -} - -bool ConfigManager::save() -{ - if(!m_fileName.empty()) { - OTMLDocumentPtr doc = OTMLDocument::create(); - doc->write(m_confsMap); - return doc->save(m_fileName); - } - return false; -} diff --git a/src/framework/core/configmanager.h b/src/framework/core/configmanager.h deleted file mode 100644 index 0e754c3d..00000000 --- a/src/framework/core/configmanager.h +++ /dev/null @@ -1,33 +0,0 @@ -#ifndef CONFIGMANAGER_H -#define CONFIGMANAGER_H - -#include "declarations.h" - -struct ConfigValueProxy { - ConfigValueProxy(const std::string& v) : value(v) { } - operator std::string() const { return fw::unsafe_cast(value); } - operator float() const { return fw::unsafe_cast(value); } - operator int() const { return fw::unsafe_cast(value); } - operator bool() const { return fw::unsafe_cast(value); } - std::string value; -}; - -class ConfigManager -{ -public: - bool load(const std::string& fileName); - bool save(); - - template - void set(const std::string& key, const T& value) { m_confsMap[key] = fw::unsafe_cast(value); } - - ConfigValueProxy get(const std::string& key) { return ConfigValueProxy(m_confsMap[key]); } - -private: - std::string m_fileName; - std::map m_confsMap; -}; - -extern ConfigManager g_configs; - -#endif diff --git a/src/framework/luascript/luafunctions.cpp b/src/framework/luascript/luafunctions.cpp index aa12ceba..3c935742 100644 --- a/src/framework/luascript/luafunctions.cpp +++ b/src/framework/luascript/luafunctions.cpp @@ -3,6 +3,7 @@ #include #include #include +#include void LuaInterface::registerFunctions() { @@ -30,6 +31,9 @@ void LuaInterface::registerFunctions() g_lua.bindClassMemberFunction("getChild", &UIWidget::getChildById); g_lua.bindClassMemberFunction("addChild", &UIWidget::addChild); g_lua.bindClassMemberFunction("lock", &UIWidget::lock); + g_lua.bindClassMemberFunction("hide", &UIWidget::hide); + g_lua.bindClassMemberFunction("show", &UIWidget::show); + // UILabel g_lua.registerClass(); @@ -55,6 +59,11 @@ void LuaInterface::registerFunctions() // Protocol g_lua.registerClass(); + // ConfigManager + g_lua.registerClass(); + g_lua.bindClassStaticFunction("set", std::bind(&Configs::set, &g_configs, _1, _2)); + g_lua.bindClassStaticFunction("get", std::bind(&Configs::get, &g_configs, _1)); + // global functions g_lua.bindGlobalFunction("importFont", std::bind(&FontManager::importFont, &g_fonts, _1)); g_lua.bindGlobalFunction("importStyles", std::bind(&UIManager::importStyles, &g_ui, _1)); diff --git a/src/framework/platform/platform.h b/src/framework/platform/platform.h index b4a41eda..a0d31fb6 100644 --- a/src/framework/platform/platform.h +++ b/src/framework/platform/platform.h @@ -14,8 +14,11 @@ public: /// Poll platform input/window events void poll(); - /// Get current time in milliseconds since init - int getTicks(); + void updateTicks(); + + /// Get current time in milliseconds since last application init + int getTicks() { return m_lastTicks; } + /// Sleep in current thread void sleep(ulong ms); @@ -58,6 +61,7 @@ public: private: PlatformListener* m_listener; + int m_lastTicks; }; extern Platform g_platform; diff --git a/src/framework/platform/x11platform.cpp b/src/framework/platform/x11platform.cpp index 81d1e9ea..ff850932 100644 --- a/src/framework/platform/x11platform.cpp +++ b/src/framework/platform/x11platform.cpp @@ -38,6 +38,7 @@ struct X11PlatformPrivate { int height; int x; int y; + int lastTicks; std::string clipboardText; std::map keyMap; PlatformListener* listener; @@ -240,7 +241,7 @@ void Platform::init(PlatformListener* platformListener, const char *appName) x11.atomWindowMaximizedHorz = XInternAtom(x11.display, "_NET_WM_STATE_MAXIMIZED_HORZ", False); // force first tick - Platform::getTicks(); + updateTicks(); } void Platform::terminate() @@ -447,7 +448,7 @@ void Platform::poll() } } -int Platform::getTicks() +void Platform::updateTicks() { static timeval tv; static ulong firstTick = 0; @@ -456,7 +457,7 @@ int Platform::getTicks() if(!firstTick) firstTick = tv.tv_sec; - return ((tv.tv_sec - firstTick) * 1000) + (tv.tv_usec / 1000); + m_lastTicks = ((tv.tv_sec - firstTick) * 1000) + (tv.tv_usec / 1000); } void Platform::sleep(ulong miliseconds) diff --git a/src/framework/util/tools.h b/src/framework/util/tools.h index 31636820..7f083e18 100644 --- a/src/framework/util/tools.h +++ b/src/framework/util/tools.h @@ -193,14 +193,23 @@ R safe_cast(const T& t) { /// Usage: /// R r = fw::unsafe_cast(t); template -R unsafe_cast(const T& t) { - R r; +R unsafe_cast(const T& t, R def = R()) { try { - r = safe_cast(t); + return safe_cast(t); } catch(bad_cast& e) { println(e.what()); + return def; } - return r; +} + +template +std::string tostring(const T& t) { + return unsafe_cast(t); +} + +template +T fromstring(const std::string& str, T def = T()) { + return unsafe_cast(str, def); } // an empty string to use anywhere needed diff --git a/src/otclient/core/effect.cpp b/src/otclient/core/effect.cpp index 71d84d74..ae20aa3f 100644 --- a/src/otclient/core/effect.cpp +++ b/src/otclient/core/effect.cpp @@ -1,34 +1,27 @@ #include "effect.h" #include "datmanager.h" +#include "map.h" +#include +#include Effect::Effect() : Thing(THING_EFFECT) { - m_timer = 0; + m_lastTicks = g_platform.getTicks(); m_animation = 0; m_finished = false; } void Effect::draw(int x, int y) { - internalDraw(x, y, 0, 0, 0, 0, m_animation); -} - -void Effect::update(int elapsedTime) -{ - if(m_finished) - return; - - if(m_timer > 75) { + if(!m_finished && g_platform.getTicks() - m_lastTicks > 75) { const ThingAttributes& attributes = getAttributes(); - - if(m_animation+1 == attributes.animcount) { - m_finished = true; - return; - } m_animation++; - m_timer = 0; + if(m_animation == attributes.animcount) + g_dispatcher.addEvent(std::bind(&Map::removeThing, &g_map, asThing())); + m_lastTicks = g_platform.getTicks(); } - m_timer += elapsedTime; + + internalDraw(x, y, 0, 0, 0, 0, m_animation); } const ThingAttributes& Effect::getAttributes() diff --git a/src/otclient/core/effect.h b/src/otclient/core/effect.h index 85630b15..5777601d 100644 --- a/src/otclient/core/effect.h +++ b/src/otclient/core/effect.h @@ -10,7 +10,6 @@ public: Effect(); void draw(int x, int y); - void update(int elapsedTime); bool finished() { return m_finished; } @@ -19,7 +18,7 @@ public: EffectPtr asEffect() { return std::static_pointer_cast(shared_from_this()); } private: - int m_timer; + int m_lastTicks; int m_animation; bool m_finished; }; diff --git a/src/otclient/core/map.cpp b/src/otclient/core/map.cpp index 47573cf7..f37fd54f 100644 --- a/src/otclient/core/map.cpp +++ b/src/otclient/core/map.cpp @@ -54,23 +54,6 @@ void Map::draw(int x, int y) m_framebuffer->draw(Rect(x, y, g_graphics.getScreenSize())); } -void Map::update(int elapsedTime) -{ - // Items - - // Effects - for(auto it = m_effects.begin(), end = m_effects.end(); it != end;) { - if((*it)->finished()) { - m_tiles[(*it)->getPosition()]->removeThing(*it, 0); - it = m_effects.erase(it); - } - else { - (*it)->update(elapsedTime); - ++it; - } - } -} - void Map::addThing(ThingPtr thing, uint8 stackpos) { if(!m_tiles[thing->getPosition()]) { @@ -80,7 +63,15 @@ void Map::addThing(ThingPtr thing, uint8 stackpos) m_tiles[thing->getPosition()]->addThing(thing, stackpos); // List with effects and shots to update them. - if(thing->asEffect()) { - m_effects.push_back(thing->asEffect()); + if(EffectPtr effect = thing->asEffect()) { + m_effects.push_back(effect); + } +} + +void Map::removeThing(ThingPtr thing) +{ + if(TilePtr& tile = m_tiles[thing->getPosition()]) { + tile->removeThing(thing, 0); } } + diff --git a/src/otclient/core/map.h b/src/otclient/core/map.h index e2ed1f0b..2da2ef42 100644 --- a/src/otclient/core/map.h +++ b/src/otclient/core/map.h @@ -9,9 +9,9 @@ class Map { public: void draw(int x, int y); - void update(int elapsedTime); void addThing(ThingPtr thing, uint8 stackpos = 0); + void removeThing(ThingPtr thing); private: std::unordered_map m_tiles; diff --git a/src/otclient/core/tile.cpp b/src/otclient/core/tile.cpp index 683e9e4e..d77054bb 100644 --- a/src/otclient/core/tile.cpp +++ b/src/otclient/core/tile.cpp @@ -33,13 +33,9 @@ void Tile::addThing(ThingPtr thing, uint8 stackpos) void Tile::removeThing(ThingPtr thing, uint8 stackpos) { if(thing->asEffect()) { - for(auto it = m_effects.begin(), end = m_effects.end(); it != end; ++it) { - if(thing == *it) { - (*it).reset(); - m_effects.erase(it); - break; - } - } + auto it = std::find(m_effects.begin(), m_effects.end(), thing); + assert(it != m_effects.end()); + m_effects.erase(it); } } diff --git a/src/otclient/otclient.cpp b/src/otclient/otclient.cpp index 36345b98..74b77bf7 100644 --- a/src/otclient/otclient.cpp +++ b/src/otclient/otclient.cpp @@ -1,7 +1,7 @@ #include "otclient.h" #include -#include +#include #include #include #include @@ -44,10 +44,14 @@ void OTClient::init(std::vector args) loadConfigurations(); // create the client window - g_platform.createWindow(g_configs.get("window x"), g_configs.get("window y"), - g_configs.get("window width"), g_configs.get("window height"), - 550, 450, - g_configs.get("window maximized")); + int minWidth = 550; + int minHeight = 450; + int windowX = fw::fromstring(g_configs.get("window x"), 0); + int windowY = fw::fromstring(g_configs.get("window y"), 0); + int windowWidth = fw::fromstring(g_configs.get("window width"), minWidth); + int windowHeight = fw::fromstring(g_configs.get("window height"), minHeight); + bool maximized = fw::fromstring(g_configs.get("window maximized"), false); + g_platform.createWindow(windowX, windowY, windowWidth, windowHeight, minWidth, minHeight, maximized); g_platform.setWindowTitle("OTClient"); // initialize graphics @@ -82,7 +86,6 @@ void OTClient::run() int frameTicks = g_platform.getTicks(); int lastFpsTicks = frameTicks; int lastPollTicks = frameTicks; - int lastUpdateTicks = frameTicks; int frameCount = 0; m_stopping = false; @@ -97,6 +100,7 @@ void OTClient::run() poll(); while(!m_stopping) { + g_platform.updateTicks(); frameTicks = g_platform.getTicks(); // calculate fps @@ -115,12 +119,6 @@ void OTClient::run() lastPollTicks = frameTicks; } - // only update to a maximum of 60 fps - if(frameTicks - lastUpdateTicks >= 1) { - g_map.update(frameTicks - lastUpdateTicks); - lastUpdateTicks = frameTicks; - } - // only render when the windows is visible if(g_platform.isWindowVisible()) { // render begin @@ -224,12 +222,12 @@ void OTClient::loadConfigurations() int defHeight = 450; // sets default window configuration - g_configs.set("window x", (g_platform.getDisplayWidth() - defWidth)/2); - g_configs.set("window y", (g_platform.getDisplayHeight() - defHeight)/2); - g_configs.set("window width", defWidth); - g_configs.set("window height", defHeight); - g_configs.set("window maximized", false); - g_configs.set("vsync", true); + g_configs.set("window x", fw::tostring((g_platform.getDisplayWidth() - defWidth)/2)); + g_configs.set("window y", fw::tostring((g_platform.getDisplayHeight() - defHeight)/2)); + g_configs.set("window width", fw::tostring(defWidth)); + g_configs.set("window height", fw::tostring(defHeight)); + g_configs.set("window maximized", fw::tostring(false)); + g_configs.set("vsync", fw::tostring(true)); // loads user configuration if(!g_configs.load("config.otml")) @@ -239,16 +237,17 @@ void OTClient::loadConfigurations() void OTClient::setupConfigurations() { // activate vertical synchronization? - g_platform.setVerticalSync(g_configs.get("vsync")); + bool vsync = fw::fromstring(g_configs.get("vsync"), true); + g_platform.setVerticalSync(vsync); } void OTClient::saveConfigurations() { - g_configs.set("window x", g_platform.getWindowX()); - g_configs.set("window y", g_platform.getWindowY()); - g_configs.set("window width", g_platform.getWindowWidth()); - g_configs.set("window height", g_platform.getWindowHeight()); - g_configs.set("window maximized", g_platform.isWindowMaximized()); + g_configs.set("window x", fw::tostring(g_platform.getWindowX())); + g_configs.set("window y", fw::tostring(g_platform.getWindowY())); + g_configs.set("window width", fw::tostring(g_platform.getWindowWidth())); + g_configs.set("window height", fw::tostring(g_platform.getWindowHeight())); + g_configs.set("window maximized", fw::tostring(g_platform.isWindowMaximized())); // saves user configuration if(!g_configs.save())