display motd message only once, remove update loop, use g_platform.getTicks() instead

This commit is contained in:
Eduardo Bart 2011-08-16 09:47:30 -03:00
parent 99d677913a
commit 758b4b5dfb
14 changed files with 86 additions and 150 deletions

View File

@ -89,7 +89,7 @@ SET(SOURCES
src/framework/util/translator.cpp src/framework/util/translator.cpp
# framework core # framework core
src/framework/core/configmanager.cpp src/framework/core/configs.cpp
src/framework/core/resourcemanager.cpp src/framework/core/resourcemanager.cpp
src/framework/core/eventdispatcher.cpp src/framework/core/eventdispatcher.cpp
src/framework/core/modulemanager.cpp src/framework/core/modulemanager.cpp

View File

@ -23,8 +23,11 @@ function EnterGame_connectToLoginServer()
loadBox:destroy() loadBox:destroy()
local motdNumber = string.sub(motd, 0, string.find(motd, "\n")) local motdNumber = string.sub(motd, 0, string.find(motd, "\n"))
local motdText = string.sub(motd, string.find(motd, "\n") + 1, string.len(motd)) local motdText = string.sub(motd, string.find(motd, "\n") + 1, string.len(motd))
if motdNumber ~= Configs.get("motd") then
displayInfoBox("Message of the day", motdText) displayInfoBox("Message of the day", motdText)
mainMenu.visible = false Configs.set("motd", motdNumber)
end
mainMenu:hide()
end end
local enterGameWindow = rootWidget:getChild("enterGameWindow") local enterGameWindow = rootWidget:getChild("enterGameWindow")

View File

@ -1,35 +0,0 @@
#include "configmanager.h"
#include "resourcemanager.h"
#include <framework/otml/otml.h>
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;
}

View File

@ -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<std::string>(value); }
operator float() const { return fw::unsafe_cast<float>(value); }
operator int() const { return fw::unsafe_cast<int>(value); }
operator bool() const { return fw::unsafe_cast<bool>(value); }
std::string value;
};
class ConfigManager
{
public:
bool load(const std::string& fileName);
bool save();
template<class T>
void set(const std::string& key, const T& value) { m_confsMap[key] = fw::unsafe_cast<std::string>(value); }
ConfigValueProxy get(const std::string& key) { return ConfigValueProxy(m_confsMap[key]); }
private:
std::string m_fileName;
std::map<std::string, std::string> m_confsMap;
};
extern ConfigManager g_configs;
#endif

View File

@ -3,6 +3,7 @@
#include <framework/ui/ui.h> #include <framework/ui/ui.h>
#include <framework/net/protocol.h> #include <framework/net/protocol.h>
#include <framework/core/eventdispatcher.h> #include <framework/core/eventdispatcher.h>
#include <framework/core/configs.h>
void LuaInterface::registerFunctions() void LuaInterface::registerFunctions()
{ {
@ -30,6 +31,9 @@ void LuaInterface::registerFunctions()
g_lua.bindClassMemberFunction<UIWidget>("getChild", &UIWidget::getChildById); g_lua.bindClassMemberFunction<UIWidget>("getChild", &UIWidget::getChildById);
g_lua.bindClassMemberFunction<UIWidget>("addChild", &UIWidget::addChild); g_lua.bindClassMemberFunction<UIWidget>("addChild", &UIWidget::addChild);
g_lua.bindClassMemberFunction<UIWidget>("lock", &UIWidget::lock); g_lua.bindClassMemberFunction<UIWidget>("lock", &UIWidget::lock);
g_lua.bindClassMemberFunction<UIWidget>("hide", &UIWidget::hide);
g_lua.bindClassMemberFunction<UIWidget>("show", &UIWidget::show);
// UILabel // UILabel
g_lua.registerClass<UILabel, UIWidget>(); g_lua.registerClass<UILabel, UIWidget>();
@ -55,6 +59,11 @@ void LuaInterface::registerFunctions()
// Protocol // Protocol
g_lua.registerClass<Protocol>(); g_lua.registerClass<Protocol>();
// ConfigManager
g_lua.registerClass<Configs>();
g_lua.bindClassStaticFunction<Configs>("set", std::bind(&Configs::set, &g_configs, _1, _2));
g_lua.bindClassStaticFunction<Configs>("get", std::bind(&Configs::get, &g_configs, _1));
// global functions // global functions
g_lua.bindGlobalFunction("importFont", std::bind(&FontManager::importFont, &g_fonts, _1)); g_lua.bindGlobalFunction("importFont", std::bind(&FontManager::importFont, &g_fonts, _1));
g_lua.bindGlobalFunction("importStyles", std::bind(&UIManager::importStyles, &g_ui, _1)); g_lua.bindGlobalFunction("importStyles", std::bind(&UIManager::importStyles, &g_ui, _1));

View File

@ -14,8 +14,11 @@ public:
/// Poll platform input/window events /// Poll platform input/window events
void poll(); void poll();
/// Get current time in milliseconds since init void updateTicks();
int getTicks();
/// Get current time in milliseconds since last application init
int getTicks() { return m_lastTicks; }
/// Sleep in current thread /// Sleep in current thread
void sleep(ulong ms); void sleep(ulong ms);
@ -58,6 +61,7 @@ public:
private: private:
PlatformListener* m_listener; PlatformListener* m_listener;
int m_lastTicks;
}; };
extern Platform g_platform; extern Platform g_platform;

