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

master
Eduardo Bart 13 years ago
parent 99d677913a
commit 758b4b5dfb

@ -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

@ -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")

@ -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;
}

@ -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

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

@ -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;

@ -38,6 +38,7 @@ struct X11PlatformPrivate {
int height;
int x;
int y;
int lastTicks;
std::string clipboardText;
std::map<int, uchar> 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)

@ -193,14 +193,23 @@ R safe_cast(const T& t) {
/// Usage:
/// R r = fw::unsafe_cast<R>(t);
template<typename R, typename T>
R unsafe_cast(const T& t) {
R r;
R unsafe_cast(const T& t, R def = R()) {
try {
r = safe_cast<R,T>(t);
return safe_cast<R,T>(t);
} catch(bad_cast& e) {
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

@ -1,34 +1,27 @@
#include "effect.h"
#include "datmanager.h"
#include "map.h"
#include <framework/platform/platform.h>
#include <framework/core/eventdispatcher.h>
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()

@ -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<Effect>(shared_from_this()); }
private:
int m_timer;
int m_lastTicks;
int m_animation;
bool m_finished;
};

@ -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);
}
}

@ -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<Position, TilePtr, PositionHasher> m_tiles;

@ -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);
}
}

@ -1,7 +1,7 @@
#include "otclient.h"
#include <framework/core/modulemanager.h>
#include <framework/core/configmanager.h>
#include <framework/core/configs.h>
#include <framework/core/resourcemanager.h>
#include <framework/core/eventdispatcher.h>
#include <framework/luascript/luainterface.h>
@ -44,10 +44,14 @@ void OTClient::init(std::vector<std::string> 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())

Loading…
Cancel
Save