diff --git a/CMakeLists.txt b/CMakeLists.txt index 0f2bce00..96aefcc7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -40,7 +40,7 @@ INCLUDE_DIRECTORIES( ${PHYSFS_INCLUDE_DIR} ${GMP_INCLUDE_DIR} ${ZLIB_INCLUDE_DIR} - "${CMAKE_CURRENT_SOURCE_DIR}/src/framework" + "${CMAKE_CURRENT_SOURCE_DIR}/src" ) IF(CMAKE_BUILD_TYPE STREQUAL "Debug") @@ -48,23 +48,29 @@ IF(CMAKE_BUILD_TYPE STREQUAL "Debug") ENDIF(CMAKE_BUILD_TYPE STREQUAL "Debug") SET(SOURCES + # main src/main.cpp - src/otclient.cpp - src/otclientluafunctions.cpp - - src/game.cpp - src/map.cpp - src/protocollogin.cpp - src/protocolgame.cpp - src/protocolgamesend.cpp - src/protocolgameparse.cpp - src/tibiadat.cpp - src/tibiaspr.cpp - src/item.cpp - src/tile.cpp - src/thing.cpp - src/creature.cpp - src/effect.cpp + + # otclient + src/otclient/otclient.cpp + src/otclient/otclientluafunctions.cpp + + # otclient core + src/otclient/core/game.cpp + src/otclient/core/map.cpp + src/otclient/core/tibiadat.cpp + src/otclient/core/tibiaspr.cpp + src/otclient/core/item.cpp + src/otclient/core/tile.cpp + src/otclient/core/thing.cpp + src/otclient/core/creature.cpp + src/otclient/core/effect.cpp + + # otclient net + src/otclient/net/protocollogin.cpp + src/otclient/net/protocolgame.cpp + src/otclient/net/protocolgamesend.cpp + src/otclient/net/protocolgameparse.cpp # framework third party src/framework/thirdparty/apngloader.cpp @@ -126,7 +132,7 @@ SET(SOURCES ) IF(WIN32) - SET(SOURCES ${SOURCES} src/framework/core/win32platform.cpp) + SET(SOURCES ${SOURCES} src/framework/platform/win32platform.cpp) SET(ADDITIONAL_LIBRARIES ws2_32) IF(CMAKE_COMPILER_IS_GNUCXX) SET(CMAKE_CXX_LINK_FLAGS "${CMAKE_CXX_LINK_FLAGS} -mwindows") @@ -134,7 +140,7 @@ IF(WIN32) ENDIF(CMAKE_COMPILER_IS_GNUCXX) ADD_DEFINITIONS(-DWIN32_NO_CONSOLE) ELSE(WIN32) - SET(SOURCES ${SOURCES} src/framework/core/x11platform.cpp) + SET(SOURCES ${SOURCES} src/framework/platform/x11platform.cpp) ENDIF(WIN32) # target executable @@ -153,7 +159,7 @@ TARGET_LINK_LIBRARIES( ) IF(USE_PCH) - ADD_PRECOMPILED_HEADER(otclient ${CMAKE_CURRENT_SOURCE_DIR}/src/framework/pch.h) + ADD_PRECOMPILED_HEADER(otclient ${CMAKE_CURRENT_SOURCE_DIR}/src/pch.h) ENDIF(USE_PCH) # installation diff --git a/src/creature.cpp b/src/creature.cpp deleted file mode 100644 index 431474c9..00000000 --- a/src/creature.cpp +++ /dev/null @@ -1,64 +0,0 @@ -#include "creature.h" -#include "tibiadat.h" -#include "graphics/graphics.h" -#include -#include "game.h" - -#include -#include -#include - - - - - - -// OW BART TEM COMO USAR 2 FRAMEBUFFER? -// SERIA O IDEAL PARA DESENHAR A COR DOS BONEQUIN. - - - - - - -Creature::Creature() -{ - m_type = Thing::TYPE_CREATURE; -} - -ThingAttributes *Creature::getAttributes() -{ - return g_tibiaDat.getCreatureAttributes(m_outfit.type); -} - -void Creature::draw(int x, int y) -{ - //ThingAttributes *creatureAttributes = getAttributes(); - int anim = 0; - - // draw outfit - internalDraw(x, y, 0, m_direction, 0, 0, anim); - - // draw addons - //for(int a = 0; a < m_outfit.addons; ++a) { - //internalDraw(x, y, 0, m_direction, m_outfit.addons & (1 << a), 0, anim); - //} - //glPushAttrib(GL_CURRENT_BIT | GL_COLOR_BUFFER_BIT); - - //glBlendFunc(GL_DST_COLOR, GL_ONE_MINUS_SRC_ALPHA); - - //Color colors[4] = {Color::yellow, Color::red, Color::green, Color::blue}; - - //for(int i = 0; i < 4; ++i) { - - - /*g_graphics.bindColor(colors[i]); - internalDraw(creatureAttributes->width*32 - 32, creatureAttributes->height*32 - 32, 1, m_direction, 0, 0, 0); - framebuffer.unbind(); - - - g_graphics.bindColor(Color::green);*/ - //framebuffer.draw(x, y, creatureAttributes->width*32, creatureAttributes->height*32); - //} - //glPopAttrib(); -} diff --git a/src/creature.h b/src/creature.h deleted file mode 100644 index 3eb0aee3..00000000 --- a/src/creature.h +++ /dev/null @@ -1,51 +0,0 @@ -#ifndef CREATURE_H -#define CREATURE_H - -#include -#include "thing.h" - -struct Outfit -{ - uint16 type; - uint8 head; - uint8 body; - uint8 legs; - uint8 feet; - uint8 addons; -}; - -class Creature; -typedef std::shared_ptr CreaturePtr; - -class Creature : public Thing -{ -public: - Creature(); - - virtual ThingAttributes *getAttributes(); - void draw(int x, int y); - - void setName(const std::string& name) { m_name = name; } - std::string getName() { return m_name; } - - void setHealthPercent(uint8 healthPercent) { m_healthPercent = healthPercent; } - uint8 getHealthPercent() { return m_healthPercent; } - - void setDirection(Direction direction) { m_direction = direction; } - Direction getDirection() { return m_direction; } - - void setOutfit(const Outfit& outfit) { m_outfit = outfit; } - Outfit getOutfit() { return m_outfit; } - - virtual Creature *getCreature() { return this; } - virtual const Creature *getCreature() const { return this; } - -private: - std::string m_name; - uint8 m_healthPercent; - Direction m_direction; - Outfit m_outfit; - -}; - -#endif // CREATURE_H diff --git a/src/effect.cpp b/src/effect.cpp deleted file mode 100644 index fcdec57f..00000000 --- a/src/effect.cpp +++ /dev/null @@ -1,18 +0,0 @@ -#include "effect.h" -#include "tibiadat.h" - -Effect::Effect() -{ - m_type = Thing::TYPE_EFFECT; -} - -ThingAttributes *Effect::getAttributes() -{ - return g_tibiaDat.getEffectAttributes(m_id); -} - -void Effect::draw(int x, int y) -{ - int anim = 0; - internalDraw(x, y, 0, 0, 0, 0, anim); -} diff --git a/src/effect.h b/src/effect.h deleted file mode 100644 index 1a1cc73c..00000000 --- a/src/effect.h +++ /dev/null @@ -1,21 +0,0 @@ -#ifndef EFFECT_H -#define EFFECT_H - -#include -#include "thing.h" - -class Effect; -typedef std::shared_ptr EffectPtr; - -class Effect : public Thing -{ -public: - Effect(); - - virtual ThingAttributes *getAttributes(); - void draw(int x, int y); - -private: -}; - -#endif // EFFECT_H diff --git a/src/framework/const.h b/src/framework/const.h index 343e3685..f3abb3d7 100644 --- a/src/framework/const.h +++ b/src/framework/const.h @@ -1,7 +1,7 @@ -#ifndef CONST_H -#define CONST_H +#ifndef FRAMEWORK_CONST_H +#define FRAMEWORK_CONST_H -//namespace Fw { +//namespace fw { enum AlignmentFlag { AlignLeft = 1, diff --git a/src/framework/core/configmanager.cpp b/src/framework/core/configmanager.cpp index 6e792d2a..42d24dd3 100644 --- a/src/framework/core/configmanager.cpp +++ b/src/framework/core/configmanager.cpp @@ -1,7 +1,7 @@ #include "configmanager.h" #include "resourcemanager.h" -#include +#include ConfigManager g_configs; diff --git a/src/framework/core/configmanager.h b/src/framework/core/configmanager.h index 89d78ed4..0e754c3d 100644 --- a/src/framework/core/configmanager.h +++ b/src/framework/core/configmanager.h @@ -1,14 +1,14 @@ #ifndef CONFIGMANAGER_H #define CONFIGMANAGER_H -#include +#include "declarations.h" struct ConfigValueProxy { ConfigValueProxy(const std::string& v) : value(v) { } - operator std::string() const { return aux::unsafe_cast(value); } - operator float() const { return aux::unsafe_cast(value); } - operator int() const { return aux::unsafe_cast(value); } - operator bool() const { return aux::unsafe_cast(value); } + 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; }; @@ -19,7 +19,7 @@ public: bool save(); template - void set(const std::string& key, const T& value) { m_confsMap[key] = aux::unsafe_cast(value); } + 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]); } diff --git a/src/framework/core/declarations.h b/src/framework/core/declarations.h new file mode 100644 index 00000000..d162016b --- /dev/null +++ b/src/framework/core/declarations.h @@ -0,0 +1,9 @@ +#ifndef FRAMEWORK_CORE_DECLARATIONS_H +#define FRAMEWORK_CORE_DECLARATIONS_H + +#include + +class Module; +typedef std::shared_ptr ModulePtr; + +#endif diff --git a/src/framework/core/eventdispatcher.cpp b/src/framework/core/eventdispatcher.cpp index c346ac80..7af725db 100644 --- a/src/framework/core/eventdispatcher.cpp +++ b/src/framework/core/eventdispatcher.cpp @@ -1,5 +1,6 @@ #include "eventdispatcher.h" -#include + +#include EventDispatcher g_dispatcher; diff --git a/src/framework/core/eventdispatcher.h b/src/framework/core/eventdispatcher.h index 6fb58bcd..337d64e0 100644 --- a/src/framework/core/eventdispatcher.h +++ b/src/framework/core/eventdispatcher.h @@ -1,7 +1,7 @@ #ifndef EVENTDISPATCHER_H #define EVENTDISPATCHER_H -#include +#include "declarations.h" struct ScheduledEvent { ScheduledEvent(int ticks, const SimpleCallback& callback) : ticks(ticks), callback(callback) { } diff --git a/src/framework/core/module.cpp b/src/framework/core/module.cpp index 57f68ca4..43510641 100644 --- a/src/framework/core/module.cpp +++ b/src/framework/core/module.cpp @@ -1,9 +1,9 @@ #include "module.h" - -#include -#include #include "modulemanager.h" +#include +#include + void Module::discover(const OTMLNodePtr& moduleNode) { m_description = moduleNode->readAt("description"); diff --git a/src/framework/core/module.h b/src/framework/core/module.h index 44ad121a..8d7d964f 100644 --- a/src/framework/core/module.h +++ b/src/framework/core/module.h @@ -1,10 +1,9 @@ #ifndef MODULE_H #define MODULE_H -#include +#include "declarations.h" -class Module; -typedef std::shared_ptr ModulePtr; +#include class Module { diff --git a/src/framework/core/modulemanager.cpp b/src/framework/core/modulemanager.cpp index 4964f85d..7b84ead0 100644 --- a/src/framework/core/modulemanager.cpp +++ b/src/framework/core/modulemanager.cpp @@ -1,6 +1,7 @@ #include "modulemanager.h" #include "resourcemanager.h" -#include + +#include ModuleManager g_modules; diff --git a/src/framework/core/resourcemanager.cpp b/src/framework/core/resourcemanager.cpp index 604471e1..a8ac4187 100644 --- a/src/framework/core/resourcemanager.cpp +++ b/src/framework/core/resourcemanager.cpp @@ -1,7 +1,7 @@ #include "resourcemanager.h" -#include -#include +#include +#include #include @@ -85,7 +85,7 @@ void ResourceManager::loadFile(const std::string& fileName, std::iostream& out) PHYSFS_file* file = PHYSFS_openRead(fullPath.c_str()); if(!file) { out.clear(std::ios::failbit); - throw std::runtime_error(aux::make_string("failed to load file '", fullPath.c_str(), "': ", PHYSFS_getLastError())); + throw std::runtime_error(fw::mkstr("failed to load file '", fullPath.c_str(), "': ", PHYSFS_getLastError())); } else { int fileSize = PHYSFS_fileLength(file); if(fileSize > 0) { diff --git a/src/framework/core/resourcemanager.h b/src/framework/core/resourcemanager.h index 7bf71ef9..c94fec2f 100644 --- a/src/framework/core/resourcemanager.h +++ b/src/framework/core/resourcemanager.h @@ -1,7 +1,7 @@ #ifndef RESOURCES_H #define RESOURCES_H -#include +#include "declarations.h" class ResourceManager { diff --git a/src/framework/global.h b/src/framework/global.h index c20ee83e..20134749 100644 --- a/src/framework/global.h +++ b/src/framework/global.h @@ -1,5 +1,5 @@ -#ifndef GLOBAL_H -#define GLOBAL_H +#ifndef FRAMEWORK_GLOBAL_H +#define FRAMEWORK_GLOBAL_H // common C headers #include @@ -28,23 +28,22 @@ #include #include #include +#include // boost utilities #include -// constants -#include +// global constants +#include "const.h" // additional utilities -#include -#include -#include -#include - -// custom types -#include -#include -#include -#include +#include "util/types.h" +#include "util/tools.h" +#include "util/logger.h" +#include "util/translator.h" +#include "util/point.h" +#include "util/color.h" +#include "util/rect.h" +#include "util/size.h" #endif diff --git a/src/framework/graphics/animatedtexture.cpp b/src/framework/graphics/animatedtexture.cpp index e9cc1660..7880bfaa 100644 --- a/src/framework/graphics/animatedtexture.cpp +++ b/src/framework/graphics/animatedtexture.cpp @@ -1,7 +1,8 @@ #include "animatedtexture.h" #include "graphics.h" -#include -#include + +#include +#include #include diff --git a/src/framework/graphics/borderimage.cpp b/src/framework/graphics/borderimage.cpp index 434053af..c417b146 100644 --- a/src/framework/graphics/borderimage.cpp +++ b/src/framework/graphics/borderimage.cpp @@ -3,7 +3,7 @@ #include "texture.h" #include "texturemanager.h" -#include +#include BorderImage::BorderImage(TexturePtr texture, const Rect& left, diff --git a/src/framework/graphics/graphicsdeclarations.h b/src/framework/graphics/declarations.h similarity index 79% rename from src/framework/graphics/graphicsdeclarations.h rename to src/framework/graphics/declarations.h index b02f3d67..211fd745 100644 --- a/src/framework/graphics/graphicsdeclarations.h +++ b/src/framework/graphics/declarations.h @@ -1,7 +1,7 @@ -#ifndef GRAPHICSDECLARATIONS_H -#define GRAPHICSDECLARATIONS_H +#ifndef FRAMEWORK_GRAPHICS_DECLARATIONS_H +#define FRAMEWORK_GRAPHICS_DECLARATIONS_H -#include +#include class Texture; class Font; diff --git a/src/framework/graphics/font.cpp b/src/framework/graphics/font.cpp index 30d33ad6..07c5be2a 100644 --- a/src/framework/graphics/font.cpp +++ b/src/framework/graphics/font.cpp @@ -2,7 +2,7 @@ #include "texturemanager.h" #include "graphics.h" -#include +#include void Font::load(const OTMLNodePtr& fontNode) { @@ -24,7 +24,7 @@ void Font::load(const OTMLNodePtr& fontNode) // read custom widths if(OTMLNodePtr node = fontNode->get("glyph widths")) { for(const OTMLNodePtr& child : node->childNodes()) - m_glyphsSize[aux::safe_cast(child->tag())].setWidth(child->read()); + m_glyphsSize[fw::safe_cast(child->tag())].setWidth(child->read()); } // calculate glyphs texture coords @@ -130,8 +130,6 @@ void Font::renderText(const std::string& text, } g_graphics.stopDrawing(); - - g_graphics.bindColor(Color::white); } const std::vector& Font::calculateGlyphsPositions(const std::string& text, diff --git a/src/framework/graphics/font.h b/src/framework/graphics/font.h index da55c1e4..c0ca086e 100644 --- a/src/framework/graphics/font.h +++ b/src/framework/graphics/font.h @@ -1,8 +1,9 @@ #ifndef FONT_H #define FONT_H -#include "graphicsdeclarations.h" -#include +#include "declarations.h" + +#include class Font { diff --git a/src/framework/graphics/fontmanager.cpp b/src/framework/graphics/fontmanager.cpp index 8e58777c..43faa349 100644 --- a/src/framework/graphics/fontmanager.cpp +++ b/src/framework/graphics/fontmanager.cpp @@ -1,7 +1,7 @@ #include "fontmanager.h" -#include -#include +#include +#include FontManager g_fonts; @@ -64,6 +64,6 @@ FontPtr FontManager::getDefaultFont() { // default font should always exists, otherwise the app may crash if(!m_defaultFont) - throw std::runtime_error("no default font to display, cannot continue to run"); + logFatal("FATAL ERROR: no default font to display, cannot continue to run"); return m_defaultFont; } diff --git a/src/framework/graphics/fontmanager.h b/src/framework/graphics/fontmanager.h index a413f497..deeabd48 100644 --- a/src/framework/graphics/fontmanager.h +++ b/src/framework/graphics/fontmanager.h @@ -6,9 +6,10 @@ class FontManager { public: - // Release fonts references, thus making possible to destruct them + /// Release fonts references, thus making possible to destruct them void releaseFonts(); + /// Import a font from .otfont file bool importFont(std::string fontFile); bool fontExists(const std::string& fontName); diff --git a/src/framework/graphics/framebuffer.cpp b/src/framework/graphics/framebuffer.cpp index 4eef35ce..4b070e28 100644 --- a/src/framework/graphics/framebuffer.cpp +++ b/src/framework/graphics/framebuffer.cpp @@ -1,6 +1,9 @@ #include "framebuffer.h" #include "graphics.h" -#include +#include "texture.h" + +#include + #include #include #include @@ -14,22 +17,13 @@ PFNGLCHECKFRAMEBUFFERSTATUSPROC oglCheckFramebufferStatus = 0; FrameBuffer::FrameBuffer(int width, int height) { m_fbo = 0; - m_width = width; - m_height = height; // create FBO texture - glGenTextures(1, &m_fboTexture); - glBindTexture(GL_TEXTURE_2D, m_fboTexture); - glTexImage2D(GL_TEXTURE_2D, 0, 4, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - - // we want bilinear filtering (for a smooth framebuffer) - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + m_texture = TexturePtr(new Texture(width, height, 4)); + m_texture->enableBilinearFilter(); // use FBO ext only if supported - if(g_graphics.isExtensionSupported("GL_ARB_framebuffer_object")) { + if(false && g_graphics.isExtensionSupported("GL_ARB_framebuffer_object")) { m_fallbackOldImp = false; if(!oglGenFramebuffers) { oglGenFramebuffers = (PFNGLGENFRAMEBUFFERSPROC)g_platform.getExtensionProcAddress("glGenFramebuffers"); @@ -44,7 +38,7 @@ FrameBuffer::FrameBuffer(int width, int height) oglBindFramebuffer(GL_FRAMEBUFFER_EXT, m_fbo); // attach 2D texture to this FBO - oglFramebufferTexture2D(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, m_fboTexture, 0); + oglFramebufferTexture2D(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, m_texture->getId(), 0); GLenum status = oglCheckFramebufferStatus(GL_FRAMEBUFFER_EXT); switch(status) { @@ -58,6 +52,8 @@ FrameBuffer::FrameBuffer(int width, int height) // restore back buffer oglBindFramebuffer(GL_FRAMEBUFFER_EXT, 0); + glDrawBuffer(GL_BACK); + glReadBuffer(GL_BACK); } else { // otherwise fallback to copy texture from screen implementation m_fallbackOldImp = true; @@ -66,7 +62,6 @@ FrameBuffer::FrameBuffer(int width, int height) FrameBuffer::~FrameBuffer() { - glDeleteTextures(1, &m_fboTexture); if(m_fbo) oglDeleteFramebuffers(1, &m_fbo); } @@ -79,10 +74,10 @@ void FrameBuffer::bind() } // setup framebuffer viewport - glViewport(0, 0, m_width, m_height); + glViewport(0, 0, m_texture->getWidth(), m_texture->getHeight()); glMatrixMode(GL_PROJECTION); glLoadIdentity(); - gluOrtho2D(0.0f, m_width, 0, m_height); + gluOrtho2D(0.0f, m_texture->getWidth(), 0, m_texture->getHeight()); // back to model view glMatrixMode(GL_MODELVIEW); @@ -105,8 +100,8 @@ void FrameBuffer::unbind() g_graphics.restoreViewport(); } else { // copy screen to texture - glBindTexture(GL_TEXTURE_2D, m_fboTexture); - glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, m_width, m_height); + glBindTexture(GL_TEXTURE_2D, m_texture->getId()); + glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, m_texture->getWidth(), m_texture->getHeight()); // restore graphics viewport g_graphics.restoreViewport(); @@ -117,14 +112,7 @@ void FrameBuffer::unbind() } } -void FrameBuffer::draw(int x, int y, int width, int height) +void FrameBuffer::draw(const Rect& screenCoords, const Rect& framebufferCoords) { - glColor4ubv(Color::white.rgbaPtr()); - glBindTexture(GL_TEXTURE_2D, m_fboTexture); - glBegin(GL_QUADS); - glTexCoord2i(0, 0); glVertex2i(x, y); - glTexCoord2i(0, 1); glVertex2i(x, y+height); - glTexCoord2i(1, 1); glVertex2i(x+width, y+height); - glTexCoord2i(1, 0); glVertex2i(x+width, y); - glEnd(); + g_graphics.drawTexturedRect(screenCoords, m_texture ,framebufferCoords); } diff --git a/src/framework/graphics/framebuffer.h b/src/framework/graphics/framebuffer.h index bf5da242..6c4586e2 100644 --- a/src/framework/graphics/framebuffer.h +++ b/src/framework/graphics/framebuffer.h @@ -1,7 +1,7 @@ #ifndef FRAMEBUFFER_H #define FRAMEBUFFER_H -#include "graphicsdeclarations.h" +#include "declarations.h" class FrameBuffer { @@ -9,21 +9,21 @@ public: FrameBuffer(int width, int height); virtual ~FrameBuffer(); - /// Bind the framebuffer, everything rendered will be draw on it + /// Binds the framebuffer, by switching render buffer to itself, everything rendered will be draw on it void bind(); - /// Unbind the framebuffer, render on back buffer again + /// Unbinds the framebuffer (switch render buffer to back buffer again) void unbind(); - /// Draw framebuffer - void draw(int x, int y, int width, int height); + /// Draws framebuffer texture + void draw(const Rect& screenCoords, const Rect& framebufferCoords = Rect()); + + TexturePtr getTexture() { return m_texture; } private: - uint m_fboTexture; + TexturePtr m_texture; uint m_fbo; bool m_fallbackOldImp; - int m_width; - int m_height; }; #endif diff --git a/src/framework/graphics/graphics.cpp b/src/framework/graphics/graphics.cpp index c44aaa15..88b884b6 100644 --- a/src/framework/graphics/graphics.cpp +++ b/src/framework/graphics/graphics.cpp @@ -1,7 +1,8 @@ -#include -#include #include "fontmanager.h" +#include +#include + #include #include #include @@ -109,7 +110,7 @@ void Graphics::drawTexturedRect(const Rect& screenCoords, const TexturePtr& texture, const Rect& textureCoords) { - if(screenCoords.isEmpty() || textureCoords.isEmpty()) + if(screenCoords.isEmpty()) return; // rect correction for opengl @@ -119,12 +120,17 @@ void Graphics::drawTexturedRect(const Rect& screenCoords, int left = screenCoords.left(); const Size& textureSize = texture->getGlSize(); - float textureRight = 0.0f; - float textureBottom = 1.0f; - float textureTop = 0.0f; - float textureLeft = 1.0f; - - if(!textureCoords.isEmpty()) { + float textureRight; + float textureBottom; + float textureTop; + float textureLeft; + + if(textureCoords.isEmpty()) { + textureRight = 1.0f; + textureBottom = 1.0f; + textureTop = 0.0f; + textureLeft = 0.0f; + } else { textureRight = (float)(textureCoords.right() + 1) / textureSize.width(); textureBottom = (float)(textureCoords.bottom() + 1) / textureSize.height(); textureTop = (float)textureCoords.top() / textureSize.height(); @@ -222,7 +228,7 @@ void Graphics::drawBoundingRect(const Rect& screenCoords, { assert(!m_drawing); - if(2 * innerLineWidth > screenCoords.height() || screenCoords.isEmpty()) + if(screenCoords.isEmpty() || 2 * innerLineWidth > screenCoords.height()) return; // rect correction for opengl @@ -274,7 +280,7 @@ void Graphics::bindColor(const Color& color) void Graphics::bindTexture(const TexturePtr& texture) { - glBindTexture(GL_TEXTURE_2D, texture->getTextureId()); + glBindTexture(GL_TEXTURE_2D, texture->getId()); } void Graphics::startDrawing() diff --git a/src/framework/graphics/graphics.h b/src/framework/graphics/graphics.h index c80c2245..62746204 100644 --- a/src/framework/graphics/graphics.h +++ b/src/framework/graphics/graphics.h @@ -1,7 +1,7 @@ #ifndef GRAPHICS_H #define GRAPHICS_H -#include "graphicsdeclarations.h" +#include "declarations.h" class Graphics { diff --git a/src/framework/graphics/image.cpp b/src/framework/graphics/image.cpp index 4f261092..6f394dd3 100644 --- a/src/framework/graphics/image.cpp +++ b/src/framework/graphics/image.cpp @@ -3,7 +3,7 @@ #include "graphics.h" #include "texturemanager.h" -#include +#include Image::Image(TexturePtr texture, Rect textureCoords) { diff --git a/src/framework/graphics/image.h b/src/framework/graphics/image.h index ee52596e..39b6d70b 100644 --- a/src/framework/graphics/image.h +++ b/src/framework/graphics/image.h @@ -1,8 +1,9 @@ #ifndef IMAGE_H #define IMAGE_H -#include "graphicsdeclarations.h" -#include +#include "declarations.h" + +#include class Image { diff --git a/src/framework/graphics/texture.cpp b/src/framework/graphics/texture.cpp index e6fcb74a..82808d2f 100644 --- a/src/framework/graphics/texture.cpp +++ b/src/framework/graphics/texture.cpp @@ -11,7 +11,9 @@ Texture::Texture(int width, int height, int channels, uchar *pixels) uint Texture::internalLoadGLTexture(uchar *pixels, int channels, int width, int height) { - // get smax texture size supported by the driver + m_size.setSize(width, height); + + // gets max texture size supported by the driver static GLint maxTexSize = -1; if(maxTexSize == -1) glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTexSize); @@ -28,7 +30,6 @@ uint Texture::internalLoadGLTexture(uchar *pixels, int channels, int width, int GLuint id; glGenTextures(1, &id); glBindTexture(GL_TEXTURE_2D, id); - m_size.setSize(width, height); bool mustFree = false; // old opengl drivers only accept power of two dimensions diff --git a/src/framework/graphics/texture.h b/src/framework/graphics/texture.h index 0bbc4cd4..496ae46d 100644 --- a/src/framework/graphics/texture.h +++ b/src/framework/graphics/texture.h @@ -1,7 +1,7 @@ #ifndef TEXTURE_H #define TEXTURE_H -#include "graphicsdeclarations.h" +#include "declarations.h" class Texture : public std::enable_shared_from_this { @@ -14,12 +14,14 @@ public: virtual void enableBilinearFilter(); /// Get OpenGL texture id - virtual uint getTextureId() const { return m_textureId; } + virtual uint getId() const { return m_textureId; } /// Copy pixels from OpenGL texture uchar* getPixels(); - const Size& getSize() const { return m_size; } + int getWidth() const { return m_size.width(); } + int getHeight() const { return m_size.height(); } + const Size getSize() const { return m_size; } const Size& getGlSize() const { return m_glSize; } protected: diff --git a/src/framework/graphics/texturemanager.cpp b/src/framework/graphics/texturemanager.cpp index 802bc6a9..a9d8ec28 100644 --- a/src/framework/graphics/texturemanager.cpp +++ b/src/framework/graphics/texturemanager.cpp @@ -1,7 +1,8 @@ #include "texturemanager.h" #include "animatedtexture.h" -#include -#include + +#include +#include TextureManager g_textures; diff --git a/src/framework/luascript/luadeclarations.h b/src/framework/luascript/declarations.h similarity index 66% rename from src/framework/luascript/luadeclarations.h rename to src/framework/luascript/declarations.h index 0ecf12fe..6ef7a1dc 100644 --- a/src/framework/luascript/luadeclarations.h +++ b/src/framework/luascript/declarations.h @@ -1,10 +1,11 @@ -#ifndef LUADECLARATIONS_H -#define LUADECLARATIONS_H +#ifndef FRAMEWORK_LUA_DECLARATIONS_H +#define FRAMEWORK_LUA_DECLARATIONS_H -#include +#include class LuaInterface; class LuaObject; + typedef std::function LuaCppFunction; typedef std::unique_ptr LuaCppFunctionPtr; typedef std::shared_ptr LuaObjectPtr; diff --git a/src/framework/luascript/luaexception.cpp b/src/framework/luascript/luaexception.cpp index 9df37b78..9b52929e 100644 --- a/src/framework/luascript/luaexception.cpp +++ b/src/framework/luascript/luaexception.cpp @@ -11,21 +11,21 @@ void LuaException::generateLuaErrorMessage(const std::string& error, int traceLe { // append trace level to error message if(traceLevel >= 0) - m_what = aux::make_string("LUA ERROR: ", g_lua.traceback(error, traceLevel)); + m_what = fw::mkstr("LUA ERROR: ", g_lua.traceback(error, traceLevel)); else - m_what = aux::make_string("LUA ERROR: ", error); + m_what = fw::mkstr("LUA ERROR: ", error); } LuaBadNumberOfArgumentsException::LuaBadNumberOfArgumentsException(int expected, int got) { std::string error = "attempt to call a function with wrong number of arguments"; if(expected >= 0 && got >= 0) - error = aux::make_string(error, " (expected ", expected, ", but got ", got, ")"); + error = fw::mkstr(error, " (expected ", expected, ", but got ", got, ")"); generateLuaErrorMessage(error, 1); } LuaBadValueCastException::LuaBadValueCastException(const std::string& luaTypeName, const std::string& cppTypeName) { - std::string error = aux::make_string("attempt to cast a '", luaTypeName, "' lua value to '", cppTypeName, "'"); + std::string error = fw::mkstr("attempt to cast a '", luaTypeName, "' lua value to '", cppTypeName, "'"); generateLuaErrorMessage(error, 0); } diff --git a/src/framework/luascript/luaexception.h b/src/framework/luascript/luaexception.h index 4463ddc1..9b27dcb4 100644 --- a/src/framework/luascript/luaexception.h +++ b/src/framework/luascript/luaexception.h @@ -1,7 +1,7 @@ #ifndef LUAEXCEPTION_H #define LUAEXCEPTION_H -#include "luadeclarations.h" +#include "declarations.h" class LuaException : public std::exception { diff --git a/src/framework/luascript/luafunctions.cpp b/src/framework/luascript/luafunctions.cpp index 5aa7f00a..14b41973 100644 --- a/src/framework/luascript/luafunctions.cpp +++ b/src/framework/luascript/luafunctions.cpp @@ -1,8 +1,8 @@ #include "luainterface.h" -#include -#include -#include -#include +#include +#include +#include +#include void LuaInterface::registerFunctions() { diff --git a/src/framework/luascript/luainterface.cpp b/src/framework/luascript/luainterface.cpp index 46fc5ba5..d8f149b4 100644 --- a/src/framework/luascript/luainterface.cpp +++ b/src/framework/luascript/luainterface.cpp @@ -1,7 +1,7 @@ #include "luainterface.h" #include "luaobject.h" -#include +#include #include LuaInterface g_lua; @@ -20,7 +20,7 @@ void LuaInterface::init() createLuaState(); // check if demangle_type is working as expected - assert(aux::demangle_type() == "LuaObject"); + assert(fw::demangle_type() == "LuaObject"); // register LuaObject, the base of all other objects registerClass(); @@ -125,12 +125,12 @@ void LuaInterface::registerClassMemberField(const std::string& className, if(getFunction) { pushCppFunction(getFunction); - setField(aux::make_string("get_", field)); + setField(fw::mkstr("get_", field)); } if(setFunction) { pushCppFunction(setFunction); - setField(aux::make_string("set_", field)); + setField(fw::mkstr("set_", field)); } pop(); @@ -273,7 +273,7 @@ void LuaInterface::loadFunction(const std::string& buffer, const std::string& so // gets the function contained in that buffer if(boost::starts_with(buffer, "function")) { // evaluate the function - std::string buf = aux::make_string("__func = ", buffer); + std::string buf = fw::mkstr("__func = ", buffer); loadBuffer(buf, source); safeCall(); @@ -295,7 +295,7 @@ void LuaInterface::evaluateExpression(const std::string& expression, const std:: { // evaluates the expression if(!expression.empty()) { - std::string buffer = aux::make_string("__exp = (", expression, ")"); + std::string buffer = fw::mkstr("__exp = (", expression, ")"); loadBuffer(buffer, source); safeCall(); @@ -898,7 +898,7 @@ void LuaInterface::pushObject(const LuaObjectPtr& obj) new(newUserdata(sizeof(LuaObjectPtr))) LuaObjectPtr(obj); // set the userdata metatable - getGlobal(aux::make_string(obj->getLuaObjectName(), "_mt")); + getGlobal(fw::mkstr(obj->getLuaObjectName(), "_mt")); assert(!isNil()); setMetatable(); } diff --git a/src/framework/luascript/luainterface.h b/src/framework/luascript/luainterface.h index 22863d05..6bde3950 100644 --- a/src/framework/luascript/luainterface.h +++ b/src/framework/luascript/luainterface.h @@ -1,7 +1,7 @@ #ifndef LUAINTERFACE_H #define LUAINTERFACE_H -#include "luadeclarations.h" +#include "declarations.h" struct lua_State; typedef int (*LuaCFunction) (lua_State *L); @@ -41,24 +41,24 @@ public: // register shortcuts using templates template void registerClass() { - registerClass(aux::demangle_type(), aux::demangle_type()); + registerClass(fw::demangle_type(), fw::demangle_type()); } template void registerClassStaticFunction(const std::string& functionName, const LuaCppFunction& function) { - registerClassStaticFunction(aux::demangle_type(), functionName, function); + registerClassStaticFunction(fw::demangle_type(), functionName, function); } template void registerClassMemberFunction(const std::string& functionName, const LuaCppFunction& function) { - registerClassMemberFunction(aux::demangle_type(), functionName, function); + registerClassMemberFunction(fw::demangle_type(), functionName, function); } template void registerClassMemberField(const std::string& field, const LuaCppFunction& getFunction, const LuaCppFunction& setFunction) { - registerClassMemberField(aux::demangle_type(), field, getFunction, setFunction); + registerClassMemberField(fw::demangle_type(), field, getFunction, setFunction); } // methods for binding functions @@ -322,7 +322,7 @@ template T LuaInterface::castValue(int index) { T o; if(!luavalue_cast(index, o)) - throw LuaBadValueCastException(typeName(index), aux::demangle_type()); + throw LuaBadValueCastException(typeName(index), fw::demangle_type()); return o; } diff --git a/src/framework/luascript/luaobject.h b/src/framework/luascript/luaobject.h index c2871f09..31193928 100644 --- a/src/framework/luascript/luaobject.h +++ b/src/framework/luascript/luaobject.h @@ -1,14 +1,14 @@ #ifndef LUAOBJECT_H #define LUAOBJECT_H -#include "luadeclarations.h" +#include "declarations.h" /// LuaObject, all script-able classes have it as base class LuaObject : public std::enable_shared_from_this { public: LuaObject(); - virtual ~LuaObject() ; + virtual ~LuaObject(); /// Calls a function or table of functions stored in a lua field, results are pushed onto the stack, /// if any lua error occurs, it will be reported to stdout and return 0 results @@ -40,7 +40,7 @@ public: /// Returns the class name used in Lua virtual std::string getLuaObjectName() const { // this could later be cached for more performance - return aux::demangle_name(typeid(*this).name()); + return fw::demangle_name(typeid(*this).name()); } LuaObjectPtr asLuaObject() { return shared_from_this(); } diff --git a/src/framework/net/connection.cpp b/src/framework/net/connection.cpp index 1633502f..4ebbac7d 100644 --- a/src/framework/net/connection.cpp +++ b/src/framework/net/connection.cpp @@ -1,6 +1,6 @@ #include "connection.h" -#include +#include static asio::io_service ioService; @@ -26,7 +26,7 @@ void Connection::connect(const std::string& host, uint16 port, const ConnectCall { m_connectCallback = connectCallback; - asio::ip::tcp::resolver::query query(host, aux::unsafe_cast(port)); + asio::ip::tcp::resolver::query query(host, fw::unsafe_cast(port)); m_resolver.async_resolve(query, std::bind(&Connection::onResolve, shared_from_this(), std::placeholders::_1, std::placeholders::_2)); m_timer.expires_from_now(boost::posix_time::seconds(2)); diff --git a/src/framework/net/connection.h b/src/framework/net/connection.h index 2091aef4..69dd0677 100644 --- a/src/framework/net/connection.h +++ b/src/framework/net/connection.h @@ -1,7 +1,8 @@ #ifndef CONNECTION_H #define CONNECTION_H -#include "netdeclarations.h" +#include "declarations.h" +#include class Connection : public std::enable_shared_from_this, boost::noncopyable { @@ -11,7 +12,7 @@ class Connection : public std::enable_shared_from_this, boost::nonco public: Connection(); - ~Connection(); + virtual ~Connection(); static void poll(); diff --git a/src/framework/net/declarations.h b/src/framework/net/declarations.h new file mode 100644 index 00000000..3c339a7d --- /dev/null +++ b/src/framework/net/declarations.h @@ -0,0 +1,17 @@ +#ifndef FRAMEWORK_NET_DECLARATIONS_H +#define FRAMEWORK_NET_DECLARATIONS_H + +#include +#include + +namespace asio = boost::asio; + +class Connection; +class InputMessage; +class OutputMessage; +class Protocol; + +typedef std::shared_ptr ConnectionPtr; +typedef std::shared_ptr ProtocolPtr; + +#endif diff --git a/src/framework/net/inputmessage.h b/src/framework/net/inputmessage.h index 9e9d3f6a..bb9eea20 100644 --- a/src/framework/net/inputmessage.h +++ b/src/framework/net/inputmessage.h @@ -1,7 +1,7 @@ #ifndef INPUTMESSAGE_H #define INPUTMESSAGE_H -#include "netdeclarations.h" +#include "declarations.h" class InputMessage { diff --git a/src/framework/net/netdeclarations.h b/src/framework/net/netdeclarations.h deleted file mode 100644 index e1bccfbf..00000000 --- a/src/framework/net/netdeclarations.h +++ /dev/null @@ -1,31 +0,0 @@ -#ifndef NETDECLARATIONS_H -#define NETDECLARATIONS_H - -#include -#include - -namespace asio = boost::asio; - -class Connection; -class InputMessage; -class OutputMessage; -class Protocol; - -typedef std::shared_ptr ConnectionPtr; -typedef std::shared_ptr ProtocolPtr; - -#define CIPSOFT_PUBLIC_RSA "1321277432058722840622950990822933849527763264961655079678763618" \ - "4334395343554449668205332383339435179772895415509701210392836078" \ - "6959821132214473291575712138800495033169914814069637740318278150" \ - "2907336840325241747827401343576296990629870233111328210165697754" \ - "88792221429527047321331896351555606801473202394175817" - - -#define OTSERV_PUBLIC_RSA "1091201329673994292788609605089955415282375029027981291234687579" \ - "3726629149257644633073969600111060390723088861007265581882535850" \ - "3429057592827629436413108566029093628212635953836686562675849720" \ - "6207862794310902180176810615217550567108238764764442605581471797" \ - "07119674283982419152118103759076030616683978566631413" - - -#endif diff --git a/src/framework/net/outputmessage.cpp b/src/framework/net/outputmessage.cpp index 9c2c8df5..5fb5f6b3 100644 --- a/src/framework/net/outputmessage.cpp +++ b/src/framework/net/outputmessage.cpp @@ -1,4 +1,4 @@ -#include +#include OutputMessage::OutputMessage() { diff --git a/src/framework/net/outputmessage.h b/src/framework/net/outputmessage.h index b19996b9..d853862d 100644 --- a/src/framework/net/outputmessage.h +++ b/src/framework/net/outputmessage.h @@ -1,7 +1,7 @@ #ifndef OUTPUTMESSAGE_H #define OUTPUTMESSAGE_H -#include "netdeclarations.h" +#include "declarations.h" class OutputMessage { diff --git a/src/framework/net/protocol.h b/src/framework/net/protocol.h index 917ea40d..f88a3350 100644 --- a/src/framework/net/protocol.h +++ b/src/framework/net/protocol.h @@ -1,11 +1,11 @@ #ifndef PROTOCOL_H #define PROTOCOL_H -#include "netdeclarations.h" +#include "declarations.h" #include "inputmessage.h" #include "outputmessage.h" -#include +#include class Protocol : public LuaObject { diff --git a/src/framework/net/rsa.h b/src/framework/net/rsa.h index eab4c198..bf15f5d5 100644 --- a/src/framework/net/rsa.h +++ b/src/framework/net/rsa.h @@ -1,7 +1,7 @@ #ifndef RSA_H #define RSA_H -#include +#include #include class Rsa diff --git a/src/framework/otml/otmldeclarations.h b/src/framework/otml/declarations.h similarity index 72% rename from src/framework/otml/otmldeclarations.h rename to src/framework/otml/declarations.h index c7c66fe2..c2a742be 100644 --- a/src/framework/otml/otmldeclarations.h +++ b/src/framework/otml/declarations.h @@ -1,7 +1,7 @@ -#ifndef OTMLDECLARATIONS_H -#define OTMLDECLARATIONS_H +#ifndef FRAMEWORK_OTML_DECLARATIONS_H +#define FRAMEWORK_OTML_DECLARATIONS_H -#include +#include class OTMLNode; class OTMLDocument; diff --git a/src/framework/otml/otmldocument.cpp b/src/framework/otml/otmldocument.cpp index a3fd08a9..da5a1682 100644 --- a/src/framework/otml/otmldocument.cpp +++ b/src/framework/otml/otmldocument.cpp @@ -2,7 +2,7 @@ #include "otmlparser.h" #include "otmlemitter.h" -#include +#include OTMLDocumentPtr OTMLDocument::create() { diff --git a/src/framework/otml/otmlemitter.h b/src/framework/otml/otmlemitter.h index ce659291..e2258fed 100644 --- a/src/framework/otml/otmlemitter.h +++ b/src/framework/otml/otmlemitter.h @@ -1,7 +1,7 @@ #ifndef OTMLEMITTER_H #define OTMLEMITTER_H -#include "otmldeclarations.h" +#include "declarations.h" class OTMLEmitter { diff --git a/src/framework/otml/otmlexception.h b/src/framework/otml/otmlexception.h index f5dedb4f..bfbe1c43 100644 --- a/src/framework/otml/otmlexception.h +++ b/src/framework/otml/otmlexception.h @@ -1,7 +1,7 @@ #ifndef OTMLEXCEPTION_H #define OTMLEXCEPTION_H -#include "otmldeclarations.h" +#include "declarations.h" /// All OTML errors throw this exception class OTMLException : public std::exception diff --git a/src/framework/otml/otmlnode.cpp b/src/framework/otml/otmlnode.cpp index d0aa891d..8cf06054 100644 --- a/src/framework/otml/otmlnode.cpp +++ b/src/framework/otml/otmlnode.cpp @@ -16,7 +16,7 @@ std::string OTMLNode::value() const { // ~ is an alias for no value if(m_value == "~") - return aux::empty_string; + return fw::empty_string; return m_value; } @@ -109,7 +109,7 @@ OTMLNodePtr OTMLNode::at(const std::string& childTag) if(child->tag() == childTag) return child; } - throw OTMLException(shared_from_this(), aux::make_string("child node with tag '", childTag, "' not found")); + throw OTMLException(shared_from_this(), fw::mkstr("child node with tag '", childTag, "' not found")); return nullptr; } @@ -117,7 +117,7 @@ OTMLNodePtr OTMLNode::at(int childIndex) { if(childIndex < size() && childIndex >= 0) return m_childNodes[childIndex]; - throw OTMLException(shared_from_this(), aux::make_string("child node at index '", childIndex, "' not found")); + throw OTMLException(shared_from_this(), fw::mkstr("child node at index '", childIndex, "' not found")); return nullptr; } diff --git a/src/framework/otml/otmlnode.h b/src/framework/otml/otmlnode.h index fe5ad7ad..e827dafc 100644 --- a/src/framework/otml/otmlnode.h +++ b/src/framework/otml/otmlnode.h @@ -1,7 +1,7 @@ #ifndef OTMLNODE_H #define OTMLNODE_H -#include "otmldeclarations.h" +#include "declarations.h" #include "otmlexception.h" class OTMLNode : public std::enable_shared_from_this @@ -90,7 +90,7 @@ T OTMLNode::read() { T v; if(!from_otmlnode(shared_from_this(), v)) throw OTMLException(shared_from_this(), - aux::make_string("failed to cast node value to type '", aux::demangle_type(), "'")); + fw::mkstr("failed to cast node value to type '", fw::demangle_type(), "'")); return v; } @@ -147,7 +147,7 @@ void OTMLNode::writeIn(const T& v) { // templates for casting a node to another type template bool from_otmlnode(OTMLNodePtr node, T& v) { - return aux::cast(node->value(), v); + return fw::cast(node->value(), v); } template @@ -169,7 +169,7 @@ template bool from_otmlnode(OTMLNodePtr node, std::map& m) { for(int i=0;isize();++i) { K k; - if(!aux::cast(node->at(i)->tag(), k)) + if(!fw::cast(node->at(i)->tag(), k)) return false; m[k] = node->at(i)->read(); } @@ -179,7 +179,7 @@ bool from_otmlnode(OTMLNodePtr node, std::map& m) { // templates for casting a type to a node template void to_otmlnode(OTMLNodePtr node, const T& v) { - node->setValue(aux::unsafe_cast(v)); + node->setValue(fw::unsafe_cast(v)); } template @@ -203,7 +203,7 @@ void to_otmlnode(OTMLNodePtr node, const std::list& v) { template void to_otmlnode(OTMLNodePtr node, const std::map& m) { for(auto it = m.begin(); it != m.end(); ++it) { - std::string k = aux::unsafe_cast(it->first); + std::string k = fw::unsafe_cast(it->first); OTMLNodePtr newNode(new OTMLNode); newNode->setTag(k); newNode->setUnique(); diff --git a/src/framework/otml/otmlnodeext.h b/src/framework/otml/otmlnodeext.h index d021924f..e69de29b 100644 --- a/src/framework/otml/otmlnodeext.h +++ b/src/framework/otml/otmlnodeext.h @@ -1,46 +0,0 @@ -#ifndef OTMLNODEEXT_H -#define OTMLNODEEXT_H - -#include -#include -#include -#include - -//inline bool from_otmlnode(const OTMLNodePtr& node, Color& color) -//{ - //int r, g, b, a; - //r = node->readAt(0); - //g = node->readAt(1); - //b = node->readAt(2); - //a = 255; - //if(node->hasChild(3)) - //a = node->readAt(3); - //return true; -//} - -//template -//bool from_otmlnode(const OTMLNodePtr& node, TPoint& point) -//{ - //point.x = node->readAt(0); - //point.y = node->readAt(1); - //return true; -//} - -//template -//bool from_otmlnode(const OTMLNodePtr& node, TSize& size) -//{ - //size.setSize(node->readAt(0), node->readAt(1)); - //return true; -//} - -//template -//bool from_otmlnode(const OTMLNodePtr& node, TRect& rect) -//{ - //rect.setRect(node->readAt(0), - //node->readAt(1), - //node->readAt(2), - //node->readAt(3)); - //return true; -//} - -#endif diff --git a/src/framework/otml/otmlparser.cpp b/src/framework/otml/otmlparser.cpp index c2c143fa..5ff35956 100644 --- a/src/framework/otml/otmlparser.cpp +++ b/src/framework/otml/otmlparser.cpp @@ -154,7 +154,7 @@ void OTMLParser::parseNode(const std::string& data) node->setUnique(dotsPos != std::string::npos); node->setTag(tag); node->setValue(value); - node->setSource(doc->source() + ":" + aux::safe_cast(currentLine)); + node->setSource(doc->source() + ":" + fw::safe_cast(currentLine)); currentParent->addChild(node); previousNode = node; } diff --git a/src/framework/otml/otmlparser.h b/src/framework/otml/otmlparser.h index 692d573d..4d9cc26f 100644 --- a/src/framework/otml/otmlparser.h +++ b/src/framework/otml/otmlparser.h @@ -1,7 +1,7 @@ #ifndef OTMLPARSER_H #define OTMLPARSER_H -#include "otmldeclarations.h" +#include "declarations.h" class OTMLParser { diff --git a/src/framework/pch.h b/src/framework/pch.h deleted file mode 100644 index a48255b1..00000000 --- a/src/framework/pch.h +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef PCH_H -#define PCH_H - -#include "global.h" - -#endif diff --git a/src/framework/core/platform.h b/src/framework/platform/platform.h similarity index 98% rename from src/framework/core/platform.h rename to src/framework/platform/platform.h index 2f9772de..b4a41eda 100644 --- a/src/framework/core/platform.h +++ b/src/framework/platform/platform.h @@ -1,7 +1,7 @@ #ifndef PLATFORM_H #define PLATFORM_H -#include +#include class PlatformListener; diff --git a/src/framework/core/inputevent.h b/src/framework/platform/platformevent.h similarity index 99% rename from src/framework/core/inputevent.h rename to src/framework/platform/platformevent.h index 9473ba95..7b716f72 100644 --- a/src/framework/core/inputevent.h +++ b/src/framework/platform/platformevent.h @@ -1,7 +1,7 @@ #ifndef INPUTEVENT_H #define INPUTEVENT_H -#include +#include enum InputKeyCode { KC_UNKNOWN = 0x00, diff --git a/src/framework/core/platformlistener.h b/src/framework/platform/platformlistener.h similarity index 93% rename from src/framework/core/platformlistener.h rename to src/framework/platform/platformlistener.h index 2c5574fa..425656f3 100644 --- a/src/framework/core/platformlistener.h +++ b/src/framework/platform/platformlistener.h @@ -1,7 +1,7 @@ #ifndef PLATFORMLISTENER_H #define PLATFORMLISTENER_H -#include "inputevent.h" +#include "platformevent.h" class PlatformListener { diff --git a/src/framework/core/win32platform.cpp b/src/framework/platform/win32platform.cpp similarity index 100% rename from src/framework/core/win32platform.cpp rename to src/framework/platform/win32platform.cpp diff --git a/src/framework/core/x11platform.cpp b/src/framework/platform/x11platform.cpp similarity index 100% rename from src/framework/core/x11platform.cpp rename to src/framework/platform/x11platform.cpp diff --git a/src/framework/ui/const.h b/src/framework/ui/const.h new file mode 100644 index 00000000..aec0cad6 --- /dev/null +++ b/src/framework/ui/const.h @@ -0,0 +1,50 @@ +#ifndef FRAMEWORK_UI_CONST_H +#define FRAMEWORK_UI_CONST_H + +// namespace ui { + +enum UIWidgetType { + UITypeWidget = 0, + UITypeLabel, + UITypeButton, + UITypeLineEdit, + UITypeWindow, + UITypeList +}; + +enum FocusReason { + MouseFocusReason = 0, + TabFocusReason, + ActiveFocusReason, + OtherFocusReason +}; + +enum MouseButton { + MouseNoButton = 0, + MouseLeftButton, + MouseRightButton, + MouseMidButton +}; + +enum MouseWheelDirection { + MouseNoWheel = 0, + MouseWheelUp, + MouseWheelDown +}; + +enum KeyboardModifier { + KeyboardNoModifier = 0, + KeyboardCtrlModifier = 1, + KeyboardAltModifier = 2, + KeyboardShiftModifier = 4 +}; + +enum ButtonState { + ButtonUp = 0, + ButtonDown, + ButtonHover +}; + +// } + +#endif diff --git a/src/framework/ui/declarations.h b/src/framework/ui/declarations.h new file mode 100644 index 00000000..d5339b44 --- /dev/null +++ b/src/framework/ui/declarations.h @@ -0,0 +1,29 @@ +#ifndef FRAMEWORK_UI_DECLARATIONS_H +#define FRAMEWORK_UI_DECLARATIONS_H + +#include +#include "const.h" + +class UIManager; +class UILayout; +class UIAnchorLayout; +class UIStyle; +class UIWidget; +class UILabel; +class UIButton; +class UILineEdit; +class UIWindow; + +typedef std::shared_ptr UIWidgetPtr; +typedef std::weak_ptr UIWidgetWeakPtr; +typedef std::deque UIWidgetList; + +typedef std::shared_ptr UILayoutPtr; +typedef std::shared_ptr UIAnchorLayoutPtr; +typedef std::shared_ptr UIStylePtr; +typedef std::shared_ptr UILabelPtr; +typedef std::shared_ptr UIButtonPtr; +typedef std::shared_ptr UILineEditPtr; +typedef std::shared_ptr UIWindowPtr; + +#endif diff --git a/src/framework/ui/uianchor.h b/src/framework/ui/uianchor.h index e356b752..9eff2968 100644 --- a/src/framework/ui/uianchor.h +++ b/src/framework/ui/uianchor.h @@ -1,7 +1,7 @@ #ifndef UIANCHOR_H #define UIANCHOR_H -#include "uideclarations.h" +#include "declarations.h" struct AnchorLine { AnchorLine(std::string widgetId, AnchorPoint edge) : widgetId(widgetId), edge(edge) { } diff --git a/src/framework/ui/uibutton.cpp b/src/framework/ui/uibutton.cpp index 2b644b8c..1b6bd449 100644 --- a/src/framework/ui/uibutton.cpp +++ b/src/framework/ui/uibutton.cpp @@ -1,8 +1,8 @@ #include "uibutton.h" -#include -#include -#include -#include +#include +#include +#include +#include UIButton::UIButton(): UIWidget(UITypeButton) { @@ -37,7 +37,7 @@ void UIButton::loadStyleFromOTML(const OTMLNodePtr& styleNode) if(OTMLNodePtr node = styleNode->get("state.down")) loadStateStyle(m_statesStyle[ButtonDown], node); - m_text = styleNode->readAt("text", aux::empty_string); + m_text = styleNode->readAt("text", fw::empty_string); if(OTMLNodePtr node = styleNode->get("onClick")) { g_lua.loadFunction(node->read(), "@" + node->source() + "[" + node->tag() + "]"); diff --git a/src/framework/ui/uideclarations.h b/src/framework/ui/uideclarations.h deleted file mode 100644 index f74bcb50..00000000 --- a/src/framework/ui/uideclarations.h +++ /dev/null @@ -1,70 +0,0 @@ -#ifndef UIDECLARATIONS_H -#define UIDECLARATIONS_H - -#include - -class UIManager; -class UILayout; -class UIAnchorLayout; -class UIStyle; -class UIWidget; -class UILabel; -class UIButton; -class UILineEdit; -class UIWindow; - -typedef std::shared_ptr UIWidgetPtr; -typedef std::weak_ptr UIWidgetWeakPtr; -typedef std::deque UIWidgetList; - -typedef std::shared_ptr UILayoutPtr; -typedef std::shared_ptr UIAnchorLayoutPtr; -typedef std::shared_ptr UIStylePtr; -typedef std::shared_ptr UILabelPtr; -typedef std::shared_ptr UIButtonPtr; -typedef std::shared_ptr UILineEditPtr; -typedef std::shared_ptr UIWindowPtr; - -enum UIWidgetType { - UITypeWidget = 0, - UITypeLabel, - UITypeButton, - UITypeLineEdit, - UITypeWindow, - UITypeList -}; - -enum FocusReason { - MouseFocusReason = 0, - TabFocusReason, - ActiveFocusReason, - OtherFocusReason -}; - -enum MouseButton { - MouseNoButton = 0, - MouseLeftButton, - MouseRightButton, - MouseMidButton -}; - -enum MouseWheelDirection { - MouseNoWheel = 0, - MouseWheelUp, - MouseWheelDown -}; - -enum KeyboardModifier { - KeyboardNoModifier = 0, - KeyboardCtrlModifier = 1, - KeyboardAltModifier = 2, - KeyboardShiftModifier = 4 -}; - -enum ButtonState { - ButtonUp = 0, - ButtonDown, - ButtonHover -}; - -#endif \ No newline at end of file diff --git a/src/framework/ui/uievent.h b/src/framework/ui/uievent.h index 93d29b49..083d849b 100644 --- a/src/framework/ui/uievent.h +++ b/src/framework/ui/uievent.h @@ -1,8 +1,8 @@ #ifndef UIEVENT_H #define UIEVENT_H -#include -#include "uideclarations.h" +#include +#include "declarations.h" class UIEvent { diff --git a/src/framework/ui/uilabel.cpp b/src/framework/ui/uilabel.cpp index cf2744ab..664c6ae1 100644 --- a/src/framework/ui/uilabel.cpp +++ b/src/framework/ui/uilabel.cpp @@ -1,6 +1,6 @@ #include "uilabel.h" -#include -#include +#include +#include UILabel::UILabel() : UIWidget(UITypeLabel) { @@ -21,7 +21,7 @@ void UILabel::loadStyleFromOTML(const OTMLNodePtr& styleNode) m_text = styleNode->readAt("text", m_text); if(styleNode->hasChild("align")) - m_align = parseAlignment(styleNode->readAt("align")); + m_align = fw::translateAlignment(styleNode->readAt("align")); // auto resize if no size supplied if(!m_text.empty() && !getGeometry().isValid()) diff --git a/src/framework/ui/uilayout.h b/src/framework/ui/uilayout.h index 31225d1f..574bae1a 100644 --- a/src/framework/ui/uilayout.h +++ b/src/framework/ui/uilayout.h @@ -1,7 +1,7 @@ #ifndef UILAYOUT_H #define UILAYOUT_H -#include "uideclarations.h" +#include "declarations.h" class UILayout { diff --git a/src/framework/ui/uilineedit.cpp b/src/framework/ui/uilineedit.cpp index ff02a437..ad3f8067 100644 --- a/src/framework/ui/uilineedit.cpp +++ b/src/framework/ui/uilineedit.cpp @@ -1,8 +1,8 @@ #include "uilineedit.h" -#include -#include -#include -#include +#include +#include +#include +#include UILineEdit::UILineEdit() : UIWidget(UITypeLabel) { diff --git a/src/framework/ui/uilist.cpp b/src/framework/ui/uilist.cpp new file mode 100644 index 00000000..bf10fd67 --- /dev/null +++ b/src/framework/ui/uilist.cpp @@ -0,0 +1,32 @@ +#include "uilist.h" + +UIList::UIList() : UIWidget(UITypeList) +{ + +} + +void UIList::loadStyleFromOTML(const OTMLNodePtr& styleNode) +{ + +} + +void UIList::render() +{ + +} + +void UIList::onKeyPress(UIKeyEvent& event) +{ + +} + +void UIList::onMousePress(UIMouseEvent& event) +{ + +} + +void UIList::onMouseMove(UIMouseEvent& event) +{ + +} + diff --git a/src/framework/ui/uilist.h b/src/framework/ui/uilist.h new file mode 100644 index 00000000..ac7ffc0c --- /dev/null +++ b/src/framework/ui/uilist.h @@ -0,0 +1,23 @@ +#ifndef UILIST_H +#define UILIST_H + +#include "uiwidget.h" + +class UIList : public UIWidget +{ +public: + UIList(); + + virtual void loadStyleFromOTML(const OTMLNodePtr& styleNode); + virtual void render(); + +protected: + virtual void onKeyPress(UIKeyEvent& event); + virtual void onMousePress(UIMouseEvent& event); + virtual void onMouseMove(UIMouseEvent& event); + +private: + std::list m_items; +}; + +#endif diff --git a/src/framework/ui/uimanager.cpp b/src/framework/ui/uimanager.cpp index 3a2b31b6..8022ad63 100644 --- a/src/framework/ui/uimanager.cpp +++ b/src/framework/ui/uimanager.cpp @@ -2,8 +2,8 @@ #include "ui.h" #include "uianchorlayout.h" -#include -#include +#include +#include UIManager g_ui; @@ -136,7 +136,7 @@ OTMLNodePtr UIManager::getStyle(const std::string& styleName) auto it = m_styles.find(styleName); if(it == m_styles.end()) - throw std::logic_error(aux::make_string("style '", styleName, "' is not a defined style")); + throw std::logic_error(fw::mkstr("style '", styleName, "' is not a defined style")); return m_styles[styleName]; } diff --git a/src/framework/ui/uimanager.h b/src/framework/ui/uimanager.h index f513093e..47cc8e2d 100644 --- a/src/framework/ui/uimanager.h +++ b/src/framework/ui/uimanager.h @@ -1,9 +1,9 @@ #ifndef UIMANAGER_H #define UIMANAGER_H -#include "uideclarations.h" -#include -#include +#include "declarations.h" +#include +#include class UIManager { diff --git a/src/framework/ui/uiwidget.cpp b/src/framework/ui/uiwidget.cpp index c62bbc67..c2b68936 100644 --- a/src/framework/ui/uiwidget.cpp +++ b/src/framework/ui/uiwidget.cpp @@ -3,12 +3,12 @@ #include "uilayout.h" #include "uianchorlayout.h" -#include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include +#include UIWidget::UIWidget(UIWidgetType type) { @@ -26,7 +26,7 @@ UIWidget::UIWidget(UIWidgetType type) // generate an unique id, this is need because anchored layouts find widgets by id static unsigned long id = 1; - m_id = aux::make_string("widget", id++); + m_id = fw::mkstr("widget", id++); } UIWidget::~UIWidget() @@ -172,7 +172,7 @@ void UIWidget::loadStyleFromOTML(const OTMLNodePtr& styleNode) } else if(what == "centerIn") { centerIn(node->value()); } else { - AnchorPoint myEdge = parseAnchorPoint(what); + AnchorPoint myEdge = fw::translateAnchorPoint(what); std::string anchorDescription = node->value(); std::vector split; @@ -181,7 +181,7 @@ void UIWidget::loadStyleFromOTML(const OTMLNodePtr& styleNode) throw OTMLException(node, "invalid anchor description"); std::string target = split[0]; - AnchorPoint targetEdge = parseAnchorPoint(split[1]); + AnchorPoint targetEdge = fw::translateAnchorPoint(split[1]); if(myEdge == AnchorNone) throw OTMLException(node, "invalid anchor edge"); diff --git a/src/framework/ui/uiwidget.h b/src/framework/ui/uiwidget.h index 44ccf3bc..52558e9e 100644 --- a/src/framework/ui/uiwidget.h +++ b/src/framework/ui/uiwidget.h @@ -1,11 +1,11 @@ #ifndef UIWIDGET_H #define UIWIDGET_H -#include "uideclarations.h" +#include "declarations.h" #include "uievent.h" -#include -#include -#include +#include +#include +#include class UIWidget : public LuaObject { diff --git a/src/framework/ui/uiwindow.cpp b/src/framework/ui/uiwindow.cpp index 96abbc07..0c8573f0 100644 --- a/src/framework/ui/uiwindow.cpp +++ b/src/framework/ui/uiwindow.cpp @@ -1,7 +1,7 @@ #include "uiwindow.h" -#include -#include -#include +#include +#include +#include UIWindow::UIWindow(): UIWidget(UITypeWindow) { @@ -25,7 +25,7 @@ void UIWindow::loadStyleFromOTML(const OTMLNodePtr& styleNode) m_headImage = BorderImage::loadFromOTML(node); m_headHeight = headNode->readAt("height", m_headImage->getDefaultSize().height()); m_headMargin = headNode->readAt("margin", 0); - m_titleAlign = parseAlignment(headNode->readAt("text align", std::string("center"))); + m_titleAlign = fw::translateAlignment(headNode->readAt("text align", std::string("center"))); } else { m_headHeight = 0; m_headMargin = 0; @@ -37,7 +37,7 @@ void UIWindow::loadStyleFromOTML(const OTMLNodePtr& styleNode) m_bodyImage = BorderImage::loadFromOTML(node); } - m_title = styleNode->readAt("title", aux::empty_string); + m_title = styleNode->readAt("title", fw::empty_string); } void UIWindow::render() diff --git a/src/framework/util/auxiliary.h b/src/framework/util/auxiliary.h deleted file mode 100644 index 95ba2a43..00000000 --- a/src/framework/util/auxiliary.h +++ /dev/null @@ -1,194 +0,0 @@ -#ifndef AUXILIARY_H -#define AUXILIARY_H - -#include -#include -#include -#include -#include -#include - -/// Namespace that contains auxiliary functions and templates -namespace aux { - /// Fill an ostream by concatenating args - /// Usage: - /// aux::fill_ostream(stream, a1, a2, ..., aN); - inline void fill_ostream(std::ostringstream&) { } - template - void fill_ostream(std::ostringstream& stream, const T& first, const Args&... rest) { - stream << first; - fill_ostream(stream, rest...); - } - - /// Makes a std::string by concatenating args - /// Usage: - /// std::string str = aux::make_string(a1, a2, ..., aN); - template - std::string make_string(const T&... args) { - std::ostringstream buf; - fill_ostream(buf, args...); - return buf.str(); - } - - // used by dumper - struct dump_util { - ~dump_util() { std::cout << std::endl; } - template - dump_util& operator<<(const T& v) { - std::cout << v << " "; - return *this; - } - }; - - /// Utility for dumping variables - /// Usage: - /// aux::dump << v1, v2, ..., vN; - static const struct dumper { - dumper() { } - template - dump_util operator<<(const T& v) const { - dump_util d; - d << v; - return d; - } - } dump; - - /// Utility for printing messages into stdout - /// Usage: - /// aux::print(v1, v2, ..., vN); - template - void print(const T&... args) { - std::ostringstream buf; - fill_ostream(buf, args...); - std::cout << buf.str(); - } - - /// Same as aux::print but adds a new line at the end - template - void println(const T&... args) { - print(args...); - std::cout << std::endl; - } - - /// Demangle names for GNU g++ compiler - inline std::string demangle_name(const char* name) { - size_t len; - int status; - std::string ret; - char* demangled = abi::__cxa_demangle(name, 0, &len, &status); - if(demangled) { - ret = demangled; - free(demangled); - } - return ret; - } - - /// Returns the name of a type - /// e.g. aux::demangle_type() returns a string containing 'Foo*' - template - std::string demangle_type() { - return demangle_name(typeid(T).name()); - } - - /// Cast a type to another type - /// @return whether the conversion was successful or not - template - bool cast(const T& in, R& out) { - std::stringstream ss; - ss << in; - ss >> out; - return !!ss && ss.eof(); - } - - // cast a type to string - template - bool cast(const T& in, std::string& out) { - std::stringstream ss; - ss << in; - out = ss.str(); - return true; - } - - // cast string to string - template<> - inline bool cast(const std::string& in, std::string& out) { - out = in; - return true; - } - - // special cast from string to boolean - template<> - inline bool cast(const std::string& in, bool& b) { - static std::string validNames[2][4] = {{"true","yes","on","1"}, {"false","no","off","0"}}; - bool ret = false; - for(int i=0;i<4;++i) { - if(in == validNames[0][i]) { - b = true; - ret = true; - break; - } else if(in == validNames[1][i]) { - b = false; - ret = true; - break; - } - } - return ret; - } - - // special cast from boolean to string - template<> - inline bool cast(const bool& in, std::string& out) { - out = (in ? "true" : "false"); - return true; - } - - // used by safe_cast - class bad_cast : public std::bad_cast { - public: - virtual ~bad_cast() throw() { } - template - void setWhat() { - m_what = make_string("failed to cast value of type '", demangle_type(), - "' to type '", demangle_type(), "'"); - } - virtual const char* what() { return m_what.c_str(); } - private: - std::string m_what; - }; - - /// Cast a type to another type, any error throws a aux::bad_cast_exception - /// Usage: - /// R r = aux::safe_cast(t); - template - R safe_cast(const T& t) { - R r; - if(!cast(t, r)) { - bad_cast e; - e.setWhat(); - throw e; - } - return r; - } - - /// Cast a type to another type, cast errors are ignored - /// Usage: - /// R r = aux::unsafe_cast(t); - template - R unsafe_cast(const T& t) { - R r; - try { - r = safe_cast(t); - } catch(bad_cast& e) { - println(e.what()); - } - return r; - } - - // an empty string to use anywhere needed - const static std::string empty_string; -} - -// shortcut for aux::dump -const static aux::dumper& dump = aux::dump; - -#endif diff --git a/src/framework/util/logger.h b/src/framework/util/logger.h index 2f222882..46ff6528 100644 --- a/src/framework/util/logger.h +++ b/src/framework/util/logger.h @@ -1,8 +1,9 @@ #ifndef LOGGER_H #define LOGGER_H -#include "auxiliary.h" +#include "tools.h" +//TODO: a real logger class Logger { public: @@ -17,16 +18,16 @@ public: }; // specialized logging -#define logDebug(...) Logger::log(Logger::LogDebug, aux::make_string(__VA_ARGS__)) -#define logInfo(...) Logger::log(Logger::LogInfo, aux::make_string(__VA_ARGS__)) -#define logWarning(...) Logger::log(Logger::LogWarning, aux::make_string(__VA_ARGS__)) -#define logError(...) Logger::log(Logger::LogError, aux::make_string(__VA_ARGS__)) -#define logFatal(...) Logger::log(Logger::LogFatal, aux::make_string(__VA_ARGS__)) +#define logDebug(...) Logger::log(Logger::LogDebug, fw::mkstr(__VA_ARGS__)) +#define logInfo(...) Logger::log(Logger::LogInfo, fw::mkstr(__VA_ARGS__)) +#define logWarning(...) Logger::log(Logger::LogWarning, fw::mkstr(__VA_ARGS__)) +#define logError(...) Logger::log(Logger::LogError, fw::mkstr(__VA_ARGS__)) +#define logFatal(...) Logger::log(Logger::LogFatal, fw::mkstr(__VA_ARGS__)) #define logTrace() Logger::log(Logger::LogDebug, "", __PRETTY_FUNCTION__) -#define logTraceDebug(...) Logger::log(Logger::LogDebug, aux::make_string(__VA_ARGS__), __PRETTY_FUNCTION__) -#define logTraceInfo(...) Logger::log(Logger::LogInfo, aux::make_string(__VA_ARGS__), __PRETTY_FUNCTION__) -#define logTraceWarning(...) log(Logger::LogWarning, aux::make_string(__VA_ARGS__), __PRETTY_FUNCTION__) -#define logTraceError(...) Logger::log(Logger::LogError, aux::make_string(__VA_ARGS__), __PRETTY_FUNCTION__) +#define logTraceDebug(...) Logger::log(Logger::LogDebug, fw::mkstr(__VA_ARGS__), __PRETTY_FUNCTION__) +#define logTraceInfo(...) Logger::log(Logger::LogInfo, fw::mkstr(__VA_ARGS__), __PRETTY_FUNCTION__) +#define logTraceWarning(...) log(Logger::LogWarning, fw::mkstr(__VA_ARGS__), __PRETTY_FUNCTION__) +#define logTraceError(...) Logger::log(Logger::LogError, fw::mkstr(__VA_ARGS__), __PRETTY_FUNCTION__) #endif diff --git a/src/framework/util/point.h b/src/framework/util/point.h index 59e40f2f..d6ef7638 100644 --- a/src/framework/util/point.h +++ b/src/framework/util/point.h @@ -58,8 +58,7 @@ typedef TPoint PointF; template std::ostream& operator<<(std::ostream& out, const TPoint& point) { - out << "Point(" << point.x << "," - << point.y << ")"; + out << point.x << " " << point.y; return out; } diff --git a/src/framework/util/tools.h b/src/framework/util/tools.h new file mode 100644 index 00000000..280ec558 --- /dev/null +++ b/src/framework/util/tools.h @@ -0,0 +1,195 @@ +#ifndef TOOLS_H +#define TOOLS_H + +#include +#include +#include +#include +#include +#include + +namespace fw { + +/// Fill an ostream by concatenating args +/// Usage: +/// fw::fill_ostream(stream, a1, a2, ..., aN); +inline void fill_ostream(std::ostringstream&) { } +template +void fill_ostream(std::ostringstream& stream, const T& first, const Args&... rest) { + stream << first; + fill_ostream(stream, rest...); +} + +/// Makes a std::string by concatenating args +/// Usage: +/// std::string str = fw::mkstr(a1, a2, ..., aN); +template +std::string mkstr(const T&... args) { + std::ostringstream buf; + fill_ostream(buf, args...); + return buf.str(); +} + +// used by dumper +struct dump_util { + ~dump_util() { std::cout << std::endl; } + template + dump_util& operator<<(const T& v) { + std::cout << v << " "; + return *this; + } +}; + +/// Utility for dumping variables +/// Usage: +/// fw::dump << v1, v2, ..., vN; +struct dumper { + dumper() { } + template + dump_util operator<<(const T& v) const { + dump_util d; + d << v; + return d; + } +}; + +/// Utility for printing messages into stdout +/// Usage: +/// fw::print(v1, v2, ..., vN); +template +void print(const T&... args) { + std::ostringstream buf; + fill_ostream(buf, args...); + std::cout << buf.str(); +} + +/// Same as fw::print but adds a new line at the end +template +void println(const T&... args) { + print(args...); + std::cout << std::endl; +} + +/// Demangle names for GNU g++ compiler +inline std::string demangle_name(const char* name) { + size_t len; + int status; + std::string ret; + char* demangled = abi::__cxa_demangle(name, 0, &len, &status); + if(demangled) { + ret = demangled; + free(demangled); + } + return ret; +} + +/// Returns the name of a type +/// e.g. fw::demangle_type() returns a string containing 'Foo*' +template +std::string demangle_type() { + return demangle_name(typeid(T).name()); +} + +/// Cast a type to another type +/// @return whether the conversion was successful or not +template +bool cast(const T& in, R& out) { + std::stringstream ss; + ss << in; + ss >> out; + return !!ss && ss.eof(); +} + +// cast a type to string +template +bool cast(const T& in, std::string& out) { + std::stringstream ss; + ss << in; + out = ss.str(); + return true; +} + +// cast string to string +template<> +inline bool cast(const std::string& in, std::string& out) { + out = in; + return true; +} + +// special cast from string to boolean +template<> +inline bool cast(const std::string& in, bool& b) { + static std::string validNames[2][4] = {{"true","yes","on","1"}, {"false","no","off","0"}}; + bool ret = false; + for(int i=0;i<4;++i) { + if(in == validNames[0][i]) { + b = true; + ret = true; + break; + } else if(in == validNames[1][i]) { + b = false; + ret = true; + break; + } + } + return ret; +} + +// special cast from boolean to string +template<> +inline bool cast(const bool& in, std::string& out) { + out = (in ? "true" : "false"); + return true; +} + +// used by safe_cast +class bad_cast : public std::bad_cast { +public: + virtual ~bad_cast() throw() { } + template + void setWhat() { + m_what = mkstr("failed to cast value of type '", demangle_type(), + "' to type '", demangle_type(), "'"); + } + virtual const char* what() { return m_what.c_str(); } +private: + std::string m_what; +}; + +/// Cast a type to another type, any error throws a fw::bad_cast_exception +/// Usage: +/// R r = fw::safe_cast(t); +template +R safe_cast(const T& t) { + R r; + if(!cast(t, r)) { + bad_cast e; + e.setWhat(); + throw e; + } + return r; +} + +/// Cast a type to another type, cast errors are ignored +/// Usage: +/// R r = fw::unsafe_cast(t); +template +R unsafe_cast(const T& t) { + R r; + try { + r = safe_cast(t); + } catch(bad_cast& e) { + println(e.what()); + } + return r; +} + +// an empty string to use anywhere needed +const static std::string empty_string; + +} + +// shortcut for fw::dump +const static fw::dumper dump; + +#endif diff --git a/src/framework/util/translator.cpp b/src/framework/util/translator.cpp index c084c832..ba46a078 100644 --- a/src/framework/util/translator.cpp +++ b/src/framework/util/translator.cpp @@ -1,7 +1,7 @@ #include "translator.h" #include -AlignmentFlag parseAlignment(std::string aligment) +AlignmentFlag fw::translateAlignment(std::string aligment) { boost::to_lower(aligment); boost::erase_all(aligment, " "); @@ -25,7 +25,7 @@ AlignmentFlag parseAlignment(std::string aligment) return AlignCenter; } -AnchorPoint parseAnchorPoint(const std::string& anchorPoint) +AnchorPoint fw::translateAnchorPoint(const std::string& anchorPoint) { if(anchorPoint == "left") return AnchorLeft; diff --git a/src/framework/util/translator.h b/src/framework/util/translator.h index a3a0c0a4..b3420aee 100644 --- a/src/framework/util/translator.h +++ b/src/framework/util/translator.h @@ -1,9 +1,14 @@ #ifndef TRANSLATOR_H #define TRANSLATOR_H -#include +#include "../const.h" #include -AlignmentFlag parseAlignment(std::string aligment); -AnchorPoint parseAnchorPoint(const std::string& anchorPoint); +namespace fw { + +AlignmentFlag translateAlignment(std::string aligment); +AnchorPoint translateAnchorPoint(const std::string& anchorPoint); + +}; + #endif diff --git a/src/game.cpp b/src/game.cpp deleted file mode 100644 index d3968153..00000000 --- a/src/game.cpp +++ /dev/null @@ -1,8 +0,0 @@ -#include "game.h" - -Game g_game; - -Game::Game() -{ - m_online = false; -} diff --git a/src/game.h b/src/game.h deleted file mode 100644 index f1eb21a4..00000000 --- a/src/game.h +++ /dev/null @@ -1,33 +0,0 @@ -#ifndef GAME_H -#define GAME_H - -#include -#include "map.h" -#include "player.h" -#include "protocolgame.h" - -class Game -{ -public: - Game(); - - void setProtocol(ProtocolGamePtr protocolGame) { m_protocolGame = protocolGame; } - ProtocolGamePtr getProtocol() { return m_protocolGame; } - - Map *getMap() { return &m_map; } - Player *getPlayer() { return &m_player; } - - void setOnline(bool online) { m_online = online; } - bool getOnline() { return m_online; } - -private: - Map m_map; - Player m_player; - ProtocolGamePtr m_protocolGame; - bool m_online; - -}; - -extern Game g_game; - -#endif // GAME_H diff --git a/src/item.cpp b/src/item.cpp deleted file mode 100644 index 3c7a866d..00000000 --- a/src/item.cpp +++ /dev/null @@ -1,35 +0,0 @@ -#include "item.h" -#include "tibiadat.h" -#include "tibiaspr.h" -#include -#include "thing.h" - -Item::Item() -{ - m_type = Thing::TYPE_ITEM; -} - -ThingAttributes *Item::getAttributes() -{ - return g_tibiaDat.getItemAttributes(m_id); -} - -void Item::draw(int x, int y) -{ - ThingAttributes *itemAttributes = getAttributes(); - - int xdiv = 0, ydiv = 0, zdiv = 0, anim = 0; - - if(itemAttributes->group == THING_GROUP_SPLASH || itemAttributes->group == THING_GROUP_FLUID || itemAttributes->stackable) { - //cDivX = subType % itemAttributes->xdiv; - //cDivY = subType / itemAttributes->xdiv; - } - else if(!itemAttributes->moveable) { - xdiv = m_position.x % itemAttributes->xdiv; - ydiv = m_position.y % itemAttributes->ydiv; - zdiv = m_position.z % itemAttributes->zdiv; - } - - for(int b = 0; b < itemAttributes->blendframes; b++) - internalDraw(x, y, b, xdiv, ydiv, zdiv, anim); -} diff --git a/src/item.h b/src/item.h deleted file mode 100644 index b0a9fb18..00000000 --- a/src/item.h +++ /dev/null @@ -1,27 +0,0 @@ -#ifndef ITEM_H -#define ITEM_H - -#include -#include "thing.h" - -class Item; -typedef std::shared_ptr ItemPtr; - -class Item : public Thing -{ -public: - Item(); - - virtual ThingAttributes *getAttributes(); - void draw(int x, int y); - - void setCount(uint8 count) { m_count = count; } - - virtual Item* getItem() { return this; } - virtual const Item* getItem() const { return this; } - -private: - uint8 m_count; -}; - -#endif // ITEM_H diff --git a/src/main.cpp b/src/main.cpp index badb11fb..f39be66d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,7 +1,6 @@ -#include "otclient.h" - +#include #include -#include +#include void signal_handler(int sig) { diff --git a/src/map.cpp b/src/map.cpp deleted file mode 100644 index f09a8ae2..00000000 --- a/src/map.cpp +++ /dev/null @@ -1,75 +0,0 @@ -#include "map.h" -#include "game.h" -#include -#include - -void Map::draw(int x, int y) -{ - if(!m_framebuffer) - m_framebuffer = FrameBufferPtr(new FrameBuffer(15*32, 11*32)); - - g_graphics.bindColor(Color::white); - m_framebuffer->bind(); - - Position *playerPos = g_game.getPlayer()->getPosition(); - - // player is above 7 - if(playerPos->z <= 7) { - - // player pos it 8-6. check if we can draw upper floors. - bool draw = true; - for(int jz = 6; jz >= 0; --jz) { - Position coverPos = Position(playerPos->x-(6-jz), playerPos->y-(6-jz), jz); - if(m_tiles[coverPos]) { - if(m_tiles[coverPos]->getStackSize() > 0 && jz < playerPos->z) { - draw = false; - } - } - } - - for(int iz = 7; iz > 0; --iz) { - - // +1 in draws cause 64x64 items may affect view. - for(int ix = -7; ix < + 8+7; ++ix) { - for(int iy = -5; iy < + 6+7; ++iy) { - Position itemPos = Position(playerPos->x + ix, playerPos->y + iy, iz); - //Position drawPos = Position(ix + 8, iy - playerPos->y + 6, iz); - //logDebug("x: ", relativePos.x, " y: ", relativePos.y, " z: ", (int)relativePos.z); - if(m_tiles[itemPos]) - m_tiles[itemPos]->draw((ix + 7 - (7-iz))*32, (iy + 5 - (7-iz))*32); - } - } - - if(!draw) - break; - } - - } - - // draw effects - for(auto it = m_effects.begin(), end = m_effects.end(); it != end; ++it) { - Position *effectPos = (*it)->getPosition(); - (*it)->draw((effectPos->x - playerPos->x + 7) * 32, (effectPos->y - playerPos->y + 5) * 32); - } - - // debug draws - g_graphics.drawBoundingRect(Rect(7*32, 5*32, 32, 32), Color::red); - - m_framebuffer->unbind(); - - m_framebuffer->draw(x, y, g_graphics.getScreenSize().width(), g_graphics.getScreenSize().height()); -} - -void Map::addThing(ThingPtr thing, uint8 stackpos) -{ - if(thing->getType() == Thing::TYPE_ITEM || thing->getType() == Thing::TYPE_CREATURE) { - if(!m_tiles[*thing->getPosition()]) { - m_tiles[*thing->getPosition()] = TilePtr(new Tile()); - } - - m_tiles[*thing->getPosition()]->addThing(thing, stackpos); - } - else if(thing->getType() == Thing::TYPE_EFFECT) { - m_effects.push_back(thing); - } -} diff --git a/src/map.h b/src/map.h deleted file mode 100644 index e8dce278..00000000 --- a/src/map.h +++ /dev/null @@ -1,26 +0,0 @@ -#ifndef MAP_H -#define MAP_H - -#include "position.h" -#include "tile.h" -#include "effect.h" -#include -#include - -class Map -{ -public: - void addThing(ThingPtr thing, uint8 stackpos = 0); - - void draw(int x, int y); - - FrameBufferPtr getFramebuffer() { return m_framebuffer; } - -private: - std::unordered_map m_tiles; - std::list m_effects; - - FrameBufferPtr m_framebuffer; -}; - -#endif diff --git a/src/otclient.cpp b/src/otclient.cpp deleted file mode 100644 index d0da0227..00000000 --- a/src/otclient.cpp +++ /dev/null @@ -1,278 +0,0 @@ -#include "otclient.h" - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "protocolgame.h" -#include "game.h" -#include "map.h" - -#define POLL_CYCLE_DELAY 10 - -OTClient g_client; - -void OTClient::init(std::vector args) -{ - m_running = false; - m_stopping = false; - - // print client information - logInfo("OTClient 0.2.0"); - - // initialize platform related stuff - g_platform.init(this, "OTClient"); - - // initialize script environment - g_lua.init(); - - // register otclient lua functions - registerLuaFunctions(); - - // initialize resources - g_resources.init(args[0].c_str()); - - // load configurations - 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")); - g_platform.setWindowTitle("OTClient"); - - // initialize graphics - g_graphics.init(); - - // initialize event dispatcher - g_dispatcher.init(); - - // initialize network - //g_network.init(); - - // initialize the ui - g_ui.init(); - - // discover and load modules - g_modules.discoverAndLoadModules(); - - // now that everything is initialized, setup configurations - setupConfigurations(); - - // now that everything is loaded, show the window - g_platform.showWindow(); -} - -void OTClient::run() -{ - std::string fpsText; - Size fpsTextSize; - FontPtr defaultFont = g_fonts.getDefaultFont(); - int frameTicks = g_platform.getTicks(); - int lastFpsTicks = frameTicks; - int lastPollTicks = frameTicks; - int frameCount = 0; - - m_stopping = false; - m_running = true; - - if(g_ui.getRootWidget()->getChildCount() == 0) { - logError("ERROR: there is no root widgets to display, the app will close"); - m_stopping = true; - } - - // run the first poll - poll(); - - while(!m_stopping) { - frameTicks = g_platform.getTicks(); - - // calculate fps - frameCount++; - if(frameTicks - lastFpsTicks >= 1000) { - fpsText = aux::make_string("FPS: ", frameCount); - fpsTextSize = defaultFont->calculateTextRectSize(fpsText); - frameCount = 0; - lastFpsTicks = frameTicks; - } - - // poll events every POLL_CYCLE_DELAY - // this delay exists to avoid massive polling thus increasing framerate - if(frameTicks - lastPollTicks >= POLL_CYCLE_DELAY) { - poll(); - lastPollTicks = frameTicks; - } - - // only render when the windows is visible - if(g_platform.isWindowVisible()) { - // render begin - g_graphics.beginRender(); - - // render everything - render(); - - // render fps - defaultFont->renderText(fpsText, Point(g_graphics.getScreenSize().width() - fpsTextSize.width() - 10, 10)); - - // render end - g_graphics.endRender(); - - // swap the old buffer with the new rendered - g_platform.swapBuffers(); - } else { - // sleeps until next poll to avoid massive cpu usage - g_platform.sleep(POLL_CYCLE_DELAY+1); - } - } - - m_stopping = false; - m_running = false; -} - -void OTClient::terminate() -{ - // hide the window because there is no render anymore - g_platform.hideWindow(); - - // run modules unload event - g_modules.unloadModules(); - - // terminate ui - g_ui.terminate(); - - // release remaining lua object references - g_lua.collectGarbage(); - - // poll remaining events - poll(); - - // terminate network - //g_network.terminate(); - - // terminate dispatcher - g_dispatcher.terminate(); - - // terminate graphics - g_graphics.terminate(); - - // save configurations - saveConfigurations(); - - // release resources - g_resources.terminate(); - - // terminate script environment - g_lua.terminate(); - - // end platform related stuff - g_platform.terminate(); - -} - -void OTClient::exit() -{ - // stops the main loop - m_stopping = true; -} - -void OTClient::poll() -{ - // poll platform events - g_platform.poll(); - - // poll network events - //g_network.poll(); - Connection::poll(); - - // poll dispatcher events - g_dispatcher.poll(); -} - -void OTClient::render() -{ - if(g_game.getOnline()) - g_game.getMap()->draw(0, 0); - - // everything is rendered by UI components - g_ui.render(); -} - -void OTClient::loadConfigurations() -{ - // default window size - int defWidth = 550; - 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); - - // loads user configuration - if(!g_configs.load("config.otml")) - logInfo("Using default configurations."); -} - -void OTClient::setupConfigurations() -{ - // activate vertical synchronization? - g_platform.setVerticalSync(g_configs.get("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()); - - // saves user configuration - if(!g_configs.save()) - logError("ERROR: configurations are lost because it couldn't be saved"); -} - -void OTClient::onClose() -{ - if(m_onCloseCallback) - m_onCloseCallback(); - else - exit(); -} - -void OTClient::onResize(const Size& size) -{ - g_graphics.resize(size); - g_ui.resize(size); -} - -void OTClient::onInputEvent(const InputEvent& event) -{ - g_ui.inputEvent(event); - - ProtocolGamePtr protocol = g_game.getProtocol(); - if(protocol) { - if(event.type == EventKeyDown) { - if(event.keycode == KC_UP) - protocol->sendWalkNorth(); - if(event.keycode == KC_RIGHT) - protocol->sendWalkEast(); - if(event.keycode == KC_DOWN) - protocol->sendWalkSouth(); - if(event.keycode == KC_LEFT) - protocol->sendWalkWest(); - } - } -} diff --git a/src/otclient.h b/src/otclient.h deleted file mode 100644 index 78215f69..00000000 --- a/src/otclient.h +++ /dev/null @@ -1,52 +0,0 @@ -#ifndef OTCLIENT_H -#define OTCLIENT_H - -#include - -class OTClient : public PlatformListener -{ -public: - /// Where everything begins... - void init(std::vector args); - /// Main loop - void run(); - /// Called when the client is terminating - void terminate(); - - /// Stop running - void exit(); - /// Poll platform, dispatcher and network events - void poll(); - /// Render each frame - void render(); - - /// Fired when user tryes to close the window - void onClose(); - /// Fired when user resize the window - void onResize(const Size& size); - /// Fired on an user input event - void onInputEvent(const InputEvent& event); - - bool isRunning() const { return m_running; } - - void setOnClose(const SimpleCallback& onCloseCallback) { m_onCloseCallback = onCloseCallback; } - SimpleCallback getOnClose() const { return m_onCloseCallback; } - -private: - /// Set default configurations and load the user configurations - void loadConfigurations(); - /// Use the loaded configurations to setup other classes - void setupConfigurations(); - void saveConfigurations(); - - void registerLuaFunctions(); - - bool m_running; - bool m_stopping; - - SimpleCallback m_onCloseCallback; -}; - -extern OTClient g_client; - -#endif diff --git a/src/otclientluafunctions.cpp b/src/otclientluafunctions.cpp deleted file mode 100644 index 77282278..00000000 --- a/src/otclientluafunctions.cpp +++ /dev/null @@ -1,25 +0,0 @@ -#include "otclient.h" -#include - -#include "tibiadat.h" -#include "tibiaspr.h" -#include "protocollogin.h" -#include "protocolgame.h" - -void OTClient::registerLuaFunctions() -{ - // easy typing _1, _2, ... - using namespace std::placeholders; - - g_lua.bindGlobalFunction("exit", std::bind(&OTClient::exit, &g_client)); - g_lua.bindGlobalFunction("setOnClose", std::bind(&OTClient::setOnClose, &g_client, _1)); - g_lua.bindGlobalFunction("importDat", std::bind(&TibiaDat::load, &g_tibiaDat, _1)); - g_lua.bindGlobalFunction("importSpr", std::bind(&TibiaSpr::load, &g_tibiaSpr, _1)); - - g_lua.registerClass(); - g_lua.bindClassStaticFunction("create", &ProtocolLogin::create); - g_lua.bindClassMemberFunction("login", &ProtocolLogin::login); - g_lua.bindClassMemberFunction("cancel", &ProtocolLogin::cancel); - - g_lua.registerClass(); -} diff --git a/src/pch.h b/src/pch.h new file mode 100644 index 00000000..65ad133c --- /dev/null +++ b/src/pch.h @@ -0,0 +1,7 @@ +#ifndef PCH_H +#define PCH_H + +#include "framework/global.h" +#include "otclient/global.h" + +#endif diff --git a/src/player.h b/src/player.h deleted file mode 100644 index b2aff45e..00000000 --- a/src/player.h +++ /dev/null @@ -1,22 +0,0 @@ -#ifndef PLAYER_H -#define PLAYER_H - -#include "creature.h" - -class Player : public Creature -{ -public: - void setDrawSpeed(uint16 drawSpeed) { m_drawSpeed = drawSpeed; } - uint16 getDrawSpeed() { return m_drawSpeed; } - - void setCanReportBugs(uint8 canReportBugs) { m_canReportBugs = (canReportBugs != 0); } - bool getCanReportBugs() { return m_canReportBugs; } - -private: - - uint16 m_drawSpeed; - bool m_canReportBugs; - -}; - -#endif diff --git a/src/position.h b/src/position.h deleted file mode 100644 index 91c106c9..00000000 --- a/src/position.h +++ /dev/null @@ -1,38 +0,0 @@ -#ifndef POSITION_H -#define POSITION_H - -#include - -enum Direction -{ - DIRECTION_NORTH, - DIRECTION_EAST, - DIRECTION_SOUTH, - DIRECTION_WEST -}; - -class Position -{ -public: - Position(uint16 x = 0, uint16 y = 0, uint8 z = 0) { - this->x = x; - this->y = y; - this->z = z; - } - - bool operator==(const Position& other) const { return other.x == x && other.y == y && other.z == z; } - const Position operator+(const Position& other) const { return Position(other.x + x, other.y + y, other.z + z); } - const Position operator-(const Position& other) const { return Position(other.x - x, other.y - y, other.z - z); } - - uint16 x, y; - uint8 z; -}; - -struct PositionHash : public std::unary_function -{ - size_t operator()(const Position& pos) const { - return ((((pos.x * 65536) + pos.y) * 15) + pos.z) % (1 * 1000000); - } -}; - -#endif diff --git a/src/protocolgame.cpp b/src/protocolgame.cpp deleted file mode 100644 index f172a2ad..00000000 --- a/src/protocolgame.cpp +++ /dev/null @@ -1,99 +0,0 @@ -#include "protocolgame.h" -#include "game.h" -#include - -ProtocolGame::ProtocolGame() -{ - m_checksumEnabled = false; - g_game.setProtocol(ProtocolGamePtr(this)); -} - -ProtocolGame::~ProtocolGame() -{ - sendLogout(); - g_game.setProtocol(NULL); -} - -void ProtocolGame::login(const std::string& accountName, const std::string& accountPassword, uint32 ip, uint16 port, const std::string& characterName) -{ - if(accountName.empty() || accountPassword.empty()) { - callLuaField("onError", "You must enter an account name and password."); - return; - } - - m_accountName = accountName; - m_accountPassword = accountPassword; - m_characterName = characterName; - - char host[16]; - sprintf(host, "%d.%d.%d.%d", (uint8)ip, (uint8)(ip >> 8), (uint8)(ip >> 16), (uint8)(ip >> 24)); - - connect(host, port); -} - -void ProtocolGame::onConnect() -{ - recv(); -} - -void ProtocolGame::sendLoginPacket(uint32 timestamp, uint8 unknown) -{ - OutputMessage oMsg; - - oMsg.addU8(0x0A); // Protocol id - oMsg.addU16(0x02); // OS - oMsg.addU16(862); // Client version - - oMsg.addU8(0); // First RSA byte must be 0x00 // 1 - - // Generete xtea key. - m_xteaKey[0] = 432324; - m_xteaKey[1] = 24324; - m_xteaKey[2] = 423432; - m_xteaKey[3] = 234324; - - // Add xtea key - oMsg.addU32(m_xteaKey[0]); // 5 - oMsg.addU32(m_xteaKey[1]); // 9 - oMsg.addU32(m_xteaKey[2]); // 13 - oMsg.addU32(m_xteaKey[3]); // 17 - - oMsg.addU8(0); // is gm set? - oMsg.addString(m_accountName); // Account Name // 20 - oMsg.addString(m_characterName); // Character Name // 22 - oMsg.addString(m_accountPassword); // Password // 24 - - oMsg.addU32(timestamp); // 28 - oMsg.addU8(unknown); // 29 - - // Packet data must have since byte 0, start, 128 bytes - oMsg.addPaddingBytes(128 - (29 + m_accountName.length() + m_characterName.length() + m_accountPassword.length())); - - // Encrypt msg with RSA - if(!Rsa::encrypt((char*)oMsg.getBuffer() + 6 + oMsg.getMessageSize() - 128, 128, OTSERV_PUBLIC_RSA)) - return; - - send(oMsg); - - m_xteaEncryptionEnabled = true; - - recv(); -} - -void ProtocolGame::onRecv(InputMessage& inputMessage) -{ - static bool firstRecv = true; - if(firstRecv) { - inputMessage.skipBytes(3); - uint32 timestamp = inputMessage.getU32(); - uint8 unknown = inputMessage.getU8(); - - m_checksumEnabled = true; - sendLoginPacket(timestamp, unknown); - - firstRecv = false; - } - else { - parseMessage(inputMessage); - } -} diff --git a/src/protocolgame.h b/src/protocolgame.h deleted file mode 100644 index c99b5ed4..00000000 --- a/src/protocolgame.h +++ /dev/null @@ -1,145 +0,0 @@ -#ifndef PROTOCOLGAME_H -#define PROTOCOLGAME_H - -#include -#include "player.h" -#include "item.h" - -class ProtocolGame; -typedef std::shared_ptr ProtocolGamePtr; - -class ProtocolGame : public Protocol -{ - -public: - ProtocolGame(); - ~ProtocolGame(); - -public: - void login(const std::string& accountName, const std::string& accountPassword, uint32 ip, uint16 port, const std::string& characterName); - - void onConnect(); - void onRecv(InputMessage& inputMessage); - - // Send Messages - void sendLogout(); - void sendPing(); - void sendWalkNorth(); - void sendWalkEast(); - void sendWalkSouth(); - void sendWalkWest(); - -private: - void sendLoginPacket(uint32 timestamp, uint8 unknown); - - // Parse Messages - void parseMessage(InputMessage& msg); - - void parsePlayerLogin(InputMessage& msg); - void parseGMActions(InputMessage& msg); - void parseErrorMessage(InputMessage& msg); - void parseFYIMessage(InputMessage& msg); - void parseWaitList(InputMessage& msg); - void parsePing(InputMessage&); - void parseDeath(InputMessage&); - void parseCanReportBugs(InputMessage& msg); - void parseMapDescription(InputMessage& msg); - void parseMoveNorth(InputMessage& msg); - void parseMoveEast(InputMessage& msg); - void parseMoveSouth(InputMessage& msg); - void parseMoveWest(InputMessage& msg); - void parseUpdateTile(InputMessage& msg); - void parseTileAddThing(InputMessage& msg); - void parseTileTransformThing(InputMessage& msg); - void parseTileRemoveThing(InputMessage& msg); - void parseCreatureMove(InputMessage& msg); - void parseOpenContainer(InputMessage& msg); - void parseCloseContainer(InputMessage& msg); - void parseContainerAddItem(InputMessage& msg); - void parseContainerUpdateItem(InputMessage& msg); - void parseContainerRemoveItem(InputMessage& msg); - void parseAddInventoryItem(InputMessage& msg); - void parseRemoveInventoryItem(InputMessage& msg); - void parseOpenShopWindow(InputMessage& msg); - void parsePlayerCash(InputMessage& msg); - void parseCloseShopWindow(InputMessage&); - void parseWorldLight(InputMessage& msg); - void parseMagicEffect(InputMessage& msg); - void parseAnimatedText(InputMessage& msg); - void parseDistanceShot(InputMessage& msg); - void parseCreatureSquare(InputMessage& msg); - void parseCreatureHealth(InputMessage& msg); - void parseCreatureLight(InputMessage& msg); - void parseCreatureOutfit(InputMessage& msg); - void parseCreatureSpeed(InputMessage& msg); - void parseCreatureSkulls(InputMessage& msg); - void parseCreatureShields(InputMessage& msg); - void parseCreatureTurn(InputMessage& msg); - void parseItemTextWindow(InputMessage& msg); - void parseHouseTextWindow(InputMessage& msg); - void parsePlayerStats(InputMessage& msg); - void parsePlayerSkills(InputMessage& msg); - void parsePlayerIcons(InputMessage& msg); - void parsePlayerCancelAttack(InputMessage& msg); - void parseCreatureSpeak(InputMessage& msg); - void parseChannelList(InputMessage& msg); - void parseOpenChannel(InputMessage& msg); - void parseOpenPrivatePlayerChat(InputMessage& msg); - void parseOpenRuleViolation(InputMessage& msg); - void parseRuleViolationAF(InputMessage& msg); - void parseRuleViolationB0(InputMessage& msg); - void parseRuleViolationB1(InputMessage& msg); - void parseCreatePrivateChannel(InputMessage& msg); - void parseClosePrivateChannel(InputMessage& msg); - void parseSafeTradeRequest(InputMessage& msg); - void parseSafeTradeClose(InputMessage&); - void parseTextMessage(InputMessage& msg); - void parseCancelWalk(InputMessage& msg); - void parseFloorChangeUp(InputMessage& msg); - void parseFloorChangeDown(InputMessage& msg); - void parseOutfitWindow(InputMessage& msg); - void parseVipState(InputMessage& msg); - void parseVipLogin(InputMessage& msg); - void parseVipLogout(InputMessage& msg); - void parseShowTutorial(InputMessage& msg); - void parseAddMarker(InputMessage& msg); - void parseQuestList(InputMessage& msg); - void parseQuestPartList(InputMessage& msg); - - void setMapDescription(InputMessage& msg, int32 x, int32 y, int32 z, int32 width, int32 height); - void setFloorDescription(InputMessage& msg, int32 x, int32 y, int32 z, int32 width, int32 height, int32 offset, int32* skipTiles); - void setTileDescription(InputMessage& msg, Position position); - ThingPtr internalGetThing(InputMessage& msg); - Outfit internalCreatureOutfit(InputMessage& msg); - ItemPtr internalGetItem(InputMessage& msg, uint16 id); - - Position parsePosition(InputMessage& msg); - - std::string m_accountName, m_accountPassword, m_characterName; -}; - -// TODO: place it somewhere else -enum SpeakClasses { - SPEAK_SAY = 0x01, //normal talk - SPEAK_WHISPER = 0x02, //whispering - #w text - SPEAK_YELL = 0x03, //yelling - #y text - SPEAK_PRIVATE_PN = 0x04, //Player-to-NPC speaking(NPCs channel) - SPEAK_PRIVATE_NP = 0x05, //NPC-to-Player speaking - SPEAK_PRIVATE = 0x06, //Players speaking privately to players - SPEAK_CHANNEL_Y = 0x07, //Yellow message in chat - SPEAK_CHANNEL_W = 0x08, //White message in chat - SPEAK_RVR_CHANNEL = 0x09, //Reporting rule violation - Ctrl+R - SPEAK_RVR_ANSWER = 0x0A, //Answering report - SPEAK_RVR_CONTINUE = 0x0B, //Answering the answer of the report - SPEAK_BROADCAST = 0x0C, //Broadcast a message - #b - SPEAK_CHANNEL_R1 = 0x0D, //Talk red on chat - #c - SPEAK_PRIVATE_RED = 0x0E, //Red private - @name@ text - SPEAK_CHANNEL_O = 0x0F, //Talk orange on text - //SPEAK_ = 0x10, //? - SPEAK_CHANNEL_R2 = 0x11, //Talk red anonymously on chat - #d - //SPEAK_ = 0x12, //? - SPEAK_MONSTER_SAY = 0x13, //Talk orange - SPEAK_MONSTER_YELL = 0x14 //Yell orange -}; - -#endif // PROTOCOLGAME_H diff --git a/src/protocolgameparse.cpp b/src/protocolgameparse.cpp deleted file mode 100644 index 77077d48..00000000 --- a/src/protocolgameparse.cpp +++ /dev/null @@ -1,943 +0,0 @@ -#include "protocolgame.h" -#include "player.h" -#include "tibiadat.h" -#include "game.h" -#include "map.h" -#include "effect.h" -#include - -void ProtocolGame::parseMessage(InputMessage& msg) -{ - while(!msg.end()) { - uint8 opt = msg.getU8(); - - dump << "Protocol opt: " << std::hex << (int)opt; - - switch(opt) { - case 0x0A: - parsePlayerLogin(msg); - break; - case 0x0B: - parseGMActions(msg); - break; - case 0x14: - parseErrorMessage(msg); - break; - case 0x15: - parseFYIMessage(msg); - break; - case 0x16: - parseWaitList(msg); - break; - case 0x1E: - parsePing(msg); - break; - case 0x28: - parseDeath(msg); - break; - case 0x32: - parseCanReportBugs(msg); - break; - case 0x64: - parseMapDescription(msg); - break; - case 0x65: - parseMoveNorth(msg); - break; - case 0x66: - parseMoveEast(msg); - break; - case 0x67: - parseMoveSouth(msg); - break; - case 0x68: - parseMoveWest(msg); - break; - case 0x69: - parseUpdateTile(msg); - break; - case 0x6A: - parseTileAddThing(msg); - break; - case 0x6B: - parseTileTransformThing(msg); - break; - case 0x6C: - parseTileRemoveThing(msg); - break; - case 0x6D: - parseCreatureMove(msg); - break; - case 0x6E: - parseOpenContainer(msg); - break; - case 0x6F: - parseCloseContainer(msg); - break; - case 0x70: - parseContainerAddItem(msg); - break; - case 0x71: - parseContainerUpdateItem(msg); - break; - case 0x72: - parseContainerRemoveItem(msg); - break; - case 0x78: - parseAddInventoryItem(msg); - break; - case 0x79: - parseRemoveInventoryItem(msg); - break; - case 0x7A: - parseOpenShopWindow(msg); - break; - case 0x7B: - parsePlayerCash(msg); - break; - case 0x7C: - parseCloseShopWindow(msg); - break; - case 0x7D: - parseSafeTradeRequest(msg); - break; - case 0x7E: - parseSafeTradeRequest(msg); - break; - case 0x7F: - parseSafeTradeClose(msg); - break; - case 0x82: - parseWorldLight(msg); - break; - case 0x83: - parseMagicEffect(msg); - break; - case 0x84: - parseAnimatedText(msg); - break; - case 0x85: - parseDistanceShot(msg); - break; - case 0x86: - parseCreatureSquare(msg); - break; - case 0x8C: - parseCreatureHealth(msg); - break; - case 0x8D: - parseCreatureLight(msg); - break; - case 0x8E: - parseCreatureOutfit(msg); - break; - case 0x8F: - parseCreatureSpeed(msg); - break; - case 0x90: - parseCreatureSkulls(msg); - break; - case 0x91: - parseCreatureShields(msg); - break; - case 0x96: - parseItemTextWindow(msg); - break; - case 0x97: - parseHouseTextWindow(msg); - break; - case 0xA0: - parsePlayerStats(msg); - break; - case 0xA1: - parsePlayerSkills(msg); - break; - case 0xA2: - parsePlayerIcons(msg); - break; - case 0xA3: - parsePlayerCancelAttack(msg); - break; - case 0xAA: - parseCreatureSpeak(msg); - break; - case 0xAB: - parseChannelList(msg); - break; - case 0xAC: - parseOpenChannel(msg); - break; - case 0xAD: - parseOpenPrivatePlayerChat(msg); - break; - case 0xAE: - parseOpenRuleViolation(msg); - break; - case 0xAF: - parseRuleViolationAF(msg); - break; - case 0xB0: - parseRuleViolationB0(msg); - break; - case 0xB1: - parseRuleViolationB1(msg); - break; - case 0xB2: - parseCreatePrivateChannel(msg); - break; - case 0xB3: - parseClosePrivateChannel(msg); - break; - case 0xB4: - parseTextMessage(msg); - break; - case 0xB5: - parseCancelWalk(msg); - break; - case 0xBE: - parseFloorChangeUp(msg); - break; - case 0xBF: - parseFloorChangeDown(msg); - break; - case 0xC8: - parseOutfitWindow(msg); - break; - case 0xD2: - parseVipState(msg); - break; - case 0xD3: - parseVipLogin(msg); - break; - case 0xD4: - parseVipLogout(msg); - break; - case 0xDC: - parseShowTutorial(msg); - break; - case 0xDD: - parseAddMarker(msg); - break; - case 0xF0: - parseQuestList(msg); - break; - case 0xF1: - parseQuestPartList(msg); - break; - default: - logDebug("UNKNOWN PACKET BYTE.", opt); - //skipPacket = true; - break; - } - } - - recv(); -} - -void ProtocolGame::parsePlayerLogin(InputMessage& msg) -{ - Player *player = g_game.getPlayer(); - player->setId(msg.getU32()); - player->setDrawSpeed(msg.getU16()); - player->setCanReportBugs(msg.getU8()); - - g_game.setOnline(true); -} - -void ProtocolGame::parseGMActions(InputMessage& msg) -{ - for(uint8 i = 0; i < 28; ++i) - msg.getU8(); -} - -void ProtocolGame::parseErrorMessage(InputMessage& msg) -{ - std::string error = msg.getString(); - callLuaField("onError", error); -} - -void ProtocolGame::parseFYIMessage(InputMessage& msg) -{ - msg.getString(); // message -} - -void ProtocolGame::parseWaitList(InputMessage& msg) -{ - msg.getString(); // message - msg.getU8();// + 1 // time -} - -void ProtocolGame::parsePing(InputMessage&) -{ - sendPing(); -} - -void ProtocolGame::parseDeath(InputMessage&) -{ - -} - -void ProtocolGame::parseCanReportBugs(InputMessage& msg) -{ - msg.getU8(); // report bugs -} - -void ProtocolGame::parseMapDescription(InputMessage& msg) -{ - Player *player = g_game.getPlayer(); - player->setPosition(parsePosition(msg)); - setMapDescription(msg, player->getPosition()->x - 8, player->getPosition()->y - 6, player->getPosition()->z, 18, 14); -} - -void ProtocolGame::parseMoveNorth(InputMessage& msg) -{ - Player *player = g_game.getPlayer(); - player->getPosition()->y--; - setMapDescription(msg, player->getPosition()->x - 8, player->getPosition()->y - 6, player->getPosition()->z, 18, 1); -} - -void ProtocolGame::parseMoveEast(InputMessage& msg) -{ - Player *player = g_game.getPlayer(); - player->getPosition()->x++; - setMapDescription(msg, player->getPosition()->x + 9, player->getPosition()->y - 6, player->getPosition()->z, 1, 14); -} - -void ProtocolGame::parseMoveSouth(InputMessage& msg) -{ - Player *player = g_game.getPlayer(); - player->getPosition()->y++; - setMapDescription(msg, player->getPosition()->x - 8, player->getPosition()->y + 7, player->getPosition()->z, 18, 1); -} - -void ProtocolGame::parseMoveWest(InputMessage& msg) -{ - Player *player = g_game.getPlayer(); - player->getPosition()->x--; - setMapDescription(msg, player->getPosition()->x - 8, player->getPosition()->y - 6, player->getPosition()->z, 1, 14); -} - -void ProtocolGame::parseUpdateTile(InputMessage& msg) -{ - Position tilePos = parsePosition(msg); - uint16 thingId = msg.getU16(true); - if(thingId == 0xFF01) { - msg.getU16(); - } - else { - setTileDescription(msg, tilePos); - msg.getU16(); - } -} - -void ProtocolGame::parseTileAddThing(InputMessage& msg) -{ - parsePosition(msg); // tilePos - msg.getU8(); // stackPos - internalGetThing(msg); -} - -void ProtocolGame::parseTileTransformThing(InputMessage& msg) -{ - parsePosition(msg); // tilePos - msg.getU8(); // stackPos - - uint16 thingId = msg.getU16(); - if(thingId == 0x0061 || thingId == 0x0062 || thingId == 0x0063) { - msg.getU32(); // creatureId - msg.getU8(); // direction - } - else { - internalGetItem(msg, thingId); - } -} - -void ProtocolGame::parseTileRemoveThing(InputMessage& msg) -{ - parsePosition(msg); // tilePos - msg.getU8(); // stackPos -} - -void ProtocolGame::parseCreatureMove(InputMessage& msg) -{ - parsePosition(msg); // oldPos - msg.getU8(); // oldStackPos - parsePosition(msg); // newPos -} - -void ProtocolGame::parseOpenContainer(InputMessage& msg) -{ - msg.getU8(); // cid - msg.getU16(); // itemid - msg.getString(); // name - msg.getU8(); // capacity - msg.getU8(); // hasParent - uint8 size = msg.getU8(); // size - - for(uint32 i = 0; i < size; i++) - internalGetItem(msg, 0xFFFF); -} - -void ProtocolGame::parseCloseContainer(InputMessage& msg) -{ - msg.getU8(); // cid -} - -void ProtocolGame::parseContainerAddItem(InputMessage& msg) -{ - msg.getU8(); // cid - internalGetItem(msg, 0xFFFF); -} - -void ProtocolGame::parseContainerUpdateItem(InputMessage& msg) -{ - msg.getU8(); // cid - msg.getU8(); // slot - internalGetItem(msg, 0xFFFF); -} - -void ProtocolGame::parseContainerRemoveItem(InputMessage& msg) -{ - msg.getU8(); // cid - msg.getU8(); // slot -} - -void ProtocolGame::parseAddInventoryItem(InputMessage& msg) -{ - msg.getU8(); // slot - internalGetItem(msg, 0xFFFF); -} - -void ProtocolGame::parseRemoveInventoryItem(InputMessage& msg) -{ - msg.getU8(); // slot -} - -void ProtocolGame::parseOpenShopWindow(InputMessage& msg) -{ - uint8 listCount = msg.getU8(); - for(uint8 i = 0; i < listCount; ++i) { - msg.getU16(); // item id - msg.getU8(); // runecharges - msg.getString(); // item name - msg.getU32(); // weight - msg.getU32(); // buy price - msg.getU32(); // sell price - } -} - -void ProtocolGame::parsePlayerCash(InputMessage& msg) -{ - msg.getU32(); - uint8 size = msg.getU8(); - - for(int i = 0; i < size; i++) { - msg.getU16(); // itemid - msg.getU8(); // runecharges - } -} - -void ProtocolGame::parseCloseShopWindow(InputMessage&) -{ -} - -void ProtocolGame::parseSafeTradeRequest(InputMessage& msg) -{ - msg.getString(); // name - uint8 count = msg.getU8(); - - for(uint8 i = 0; i < count; i++) - internalGetItem(msg, 0xFFFF); -} - -void ProtocolGame::parseSafeTradeClose(InputMessage&) -{ -} - -void ProtocolGame::parseWorldLight(InputMessage& msg) -{ - msg.getU8(); // level - msg.getU8(); // color -} - -void ProtocolGame::parseMagicEffect(InputMessage& msg) -{ - EffectPtr effect = EffectPtr(new Effect()); - effect->setPosition(parsePosition(msg)); - effect->setId(msg.getU8()); - - g_game.getMap()->addThing(effect); -} - -void ProtocolGame::parseAnimatedText(InputMessage& msg) -{ - parsePosition(msg); // textPos - msg.getU8(); // color - msg.getString(); // text -} - -void ProtocolGame::parseDistanceShot(InputMessage& msg) -{ - parsePosition(msg); // fromPos - parsePosition(msg); // toPos - msg.getU8(); // effect -} - -void ProtocolGame::parseCreatureSquare(InputMessage& msg) -{ - msg.getU32(); // creatureId - msg.getU8(); // color -} - -void ProtocolGame::parseCreatureHealth(InputMessage& msg) -{ - msg.getU32(); // creatureId - msg.getU8(); // percent -} - -void ProtocolGame::parseCreatureLight(InputMessage& msg) -{ - msg.getU32(); // creature id - msg.getU8(); // level - msg.getU8(); // color -} - -void ProtocolGame::parseCreatureOutfit(InputMessage& msg) -{ - msg.getU32(); // creature id - internalCreatureOutfit(msg); -} - -void ProtocolGame::parseCreatureSpeed(InputMessage& msg) -{ - msg.getU32(); // creature id - msg.getU16(); // speed -} - -void ProtocolGame::parseCreatureSkulls(InputMessage& msg) -{ - msg.getU32(); // creature id - msg.getU8(); // skull -} - -void ProtocolGame::parseCreatureShields(InputMessage& msg) -{ - msg.getU32(); // creature id - msg.getU8(); // shield -} - -void ProtocolGame::parseCreatureTurn(InputMessage& msg) -{ - msg.getU32(); // creature id - msg.getU8(); // direction -} - -void ProtocolGame::parseItemTextWindow(InputMessage& msg) -{ - msg.getU32(); // windowId - msg.getU16(); // itemid - msg.getU16(); // max length - msg.getString(); // text - msg.getString(); // writter - msg.getString(); // date -} - -void ProtocolGame::parseHouseTextWindow(InputMessage& msg) -{ - msg.getU8(); // unknown - msg.getU32(); // windowId - msg.getString(); // text -} - -void ProtocolGame::parsePlayerStats(InputMessage& msg) -{ - msg.getU16(); // health - msg.getU16(); // max health - msg.getU32(); // free capacity - msg.getU32(); // experience - msg.getU16(); // level - msg.getU8(); // level percent - msg.getU16(); // mana - msg.getU16(); // max mana - msg.getU8(); // magic level - msg.getU8(); // magic level percent - msg.getU8(); // soul - msg.getU16(); // stamina -} - -void ProtocolGame::parsePlayerSkills(InputMessage& msg) -{ - msg.getU8(); // fist skill - msg.getU8(); // fist percent - - msg.getU8(); // club skill - msg.getU8(); // club percent - - msg.getU8(); // sword skill - msg.getU8(); // sword percent - - msg.getU8(); // axe skill - msg.getU8(); // axe percent - - msg.getU8(); // distance skill - msg.getU8(); // distance percent - - msg.getU8(); // shielding skill - msg.getU8(); // shielding percent - - msg.getU8(); // fishing skill - msg.getU8(); // fishing percent -} - -void ProtocolGame::parsePlayerIcons(InputMessage& msg) -{ - msg.getU16(); // icons -} - -void ProtocolGame::parsePlayerCancelAttack(InputMessage& msg) -{ - msg.getU32(); -} - -void ProtocolGame::parseCreatureSpeak(InputMessage& msg) -{ - msg.getU32(); // unkSpeak - msg.getString(); // name - msg.getU16(); // level - uint8 type = msg.getU8(); - - switch(type) { - case SPEAK_SAY: - case SPEAK_WHISPER: - case SPEAK_YELL: - case SPEAK_MONSTER_SAY: - case SPEAK_MONSTER_YELL: - case SPEAK_PRIVATE_NP: - parsePosition(msg); // creaturePos - break; - case SPEAK_CHANNEL_R1: - case SPEAK_CHANNEL_R2: - case SPEAK_CHANNEL_O: - case SPEAK_CHANNEL_Y: - case SPEAK_CHANNEL_W: - msg.getU16(); // channelId - break; - case SPEAK_RVR_CHANNEL: - msg.getU32(); // time - break; - default: - //qDebug() << "Unknown speak type. opt: 0xAA, type: " << type; - break; - } - - msg.getString(); // message -} - -void ProtocolGame::parseChannelList(InputMessage& msg) -{ - uint8 count = msg.getU8(); - for(uint8 i = 0; i < count; i++) { - msg.getU16(); - msg.getString(); - } -} - -void ProtocolGame::parseOpenChannel(InputMessage& msg) -{ - msg.getU16(); // channelId - msg.getString(); // name -} - -void ProtocolGame::parseOpenPrivatePlayerChat(InputMessage& msg) -{ - msg.getString(); // name -} - -void ProtocolGame::parseOpenRuleViolation(InputMessage& msg) -{ - msg.getU16(); // a -} - -void ProtocolGame::parseRuleViolationAF(InputMessage& msg) -{ - msg.getU16(); // a -} - -void ProtocolGame::parseRuleViolationB0(InputMessage& msg) -{ - msg.getU16(); // a -} - -void ProtocolGame::parseRuleViolationB1(InputMessage& msg) -{ - msg.getU16(); // a -} - -void ProtocolGame::parseCreatePrivateChannel(InputMessage& msg) -{ - msg.getU16(); // channel id - msg.getString(); // channel name -} - -void ProtocolGame::parseClosePrivateChannel(InputMessage& msg) -{ - msg.getU16(); // channel id -} - -void ProtocolGame::parseTextMessage(InputMessage& msg) -{ - msg.getU8(); // messageType - std::string message = msg.getString(); - logDebug(message); -} - -void ProtocolGame::parseCancelWalk(InputMessage& msg) -{ - msg.getU8(); // direction -} - -void ProtocolGame::parseFloorChangeUp(InputMessage& msg) -{ - Player *player = g_game.getPlayer(); - player->getPosition()->z--; - - int32 skip = 0; - if(player->getPosition()->z == 7) - for(int32 i = 5; i >= 0; i--) - setFloorDescription(msg, player->getPosition()->x - 8, player->getPosition()->y - 6, i, 18, 14, 8 - i, &skip); - else if(player->getPosition()->z > 7) - setFloorDescription(msg, player->getPosition()->x - 8, player->getPosition()->y - 6, player->getPosition()->z - 2, 18, 14, 3, &skip); - - player->getPosition()->x++; - player->getPosition()->y++; -} - -void ProtocolGame::parseFloorChangeDown(InputMessage& msg) -{ - Player *player = g_game.getPlayer(); - player->getPosition()->z++; - - int32 skip = 0; - if(player->getPosition()->z == 8) { - int32 j, i; - for(i = player->getPosition()->z, j = -1; i < (int32)player->getPosition()->z + 3; ++i, --j) - setFloorDescription(msg, player->getPosition()->x - 8, player->getPosition()->y - 6, i, 18, 14, j, &skip); - } - else if(player->getPosition()->z > 8 && player->getPosition()->z < 14) - setFloorDescription(msg, player->getPosition()->x - 8, player->getPosition()->y - 6, player->getPosition()->z + 2, 18, 14, -3, &skip); - - player->getPosition()->x--; - player->getPosition()->y--; -} - -void ProtocolGame::parseOutfitWindow(InputMessage& msg) -{ - internalCreatureOutfit(msg); - uint8 outfitCount = msg.getU8(); - - for(int i = 0; i < outfitCount; i++) { - msg.getU16(); // outfit id - msg.getString(); // outfit name - msg.getU8(); // addons - } -} - -void ProtocolGame::parseVipState(InputMessage& msg) -{ - msg.getU32(); // player id - msg.getString(); // player name - msg.getU8(); // online -} - -void ProtocolGame::parseVipLogin(InputMessage& msg) -{ - msg.getU32(); // player id -} - -void ProtocolGame::parseVipLogout(InputMessage& msg) -{ - msg.getU32(); // player id -} - -void ProtocolGame::parseShowTutorial(InputMessage& msg) -{ - msg.getU8(); // tutorial id -} - -void ProtocolGame::parseAddMarker(InputMessage& msg) -{ - parsePosition(msg); // position - msg.getU8(); // icon - msg.getString(); // message -} - -void ProtocolGame::parseQuestList(InputMessage& msg) -{ - uint16 questsCount = msg.getU16(); - for(uint16 i = 0; i < questsCount; i++) { - msg.getU16(); // quest id - msg.getString(); // quest name - msg.getU8(); // quest state - } -} - -void ProtocolGame::parseQuestPartList(InputMessage& msg) -{ - msg.getU16(); // quest id - uint8 missionCount = msg.getU8(); - for(uint8 i = 0; i < missionCount; i++) { - msg.getString(); // quest name - msg.getString(); // quest description - } -} - -void ProtocolGame::setMapDescription(InputMessage& msg, int32 x, int32 y, int32 z, int32 width, int32 height) -{ - int startz, endz, zstep, skip = 0; - - if(z > 7) { - startz = z - 2; - endz = (15 < z+2) ? 15 : z+2; - zstep = 1; - } - else { - startz = 7; - endz = 0; - zstep = -1; - } - - logDebug((int)startz); - - for(int nz = startz; nz != endz + zstep; nz += zstep) - setFloorDescription(msg, x, y, nz, width, height, z - nz, &skip); -} - -void ProtocolGame::setFloorDescription(InputMessage& msg, int32 x, int32 y, int32 z, int32 width, int32 height, int32 offset, int32* skipTiles) -{ - int32 skip = *skipTiles; - - for(int32 nx = 0; nx < width; nx++) { - for(int32 ny = 0; ny < height; ny++) { - if(skip == 0) { - uint16 tileOpt = msg.getU16(true); - if(tileOpt >= 0xFF00) - skip = (msg.getU16() & 0xFF); - else { - Position pos(x + nx + offset, y + ny + offset, z); - setTileDescription(msg, pos); - skip = (msg.getU16() & 0xFF); - } - } - else - skip--; - } - } - *skipTiles = skip; -} - -void ProtocolGame::setTileDescription(InputMessage& msg, Position position) -{ - int stackpos = 0; - while(1){ - stackpos++; - uint16 inspectTileId = msg.getU16(true); - if(inspectTileId >= 0xFF00) - return; - else { - if(stackpos > 10) { - logDebug("[ProtocolGame::setTileDescription] Too many things!."); - return; - } - - ThingPtr thing = internalGetThing(msg); - if(thing) - thing->setPosition(position); - g_game.getMap()->addThing(thing, stackpos); - } - } -} - -ThingPtr ProtocolGame::internalGetThing(InputMessage& msg) -{ - ThingPtr thing; - - uint16 thingId = msg.getU16(); - if(thingId == 0x0061 || thingId == 0x0062) { // add new creature - CreaturePtr creature = CreaturePtr(new Creature); - - if(thingId == 0x0062) { //creature is known - creature->setId(msg.getU32()); - } - else if(thingId == 0x0061) { //creature is not known - msg.getU32(); // remove id - creature->setId(msg.getU32()); - creature->setName(msg.getString()); - } - - creature->setHealthPercent(msg.getU8()); - creature->setDirection((Direction)msg.getU8()); - creature->setOutfit(internalCreatureOutfit(msg)); - msg.getU8(); // light level - msg.getU8(); // light color - msg.getU16(); // speed - msg.getU8(); // skull - msg.getU8(); // shield - - if(thingId == 0x0061) // emblem is sent only in packet type 0x61 - msg.getU8(); - msg.getU8(); // impassable - - thing = creature; - - } - else if(thingId == 0x0063) { // creature turn - parseCreatureTurn(msg); - } - else // item - thing = internalGetItem(msg, thingId); - - return thing; -} - -Outfit ProtocolGame::internalCreatureOutfit(InputMessage& msg) -{ - Outfit outfit; - - outfit.type = msg.getU16(); // looktype - if(outfit.type != 0) { - outfit.head = msg.getU8(); - outfit.body = msg.getU8(); - outfit.legs = msg.getU8(); - outfit.feet = msg.getU8(); - outfit.addons = msg.getU8(); - } - else { - outfit.type = msg.getU16(); - } - - return outfit; -} - -ItemPtr ProtocolGame::internalGetItem(InputMessage& msg, uint16 id) -{ - ItemPtr item = ItemPtr(new Item()); - - if(id == 0xFFFF) - id = msg.getU16(); - item->setId(id); - - ThingAttributes *itemAttributes = g_tibiaDat.getItemAttributes(id); - if(itemAttributes->stackable || itemAttributes->group == THING_GROUP_FLUID || itemAttributes->group == THING_GROUP_SPLASH) - item->setCount(msg.getU8()); - - return item; -} - -Position ProtocolGame::parsePosition(InputMessage& msg) -{ - uint16 x = msg.getU16(); - uint16 y = msg.getU16(); - uint8 z = msg.getU8(); - - return Position(x, y, z); -} diff --git a/src/protocolgamesend.cpp b/src/protocolgamesend.cpp deleted file mode 100644 index 696f794c..00000000 --- a/src/protocolgamesend.cpp +++ /dev/null @@ -1,44 +0,0 @@ -#include "protocolgame.h" - -void ProtocolGame::sendLogout() -{ - OutputMessage oMsg; - oMsg.addU8(0x14); - send(oMsg); -} - -void ProtocolGame::sendPing() -{ - OutputMessage oMsg; - oMsg.addU8(0x1E); - send(oMsg); -} - -void ProtocolGame::sendWalkNorth() -{ - OutputMessage oMsg; - oMsg.addU8(0x65); - send(oMsg); -} - -void ProtocolGame::sendWalkEast() -{ - OutputMessage oMsg; - oMsg.addU8(0x66); - send(oMsg); -} - -void ProtocolGame::sendWalkSouth() -{ - OutputMessage oMsg; - oMsg.addU8(0x67); - send(oMsg); -} - -void ProtocolGame::sendWalkWest() -{ - OutputMessage oMsg; - oMsg.addU8(0x68); - send(oMsg); -} - diff --git a/src/protocollogin.cpp b/src/protocollogin.cpp deleted file mode 100644 index 74e7db80..00000000 --- a/src/protocollogin.cpp +++ /dev/null @@ -1,153 +0,0 @@ -#include "protocollogin.h" -#include -#include -#include -#include - -// TODO just testing -#include "protocolgame.h" - -ProtocolLogin::ProtocolLogin() -{ - -} - -ProtocolLogin::~ProtocolLogin() -{ - -} - -void ProtocolLogin::login(const std::string& accountName, const std::string& accountPassword) -{ - if(accountName.empty() || accountPassword.empty()) { - callLuaField("onError", "You must enter an account name and password."); - return; - } - - m_accountName = accountName; - m_accountPassword = accountPassword; - - /*static const char hosts[][32] = { - "login01.tibia.com", - "login02.tibia.com", - "login03.tibia.com", - "login04.tibia.com", - "login05.tibia.com", - "tibia01.cipsoft.com", - "tibia02.cipsoft.com", - "tibia03.cipsoft.com", - "tibia04.cipsoft.com", - "tibia05.cipsoft.com" - }; - - std::string host = hosts[rand() % 10];*/ - std::string host = "sv3.radbr.com"; - uint16 port = 7171; - - connect(host, port); -} - -void ProtocolLogin::onConnect() -{ - sendLoginPacket(); -} - -void ProtocolLogin::sendLoginPacket() -{ - OutputMessage oMsg; - - oMsg.addU8(0x01); // Protocol id - oMsg.addU16(0x02); // OS - oMsg.addU16(862); // Client version - - oMsg.addU32(0x4E12DAFF); // Data Signature - oMsg.addU32(0x4E12DB27); // Sprite Signature - oMsg.addU32(0x4E119CBF); // Picture Signature - - oMsg.addU8(0); // First RSA byte must be 0x00 // 1 - - // Generete xtea key. - m_xteaKey[0] = 432324; - m_xteaKey[1] = 24324; - m_xteaKey[2] = 423432; - m_xteaKey[3] = 234324; - - // Add xtea key - oMsg.addU32(m_xteaKey[0]); // 5 - oMsg.addU32(m_xteaKey[1]); // 9 - oMsg.addU32(m_xteaKey[2]); // 13 - oMsg.addU32(m_xteaKey[3]); // 17 - - oMsg.addString(m_accountName); // Account Name // 19 - oMsg.addString(m_accountPassword); // Password // 21 - - // Packet data must have since byte 0, start, 128 bytes - oMsg.addPaddingBytes(128 - (21 + m_accountName.length() + m_accountPassword.length())); - - // Encrypt msg with RSA - if(!Rsa::encrypt((char*)oMsg.getBuffer() + 6 + oMsg.getMessageSize() - 128, 128, OTSERV_PUBLIC_RSA)) - return; - - send(oMsg); - - m_xteaEncryptionEnabled = true; - - recv(); -} - -void ProtocolLogin::onRecv(InputMessage& inputMessage) -{ - while(!inputMessage.end()) { - uint8 opt = inputMessage.getU8(); - logDebug("opt:",(uint)opt); - switch(opt) { - case 0x0A: - parseError(inputMessage); - break; - case 0x14: - parseMOTD(inputMessage); - break; - case 0x1e: - callLuaField("onError", "Client needs update."); - break; - case 0x64: - parseCharacterList(inputMessage); - break; - } - } -} - -void ProtocolLogin::parseError(InputMessage& inputMessage) -{ - std::string error = inputMessage.getString(); - logDebug(error); - callLuaField("onError", error); -} - -void ProtocolLogin::parseMOTD(InputMessage& inputMessage) -{ - std::string motd = inputMessage.getString(); - logDebug(motd); - callLuaField("onMotd", motd); -} - -void ProtocolLogin::parseCharacterList(InputMessage& inputMessage) -{ - uint8 characters = inputMessage.getU8(); - for(int i = 0; i < characters; ++i) { - std::string name = inputMessage.getString(); - std::string world = inputMessage.getString(); - uint32 ip = inputMessage.getU32(); - uint16 port = inputMessage.getU16(); - - logDebug("character: ", name.c_str(), world.c_str(), ip, " ", port); - - // TODO just test - if(i == 0) { - ProtocolGamePtr protocolGame = ProtocolGamePtr(new ProtocolGame); - protocolGame->login(m_accountName, m_accountPassword, ip, port, name); - } - } - uint16 premiumDays = inputMessage.getU16(); - logDebug("prem days: ", premiumDays); -} diff --git a/src/protocollogin.h b/src/protocollogin.h deleted file mode 100644 index b2f48386..00000000 --- a/src/protocollogin.h +++ /dev/null @@ -1,38 +0,0 @@ -#ifndef PROTOCOLLOGIN_H -#define PROTOCOLLOGIN_H - -#include - -class ProtocolLogin; -typedef std::shared_ptr ProtocolLoginPtr; - -class ProtocolLogin : public Protocol -{ -public: - ProtocolLogin(); - ~ProtocolLogin(); - - static ProtocolLoginPtr create() { return ProtocolLoginPtr(new ProtocolLogin); } - - void login(const std::string& accountName, const std::string& accountPassword); - - void onConnect(); - void onRecv(InputMessage& inputMessage); - - void cancel() { /* TODO: this func */ } - - ProtocolLoginPtr asProtocolLogin() { return std::static_pointer_cast(shared_from_this()); } - virtual const char* getLuaTypeName() const { return "ProtocolLogin"; } - -private: - void sendLoginPacket(); - - void parseError(InputMessage& inputMessage); - void parseMOTD(InputMessage& inputMessage); - void parseCharacterList(InputMessage& inputMessage); - - std::string m_accountName, m_accountPassword; - -}; - -#endif diff --git a/src/thing.cpp b/src/thing.cpp deleted file mode 100644 index fe8f042d..00000000 --- a/src/thing.cpp +++ /dev/null @@ -1,83 +0,0 @@ -#include "thing.h" -#include "tibiaspr.h" -#include - -ThingAttributes::ThingAttributes() -{ - group = THING_GROUP_NONE; - blockSolid = false; - hasHeight = false; - blockPathFind = false; - blockProjectile = false; - alwaysOnTop = false; - alwaysOnTopOrder = 0; - stackable = false; - useable = false; - moveable = true; - pickupable = false; - rotable = false; - readable = false; - lookThrough = false; - speed = 0; - lightLevel = 0; - lightColor = 0; - isVertical = false; - isHorizontal = false; - isHangable = false; - miniMapColor = 0; - hasMiniMapColor = false; - subParam07 = 0; - subParam08 = 0; - sprites = NULL; - width = 0; - height = 0; - blendframes = 0; - xdiv = 0; - ydiv = 0; - zdiv = 0; - animcount = 0; - xOffset = 0; - yOffset = 0; -} - -ThingAttributes::~ThingAttributes() -{ - if(sprites) - delete []sprites; -} - -Thing::Thing() -{ - m_type = TYPE_NONE; -} - -void Thing::internalDraw(int x, int y, int blendframes, int xdiv, int ydiv, int zdiv, int anim) -{ - ThingAttributes *thingAttributes = getAttributes(); - if(!thingAttributes) - return; - - for(int yi = 0; yi < thingAttributes->height; yi++) { - for(int xi = 0; xi < thingAttributes->width; xi++) { - uint16 sprIndex = xi + - yi * thingAttributes->width + - blendframes * thingAttributes->width * thingAttributes->height + - xdiv * thingAttributes->width * thingAttributes->height * thingAttributes->blendframes + - ydiv * thingAttributes->width * thingAttributes->height * thingAttributes->blendframes * thingAttributes->xdiv + - zdiv * thingAttributes->width * thingAttributes->height * thingAttributes->blendframes * thingAttributes->xdiv * thingAttributes->ydiv + - anim * thingAttributes->width * thingAttributes->height * thingAttributes->blendframes * thingAttributes->xdiv * thingAttributes->ydiv * thingAttributes->zdiv; - uint16 itemId = thingAttributes->sprites[sprIndex]; - if(itemId == 0xFFFF) - continue; - TexturePtr data = g_tibiaSpr.getSprite(itemId); - - int offsetX = 0, offsetY = 0; - if(thingAttributes->hasHeight) { - offsetX = thingAttributes->xOffset; - offsetY = thingAttributes->xOffset; // << look to xoffset - } - - g_graphics.drawTexturedRect(Rect((x - xi*32) - offsetX, (y - yi*32) - offsetY, 32, 32), data, Rect(0, 0, 32, 32)); - } - } -} diff --git a/src/thing.h b/src/thing.h deleted file mode 100644 index 80145bc3..00000000 --- a/src/thing.h +++ /dev/null @@ -1,83 +0,0 @@ -#ifndef THING_H -#define THING_H - -#include -#include "position.h" - -enum ThingAttributesGroup { - THING_GROUP_NONE = 0, - THING_GROUP_GROUND, - THING_GROUP_CONTAINER, - THING_GROUP_WEAPON, - THING_GROUP_AMMUNITION, - THING_GROUP_ARMOR, - THING_GROUP_RUNE, - THING_GROUP_TELEPORT, - THING_GROUP_MAGICFIELD, - THING_GROUP_WRITEABLE, - THING_GROUP_KEY, - THING_GROUP_SPLASH, - THING_GROUP_FLUID, - THING_GROUP_DOOR, - THING_GROUP_LAST -}; - -struct ThingAttributes -{ - ThingAttributes(); - ~ThingAttributes(); - - bool stackable, alwaysOnTop, useable, readable, moveable, blockSolid, blockProjectile, blockPathFind, pickupable, - isHangable, isHorizontal, isVertical, rotable, hasHeight, lookThrough, hasMiniMapColor; - uint8 alwaysOnTopOrder, width, height, blendframes, xdiv, ydiv, zdiv, animcount, xOffset, yOffset; - uint16 id, speed, subParam07, subParam08, lightLevel, lightColor, miniMapColor; - uint16 *sprites; - ThingAttributesGroup group; -}; - -class Creature; -class Item; - -class Thing; -typedef std::shared_ptr ThingPtr; - -class Thing -{ -public: - Thing(); - - enum Type { - TYPE_NONE, - TYPE_ITEM, - TYPE_CREATURE, - TYPE_EFFECT, - TYPE_SHOT - }; - - void setId(uint32 id) { m_id = id; } - uint32 getId() { return m_id; } - - void setType(Type type) { m_type = type; } - Type getType() const { return m_type; } - - void setPosition(const Position& position) { m_position = position; } - Position *getPosition() { return &m_position; } - - virtual void draw(int, int) {} - virtual ThingAttributes *getAttributes() { return NULL; } - - virtual Item* getItem() { return NULL; } - virtual const Item *getItem() const { return NULL; } - virtual Creature *getCreature() { return NULL; } - virtual const Creature *getCreature() const { return NULL; } - -protected: - void internalDraw(int x, int y, int blendframes, int xdiv, int ydiv, int zdiv, int anim); - - uint32 m_id; - Type m_type; - Position m_position; - -}; - -#endif // THING_H diff --git a/src/tibiadat.cpp b/src/tibiadat.cpp deleted file mode 100644 index d437c864..00000000 --- a/src/tibiadat.cpp +++ /dev/null @@ -1,205 +0,0 @@ -#include "tibiadat.h" -#include "tibiaspr.h" -#include - -TibiaDat g_tibiaDat; - -bool TibiaDat::load(const std::string& filename) -{ - std::stringstream fin; - g_resources.loadFile(filename, fin); - - fin.read((char*)&m_signature, 4); - - fin.read((char*)&m_groupCount[0], 2); - fin.read((char*)&m_groupCount[1], 2); - fin.read((char*)&m_groupCount[2], 2); - fin.read((char*)&m_groupCount[3], 2); - - m_groupCount[0] -= 99; - - m_totalCount = m_groupCount[0] + m_groupCount[1] + m_groupCount[2] + m_groupCount[3]; - - m_thingsAttributes = new ThingAttributes*[m_totalCount+1]; - for(uint16 i = 0; i <= m_totalCount; i++) - m_thingsAttributes[i] = NULL; - - uint8 read_byte; - uint16 read_short; - //uint32 read_long; - - for(uint16 id = 0; (id <= m_totalCount) && !fin.eof(); id++) { - m_thingsAttributes[id] = new ThingAttributes(); - m_thingsAttributes[id]->id = id; - - uint8 opt; - bool done = false; - while(!done) { - fin.read((char*)&opt, 1); - - if(opt == 0x00) { // Ground tile - fin.read((char*)&m_thingsAttributes[id]->speed, 2); - m_thingsAttributes[id]->group = THING_GROUP_GROUND; - } - else if(opt == 0x01) { // All OnTop - m_thingsAttributes[id]->alwaysOnTop = true; - m_thingsAttributes[id]->alwaysOnTopOrder = 1; - } - else if(opt == 0x02) { // Can walk trough (open doors, arces, bug pen fence) - m_thingsAttributes[id]->alwaysOnTop = true; - m_thingsAttributes[id]->alwaysOnTopOrder = 2; - } - else if(opt == 0x03) { // Can walk trough (arces) - m_thingsAttributes[id]->alwaysOnTop = true; - m_thingsAttributes[id]->alwaysOnTopOrder = 3; - } - else if(opt == 0x04) { // Container - m_thingsAttributes[id]->group = THING_GROUP_CONTAINER; - } - else if(opt == 0x05) { // Stackable - m_thingsAttributes[id]->stackable = true; - } - else if(opt == 0x06) { // Unknown - - } - else if(opt == 0x07) { // Useable - m_thingsAttributes[id]->useable = true; - } - else if(opt == 0x08) { // Writtable - m_thingsAttributes[id]->group = THING_GROUP_WRITEABLE; - m_thingsAttributes[id]->readable = true; - fin.read((char*)&m_thingsAttributes[id]->subParam07, 2); - } - else if(opt == 0x09) { // Writtable once - // Writtable objects that can't be edited by players - m_thingsAttributes[id]->readable = true; - fin.read((char*)&m_thingsAttributes[id]->subParam08, 2); - } - else if(opt == 0x0A) { // Fluid containers - fin.read((char*)&read_byte, 1); - m_thingsAttributes[id]->group = THING_GROUP_FLUID; - } - else if(opt == 0x0B) { // Splashes - m_thingsAttributes[id]->group = THING_GROUP_SPLASH; - } - else if(opt == 0x0C) { // Blocks solid objects (creatures, walls etc) - m_thingsAttributes[id]->blockSolid = true; - } - else if(opt == 0x0D) { // Not moveable - m_thingsAttributes[id]->moveable = false; - } - else if(opt == 0x0E) { // Blocks missiles (walls, magic wall etc) - m_thingsAttributes[id]->blockProjectile = true; - } - else if(opt == 0x0F) { // Blocks pathfind algorithms (monsters) - m_thingsAttributes[id]->blockPathFind = true; - } - else if(opt == 0x10) { // Blocks monster movement (flowers, parcels etc) - m_thingsAttributes[id]->pickupable = true; - } - else if(opt == 0x11) { // Hangable objects (wallpaper etc) - m_thingsAttributes[id]->isHangable = true; - } - else if(opt == 0x12) { // Horizontal wall - m_thingsAttributes[id]->isHorizontal = true; - } - else if(opt == 0x13) { // Vertical wall - m_thingsAttributes[id]->isVertical = true; - } - else if(opt == 0x14) { // Rotable - m_thingsAttributes[id]->rotable = true; - } - else if(opt == 0x15) { // Light info - fin.read((char*)&m_thingsAttributes[id]->lightLevel, 2); - fin.read((char*)&m_thingsAttributes[id]->lightColor, 2); - } - else if(opt == 0x16) { - } - else if(opt == 0x17) { // Changes floor - } - else if(opt == 0x18) { // Thing must be drawed with offset - m_thingsAttributes[id]->hasHeight = true; - fin.read((char*)&m_thingsAttributes[id]->xOffset, 1); - fin.read((char*)&m_thingsAttributes[id]->yOffset, 1); - - fin.read((char*)&read_short, 2); - } - else if(opt == 0x19) { // pixels characters height - - fin.read((char*)&m_thingsAttributes[id]->xOffset, 1); - fin.read((char*)&m_thingsAttributes[id]->yOffset, 1); - //logDebug((int)m_thingsAttributes[id]->xOffset, " ", (int)m_thingsAttributes[id]->yOffset); - } - else if(opt == 0x1A) { - //m_thingsAttributes[id]->hasHeight = true; - } - else if(opt == 0x1B) { - } - else if(opt == 0x1C) { // Minimap color - fin.read((char*)&m_thingsAttributes[id]->miniMapColor, 2); - m_thingsAttributes[id]->hasMiniMapColor = true; - } - else if(opt == 0x1D) { // Unknown - fin.read((char*)&read_short, 2); - if(read_short == 1112) - m_thingsAttributes[id]->readable = true; - } - else if(opt == 0x1E) { - } - else if(opt == 0x1F) { - m_thingsAttributes[id]->lookThrough = true; - } - else if(opt == 0x20) { - } - else if(opt == 0xFF) { - done = true; - } - else { - logDebug("Unknown byte code: ", opt); - return false; - } - } - - fin.read((char*)&m_thingsAttributes[id]->width, 1); - fin.read((char*)&m_thingsAttributes[id]->height, 1); - if((m_thingsAttributes[id]->width > 1) || (m_thingsAttributes[id]->height > 1)) - fin.read((char*)&read_byte, 1); - - fin.read((char*)&m_thingsAttributes[id]->blendframes, 1); - fin.read((char*)&m_thingsAttributes[id]->xdiv, 1); - fin.read((char*)&m_thingsAttributes[id]->ydiv, 1); - fin.read((char*)&m_thingsAttributes[id]->zdiv, 1); - fin.read((char*)&m_thingsAttributes[id]->animcount, 1); - - // Read sprites id. - uint16 totalSprites = m_thingsAttributes[id]->width * m_thingsAttributes[id]->height * m_thingsAttributes[id]->blendframes * m_thingsAttributes[id]->xdiv * m_thingsAttributes[id]->ydiv * m_thingsAttributes[id]->zdiv * m_thingsAttributes[id]->animcount; - - m_thingsAttributes[id]->sprites = new uint16[totalSprites]; - for(uint16 i = 0; i < totalSprites; i++) { - fin.read((char*)&read_short, 2); - m_thingsAttributes[id]->sprites[i] = read_short-1; - } - } - - return true; -} - -ThingAttributes *TibiaDat::getItemAttributes(uint16 id) -{ - return m_thingsAttributes[id - 100]; -} - -ThingAttributes *TibiaDat::getCreatureAttributes(uint16 id) -{ - return m_thingsAttributes[id - 1 + m_groupCount[0]]; -} - -ThingAttributes *TibiaDat::getEffectAttributes(uint16 id) -{ - return m_thingsAttributes[id - 1 + m_groupCount[0] + m_groupCount[1]]; -} - -ThingAttributes *TibiaDat::getShotAttributes(uint16 id) -{ - return m_thingsAttributes[id - 100 + m_groupCount[0] + m_groupCount[1] + m_groupCount[2]]; -} diff --git a/src/tibiadat.h b/src/tibiadat.h deleted file mode 100644 index 362a3a8f..00000000 --- a/src/tibiadat.h +++ /dev/null @@ -1,31 +0,0 @@ -#ifndef TIBIADAT_H -#define TIBIADAT_H - -#include -#include "thing.h" - -class TibiaDat -{ -public: - bool load(const std::string& filename); - - ThingAttributes *getItemAttributes(uint16 id); - ThingAttributes *getCreatureAttributes(uint16 id); - ThingAttributes *getEffectAttributes(uint16 id); - ThingAttributes *getShotAttributes(uint16 id); - - uint16 getGroupCount(int i) { return m_groupCount[i]; } - - uint32 getSignature() { return m_signature; } - uint16 getTotalCount() { return m_totalCount; } - -private: - uint32 m_signature, m_totalCount; - uint16 m_groupCount[4]; - - ThingAttributes **m_thingsAttributes; -}; - -extern TibiaDat g_tibiaDat; - -#endif // TIBIADAT_H diff --git a/src/tibiaspr.cpp b/src/tibiaspr.cpp deleted file mode 100644 index 78cf38a0..00000000 --- a/src/tibiaspr.cpp +++ /dev/null @@ -1,120 +0,0 @@ -#include "tibiaspr.h" -#include - -TibiaSpr g_tibiaSpr; - -TibiaSpr::TibiaSpr() -{ - m_sprites = NULL; - m_spritesCount = 0; -} - -TibiaSpr::~TibiaSpr() -{ - //for(uint16 i = 0; i < m_spritesCount; i++) - //if(m_sprites[i]) - //delete m_sprites[i]; - - if(m_sprites) - delete []m_sprites; - - //delete m_transparentSprite; -} - -bool TibiaSpr::load(const std::string &filename) -{ - g_resources.loadFile(filename, m_fin); - - m_fin.read((char*)&m_signature, 4); - m_fin.read((char*)&m_spritesCount, 2); - - m_sprites = new TexturePtr[m_spritesCount]; - - //for(uint16 i = 0; i < m_spritesCount; i++) - //m_sprites[i] = NULL; - - - uchar pixels[4096]; - for(int i = 0; i < 32*32*4; i += 4) { - pixels[i + 0] = 0xFF; - pixels[i + 1] = 0x00; - pixels[i + 2] = 0xFF; - pixels[i + 3] = 0xFF; - } - m_transparentSprite = TexturePtr(new Texture(32, 32, 4, pixels)); - - return true; -} - -TexturePtr TibiaSpr::loadSprite(uint32 id) -{ - if(m_fin.eof()) - return TexturePtr(); - - m_fin.seekg((id * 4) + 6, std::ios_base::beg); - - uint32 spriteAddress; - m_fin.read((char*)&spriteAddress, 4); - - if(spriteAddress == 0) // Some error on tibia.spr, save a blank image. - return TexturePtr(); - - m_fin.seekg(spriteAddress, std::ios_base::beg); - - uint32 colorKey = 0; - m_fin.read((char*)&colorKey, 3); - - uint16 spriteSize; - m_fin.read((char*)&spriteSize, 2); - - uchar image[4096]; - uint16 imgPos = 0, read = 0, transparentPixels, coloredPixels, x; - while(read < spriteSize) { - m_fin.read((char*)&transparentPixels, 2); - m_fin.read((char*)&coloredPixels, 2); - - for(x = 0; x < transparentPixels; x++) { - image[imgPos + 0] = 0x00; - image[imgPos + 1] = 0x00; - image[imgPos + 2] = 0x00; - image[imgPos + 3] = 0x00; - imgPos += 4; - } - - for(x = 0; x < coloredPixels; x++) { - m_fin.read((char*)&image[imgPos + 0], 1); - m_fin.read((char*)&image[imgPos + 1], 1); - m_fin.read((char*)&image[imgPos + 2], 1); - image[imgPos + 3] = 0xFF; - - imgPos += 4; - } - - read += 4 + (3 * coloredPixels); - } - - // Fill remaining bytes with pink. - if(imgPos < 32 * 32 * 4) { - for(x = imgPos; x < 32 * 32 * 4; x += 4) { - image[x + 0] = 0xFF; - image[x + 1] = 0x00; - image[x + 2] = 0xFF; - image[x + 3] = 0x00; - } - } - - return TexturePtr(new Texture(32, 32, 4, image)); -} - -TexturePtr TibiaSpr::getSprite(uint32 id) -{ - if(!m_sprites) - return m_transparentSprite; - - if(!m_sprites[id]) { - m_sprites[id] = loadSprite(id); - if(!m_sprites[id]) - return m_transparentSprite; - } - return m_sprites[id]; -} diff --git a/src/tibiaspr.h b/src/tibiaspr.h deleted file mode 100644 index ace3afd2..00000000 --- a/src/tibiaspr.h +++ /dev/null @@ -1,31 +0,0 @@ -#ifndef TIBIASPR_H -#define TIBIASPR_H - -#include -#include - -class TibiaSpr -{ -public: - TibiaSpr(); - ~TibiaSpr(); - - bool load(const std::string& filename); - - uint32 getSignature() { return m_signature; } - uint16 getSpritesCount() { return m_spritesCount; } - TexturePtr getSprite(uint32 id); - -private: - TexturePtr loadSprite(uint32 id); - - std::stringstream m_fin; - uint32 m_signature; - uint16 m_spritesCount; - TexturePtr *m_sprites; - TexturePtr m_transparentSprite; -}; - -extern TibiaSpr g_tibiaSpr; - -#endif // TIBIASPR_H diff --git a/src/tile.cpp b/src/tile.cpp deleted file mode 100644 index f39620fb..00000000 --- a/src/tile.cpp +++ /dev/null @@ -1,66 +0,0 @@ -#include "tile.h" -#include "item.h" -#include "tibiadat.h" - -Tile::Tile() -{ - m_ground = NULL; -} - -void Tile::addThing(ThingPtr thing, uint8 stackpos) -{ - if(!thing) - return; - - ThingAttributes *thingAttributes = thing->getAttributes(); - if(thingAttributes) { - if(thing->getType() == Thing::TYPE_ITEM) { - if(thingAttributes->group == THING_GROUP_GROUND) - m_ground = thing; - else { - if(thingAttributes->alwaysOnTop) - m_itemsTop.push_back(thing); - else - m_itemsBot.push_back(thing); - } - } - else if(thing->getType() == Thing::TYPE_CREATURE) { - m_creatures.push_back(thing); - } - } - -} - -void Tile::draw(int x, int y) -{ - if(m_ground) - m_ground->draw(x, y); - - for(auto it = m_itemsTop.begin(), end = m_itemsTop.end(); it != end; ++it) - (*it)->draw(x, y); - - for(auto it = m_creatures.begin(), end = m_creatures.end(); it != end; ++it) - (*it)->draw(x, y); - - for(auto it = m_itemsBot.begin(), end = m_itemsBot.end(); it != end; ++it) - (*it)->draw(x, y); - -} - -bool Tile::hasGround() -{ - return m_ground != 0; -} - -int Tile::getStackSize() -{ - int ret = 0; - if(m_ground) - ret++; - - ret += m_itemsBot.size(); - ret += m_creatures.size(); - ret += m_itemsTop.size(); - - return ret; -} diff --git a/src/tile.h b/src/tile.h deleted file mode 100644 index 852e0b7b..00000000 --- a/src/tile.h +++ /dev/null @@ -1,29 +0,0 @@ -#ifndef TILE_H -#define TILE_H - -#include -#include "thing.h" -#include "position.h" - -class Tile; -typedef std::shared_ptr TilePtr; - -class Tile -{ -public: - Tile(); - - void addThing(ThingPtr thing, uint8 stackpos); - - void draw(int x, int y); - bool hasGround(); - int getStackSize(); - -private: - ThingPtr m_ground; - std::list m_itemsBot; - std::list m_creatures; - std::list m_itemsTop; -}; - -#endif // TILE_H