View File

@ -38,6 +38,7 @@ struct X11PlatformPrivate {
int height; int height;
int x; int x;
int y; int y;
int lastTicks;
std::string clipboardText; std::string clipboardText;
std::map<int, uchar> keyMap; std::map<int, uchar> keyMap;
PlatformListener* listener; 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); x11.atomWindowMaximizedHorz = XInternAtom(x11.display, "_NET_WM_STATE_MAXIMIZED_HORZ", False);
// force first tick // force first tick
Platform::getTicks(); updateTicks();
} }
void Platform::terminate() void Platform::terminate()
@ -447,7 +448,7 @@ void Platform::poll()
} }
} }
int Platform::getTicks() void Platform::updateTicks()
{ {
static timeval tv; static timeval tv;
static ulong firstTick = 0; static ulong firstTick = 0;
@ -456,7 +457,7 @@ int Platform::getTicks()
if(!firstTick) if(!firstTick)
firstTick = tv.tv_sec; 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) void Platform::sleep(ulong miliseconds)

View File

@ -193,14 +193,23 @@ R safe_cast(const T& t) {
/// Usage: /// Usage:
/// R r = fw::unsafe_cast<R>(t); /// R r = fw::unsafe_cast<R>(t);
template<typename R, typename T> template<typename R, typename T>
R unsafe_cast(const T& t) { R unsafe_cast(const T& t, R def = R()) {
R r;
try { try {
r = safe_cast<R,T>(t); return safe_cast<R,T>(t);
} catch(bad_cast& e) { } catch(bad_cast& e) {
println(e.what()); println(e.what());
return def;
} }
return r; }
template<typename T>
std::string tostring(const T& t) {
return unsafe_cast<std::string, T>(t);
}
template<typename T>
T fromstring(const std::string& str, T def = T()) {
return unsafe_cast<T, std::string>(str, def);
} }
// an empty string to use anywhere needed // an empty string to use anywhere needed

View File

@ -1,34 +1,27 @@
#include "effect.h" #include "effect.h"
#include "datmanager.h" #include "datmanager.h"
#include "map.h"
#include <framework/platform/platform.h>
#include <framework/core/eventdispatcher.h>
Effect::Effect() : Thing(THING_EFFECT) Effect::Effect() : Thing(THING_EFFECT)
{ {
m_timer = 0; m_lastTicks = g_platform.getTicks();
m_animation = 0; m_animation = 0;
m_finished = false; m_finished = false;
} }
void Effect::draw(int x, int y) void Effect::draw(int x, int y)
{ {
internalDraw(x, y, 0, 0, 0, 0, m_animation); if(!m_finished && g_platform.getTicks() - m_lastTicks > 75) {
}
void Effect::update(int elapsedTime)
{
if(m_finished)
return;
if(m_timer > 75) {
const ThingAttributes& attributes = getAttributes(); const ThingAttributes& attributes = getAttributes();
if(m_animation+1 == attributes.animcount) {
m_finished = true;
return;
}
m_animation++; 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() const ThingAttributes& Effect::getAttributes()

View File

@ -10,7 +10,6 @@ public:
Effect(); Effect();
void draw(int x, int y); void draw(int x, int y);
void update(int elapsedTime);
bool finished() { return m_finished; } bool finished() { return m_finished; }
@ -19,7 +18,7 @@ public:
EffectPtr asEffect() { return std::static_pointer_cast<Effect>(shared_from_this()); } EffectPtr asEffect() { return std::static_pointer_cast<Effect>(shared_from_this()); }
private: private:
int m_timer; int m_lastTicks;
int m_animation; int m_animation;
bool m_finished; bool m_finished;
}; };

View File

@ -54,23 +54,6 @@ void Map::draw(int x, int y)
m_framebuffer->draw(Rect(x, y, g_graphics.getScreenSize())); 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) void Map::addThing(ThingPtr thing, uint8 stackpos)
{ {
if(!m_tiles[thing->getPosition()]) { if(!m_tiles[thing->getPosition()]) {
@ -80,7 +63,15 @@ void Map::addThing(ThingPtr thing, uint8 stackpos)
m_tiles[thing->getPosition()]->addThing(thing, stackpos); m_tiles[thing->getPosition()]->addThing(thing, stackpos);
// List with effects and shots to update them. // List with effects and shots to update them.
if(thing->asEffect()) { if(EffectPtr effect = thing->asEffect()) {
m_effects.push_back(thing->asEffect()); m_effects.push_back(effect);
} }
} }
void Map::removeThing(ThingPtr thing)
{
if(TilePtr& tile = m_tiles[thing->getPosition()]) {
tile->removeThing(thing, 0);
}
}

View File

@ -9,9 +9,9 @@ class Map
{ {
public: public:
void draw(int x, int y); void draw(int x, int y);
void update(int elapsedTime);
void addThing(ThingPtr thing, uint8 stackpos = 0); void addThing(ThingPtr thing, uint8 stackpos = 0);
void removeThing(ThingPtr thing);
private: private:
std::unordered_map<Position, TilePtr, PositionHasher> m_tiles; std::unordered_map<Position, TilePtr, PositionHasher> m_tiles;

View File

@ -33,13 +33,9 @@ void Tile::addThing(ThingPtr thing, uint8 stackpos)
void Tile::removeThing(ThingPtr thing, uint8 stackpos) void Tile::removeThing(ThingPtr thing, uint8 stackpos)
{ {
if(thing->asEffect()) { if(thing->asEffect()) {
for(auto it = m_effects.begin(), end = m_effects.end(); it != end; ++it) { auto it = std::find(m_effects.begin(), m_effects.end(), thing);
if(thing == *it) { assert(it != m_effects.end());
(*it).reset();
m_effects.erase(it); m_effects.erase(it);
break;
}
}
} }
} }

View File

@ -1,7 +1,7 @@
#include "otclient.h" #include "otclient.h"
#include <framework/core/modulemanager.h> #include <framework/core/modulemanager.h>
#include <framework/core/configmanager.h> #include <framework/core/configs.h>
#include <framework/core/resourcemanager.h> #include <framework/core/resourcemanager.h>
#include <framework/core/eventdispatcher.h> #include <framework/core/eventdispatcher.h>
#include <framework/luascript/luainterface.h> #include <framework/luascript/luainterface.h>
@ -44,10 +44,14 @@ void OTClient::init(std::vector<std::string> args)
loadConfigurations(); loadConfigurations();
// create the client window // create the client window
g_platform.createWindow(g_configs.get("window x"), g_configs.get("window y"), int minWidth = 550;
g_configs.get("window width"), g_configs.get("window height"), int minHeight = 450;
550, 450, int windowX = fw::fromstring(g_configs.get("window x"), 0);
g_configs.get("window maximized")); 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"); g_platform.setWindowTitle("OTClient");
// initialize graphics // initialize graphics
@ -82,7 +86,6 @@ void OTClient::run()
int frameTicks = g_platform.getTicks(); int frameTicks = g_platform.getTicks();
int lastFpsTicks = frameTicks; int lastFpsTicks = frameTicks;
int lastPollTicks = frameTicks; int lastPollTicks = frameTicks;
int lastUpdateTicks = frameTicks;
int frameCount = 0; int frameCount = 0;
m_stopping = false; m_stopping = false;
@ -97,6 +100,7 @@ void OTClient::run()
poll(); poll();
while(!m_stopping) { while(!m_stopping) {
g_platform.updateTicks();
frameTicks = g_platform.getTicks(); frameTicks = g_platform.getTicks();
// calculate fps // calculate fps
@ -115,12 +119,6 @@ void OTClient::run()
lastPollTicks = frameTicks; 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 // only render when the windows is visible
if(g_platform.isWindowVisible()) { if(g_platform.isWindowVisible()) {
// render begin // render begin
@ -224,12 +222,12 @@ void OTClient::loadConfigurations()
int defHeight = 450; int defHeight = 450;
// sets default window configuration // sets default window configuration
g_configs.set("window x", (g_platform.getDisplayWidth() - defWidth)/2); g_configs.set("window x", fw::tostring((g_platform.getDisplayWidth() - defWidth)/2));
g_configs.set("window y", (g_platform.getDisplayHeight() - defHeight)/2); g_configs.set("window y", fw::tostring((g_platform.getDisplayHeight() - defHeight)/2));
g_configs.set("window width", defWidth); g_configs.set("window width", fw::tostring(defWidth));
g_configs.set("window height", defHeight); g_configs.set("window height", fw::tostring(defHeight));
g_configs.set("window maximized", false); g_configs.set("window maximized", fw::tostring(false));
g_configs.set("vsync", true); g_configs.set("vsync", fw::tostring(true));
// loads user configuration // loads user configuration
if(!g_configs.load("config.otml")) if(!g_configs.load("config.otml"))
@ -239,16 +237,17 @@ void OTClient::loadConfigurations()
void OTClient::setupConfigurations() void OTClient::setupConfigurations()
{ {
// activate vertical synchronization? // 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() void OTClient::saveConfigurations()
{ {
g_configs.set("window x", g_platform.getWindowX()); g_configs.set("window x", fw::tostring(g_platform.getWindowX()));
g_configs.set("window y", g_platform.getWindowY()); g_configs.set("window y", fw::tostring(g_platform.getWindowY()));
g_configs.set("window width", g_platform.getWindowWidth()); g_configs.set("window width", fw::tostring(g_platform.getWindowWidth()));
g_configs.set("window height", g_platform.getWindowHeight()); g_configs.set("window height", fw::tostring(g_platform.getWindowHeight()));
g_configs.set("window maximized", g_platform.isWindowMaximized()); g_configs.set("window maximized", fw::tostring(g_platform.isWindowMaximized()));
// saves user configuration // saves user configuration
if(!g_configs.save()) if(!g_configs.save())