This commit is contained in:
Eduardo Bart 2011-04-17 16:14:24 -03:00
commit c0fce81238
93 changed files with 862 additions and 824 deletions

View File

@ -59,7 +59,7 @@ SET(SOURCES
src/teststate.cpp src/teststate.cpp
# game net # game net
src/net/protocoltibia87.cpp src/protocollogin.cpp
# framework core # framework core
src/framework/core/dispatcher.cpp src/framework/core/dispatcher.cpp
@ -108,7 +108,6 @@ SET(SOURCES
# framework net # framework net
src/framework/net/connection.cpp src/framework/net/connection.cpp
src/framework/net/connections.cpp
src/framework/net/protocol.cpp src/framework/net/protocol.cpp
src/framework/net/networkmessage.cpp src/framework/net/networkmessage.cpp
) )

View File

@ -22,8 +22,9 @@
*/ */
#include "configs.h" #include <prerequisites.h>
#include "resources.h" #include <core/configs.h>
#include <core/resources.h>
Configs g_configs; Configs g_configs;

View File

@ -25,7 +25,7 @@
#ifndef CONFIGS_H #ifndef CONFIGS_H
#define CONFIGS_H #define CONFIGS_H
#include "prerequisites.h" #include <prerequisites.h>
class Configs class Configs
{ {

View File

@ -22,9 +22,9 @@
*/ */
#include "dispatcher.h" #include <prerequisites.h>
#include "platform.h" #include <core/dispatcher.h>
#include "engine.h" #include <core/engine.h>
Dispatcher g_dispatcher; Dispatcher g_dispatcher;
@ -32,7 +32,7 @@ void Dispatcher::poll()
{ {
while(!m_taskList.empty()) { while(!m_taskList.empty()) {
Task *task = m_taskList.top(); Task *task = m_taskList.top();
if(g_engine.getLastFrameTicks() < task->ticks) if(g_engine.getCurrentFrameTicks() < task->ticks)
break; break;
task->callback(); task->callback();
@ -43,7 +43,7 @@ void Dispatcher::poll()
void Dispatcher::scheduleTask(const Callback& callback, int delay) void Dispatcher::scheduleTask(const Callback& callback, int delay)
{ {
m_taskList.push(new Task(Platform::getTicks() + delay, callback)); m_taskList.push(new Task(g_engine.getCurrentFrameTicks() + delay, callback));
} }
void Dispatcher::addTask(const Callback& callback) void Dispatcher::addTask(const Callback& callback)

View File

@ -25,9 +25,7 @@
#ifndef DISPATCHER_H #ifndef DISPATCHER_H
#define DISPATCHER_H #define DISPATCHER_H
#include "prerequisites.h" #include <prerequisites.h>
#include <queue>
class Task { class Task {
public: public:

View File

@ -22,15 +22,14 @@
*/ */
#include "engine.h" #include <prerequisites.h>
#include "graphics/fonts.h" #include <core/engine.h>
#include "platform.h" #include <core/platform.h>
#include "graphics/graphics.h" #include <core/dispatcher.h>
#include "configs.h" #include <graphics/graphics.h>
#include "dispatcher.h" #include <graphics/fonts.h>
#include "net/connections.h" #include <ui/uicontainer.h>
#include "ui/uicontainer.h" //#include <net/connection.h>
#include "graphics/fonts.h"
Engine g_engine; Engine g_engine;
@ -70,7 +69,7 @@ void Engine::run()
Platform::poll(); Platform::poll();
// poll network events // poll network events
g_connections.poll(); //Connection::poll();
// poll diaptcher tasks // poll diaptcher tasks
g_dispatcher.poll(); g_dispatcher.poll();

View File

@ -25,8 +25,8 @@
#ifndef ENGINE_H #ifndef ENGINE_H
#define ENGINE_H #define ENGINE_H
#include "prerequisites.h" #include <prerequisites.h>
#include "gamestate.h" #include <core/gamestate.h>
class Engine class Engine
{ {
@ -61,7 +61,7 @@ public:
/// Enable FPS counter on screen /// Enable FPS counter on screen
void enableFpsCounter(bool enable = true) { m_calculateFps = enable; }; void enableFpsCounter(bool enable = true) { m_calculateFps = enable; };
int getLastFrameTicks() const { return m_lastFrameTicks; } int getCurrentFrameTicks() const { return m_lastFrameTicks; }
private: private:
/// Called to render every frame /// Called to render every frame

View File

@ -25,8 +25,8 @@
#ifndef GAMESTATE_H #ifndef GAMESTATE_H
#define GAMESTATE_H #define GAMESTATE_H
#include "prerequisites.h" #include <prerequisites.h>
#include "input.h" #include <core/input.h>
struct InputEvent; struct InputEvent;

View File

@ -25,7 +25,7 @@
#ifndef INPUT_H #ifndef INPUT_H
#define INPUT_H #define INPUT_H
#include "prerequisites.h" #include <prerequisites.h>
enum EKeyCode { enum EKeyCode {
KC_UNKNOWN = 0x00, KC_UNKNOWN = 0x00,

View File

@ -25,7 +25,7 @@
#ifndef PLATFORM_H #ifndef PLATFORM_H
#define PLATFORM_H #define PLATFORM_H
#include "prerequisites.h" #include <prerequisites.h>
namespace Platform namespace Platform
{ {

View File

@ -22,7 +22,8 @@
*/ */
#include "resources.h" #include <prerequisites.h>
#include <core/resources.h>
#include <physfs.h> #include <physfs.h>

View File

@ -25,7 +25,7 @@
#ifndef RESOURCES_H #ifndef RESOURCES_H
#define RESOURCES_H #define RESOURCES_H
#include "prerequisites.h" #include <prerequisites.h>
class Resources class Resources
{ {

View File

@ -22,9 +22,10 @@
*/ */
#include "borderedimage.h" #include <prerequisites.h>
#include "graphics.h" #include <graphics/borderedimage.h>
#include "textures.h" #include <graphics/graphics.h>
#include <graphics/textures.h>
BorderedImage::BorderedImage(TexturePtr texture, BorderedImage::BorderedImage(TexturePtr texture,
const Rect& left, const Rect& left,

View File

@ -25,9 +25,9 @@
#ifndef BORDEREDIMAGE_H #ifndef BORDEREDIMAGE_H
#define BORDEREDIMAGE_H #define BORDEREDIMAGE_H
#include "prerequisites.h" #include <prerequisites.h>
#include "image.h" #include <graphics/image.h>
#include "texture.h" #include <graphics/texture.h>
class BorderedImage : public Image class BorderedImage : public Image
{ {

View File

@ -22,10 +22,11 @@
*/ */
#include "font.h" #include <prerequisites.h>
#include "core/resources.h" #include <core/resources.h>
#include "textures.h" #include <graphics/font.h>
#include "graphics.h" #include <graphics/textures.h>
#include <graphics/graphics.h>
void Font::calculateGlyphsWidthsAutomatically(const Size& glyphSize) void Font::calculateGlyphsWidthsAutomatically(const Size& glyphSize)
{ {

View File

@ -25,8 +25,8 @@
#ifndef FONT_H #ifndef FONT_H
#define FONT_H #define FONT_H
#include "prerequisites.h" #include <prerequisites.h>
#include "texture.h" #include <graphics/texture.h>
enum EAlign { enum EAlign {
ALIGN_TOP = 1 << 0, ALIGN_TOP = 1 << 0,

View File

@ -22,8 +22,9 @@
*/ */
#include "fonts.h" #include <prerequisites.h>
#include "core/resources.h" #include <core/resources.h>
#include <graphics/fonts.h>
Fonts g_fonts; Fonts g_fonts;

View File

@ -25,8 +25,8 @@
#ifndef FONTS_H #ifndef FONTS_H
#define FONTS_H #define FONTS_H
#include "prerequisites.h" #include <prerequisites.h>
#include "font.h" #include <graphics/font.h>
class Fonts class Fonts
{ {

View File

@ -21,9 +21,10 @@
* THE SOFTWARE. * THE SOFTWARE.
*/ */
#include "framebuffer.h" #include <prerequisites.h>
#include "core/platform.h" #include <core/platform.h>
#include "graphics.h" #include <graphics/framebuffer.h>
#include <graphics/graphics.h>
#include <GL/gl.h> #include <GL/gl.h>
#include <GL/glu.h> #include <GL/glu.h>

View File

@ -25,7 +25,7 @@
#ifndef FRAMEBUFFER_H #ifndef FRAMEBUFFER_H
#define FRAMEBUFFER_H #define FRAMEBUFFER_H
#include "prerequisites.h" #include <prerequisites.h>
class FrameBuffer class FrameBuffer
{ {

View File

@ -22,8 +22,8 @@
*/ */
#include "graphics.h" #include <prerequisites.h>
#include "texture.h" #include <graphics/graphics.h>
#include <GL/gl.h> #include <GL/gl.h>
#include <GL/glu.h> #include <GL/glu.h>

View File

@ -25,8 +25,8 @@
#ifndef GRAPHICS_H #ifndef GRAPHICS_H
#define GRAPHICS_H #define GRAPHICS_H
#include "prerequisites.h" #include <prerequisites.h>
#include "texture.h" #include <graphics/texture.h>
class Graphics class Graphics
{ {

View File

@ -22,9 +22,10 @@
*/ */
#include "image.h" #include <prerequisites.h>
#include "graphics.h" #include <graphics/image.h>
#include "textures.h" #include <graphics/graphics.h>
#include <graphics/textures.h>
Image::Image(const std::string& texture) Image::Image(const std::string& texture)
{ {

View File

@ -25,8 +25,8 @@
#ifndef IMAGE_H #ifndef IMAGE_H
#define IMAGE_H #define IMAGE_H
#include "prerequisites.h" #include <prerequisites.h>
#include "texture.h" #include <graphics/texture.h>
class Image class Image
{ {

View File

@ -22,9 +22,10 @@
*/ */
#include "textarea.h" #include <prerequisites.h>
#include "graphics.h" #include <core/engine.h>
#include "core/engine.h" #include <graphics/textarea.h>
#include <graphics/graphics.h>
TextArea::TextArea() : TextArea::TextArea() :
m_font(0), m_font(0),
@ -65,7 +66,7 @@ void TextArea::draw()
if(m_cursorVisible && m_cursorPos >= 0) { if(m_cursorVisible && m_cursorPos >= 0) {
assert(m_cursorPos <= textLength); assert(m_cursorPos <= textLength);
const int delay = 500; const int delay = 500;
int ticks = g_engine.getLastFrameTicks(); int ticks = g_engine.getCurrentFrameTicks();
// draw every 500ms // draw every 500ms
if(ticks - m_cursorTicks <= delay) { if(ticks - m_cursorTicks <= delay) {
Rect cursorRect; Rect cursorRect;
@ -76,7 +77,7 @@ void TextArea::draw()
cursorRect = Rect(m_glyphsCoords[m_cursorPos-1].right(), m_glyphsCoords[m_cursorPos-1].top(), 1, m_font->getGlyphHeight()); cursorRect = Rect(m_glyphsCoords[m_cursorPos-1].right(), m_glyphsCoords[m_cursorPos-1].top(), 1, m_font->getGlyphHeight());
g_graphics.drawFilledRect(cursorRect, m_color); g_graphics.drawFilledRect(cursorRect, m_color);
} else if(ticks - m_cursorTicks >= 2*delay) { } else if(ticks - m_cursorTicks >= 2*delay) {
m_cursorTicks = g_engine.getLastFrameTicks(); m_cursorTicks = g_engine.getCurrentFrameTicks();
} }
} }
} }
@ -234,7 +235,7 @@ void TextArea::setText(const std::string& text)
m_text = text; m_text = text;
if(m_cursorPos >= 0) { if(m_cursorPos >= 0) {
m_cursorPos = 0; m_cursorPos = 0;
m_cursorTicks = g_engine.getLastFrameTicks(); m_cursorTicks = g_engine.getCurrentFrameTicks();
} }
recalculate(); recalculate();
} }
@ -273,7 +274,7 @@ void TextArea::enableCursor(bool enable)
{ {
if(enable) { if(enable) {
m_cursorPos = 0; m_cursorPos = 0;
m_cursorTicks = g_engine.getLastFrameTicks(); m_cursorTicks = g_engine.getCurrentFrameTicks();
} else } else
m_cursorPos = -1; m_cursorPos = -1;
recalculate(); recalculate();
@ -286,7 +287,7 @@ void TextArea::appendCharacter(char c)
tmp = c; tmp = c;
m_text.insert(m_cursorPos, tmp); m_text.insert(m_cursorPos, tmp);
m_cursorPos++; m_cursorPos++;
m_cursorTicks = g_engine.getLastFrameTicks(); m_cursorTicks = g_engine.getCurrentFrameTicks();
recalculate(); recalculate();
} }
} }
@ -298,7 +299,7 @@ void TextArea::removeCharacter(bool right)
m_text.erase(m_text.begin() + m_cursorPos); m_text.erase(m_text.begin() + m_cursorPos);
else if((uint)m_cursorPos == m_text.length()) { else if((uint)m_cursorPos == m_text.length()) {
m_text.erase(m_text.begin() + (--m_cursorPos)); m_text.erase(m_text.begin() + (--m_cursorPos));
m_cursorTicks = g_engine.getLastFrameTicks(); m_cursorTicks = g_engine.getCurrentFrameTicks();
} }
recalculate(); recalculate();
} }
@ -309,12 +310,12 @@ void TextArea::moveCursor(bool right)
if(right) { if(right) {
if((uint)m_cursorPos+1 <= m_text.length()) { if((uint)m_cursorPos+1 <= m_text.length()) {
m_cursorPos++; m_cursorPos++;
m_cursorTicks = g_engine.getLastFrameTicks(); m_cursorTicks = g_engine.getCurrentFrameTicks();
} }
} else { } else {
if(m_cursorPos-1 >= 0) { if(m_cursorPos-1 >= 0) {
m_cursorPos--; m_cursorPos--;
m_cursorTicks = g_engine.getLastFrameTicks(); m_cursorTicks = g_engine.getCurrentFrameTicks();
} }
} }
recalculate(); recalculate();

View File

@ -25,7 +25,7 @@
#ifndef TEXTAREA_H #ifndef TEXTAREA_H
#define TEXTAREA_H #define TEXTAREA_H
#include "prerequisites.h" #include <prerequisites.h>
#include "font.h" #include "font.h"
class TextArea class TextArea
@ -77,6 +77,4 @@ private:
std::vector<Rect> m_glyphsTexCoords; std::vector<Rect> m_glyphsTexCoords;
}; };
typedef boost::shared_ptr<TextArea> TextAreaPtr;
#endif // TEXTAREA_H #endif // TEXTAREA_H

View File

@ -22,7 +22,8 @@
*/ */
#include "texture.h" #include <prerequisites.h>
#include <graphics/texture.h>
#include <GL/gl.h> #include <GL/gl.h>
#include <GL/glext.h> #include <GL/glext.h>

View File

@ -25,9 +25,7 @@
#ifndef TEXTURE_H #ifndef TEXTURE_H
#define TEXTURE_H #define TEXTURE_H
#include "prerequisites.h" #include <prerequisites.h>
class Textures;
class Texture class Texture
{ {
@ -53,5 +51,6 @@ private:
}; };
typedef boost::shared_ptr<Texture> TexturePtr; typedef boost::shared_ptr<Texture> TexturePtr;
typedef boost::weak_ptr<Texture> TextureWeakPtr;
#endif // TEXTURE_H #endif // TEXTURE_H

View File

@ -22,8 +22,9 @@
*/ */
#include "textureloader.h" #include <prerequisites.h>
#include "texture.h" #include <graphics/textureloader.h>
#include <graphics/texture.h>
#include <png.h> #include <png.h>

View File

@ -25,7 +25,7 @@
#ifndef TEXTURELOADER_H #ifndef TEXTURELOADER_H
#define TEXTURELOADER_H #define TEXTURELOADER_H
#include "prerequisites.h" #include <prerequisites.h>
class Texture; class Texture;

View File

@ -22,9 +22,10 @@
*/ */
#include "textures.h" #include <prerequisites.h>
#include "core/resources.h" #include <core/resources.h>
#include "textureloader.h" #include <graphics/textures.h>
#include <graphics/textureloader.h>
Textures g_textures; Textures g_textures;

View File

@ -25,10 +25,8 @@
#ifndef TEXTURES_H #ifndef TEXTURES_H
#define TEXTURES_H #define TEXTURES_H
#include "prerequisites.h" #include <prerequisites.h>
#include "texture.h" #include <graphics/texture.h>
typedef boost::weak_ptr<Texture> TextureWeakPtr;
class Textures class Textures
{ {

View File

@ -21,160 +21,250 @@
* THE SOFTWARE. * THE SOFTWARE.
*/ */
#include "connection.h"
#include <boost/bind.hpp> #include <prerequisites.h>
#include <core/dispatcher.h>
#include <net/connection.h>
Connection::Connection(boost::asio::io_service& ioService) static boost::asio::io_service ioService;
: m_socket(ioService), m_resolver(ioService)
Connection::Connection() :
m_socket(ioService),
m_resolver(ioService),
m_writeError(false),
m_readError(false),
m_readTimer(ioService),
m_writeTimer(ioService),
m_state(STATE_CLOSED)
{ {
m_connected = false; logTrace();
m_connecting = false;
m_port = 0;
} }
void Connection::stop() Connection::~Connection()
{ {
if(m_connecting){ logTrace();
}
void Connection::poll()
{
ioService.poll();
ioService.reset();
}
void Connection::close()
{
logTrace();
ioService.post(boost::bind(&Connection::internalCloseConnection, shared_from_this()));
}
void Connection::internalCloseConnection()
{
if(m_state != STATE_CLOSED) {
m_pendingRead = 0;
m_pendingWrite = 0;
m_resolver.cancel(); m_resolver.cancel();
m_socket.cancel(); m_readTimer.cancel();
m_writeTimer.cancel();
m_connecting = false; g_dispatcher.addTask(m_closeCallback);
if(m_socket.is_open()) {
boost::system::error_code error;
m_socket->shutdown(boost::asio::ip::tcp::socket::shutdown_both, error);
if(error) {
if(error == boost::asio::error::not_connected) {
//Transport endpoint is not connected.
} else {
logError("shutdown socket error = %s", error.message());
}
}
m_socket->close(error);
if(error) {
logError("close socket error = %s", error.message());
}
}
m_state = STATE_CLOSED;
} }
} }
bool Connection::connect(const std::string& ip, uint16 port, ConnectionCallback onConnect) bool Connection::connect(const std::string& host, uint16 port, const Callback& callback)
{ {
if(m_connecting){ logTrace();
logError("Already is connecting.");
if(m_state != STATE_CLOSED) {
logTraceError("connection not closed");
return false; return false;
} }
if(m_connected){ m_connectCallback = callback;
logError("Already is connected."); boost::asio::ip::tcp::resolver::query query(ip, convertType<std::string>(port));
return false; m_resolver.async_resolve(query, boost::bind(&Connection::onResolveDns, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::iterator));
}
m_connectCallback = onConnect;
m_connecting = true;
m_ip = ip;
m_port = port;
//first resolve dns
boost::asio::ip::tcp::resolver::query query(ip, convertType<std::string, uint16>(port));
m_resolver.async_resolve(query, boost::bind(&Connection::onResolveDns, this, boost::asio::placeholders::error, boost::asio::placeholders::iterator));
return true; return true;
} }
void Connection::onResolveDns(const boost::system::error_code& error, boost::asio::ip::tcp::resolver::iterator endpointIt) void Connection::onResolveDns(const boost::system::error_code& error, boost::asio::ip::tcp::resolver::iterator endpointIt)
{ {
if(error){ logTrace();
m_connecting = false; if(error) {
m_errorCallback(error, __FUNCTION__); handleError(error);
return; return;
} } else {
//lets connect m_socket.async_connect(*endpointIt, boost::bind(&Connection::onConnect, shared_from_this(), boost::asio::placeholders::error));
m_socket.async_connect(*endpointIt, boost::bind(&Connection::onConnect, this, boost::asio::placeholders::error));
} }
void Connection::onConnect(const boost::system::error_code& error) void Connection::onConnect(const boost::system::error_code& error)
{ {
if(error){ logTrace();
m_connecting = false; if(error) {
m_errorCallback(error, __FUNCTION__); handleError(error);
return; return;
} }
m_connected = true; m_state = STATE_OPEN;
m_connectCallback(); if(m_connectCallback)
g_dispatcher.addTask(m_connectCallback);
recvNext();
}
void Connection::recvNext()
{
logTrace();
++m_pendingRead;
m_readTimer.expires_from_now(boost::posix_time::seconds(READ_TIMEOUT));
m_readTimer.async_wait(boost::bind(&Connection::handleReadTimeout,
boost::weak_ptr<Connection>(shared_from_this()),
boost::asio::placeholders::error));
static InputMessage inputMessage;
boost::asio::async_read(*m_socket,
boost::asio::buffer(inputMessage->getBuffer(), InputMessage::HEADER_LENGTH),
boost::bind(&Connection::parseHeader, shared_from_this(), inputMessage, boost::asio::placeholders::error));
}
void Connection::parseHeader(const InputMessage& inputMessage, const boost::system::error_code& error)
{
logTrace();
--m_pendingRead;
m_readTimer.cancel();
if(error && !handleReadError(error))
return;
uint16_t size = inputMessage->decodeHeader();
if(size <= 0 || size + 2 > InputMessage::INPUTMESSAGE_MAXSIZE) {
internalCloseConnection();
return;
}
try {
++m_pendingRead;
m_readTimer.expires_from_now(boost::posix_time::seconds(Connection::read_timeout));
m_readTimer.async_wait(boost::bind(&Connection::handleReadTimeout, boost::weak_ptr<Connection>(shared_from_this()),
boost::asio::placeholders::error));
inputMessage->setMessageLength(size + InputMessage::HEADER_LENGTH);
boost::asio::async_read(*m_socket, boost::asio::buffer(inputMessage->getBuffer() + InputMessage::HEADER_LENGTH, size),
boost::bind(&Connection::parsePacket, shared_from_this(), inputMessage, boost::asio::placeholders::error));
} catch(boost::system::system_error& e) {
logError("async read error = " << e.what());
internalCloseConnection();
}
}
void Connection::parsePacket(const InputMessage& inputMessage, const boost::system::error_code& error)
{
logTrace();
--m_pendingRead;
m_readTimer.cancel();
if(error && !handleReadError(error))
return;
//g_dispatcher.addTask(boost);
} }
void Connection::handleError(const boost::system::error_code& error) void Connection::handleError(const boost::system::error_code& error)
{ {
stop(); logTrace();
if(isConnected()){ internalCloseConnection();
closeSocket(); m_errorCallback(error);
}
} }
void Connection::closeSocket() void Connection::send(const NetworkMessage& networkMessage, const ConnectionCallback& onSend)
{ {
boost::system::error_code error; logTrace();
m_socket.shutdown(boost::asio::ip::tcp::socket::shutdown_both, error);
if(error) {
logError("Connection::closeSocket(): %s", error.message().c_str());
}
m_socket.close(error);
if(error) {
logError("Connection::closeSocket(): %s", error.message().c_str());
}
}
void Connection::send(NetworkMessagePtr networkMessage, ConnectionCallback onSend)
{
boost::asio::async_write(m_socket, boost::asio::async_write(m_socket,
boost::asio::buffer(networkMessage->getBuffer(), NetworkMessage::header_length), boost::asio::buffer(networkMessage.getBuffer(), NetworkMessage::header_length),
boost::bind(&Connection::onSendHeader, shared_from_this(), networkMessage, onSend, boost::asio::placeholders::error)); boost::bind(&Connection::onSendHeader, shared_from_this(), networkMessage, onSend, boost::asio::placeholders::error));
} }
void Connection::recv(RecvCallback onRecv) void Connection::recv(const RecvCallback& onRecv)
{ {
NetworkMessagePtr networkMessage(new NetworkMessage); logTrace();
static NetworkMessage networkMessage;
boost::asio::async_read(m_socket, boost::asio::async_read(m_socket,
boost::asio::buffer(networkMessage->getBuffer(), NetworkMessage::header_length), boost::asio::buffer(networkMessage.getBuffer(), NetworkMessage::header_length),
boost::bind(&Connection::onRecvHeader, shared_from_this(), networkMessage, onRecv, boost::asio::placeholders::error)); boost::bind(&Connection::onRecvHeader, shared_from_this(), networkMessage, onRecv, boost::asio::placeholders::error));
} }
void Connection::onRecvHeader(ConnectionPtr connection, NetworkMessagePtr networkMessage, RecvCallback onRecv, const boost::system::error_code& error) void Connection::onRecvHeader(const NetworkMessage& networkMessage, const RecvCallback& onRecv, const boost::system::error_code& error)
{ {
if(error){ logTrace();
connection->handleError(error);
connection->onError(error, __FUNCTION__); if(error) {
handleError(error);
return; return;
} }
boost::asio::async_read(connection->getSocket(), boost::asio::async_read(m_socket,
boost::asio::buffer(networkMessage->getBodyBuffer(), networkMessage->getMessageLength()), boost::asio::buffer(networkMessage.getBodyBuffer(), networkMessage.getMessageLength()),
boost::bind(&Connection::onRecvBody, connection, networkMessage, onRecv, boost::asio::placeholders::error)); boost::bind(&Connection::onRecvBody, shared_from_this(), networkMessage, onRecv, boost::asio::placeholders::error));
} }
void Connection::onRecvBody(ConnectionPtr connection, NetworkMessagePtr networkMessage, RecvCallback onRecv, const boost::system::error_code& error) void Connection::onRecvBody(const NetworkMessage& networkMessage, const RecvCallback& onRecv, const boost::system::error_code& error)
{ {
logTrace();
if(error){ if(error){
connection->handleError(error); handleError(error);
connection->onError(error, __FUNCTION__);
return; return;
} }
onRecv(networkMessage); onRecv(networkMessage);
} }
void Connection::onSendHeader(ConnectionPtr connection, NetworkMessagePtr networkMessage, ConnectionCallback onSend, const boost::system::error_code& error) void Connection::onSendHeader(const NetworkMessage& networkMessage, const ConnectionCallback& onSend, const boost::system::error_code& error)
{ {
logTrace();
if(error){ if(error){
connection->handleError(error); handleError(error);
connection->onError(error, __FUNCTION__);
return; return;
} }
boost::asio::async_write(connection->getSocket(), boost::asio::async_write(m_socket,
boost::asio::buffer(networkMessage->getBodyBuffer(), networkMessage->getMessageLength()), boost::asio::buffer(networkMessage.getBodyBuffer(), networkMessage.getMessageLength()),
boost::bind(&Connection::onSendBody, connection, networkMessage, onSend, boost::asio::placeholders::error)); boost::bind(&Connection::onSendBody, shared_from_this(), networkMessage, onSend, boost::asio::placeholders::error));
} }
void Connection::onSendBody(ConnectionPtr connection, NetworkMessagePtr networkMessage, ConnectionCallback onSend, const boost::system::error_code& error) void Connection::onSendBody(const NetworkMessage& networkMessage, const ConnectionCallback& onSend, const boost::system::error_code& error)
{ {
if(error){ logTrace();
connection->handleError(error);
connection->onError(error, __FUNCTION__); if(error) {
handleError(error);
return; return;
} }

View File

@ -21,80 +21,85 @@
* THE SOFTWARE. * THE SOFTWARE.
*/ */
#ifndef CONNECTION_H #ifndef CONNECTION_H
#define CONNECTION_H #define CONNECTION_H
#include "prerequisites.h" #include <prerequisites.h>
#include <net/networkmessage.h>
#include <boost/asio.hpp> #include <boost/asio.hpp>
#include "networkmessage.h"
class TestState;
class Protocol; class Protocol;
class Connections;
class Connection; class Connection;
typedef boost::shared_ptr<Connection> ConnectionPtr;
typedef boost::function<void()> ConnectionCallback;
typedef boost::function<void(const NetworkMessage&)> RecvCallback;
typedef boost::function<void(const boost::system::error_code&)> ErrorCallback;
class Connection : public boost::enable_shared_from_this<Connection> class Connection : public boost::enable_shared_from_this<Connection>
{ {
public: public:
typedef boost::function<void()> ConnectionCallback; enum {
typedef boost::function<void(NetworkMessagePtr)> RecvCallback; WRITE_TIMEOUT = 10,
typedef boost::function<void(const boost::system::error_code&, const std::string&)> ErrorCallback; READ_TIMEOUT = 10
};
typedef boost::shared_ptr<Connection> ConnectionPtr; enum EConnectionState {
STATE_CONNECTING,
STATE_OPEN,
STATE_CLOSED
}
private: Connection();
Connection(boost::asio::io_service& ioService); ~Connection();
bool connect(const std::string& ip, uint16 port, ConnectionCallback onConnect); bool connect(const std::string& host, uint16 port, const Callback& callback);
void stop(); void close();
void setErrorCallback(ErrorCallback c) { m_errorCallback = c; } void setOnError(const ErrorCallback& callback) { m_errorCallback = callback; }
void setOnRecv(const RecvCallback& callback) { m_recvCallback = callback; }
void recv(RecvCallback onSend); void send(const OutputMessage& networkMessage);
void send(NetworkMessagePtr networkMessage, ConnectionCallback onRecv);
bool isConnecting() const { return m_connecting; } bool isConnecting() const { return m_state == STATE_CONNECTING; }
bool isConnected() const { return m_connected; } bool isConnected() const { return m_state == STATE_OPEN; }
boost::asio::ip::tcp::socket& getSocket() { return m_socket; } static void poll();
void onError(const boost::system::error_code& error, const std::string& msg) { m_errorCallback(error, msg); }
private:
static void onSendHeader(ConnectionPtr connection, NetworkMessagePtr networkMessage, ConnectionCallback onSend, const boost::system::error_code& error);
static void onSendBody(ConnectionPtr connection, NetworkMessagePtr networkMessage, ConnectionCallback onSend, const boost::system::error_code& error);
static void onRecvHeader(ConnectionPtr connection, NetworkMessagePtr networkMessage, RecvCallback onRecv, const boost::system::error_code& error);
static void onRecvBody(ConnectionPtr connection, NetworkMessagePtr networkMessage, RecvCallback onRecv, const boost::system::error_code& error);
private: private:
void onResolveDns(const boost::system::error_code& error, boost::asio::ip::tcp::resolver::iterator endpointIt); void onResolveDns(const boost::system::error_code& error, boost::asio::ip::tcp::resolver::iterator endpointIt);
void onConnect(const boost::system::error_code& error); void onConnect(const boost::system::error_code& error);
private: void recvNext();
void closeSocket();
void onRecvBody(const NetworkMessage& networkMessage, const RecvCallback& onRecv, const boost::system::error_code& error);
void onSendHeader(const NetworkMessage& networkMessage, const ConnectionCallback& onSend, const boost::system::error_code& error);
void onSendBody(const NetworkMessage& networkMessage, const ConnectionCallback& onSend, const boost::system::error_code& error);
void onRecvHeader(const NetworkMessage& networkMessage, const RecvCallback& onRecv, const boost::system::error_code& error);
private:
void handleError(const boost::system::error_code& error); void handleError(const boost::system::error_code& error);
void internalCloseConnection();
boost::asio::ip::tcp::socket m_socket;
boost::asio::ip::tcp::resolver m_resolver; boost::asio::ip::tcp::resolver m_resolver;
boost::asio::ip::tcp::socket m_socket;
bool m_connecting; int32_t m_pendingWrite;
bool m_connected; int32_t m_pendingRead;
bool m_writeError;
bool m_readError;
boost::asio::deadline_timer m_readTimer;
boost::asio::deadline_timer m_writeTimer;
std::string m_ip; EConnectionState m_state;
uint16_t m_port;
ConnectionCallback m_connectCallback; Callback m_connectCallback;
Callback m_closeCallback;
ErrorCallback m_errorCallback; ErrorCallback m_errorCallback;
RecvCallback m_recvCallback;
friend class Protocol;
friend class Connections;
}; };
typedef boost::shared_ptr<Connection> ConnectionPtr;
#endif //CONNECTION_h #endif //CONNECTION_h

View File

@ -21,158 +21,6 @@
* THE SOFTWARE. * THE SOFTWARE.
*/ */
#include "networkmessage.h"
void NetworkMessage::updateHeaderLength() #include <prerequisites.h>
{ #include <net/networkmessage.h>
uint16 size = m_msgSize;
memcpy(m_msgBuf, &size, 2);
}
bool NetworkMessage::canAdd(int size) {
return (size + m_readPos < NETWORKMESSAGE_MAXSIZE - 16);
}
std::string NetworkMessage::getString()
{
uint16 stringlen = getU16();
if(stringlen >= (16384 - m_readPos))
return std::string();
char* v = (char*)(m_msgBuf + m_readPos);
m_readPos += stringlen;
return std::string(v, stringlen);
}
std::string NetworkMessage::getRaw()
{
uint16 stringlen = m_msgSize - m_readPos;
if(stringlen >= (16384 - m_readPos))
return std::string();
char* v = (char*)(m_msgBuf + m_readPos);
m_readPos += stringlen;
return std::string(v, stringlen);
}
void NetworkMessage::addString(const char* value)
{
uint32 stringlen = (uint32)strlen(value);
if(!canAdd(stringlen + 2) || stringlen > 8192)
return;
addU16(stringlen);
strcpy((char*)(m_msgBuf + m_readPos), value);
m_readPos += stringlen;
m_msgSize += stringlen;
}
void NetworkMessage::addBytes(const char* bytes, uint32 size)
{
if(!canAdd(size) || size > 8192)
return;
memcpy(m_msgBuf + m_readPos, bytes, size);
m_readPos += size;
m_msgSize += size;
}
void NetworkMessage::addPaddingBytes(uint32 n)
{
if(!canAdd(n))
return;
memset((void*)&m_msgBuf[m_readPos], 0x33, n);
m_msgSize = m_msgSize + n;
}
void NetworkMessage::skipBytes(int count) {
m_readPos += count;
}
// simply write functions for outgoing message
void NetworkMessage::addByte(uint8 value) {
if(!canAdd(1))
return;
m_msgBuf[m_readPos++] = value;
m_msgSize++;
}
void NetworkMessage::addU16(uint16 value) {
if(!canAdd(2))
return;
*(uint16*)(m_msgBuf + m_readPos) = value;
m_readPos += 2;
m_msgSize += 2;
}
void NetworkMessage::addU32(uint32 value) {
if(!canAdd(4))
return;
*(uint32*)(m_msgBuf + m_readPos) = value;
m_readPos += 4;
m_msgSize += 4;
}
void NetworkMessage::addU64(uint64 value) {
if(!canAdd(8))
return;
*(uint64*)(m_msgBuf + m_readPos) = value;
m_readPos += 8;
m_msgSize += 8;
}
void NetworkMessage::addString(const std::string &value) {
addString(value.c_str());
}
int32 NetworkMessage::getMessageLength() const {
return m_msgSize;
}
void NetworkMessage::setMessageLength(int32 newSize) {
m_msgSize = newSize;
}
int32 NetworkMessage::getReadPos() const {
return m_readPos;
}
uint8 NetworkMessage::getByte()
{
return m_msgBuf[m_readPos++];
}
uint16 NetworkMessage::getU16()
{
uint16 v = *(uint16*)(m_msgBuf + m_readPos);
m_readPos += 2;
return v;
}
uint32 NetworkMessage::getU32()
{
uint32 v = *(uint32*)(m_msgBuf + m_readPos);
m_readPos += 4;
return v;
}
uint64 NetworkMessage::getU64()
{
uint64 v = *(uint64*)(m_msgBuf + m_readPos);
m_readPos += 8;
return v;
}
char* NetworkMessage::getBuffer() {
return (char*)&m_msgBuf[0];
}
char* NetworkMessage::getBodyBuffer() {
m_readPos = 2;
return (char*)&m_msgBuf[header_length];
}

View File

@ -21,77 +21,190 @@
* THE SOFTWARE. * THE SOFTWARE.
*/ */
#ifndef NETWORKMESSAGE_H #ifndef NETWORKMESSAGE_H
#define NETWORKMESSAGE_H #define NETWORKMESSAGE_H
#include "prerequisites.h" #include <prerequisites.h>
class Rsa; class InputMessage
class NetworkMessage
{ {
public: public:
enum { enum {
header_length = 2, INPUTMESSAGE_MAXSIZE = 16834,
NETWORKMESSAGE_MAXSIZE = 1500 HEADER_LENGTH = 2
}; };
enum { InputMessage() : m_messageSize(0), m_readPos(HEADER_LENGTH) { }
max_body_length = NETWORKMESSAGE_MAXSIZE - header_length ~InputMessage() { }
};
// constructor/destructor inline void reset() {
NetworkMessage() { m_messageSize = 0;
reset(); m_readPos = HEADER_LENGTH;
} }
// resets the internal buffer to an empty message uint16_t decodeHeader() {
protected: return (int32_t)(m_buffer[0] | m_buffer[1] << 8);
void reset() {
m_msgSize = 0;
m_readPos = 2;
} }
public:
// simply read functions for incoming message
uint8 getByte();
uint16 getU16();
uint32 getU32();
uint64 getU64();
std::string getString(); uint8_t getByte() {
std::string getRaw(); return m_buffer[m_readPos++];
}
// skips count unknown/unused bytes in an incoming message uint16_t getU16() {
void skipBytes(int count); uint16_t v = *(uint16_t*)(m_buffer + m_readPos);
m_readPos += 2;
return v;
}
// simply write functions for outgoing message uint32_t getU32() {
void addByte(uint8 value); uint32_t v = *(uint32_t*)(m_buffer + m_readPos);
void addU16(uint16 value); m_readPos += 4;
void addU32(uint32 value); return v;
void addU64(uint64 value); }
void addBytes(const char* bytes, uint32_t size);
void addPaddingBytes(uint32 n);
void addString(const std::string &value);
void addString(const char* value);
int32 getMessageLength() const;
void setMessageLength(int32 newSize); uint64_t getU64() {
int32 getReadPos() const; uint64_t v = *(uint64_t*)(m_buffer + m_readPos);
int32 getHeaderSize(); m_readPos += 8;
char* getBuffer(); return v;
char* getBodyBuffer(); }
void updateHeaderLength(); std::string getString() {
uint16_t stringlen = getU16();
if(stringlen >= (INPUTMESSAGE_MAXSIZE - m_readPos))
return std::string();
protected: char* v = (char*)(m_buffer + m_readPos);
inline bool canAdd(int size); m_readPos += stringlen;
return std::string(v, stringlen);
}
int32 m_msgSize; void skipBytes(int count) { m_readPos += count; }
int32 m_readPos;
uint8 m_msgBuf[NETWORKMESSAGE_MAXSIZE]; int32_t getMessageLength() const {return m_messageSize; }
void setMessageLength(int32_t newSize) { m_messageSize = newSize; }
int32_t getReadPos() const { return m_readPos; }
const char *getBuffer() const { return (char*)&m_buffer[0]; }
private:
uint16_t m_messageSize;
uint16_t m_readPos;
uint8_t m_buffer[INPUTMESSAGE_MAXSIZE];
}; };
typedef boost::shared_ptr<NetworkMessage> NetworkMessagePtr; class OutputMessage
{
public:
enum {
OUTPUTMESSAGE_MAXSIZE = 1460
};
OutputMessage() : m_outputBufferStart(4), m_messageSize(0), m_writePos(4) { }
~OutputMessage() { }
void reset() {
m_messageSize = 0;
m_writePos = 4;
m_outputBufferStart = 4;
}
void addByte(uint8_t value)
{
if(!canAdd(1))
return;
m_buffer[m_writePos++] = value;
m_messageSize++;
}
void addU16(uint16_t value)
{
if(!canAdd(2))
return;
*(uint16_t*)(m_buffer + m_writePos) = value;
m_writePos += 2;
m_messageSize += 2;
}
void addU32(uint32_t value)
{
if(!canAdd(4))
return;
*(uint32_t*)(m_buffer + m_writePos) = value;
m_writePos += 4;
m_messageSize += 4;
}
void addU64(uint64_t value)
{
if(!canAdd(8))
return;
*(uint64_t*)(m_buffer + m_writePos) = value;
m_writePos += 8;
m_messageSize += 8;
}
void addBytes(const char* bytes, uint32_t size)
{
if(!canAdd(size) || size > 8192)
return;
memcpy(m_buffer + m_writePos, bytes, size);
m_writePos += size;
m_messageSize += size;
}
void addPaddingBytes(uint32_t n) {
if(!canAdd(n))
return;
memset((void*)&m_buffer[m_writePos], 0x33, n);
m_messageSize = m_messageSize + n;
}
void addString(const char* value)
{
uint32_t stringlen = (uint32_t)strlen(value);
if(!canAdd(stringlen + 2) || stringlen > 8192)
return;
addU16(stringlen);
strcpy((char*)(m_buffer + m_writePos), value);
m_writePos += stringlen;
m_messageSize += stringlen;
}
void addString(const std::string &value) {
addString(value.c_str());
}
void writeMessageLength() {
*(uint16_t*)(m_buffer + 2) = m_messageSize;
m_messageSize += 2;
m_outputBufferStart = 2;
}
void writeCryptoHeader() {
*(uint16_t*)(m_buffer) = m_messageSize;
m_messageSize += 2;
m_outputBufferStart = 0;
}
int32_t getMessageLength() const { return m_messageSize; }
void setMessageLength(int32_t newSize) { m_messageSize = newSize; }
const char *getBuffer() const { return (char*)&m_buffer[0]; }
const char *getOutputBuffer() const { return (char*)&m_buffer[m_outputBufferStart]; }
private:
inline bool canAdd(int size) {
return (size + m_writePos < OUTPUTMESSAGE_MAXSIZE);
}
uint16_t m_outputBufferStart;
uint16_t m_messageSize;
uint16_t m_writePos;
uint8_t m_buffer[OUTPUTMESSAGE_MAXSIZE];
};
#endif //NETWORKMESSAGE_H #endif //NETWORKMESSAGE_H

View File

@ -21,30 +21,33 @@
* THE SOFTWARE. * THE SOFTWARE.
*/ */
#include "protocol.h"
#include "connections.h"
Protocol::Protocol() #include <prerequisites.h>
#include <net/protocol.h>
Protocol::Protocol() :
m_connection(new Connection)
{ {
m_connection = g_connections.createConnection(); logTrace();
/*m_connection->setErrorCallback( m_connection->setOnError(boost::bind(&Protocol::onError, this, boost::asio::placeholders::error));
[this](const boost::system::error_code& error, const std::string& msg){
this->onError(error, msg);
}
);*/
} }
void Protocol::send(NetworkMessagePtr networkMessage, Connection::ConnectionCallback onSend) Protocol::~Protocol()
{
logTrace();
}
void Protocol::send(const NetworkMessage& networkMessage, const ConnectionCallback& onSend)
{ {
m_connection->send(networkMessage, onSend); m_connection->send(networkMessage, onSend);
} }
bool Protocol::connect(const std::string& ip, uint16 port, Connection::ConnectionCallback onConnect) bool Protocol::connect(const std::string& ip, uint16 port, const Callback& callback)
{ {
return m_connection->connect(ip, port, onConnect); return m_connection->connect(ip, port, callback);
} }
void Protocol::recv(Connection::RecvCallback onRecv) void Protocol::recv(const RecvCallback& onRecv)
{ {
m_connection->recv(onRecv); m_connection->recv(onRecv);
} }

View File

@ -21,26 +21,24 @@
* THE SOFTWARE. * THE SOFTWARE.
*/ */
#ifndef PROTOCOL_H #ifndef PROTOCOL_H
#define PROTOCOL_H #define PROTOCOL_H
#include "prerequisites.h" #include <prerequisites.h>
#include "connection.h" #include <net/connection.h>
class Protocol class Protocol
{ {
public: public:
Protocol(); Protocol();
virtual ~Protocol();
virtual void begin() = 0;
protected: protected:
void send(NetworkMessagePtr networkMessage, Connection::ConnectionCallback onSend); void send(const NetworkMessage& networkMessage, const ConnectionCallback& onSend);
void recv(Connection::RecvCallback onRecv); void recv(const RecvCallback& onRecv);
bool connect(const std::string& ip, uint16 port, const Callback& callback);
bool connect(const std::string& ip, uint16 port, Connection::ConnectionCallback onConnect); virtual void onError(const boost::system::error_code& error) = 0;
virtual void onError(const boost::system::error_code& error, const std::string& msg) = 0;
ConnectionPtr m_connection; ConnectionPtr m_connection;
}; };

View File

@ -21,12 +21,15 @@
* THE SOFTWARE. * THE SOFTWARE.
*/ */
#include "core/platform.h"
#include "core/engine.h"
#include <dir.h> #include <prerequisites.h>
#include <physfs.h> #include <core/platform.h>
#include <core/engine.h>
#include <windows.h> #include <windows.h>
#include <dir.h>
#include <physfs.h>
#include <GL/gl.h> #include <GL/gl.h>
#include <GL/glext.h> #include <GL/glext.h>

View File

@ -22,17 +22,20 @@
*/ */
#include "core/platform.h" #include <prerequisites.h>
#include "core/engine.h" #include <core/platform.h>
#include <core/engine.h>
#include <sys/time.h> #include <sys/time.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <errno.h> #include <errno.h>
#include <physfs.h>
#include <GL/glx.h>
#include <X11/Xlib.h> #include <X11/Xlib.h>
#include <X11/Xatom.h> #include <X11/Xatom.h>
#include <GL/glx.h>
#include <physfs.h>
struct X11PlatformPrivate { struct X11PlatformPrivate {
Display *display; Display *display;

View File

@ -58,6 +58,7 @@ typedef int8_t int8;
#include <map> #include <map>
#include <string> #include <string>
#include <list> #include <list>
#include <queue>
// boost utilities // boost utilities
#include <boost/algorithm/string.hpp> #include <boost/algorithm/string.hpp>
@ -66,6 +67,7 @@ typedef int8_t int8;
#include <boost/bind.hpp> #include <boost/bind.hpp>
#include <boost/function.hpp> #include <boost/function.hpp>
#include <boost/shared_ptr.hpp> #include <boost/shared_ptr.hpp>
#include <boost/scoped_ptr.hpp>
#include <boost/enable_shared_from_this.hpp> #include <boost/enable_shared_from_this.hpp>
#define foreach BOOST_FOREACH #define foreach BOOST_FOREACH
@ -75,11 +77,11 @@ typedef boost::function<void()> Callback;
#include <yaml-cpp/yaml.h> #include <yaml-cpp/yaml.h>
// common utilities // common utilities
#include "util/util.h" #include <util/util.h>
#include "util/logger.h" #include <util/logger.h>
#include "util/color.h" #include <util/color.h>
#include "util/point.h" #include <util/point.h>
#include "util/size.h" #include <util/size.h>
#include "util/rect.h" #include <util/rect.h>
#endif // PREREQUISITES_H #endif // PREREQUISITES_H

View File

@ -25,19 +25,20 @@
#ifndef UI_H #ifndef UI_H
#define UI_H #define UI_H
#include "prerequisites.h" #include <prerequisites.h>
#include <ui/uiconstants.h>
#include "uiconstants.h" #include <ui/uiskins.h>
#include "uielement.h" #include <ui/uiloader.h>
#include "uicontainer.h"
#include "uipanel.h" #include <ui/uielement.h>
#include "uibutton.h" #include <ui/uielementskin.h>
#include "uilabel.h"
#include "uiskins.h" #include <ui/uicontainer.h>
#include "uiwindow.h" #include <ui/uibutton.h>
#include "uitextedit.h" #include <ui/uilabel.h>
#include "uiloader.h" #include <ui/uiwindow.h>
#include "uilinedecoration.h" #include <ui/uitextedit.h>
#include "uicheckbox.h" #include <ui/uicheckbox.h>
#endif // UI_H #endif // UI_H

View File

@ -22,8 +22,9 @@
*/ */
#include "uibutton.h" #include <prerequisites.h>
#include "core/dispatcher.h" #include <core/dispatcher.h>
#include <ui/uibutton.h>
void UIButton::onInputEvent(const InputEvent& event) void UIButton::onInputEvent(const InputEvent& event)
{ {

View File

@ -25,9 +25,9 @@
#ifndef UIBUTTON_H #ifndef UIBUTTON_H
#define UIBUTTON_H #define UIBUTTON_H
#include "prerequisites.h" #include <prerequisites.h>
#include "uielement.h" #include <ui/uielement.h>
#include "graphics/borderedimage.h" #include <graphics/borderedimage.h>
class UIButton : public UIElement class UIButton : public UIElement
{ {

View File

@ -22,9 +22,10 @@
*/ */
#include "uibuttonskin.h" #include <prerequisites.h>
#include "uibutton.h" #include <graphics/fonts.h>
#include "graphics/fonts.h" #include <ui/uibuttonskin.h>
#include <ui/uibutton.h>
void UIButtonSkin::load(const YAML::Node& node) void UIButtonSkin::load(const YAML::Node& node)
{ {

View File

@ -25,9 +25,9 @@
#ifndef UIBUTTONSKIN_H #ifndef UIBUTTONSKIN_H
#define UIBUTTONSKIN_H #define UIBUTTONSKIN_H
#include "prerequisites.h" #include <prerequisites.h>
#include "uiconstants.h" #include <ui/uiconstants.h>
#include "uielementskin.h" #include <ui/uielementskin.h>
class Font; class Font;

View File

@ -1,4 +1,29 @@
#include "uicheckbox.h" /* The MIT License
*
* Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <prerequisites.h>
#include <ui/uicheckbox.h>
UICheckBox::UICheckBox(UI::EElementType type): UIElement(type) UICheckBox::UICheckBox(UI::EElementType type): UIElement(type)
{ {

View File

@ -1,9 +1,33 @@
/* The MIT License
*
* Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef UICHECKBOX_H #ifndef UICHECKBOX_H
#define UICHECKBOX_H #define UICHECKBOX_H
#include <prerequisites.h>
#include <ui/uielement.h> #include <ui/uielement.h>
class UICheckBox : public UIElement class UICheckBox : public UIElement
{ {

View File

@ -1,2 +1,27 @@
#include "uicheckboxskin.h" /* The MIT License
*
* Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <prerequisites.h>
#include <ui/uicheckboxskin.h>

View File

@ -1,6 +1,32 @@
/* The MIT License
*
* Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef UICHECKBOXSKIN_H #ifndef UICHECKBOXSKIN_H
#define UICHECKBOXSKIN_H #define UICHECKBOXSKIN_H
#include <prerequisites.h>
class UICheckBoxSkin class UICheckBoxSkin
{ {
}; };

View File

@ -22,13 +22,9 @@
*/ */
#include "uicontainer.h" #include <prerequisites.h>
#include "core/resources.h" #include <core/resources.h>
#include "uibutton.h" #include <ui/uicontainer.h>
#include "uipanel.h"
#include "uilabel.h"
#include "uitextedit.h"
#include "uiwindow.h"
UIContainerPtr rootContainer(new UIContainer); UIContainerPtr rootContainer(new UIContainer);

View File

@ -25,8 +25,8 @@
#ifndef UICONTAINER_H #ifndef UICONTAINER_H
#define UICONTAINER_H #define UICONTAINER_H
#include "prerequisites.h" #include <prerequisites.h>
#include "uielement.h" #include <ui/uielement.h>
class UIContainer : public UIElement class UIContainer : public UIElement
{ {

View File

@ -22,16 +22,17 @@
*/ */
#include "uielement.h" #include <prerequisites.h>
#include "uiskins.h"
#include "uielementskin.h"
#include "graphics/graphics.h"
#include <core/dispatcher.h> #include <core/dispatcher.h>
#include <graphics/graphics.h>
#include <ui/uielement.h>
#include <ui/uiskins.h>
#include <ui/uielementskin.h>
#include <ui/uicontainer.h>
UIElement::UIElement(UI::EElementType type) : UIElement::UIElement(UI::EElementType type) :
UILayout(), UILayout(),
m_type(type), m_type(type),
m_skin(NULL),
m_visible(true), m_visible(true),
m_enabled(true), m_enabled(true),
m_focused(false) m_focused(false)
@ -49,7 +50,7 @@ void UIElement::destroy()
assert(asUIElement().use_count() == 4); assert(asUIElement().use_count() == 4);
} }
void UIElement::setSkin(UIElementSkin* skin) void UIElement::setSkin(const UIElementSkinPtr& skin)
{ {
m_skin = skin; m_skin = skin;
if(skin) if(skin)

View File

@ -25,10 +25,11 @@
#ifndef UIELEMENT_H #ifndef UIELEMENT_H
#define UIELEMENT_H #define UIELEMENT_H
#include "prerequisites.h" #include <prerequisites.h>
#include "core/input.h" #include <core/input.h>
#include "uiconstants.h" #include <ui/uiconstants.h>
#include "uilayout.h" #include <ui/uilayout.h>
#include <ui/uielementskin.h>
class UIElementSkin; class UIElementSkin;
@ -60,8 +61,8 @@ public:
void moveTo(Point pos); void moveTo(Point pos);
void setSkin(UIElementSkin *skin); void setSkin(const UIElementSkinPtr& skin);
UIElementSkin *getSkin() { return m_skin; } const UIElementSkinPtr& getSkin() const { return m_skin; }
void setParent(UIContainerPtr parent) { m_parent = parent; } void setParent(UIContainerPtr parent) { m_parent = parent; }
UIContainerPtr getParent() const { return m_parent.lock(); } UIContainerPtr getParent() const { return m_parent.lock(); }
@ -89,7 +90,7 @@ public:
private: private:
UI::EElementType m_type; UI::EElementType m_type;
UIContainerWeakPtr m_parent; UIContainerWeakPtr m_parent;
UIElementSkin *m_skin; UIElementSkinPtr m_skin;
std::string m_id; std::string m_id;
bool m_visible; bool m_visible;
bool m_enabled; bool m_enabled;

View File

@ -22,11 +22,12 @@
*/ */
#include "uielementskin.h" #include <prerequisites.h>
#include "uielement.h" #include <ui/uiskins.h>
#include "graphics/borderedimage.h" #include <ui/uielement.h>
#include "graphics/textures.h" #include <ui/uielementskin.h>
#include "uiskins.h" #include <graphics/borderedimage.h>
#include <graphics/textures.h>
void UIElementSkin::load(const YAML::Node& node) void UIElementSkin::load(const YAML::Node& node)
{ {

View File

@ -25,9 +25,9 @@
#ifndef UIELEMENTSKIN_H #ifndef UIELEMENTSKIN_H
#define UIELEMENTSKIN_H #define UIELEMENTSKIN_H
#include "prerequisites.h" #include <prerequisites.h>
#include "uiconstants.h" #include <graphics/image.h>
#include "graphics/image.h" #include <ui/uiconstants.h>
class UIElement; class UIElement;
@ -58,4 +58,6 @@ private:
ImagePtr m_defaultImage; ImagePtr m_defaultImage;
}; };
typedef boost::shared_ptr<UIElementSkin> UIElementSkinPtr;
#endif // UIELEMENTSKIN_H #endif // UIELEMENTSKIN_H

View File

@ -22,4 +22,5 @@
*/ */
#include "uilabel.h" #include <prerequisites.h>
#include <ui/uilabel.h>

View File

@ -25,9 +25,9 @@
#ifndef UILABEL_H #ifndef UILABEL_H
#define UILABEL_H #define UILABEL_H
#include "prerequisites.h" #include <prerequisites.h>
#include "uielement.h" #include <ui/uielement.h>
#include "graphics/font.h" #include <graphics/font.h>
class UILabel : public UIElement class UILabel : public UIElement
{ {

View File

@ -22,9 +22,10 @@
*/ */
#include "uilabelskin.h" #include <prerequisites.h>
#include "uilabel.h" #include <graphics/fonts.h>
#include "graphics/fonts.h" #include <ui/uilabelskin.h>
#include <ui/uilabel.h>
void UILabelSkin::load(const YAML::Node& node) void UILabelSkin::load(const YAML::Node& node)
{ {

View File

@ -25,9 +25,9 @@
#ifndef UILABELSKIN_H #ifndef UILABELSKIN_H
#define UILABELSKIN_H #define UILABELSKIN_H
#include "prerequisites.h" #include <prerequisites.h>
#include "uielementskin.h" #include <graphics/font.h>
#include "graphics/font.h" #include <ui/uielementskin.h>
class UILabelSkin : public UIElementSkin class UILabelSkin : public UIElementSkin
{ {

View File

@ -22,8 +22,9 @@
*/ */
#include "uilayout.h" #include <prerequisites.h>
#include "uielement.h" #include <ui/uielement.h>
#include <ui/uilayout.h>
int AnchorLine::getPos() const int AnchorLine::getPos() const
{ {

View File

@ -25,8 +25,8 @@
#ifndef UILAYOUT_H #ifndef UILAYOUT_H
#define UILAYOUT_H #define UILAYOUT_H
#include "prerequisites.h" #include <prerequisites.h>
#include "uiconstants.h" #include <ui/uiconstants.h>
enum EAnchorType { enum EAnchorType {
ANCHOR_LEFT = 0, ANCHOR_LEFT = 0,

View File

@ -22,9 +22,10 @@
*/ */
#include "uiloader.h" #include <prerequisites.h>
#include "core/resources.h" #include <core/resources.h>
#include "ui.h" #include <ui/ui.h>
#include <ui/uiloader.h>
UIElementPtr UILoader::createElementFromId(const std::string& id) UIElementPtr UILoader::createElementFromId(const std::string& id)
{ {
@ -39,7 +40,7 @@ UIElementPtr UILoader::createElementFromId(const std::string& id)
std::string elementId = split[1]; std::string elementId = split[1];
if(elementType == "panel") { if(elementType == "panel") {
element = UIElementPtr(new UIPanel); element = UIElementPtr(new UIContainer(UI::Panel));
} else if(elementType == "button") { } else if(elementType == "button") {
element = UIElementPtr(new UIButton); element = UIElementPtr(new UIButton);
} else if(elementType == "label") { } else if(elementType == "label") {
@ -49,7 +50,7 @@ UIElementPtr UILoader::createElementFromId(const std::string& id)
} else if(elementType == "textEdit") { } else if(elementType == "textEdit") {
element = UIElementPtr(new UITextEdit); element = UIElementPtr(new UITextEdit);
} else if(elementType == "lineDecoration") { } else if(elementType == "lineDecoration") {
element = UIElementPtr(new UILineDecoration); element = UIElementPtr(new UIElement(UI::LineDecoration));
} else if(elementType == "checkBox") { } else if(elementType == "checkBox") {
element = UIElementPtr(new UICheckBox); element = UIElementPtr(new UICheckBox);
} }

View File

@ -25,9 +25,9 @@
#ifndef UILOADER_H #ifndef UILOADER_H
#define UILOADER_H #define UILOADER_H
#include "prerequisites.h" #include <prerequisites.h>
#include "uiconstants.h" #include <ui/uiconstants.h>
#include "uicontainer.h" #include <ui/uicontainer.h>
namespace UILoader namespace UILoader
{ {

View File

@ -22,15 +22,15 @@
*/ */
#include "uiskins.h" #include <prerequisites.h>
#include "core/resources.h" #include <core/resources.h>
#include "graphics/textures.h" #include <graphics/textures.h>
#include "uielementskin.h" #include <ui/uiskins.h>
#include "uibuttonskin.h" #include <ui/uielementskin.h>
#include "uiwindowskin.h" #include <ui/uibuttonskin.h>
#include "uitexteditskin.h" #include <ui/uiwindowskin.h>
#include "uilabelskin.h" #include <ui/uitexteditskin.h>
#include "uilinedecorationskin.h" #include <ui/uilabelskin.h>
UISkins g_uiSkins; UISkins g_uiSkins;
@ -58,7 +58,7 @@ void UISkins::load(const std::string& skinsFile)
std::string name; std::string name;
it.first() >> name; it.first() >> name;
UIElementSkin *skin = new UIButtonSkin(name); UIElementSkinPtr skin = UIElementSkinPtr(new UIButtonSkin(name));
skin->load(it.second()); skin->load(it.second());
m_elementSkins.push_back(skin); m_elementSkins.push_back(skin);
} }
@ -70,7 +70,7 @@ void UISkins::load(const std::string& skinsFile)
std::string name; std::string name;
it.first() >> name; it.first() >> name;
UIElementSkin *skin = new UIElementSkin(name, UI::Panel); UIElementSkinPtr skin = UIElementSkinPtr(new UIElementSkin(name, UI::Panel));
skin->load(it.second()); skin->load(it.second());
m_elementSkins.push_back(skin); m_elementSkins.push_back(skin);
} }
@ -82,7 +82,7 @@ void UISkins::load(const std::string& skinsFile)
std::string name; std::string name;
it.first() >> name; it.first() >> name;
UIElementSkin *skin = new UIWindowSkin(name); UIElementSkinPtr skin = UIElementSkinPtr(new UIWindowSkin(name));
skin->load(it.second()); skin->load(it.second());
m_elementSkins.push_back(skin); m_elementSkins.push_back(skin);
} }
@ -94,7 +94,7 @@ void UISkins::load(const std::string& skinsFile)
std::string name; std::string name;
it.first() >> name; it.first() >> name;
UIElementSkin *skin = new UILabelSkin(name); UIElementSkinPtr skin = UIElementSkinPtr(new UILabelSkin(name));
skin->load(it.second()); skin->load(it.second());
m_elementSkins.push_back(skin); m_elementSkins.push_back(skin);
} }
@ -107,7 +107,7 @@ void UISkins::load(const std::string& skinsFile)
std::string name; std::string name;
it.first() >> name; it.first() >> name;
UIElementSkin *skin = new UITextEditSkin(name); UIElementSkinPtr skin = UIElementSkinPtr(new UITextEditSkin(name));
skin->load(it.second()); skin->load(it.second());
m_elementSkins.push_back(skin); m_elementSkins.push_back(skin);
} }
@ -120,7 +120,7 @@ void UISkins::load(const std::string& skinsFile)
std::string name; std::string name;
it.first() >> name; it.first() >> name;
UIElementSkin *skin = new UILineDecorationSkin(name); UIElementSkinPtr skin = UIElementSkinPtr(new UIElementSkin(name, UI::LineDecoration));
skin->load(it.second()); skin->load(it.second());
m_elementSkins.push_back(skin); m_elementSkins.push_back(skin);
} }
@ -132,20 +132,17 @@ void UISkins::load(const std::string& skinsFile)
void UISkins::terminate() void UISkins::terminate()
{ {
for(auto it = m_elementSkins.begin(); it != m_elementSkins.end(); ++it)
delete (*it);
m_elementSkins.clear();
} }
UIElementSkin* UISkins::getElementSkin(UI::EElementType elementType, const std::string& name) UIElementSkinPtr UISkins::getElementSkin(UI::EElementType elementType, const std::string& name)
{ {
for(auto it = m_elementSkins.begin(); it != m_elementSkins.end(); ++it) { for(auto it = m_elementSkins.begin(); it != m_elementSkins.end(); ++it) {
UIElementSkin *skin = (*it); const UIElementSkinPtr& skin = (*it);
if(elementType == skin->getElementType() && name == skin->getName()) { if(elementType == skin->getElementType() && name == skin->getName())
return skin; return skin;
}
} }
logWarning("Element skin '%s' not found", name.c_str()); logWarning("Element skin '%s' not found", name.c_str());
return NULL; return UIElementSkinPtr();
} }

View File

@ -25,11 +25,9 @@
#ifndef UISKIN_H #ifndef UISKIN_H
#define UISKIN_H #define UISKIN_H
#include "prerequisites.h" #include <prerequisites.h>
#include "uicontainer.h" #include <graphics/texture.h>
#include "graphics/texture.h" #include <ui/uielementskin.h>
class UIElementSkin;
class UISkins class UISkins
{ {
@ -39,13 +37,13 @@ public:
void load(const std::string& skinsFile); void load(const std::string& skinsFile);
void terminate(); void terminate();
UIElementSkin *getElementSkin(UI::EElementType elementType, const std::string& name = "default"); UIElementSkinPtr getElementSkin(UI::EElementType elementType, const std::string& name = "default");
TexturePtr getDefaultTexture() { return m_defaultTexture; } TexturePtr getDefaultTexture() { return m_defaultTexture; }
private: private:
TexturePtr m_defaultTexture; TexturePtr m_defaultTexture;
std::vector<UIElementSkin *> m_elementSkins; std::vector<UIElementSkinPtr> m_elementSkins;
}; };
extern UISkins g_uiSkins; extern UISkins g_uiSkins;

View File

@ -22,10 +22,11 @@
*/ */
#include "uitextedit.h" #include <prerequisites.h>
#include "uitexteditskin.h" #include <graphics/fonts.h>
#include "graphics/fonts.h" #include <ui/uitextedit.h>
#include "uicontainer.h" #include <ui/uitexteditskin.h>
#include <ui/uicontainer.h>
UITextEdit::UITextEdit() : UITextEdit::UITextEdit() :
UIElement(UI::TextEdit) UIElement(UI::TextEdit)
@ -61,7 +62,7 @@ void UITextEdit::onInputEvent(const InputEvent& event)
void UITextEdit::onLayoutRectChange(const Rect& rect) void UITextEdit::onLayoutRectChange(const Rect& rect)
{ {
UITextEditSkin *skin = static_cast<UITextEditSkin*>(getSkin()); UITextEditSkin *skin = static_cast<UITextEditSkin*>(getSkin().get());
Rect textRect = rect; Rect textRect = rect;
int margin = skin->getTextMargin(); int margin = skin->getTextMargin();
textRect.setLeft(textRect.left()+margin); textRect.setLeft(textRect.left()+margin);

View File

@ -25,9 +25,9 @@
#ifndef UITEXTEDIT_H #ifndef UITEXTEDIT_H
#define UITEXTEDIT_H #define UITEXTEDIT_H
#include "prerequisites.h" #include <prerequisites.h>
#include "uielement.h" #include <graphics/textarea.h>
#include "graphics/textarea.h" #include <ui/uielement.h>
class Font; class Font;

View File

@ -22,9 +22,10 @@
*/ */
#include "uitexteditskin.h" #include <prerequisites.h>
#include "uitextedit.h" #include <graphics/fonts.h>
#include "graphics/fonts.h" #include <ui/uitexteditskin.h>
#include <ui/uitextedit.h>
void UITextEditSkin::load(const YAML::Node& node) void UITextEditSkin::load(const YAML::Node& node)
{ {

View File

@ -25,9 +25,8 @@
#ifndef UITEXTEDITSKIN_H #ifndef UITEXTEDITSKIN_H
#define UITEXTEDITSKIN_H #define UITEXTEDITSKIN_H
#include "prerequisites.h" #include <prerequisites.h>
#include "uiconstants.h" #include <ui/uielementskin.h>
#include "uielementskin.h"
class Font; class Font;

View File

@ -22,14 +22,15 @@
*/ */
#include "uiwindow.h" #include <prerequisites.h>
#include "uiwindowskin.h" #include <ui/uiwindow.h>
#include <ui/uiwindowskin.h>
void UIWindow::onInputEvent(const InputEvent& event) void UIWindow::onInputEvent(const InputEvent& event)
{ {
UIContainer::onInputEvent(event); UIContainer::onInputEvent(event);
if(event.type == EV_MOUSE_LDOWN) { if(event.type == EV_MOUSE_LDOWN) {
UIWindowSkin *skin = static_cast<UIWindowSkin*>(getSkin()); UIWindowSkin *skin = static_cast<UIWindowSkin*>(getSkin().get());
Rect headRect = getRect(); Rect headRect = getRect();
headRect.setHeight(skin->getHeadHeight()); headRect.setHeight(skin->getHeadHeight());
if(headRect.contains(event.mousePos)) { if(headRect.contains(event.mousePos)) {

View File

@ -25,8 +25,8 @@
#ifndef UIWINDOW_H #ifndef UIWINDOW_H
#define UIWINDOW_H #define UIWINDOW_H
#include "prerequisites.h" #include <prerequisites.h>
#include "uicontainer.h" #include <ui/uicontainer.h>
class UIWindow : public UIContainer class UIWindow : public UIContainer
{ {

View File

@ -21,9 +21,10 @@
* THE SOFTWARE. * THE SOFTWARE.
*/ */
#include "uiwindowskin.h" #include <prerequisites.h>
#include "uiwindow.h" #include <graphics/fonts.h>
#include "graphics/fonts.h" #include <ui/uiwindowskin.h>
#include <ui/uiwindow.h>
void UIWindowSkin::load(const YAML::Node& node) void UIWindowSkin::load(const YAML::Node& node)
{ {

View File

@ -24,10 +24,9 @@
#ifndef UIWINDOWSKIN_H #ifndef UIWINDOWSKIN_H
#define UIWINDOWSKIN_H #define UIWINDOWSKIN_H
#include "prerequisites.h" #include <prerequisites.h>
#include "uiconstants.h" #include <graphics/font.h>
#include "uielementskin.h" #include <ui/uielementskin.h>
#include "graphics/font.h"
class UIWindowSkin : public UIElementSkin class UIWindowSkin : public UIElementSkin
{ {

View File

@ -22,7 +22,8 @@
*/ */
#include "color.h" #include <prerequisites.h>
#include <util/color.h>
Color Color::white(0xFF, 0xFF, 0xFF, 0xFF); Color Color::white(0xFF, 0xFF, 0xFF, 0xFF);
Color Color::black(0x00, 0x00, 0x00, 0xFF); Color Color::black(0x00, 0x00, 0x00, 0xFF);

View File

@ -25,7 +25,7 @@
#ifndef COLOR_H #ifndef COLOR_H
#define COLOR_H #define COLOR_H
#include "prerequisites.h" #include <prerequisites.h>
typedef uint32 RGBA; typedef uint32 RGBA;

View File

@ -21,8 +21,8 @@
* THE SOFTWARE. * THE SOFTWARE.
*/ */
#include "logger.h" #include <util/logger.h>
#include "util.h" #include <util/util.h>
#include <iostream> #include <iostream>
#include <cstdlib> #include <cstdlib>

View File

@ -25,7 +25,7 @@
#ifndef POINT_H #ifndef POINT_H
#define POINT_H #define POINT_H
#include "prerequisites.h" #include <prerequisites.h>
template <class T> template <class T>
class TSize; class TSize;

View File

@ -25,7 +25,7 @@
#ifndef RECT_H #ifndef RECT_H
#define RECT_H #define RECT_H
#include "prerequisites.h" #include <prerequisites.h>
template <class T> template <class T>
class TPoint; class TPoint;

View File

@ -21,7 +21,8 @@
* THE SOFTWARE. * THE SOFTWARE.
*/ */
#include "rsa.h" #include <prerequisites.h>
#include <util/rsa.h>
Rsa::Rsa() Rsa::Rsa()
{ {

View File

@ -24,7 +24,7 @@
#ifndef RSA_H #ifndef RSA_H
#define RSA_H #define RSA_H
#include "prerequisites.h" #include <prerequisites.h>
#include <gmp.h> #include <gmp.h>

View File

@ -25,8 +25,8 @@
#ifndef SIZE_H #ifndef SIZE_H
#define SIZE_H #define SIZE_H
#include "prerequisites.h" #include <prerequisites.h>
#include "point.h" #include <util/point.h>
enum ESizeScaleMode { enum ESizeScaleMode {
IGNORE_ASPECT_RATIO, IGNORE_ASPECT_RATIO,

View File

@ -21,7 +21,7 @@
* THE SOFTWARE. * THE SOFTWARE.
*/ */
#include "util.h" #include <util/util.h>
#include <cstdio> #include <cstdio>
std::string vformat(const char *format, va_list args) std::string vformat(const char *format, va_list args)

View File

@ -25,7 +25,7 @@
#ifndef UTIL_H #ifndef UTIL_H
#define UTIL_H #define UTIL_H
#include "logger.h" #include <util/logger.h>
#include <stdarg.h> #include <stdarg.h>
#include <boost/lexical_cast.hpp> #include <boost/lexical_cast.hpp>

View File

@ -22,14 +22,15 @@
*/ */
#include "core/engine.h" #include <prerequisites.h>
#include "core/configs.h" #include <core/engine.h>
#include "core/resources.h" #include <core/configs.h>
#include "core/platform.h" #include <core/resources.h>
#include "core/dispatcher.h" #include <core/platform.h>
#include <core/dispatcher.h>
#include <ui/uiskins.h>
#include "menustate.h" #include "menustate.h"
#include "teststate.h" #include "teststate.h"
#include "ui/uiskins.h"
/// Catches signals so we can exit nicely /// Catches signals so we can exit nicely
void signal_handler(int sig) void signal_handler(int sig)

View File

@ -22,17 +22,16 @@
*/ */
#include <prerequisites.h>
#include <core/engine.h>
#include <core/dispatcher.h>
#include <graphics/framebuffer.h>
#include <graphics/graphics.h>
#include <graphics/textures.h>
#include <graphics/borderedimage.h>
#include <graphics/fonts.h>
#include <ui/ui.h>
#include "menustate.h" #include "menustate.h"
#include "graphics/framebuffer.h"
#include "graphics/graphics.h"
#include "graphics/textures.h"
#include "core/engine.h"
#include "graphics/fonts.h"
#include "core/dispatcher.h"
#include "ui/ui.h"
#include "net/connections.h"
#include "graphics/borderedimage.h"
void MenuState::onEnter() void MenuState::onEnter()
{ {
@ -97,6 +96,9 @@ void MenuState::enterGameButton_clicked()
window->getParent()->unlockElement(); window->getParent()->unlockElement();
window->destroy(); window->destroy();
}); });
button = boost::static_pointer_cast<UIButton>(window->getChildById("okButton"));
button->setOnClick(boost::bind(&MenuState::enterGameWindowOkButton_clicked, this));
} }
void MenuState::infoButton_clicked() void MenuState::infoButton_clicked()
@ -111,3 +113,14 @@ void MenuState::infoButton_clicked()
}); });
} }
void MenuState::enterGameWindowOkButton_clicked()
{
UIContainerPtr enterGameWindow = boost::static_pointer_cast<UIContainer>(UIContainer::getRootContainer()->getChildById("enterGameWindow"));
std::string accountName = boost::static_pointer_cast<UITextEdit>(enterGameWindow->getChildById("accountNameTextEdit"))->getText();
std::string password = boost::static_pointer_cast<UITextEdit>(enterGameWindow->getChildById("passwordTextEdit"))->getText();
//m_protocolLogin = ProtocolLoginPtr(new ProtocolLogin);
//m_protocolLogin->login(accountName, password);
}

View File

@ -25,10 +25,10 @@
#ifndef MENUSTATE_H #ifndef MENUSTATE_H
#define MENUSTATE_H #define MENUSTATE_H
#include "core/gamestate.h" #include <prerequisites.h>
#include "graphics/texture.h" #include <core/gamestate.h>
#include "net/connection.h" #include <graphics/texture.h>
#include "ui/uipanel.h" //#include "protocollogin.h"
class MenuState : public GameState class MenuState : public GameState
{ {
@ -49,8 +49,10 @@ private:
void enterGameButton_clicked(); void enterGameButton_clicked();
void infoButton_clicked(); void infoButton_clicked();
UIPanelPtr m_menuPanel; void enterGameWindowOkButton_clicked();
TexturePtr m_background; TexturePtr m_background;
//ProtocolLoginPtr m_protocolLogin;
}; };
#endif // MENUSTATE_H #endif // MENUSTATE_H

View File

@ -1,112 +0,0 @@
/* The MIT License
*
* Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "protocoltibia87.h"
#include "util/rsa.h"
const char* ProtocolTibia87::rsa = "4673033022358411862216018001503683214873298680851934467521055526294025873980576"
"6860224610646919605860206328024326703361630109888417839241959507572247284807035"
"2355696191737922927869078457919049551036016528225191219083671878855092700253886"
"41700821735345222087940578381210879116823013776808975766851829020659073";
ProtocolTibia87::ProtocolTibia87()
{
}
void ProtocolTibia87::begin()
{
/*
connect("icechaw.otland.net", 7171,
[this](){
this->afterConnect();
}
);
*/
}
void ProtocolTibia87::login(const std::string& account, const std::string& password)
{
sendAccount(account, password);
}
void ProtocolTibia87::sendAccount(const std::string& account, const std::string& password)
{
NetworkMessagePtr networkMessage(new NetworkMessage);
networkMessage->addByte(0x01);//login Server
networkMessage->addU16(0x00);//OS
networkMessage->addU16(0x00);//VERSION
networkMessage->addPaddingBytes(12);//
int32 m_notEncriptedLen = networkMessage->getMessageLength();
//begin RSA encrypt
networkMessage->addU32(1); //xtea
networkMessage->addU32(2); //xtea
networkMessage->addU32(3); //xtea
networkMessage->addU32(4); //xtea
networkMessage->addString(account);
networkMessage->addString(password);
Rsa::encrypt(networkMessage->getBodyBuffer() + m_notEncriptedLen, networkMessage->getMessageLength() - m_notEncriptedLen, ProtocolTibia87::rsa);
networkMessage->setMessageLength(m_notEncriptedLen + 128);
networkMessage->updateHeaderLength();
/*
send(networkMessage,
[this](){
this->afterSendAccount();
}
);
*/
}
void ProtocolTibia87::afterConnect()
{
login("9418347", "lollol");
}
void ProtocolTibia87::afterSendAccount()
{
/*
recv(
[this](NetworkMessagePtr networkMessage){
this->parseCharacterList(networkMessage);
}
);
*/
}
void ProtocolTibia87::onError(const boost::system::error_code& error, const std::string& msg)
{
logError("%s; %s", error.message().c_str(), msg.c_str());
}
void ProtocolTibia87::parseCharacterList(NetworkMessagePtr networkMessage)
{
logInfo("Parsing characters. msglen: %d", networkMessage->getMessageLength());
}

View File

@ -1,55 +0,0 @@
/* The MIT License
*
* Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef PROTOCOLTIBIA87_H
#define PROTOCOLTIBIA87_H
#include "prerequisites.h"
#include "net/protocol.h"
class ProtocolTibia87 : public Protocol
{
public:
ProtocolTibia87();
virtual void begin();
protected:
void login(const std::string& account, const std::string& password);
void sendAccount(const std::string& account, const std::string& password);
void parseCharacterList(NetworkMessagePtr networkMessage);
void readCharacterList();
void afterConnect();
void afterSendAccount();
virtual void onError(const boost::system::error_code& error, const std::string& msg);
private:
static const char* rsa;
};
typedef boost::shared_ptr<ProtocolTibia87> ProtocolTibia87Ptr;
#endif //PROTOCOLTIBIA87_H

View File

@ -21,19 +21,39 @@
* THE SOFTWARE. * THE SOFTWARE.
*/ */
#include "connections.h"
Connections g_connections; #include <prerequisites.h>
#include <util/rsa.h>
#include "protocollogin.h"
size_t Connections::poll() ProtocolLogin::ProtocolLogin()
{ {
return m_ioService.poll(); logTrace();
} }
ConnectionPtr Connections::createConnection() ProtocolLogin::~ProtocolLogin()
{ {
ConnectionPtr connection(new Connection(m_ioService)); logTrace();
m_connections.push_back(connection);
return connection;
} }
void ProtocolLogin::login(const std::string& account, const std::string& password)
{
logTrace();
m_connection = ConnectionPtr(new Connection);
m_connection->connect("www.google.com", 80, boost::bind(&ProtocolLogin::afterConnect, this));
}
void ProtocolLogin::afterConnect()
{
logTrace();
}
void ProtocolLogin::onError(const boost::system::error_code& error, const std::string& msg)
{
logTrace();
logError("Connection error: %s", error.message().c_str());
}

View File

@ -21,27 +21,28 @@
* THE SOFTWARE. * THE SOFTWARE.
*/ */
#ifndef CONNECTIONS_H
#define CONNECTIONS_H
#include "prerequisites.h" #ifndef PROTOCOLLOGIN_H
#define PROTOCOLLOGIN_H
#include "connection.h" #include <prerequisites.h>
#include <net/connection.h>
class Connections class ProtocolLogin
{ {
public: public:
size_t poll(); ProtocolLogin();
~ProtocolLogin();
ConnectionPtr createConnection();
void login(const std::string& account, const std::string& password);
void afterConnect();
void sendAccount();
void onError(const boost::system::error_code& error, const std::string& msg);
private: private:
boost::asio::io_service m_ioService; ConnectionPtr m_connection;
typedef std::vector<ConnectionPtr> ConnectionVector;
ConnectionVector m_connections;
}; };
extern Connections g_connections; typedef boost::shared_ptr<ProtocolLogin> ProtocolLoginPtr;
#endif //CONNECTIONS_H #endif // PROTOCOLLOGIN_H

View File

@ -22,16 +22,13 @@
*/ */
#include <core/engine.h>
#include <graphics/graphics.h>
#include "teststate.h" #include "teststate.h"
#include "graphics/graphics.h"
#include "core/engine.h"
#include "net/connections.h"
#include "net/protocoltibia87.h"
void TestState::onEnter() void TestState::onEnter()
{ {
m_protocol = ProtocolTibia87Ptr(new ProtocolTibia87);
m_protocol->begin();
} }
void TestState::onLeave() void TestState::onLeave()

View File

@ -25,8 +25,8 @@
#ifndef TESTSTATE_H #ifndef TESTSTATE_H
#define TESTSTATE_H #define TESTSTATE_H
#include "core/gamestate.h" #include <prerequisites.h>
#include "net/protocoltibia87.h" #include <core/gamestate.h>
class TestState : public GameState class TestState : public GameState
{ {
@ -42,9 +42,6 @@ public:
virtual void render(); virtual void render();
virtual void update(int ticks, int elapsedTicks); virtual void update(int ticks, int elapsedTicks);
private:
ProtocolTibia87Ptr m_protocol;
}; };
#endif // TESTSTATE_H #endif // TESTSTATE_H