refactoring paths and includes

This commit is contained in:
Eduardo Bart
2011-04-10 12:37:15 -03:00
parent 792d661dad
commit 1f78f93096
74 changed files with 130 additions and 162 deletions

View File

@@ -0,0 +1,72 @@
/* 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 COLOR_H
#define COLOR_H
#include "prerequisites.h"
typedef uint32 RGBA;
class Color
{
public:
inline Color() : color(0) { }
inline Color(uint8 r, uint8 g, uint8 b, uint8 a = 0xFF) : color(((r & 0xff)<<24) | ((g & 0xff)<<16) | ((b & 0xff)<<8) | (a & 0xff)) { }
inline Color(const Color& other) : color(other.color) { }
inline Color(RGBA rgba) : color(rgba) { }
inline uint8 red() const { return (color >> 24) & 0xFF; }
inline uint8 green() const { return (color >> 16) & 0xFF; }
inline uint8 blue() const { return (color >> 8) & 0xFF; }
inline uint8 alpha() const { return color & 0xFF; }
inline RGBA rgba() const { return color; }
inline const uint8* rgbaPtr() const { return (const uint8*)&color; }
inline void setRed(uint8 r) { color = ((r & 0xff)<<24) | (color & 0x00ffffff); }
inline void setGreen(uint8 g) { color = ((g & 0xff)<<16) | (color & 0xff00ffff); }
inline void setBlue(uint8 b) { color = ((b & 0xff)<<8) | (color & 0xffff00ff); }
inline void setAlpha(uint8 a) { color = (a & 0xff) | (color & 0xffffff00); }
inline void setRGBA(uint8 r, uint8 g, uint8 b, uint8 a = 0xFF) { color = ((r & 0xff)<<24) | ((g & 0xff)<<16) | ((b & 0xff)<<8) | (a & 0xff); }
inline void setRGBA(uint32 rgba) { color = rgba; }
inline Color& operator=(const Color& other) { color = other.color; return *this; }
inline bool operator==(const Color& other) const { return other.color == color; }
inline bool operator!=(const Color& other) const { return other.color != color; }
private:
RGBA color;
};
inline void operator>>(const YAML::Node& node, Color& color)
{
int r, g, b, a;
node[0] >> r;
node[1] >> g;
node[2] >> b;
node[3] >> a;
color.setRGBA(r,g,b,a);
}
#endif // COLOR_H

View File

@@ -0,0 +1,71 @@
/* 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 "logger.h"
#include "util.h"
#include <iostream>
#include <cstdlib>
#include <stdarg.h>
void Logger::_log(int level, const char *trace, const char *format, ...)
{
va_list args;
std::string strace;
va_start(args, format);
std::string text = vformat(format, args);
va_end(args);
if(trace) {
strace = trace;
strace = strace.substr(0, strace.find_first_of('('));
if(strace.find_last_of(' ') != std::string::npos)
strace = strace.substr(strace.find_last_of(' ') + 1);
}
#ifdef linux
static char const *colors[] = { "\033[01;31m ", "\033[01;31m", "\033[01;33m", "\033[0;32m", "\033[01;34m" };
static bool colored = getenv("COLORED_OUTPUT");
if(colored)
std::cout << colors[level];
#endif
if(!strace.empty())
std::cout << "[" << strace << "] ";
static char const *prefixes[] = { "FATAL ERROR: ", "ERROR: ", "WARNING: ", "", "", "" };
std::cout << prefixes[level];
std::cout << text;
#ifdef linux
if(colored)
std::cout << "\033[0m";
#endif
std::cout << std::endl;
if(level == LFATAL)
exit(-1);
}

View File

@@ -0,0 +1,68 @@
/* 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 LOGGER_H
#define LOGGER_H
#include <sstream>
namespace Logger {
enum ELogLevel {
LFATAL = 0,
LERROR,
LWARNING,
LINFO,
LDEBUG
};
void _log(int level, const char *trace, const char *format, ...);
}
#define logFatal(...) Logger::_log(Logger::LFATAL, NULL, __VA_ARGS__)
#define logError(...) Logger::_log(Logger::LERROR, NULL, __VA_ARGS__)
#define logWarning(...) Logger::_log(Logger::LWARNING, NULL, __VA_ARGS__)
#define logDebug(...) Logger::_log(Logger::LDEBUG, NULL, __VA_ARGS__)
#define logInfo(...) Logger::_log(Logger::LINFO, NULL, __VA_ARGS__)
#define logTrace() Logger::_log(Logger::LDEBUG, __PRETTY_FUNCTION__, "")
#define logTraceFatal(...) Logger::_log(Logger::LFATAL, __PRETTY_FUNCTION__, __VA_ARGS__)
#define logTraceError(...) Logger::_log(Logger::LERROR, __PRETTY_FUNCTION__, __VA_ARGS__)
#define logTraceWarning(...) Logger::_log(Logger::LWARNING, __PRETTY_FUNCTION__, __VA_ARGS__)
#define logTraceDebug(...) Logger::_log(Logger::LDEBUG, __PRETTY_FUNCTION__, __VA_ARGS__)
#define logTraceInfo(...) Logger::_log(Logger::LINFO, __PRETTY_FUNCTION__, __VA_ARGS__)
struct Dump {
public:
Dump() { }
~Dump() { logDebug(m_buf.str().c_str()); }
template<class T> Dump &operator<<(const T &x) { m_buf << x << " "; return *this; }
private:
std::ostringstream m_buf;
};
#define dump Dump()
#endif

View File

@@ -0,0 +1,88 @@
/* 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 POINT_H
#define POINT_H
#include "prerequisites.h"
template <class T>
class TSize;
template <class T>
class TPoint
{
public:
inline TPoint() : x(0), y(0) {};
inline TPoint(T x, T y) : x(x), y(y) { };
inline TPoint(const TPoint<T>& other) : x(other.x), y(other.y) { };
inline bool isNull() const { return x==0 && y==0; }
inline TSize<T> toSize() const { return TSize<T>(x, y); }
inline TPoint<T> operator-() const { return TPoint<T>(-x, -y); }
inline TPoint<T> operator+(const TPoint<T>& other) const { return TPoint<T>(x + other.x, y + other.y); }
inline TPoint<T>& operator+=(const TPoint<T>& other) { x+=other.x; y+=other.y; return *this; }
inline TPoint<T> operator-(const TPoint<T>& other) const { return TPoint<T>(x - other.x, y - other.y); }
inline TPoint<T>& operator-=(const TPoint<T>& other) { x-=other.x; y-=other.y; return *this; }
inline TPoint<T> operator*(const TPoint<T>& other) const { return TPoint<T>(x * other.x, y * other.y); }
inline TPoint<T>& operator*=(const TPoint<T>& other) { x*=other.x; y*=other.y; return *this; }
inline TPoint<T> operator*(const T v) const { return TPoint<T>(x * v, y * v); }
inline TPoint<T>& operator*=(const T v) { x*=v; y*=v; return *this; }
inline TPoint<T> operator/(const TPoint<T>& other) const { return TPoint<T>(x/other.x, y/other.y); }
inline TPoint<T>& operator/=(const TPoint<T>& other) { x/=other.x; y/=other.y; return *this; }
inline TPoint<T> operator/(const T v) const { return TPoint<T>(x/v, y/v); }
inline TPoint<T>& operator/=(const T v) { x/=v; y/=v; return *this; }
inline bool operator<=(const TPoint<T>&other) const { return x<=other.x && y<=other.y; }
inline bool operator>=(const TPoint<T>&other) const { return x>=other.x && y>=other.y; }
inline bool operator<(const TPoint<T>&other) const { return x<other.x && y<other.y; }
inline bool operator>(const TPoint<T>&other) const { return x>other.x && y>other.y; }
inline TPoint<T>& operator=(const TPoint<T>& other) { x = other.x; y = other.y; return *this; }
inline bool operator==(const TPoint<T>& other) const { return other.x==x && other.y==y; }
inline bool operator!=(const TPoint<T>& other) const { return other.x!=x || other.y!=y; }
inline float length() const { return sqrtf((float)(x*x + y*y)); }
inline T manhattanLength() const { return std::abs(x) + std::abs(y); }
inline float distanceFrom(const TPoint<T>& other) const {
return TPoint<T>(x - other.x, y - other.y).getLength();
}
T x, y;
};
typedef TPoint<int> Point;
typedef TPoint<float> PointF;
template <class T>
inline void operator>>(const YAML::Node& node, TPoint<T>& point)
{
T x, y;
node[0] >> point.x;
node[1] >> point.y;
}
#endif

307
src/framework/util/rect.h Normal file
View File

@@ -0,0 +1,307 @@
/* 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 RECT_H
#define RECT_H
#include "prerequisites.h"
template <class T>
class TPoint;
template <class T>
class TSize;
template <class T>
class TRect
{
public:
inline TRect() : x1(0), y1(0), x2(-1), y2(-1) { }
inline TRect(T x, T y, T width, T height) : x1(x), y1(y), x2(x+width-1), y2(y+height-1) { }
inline TRect(const TPoint<T>& topLeft, const TPoint<T>& bottomRight) : x1(topLeft.x), y1(topLeft.y), x2(bottomRight.x), y2(bottomRight.y) { }
inline TRect(const TRect<T>& other) : x1(other.x1), y1(other.y1), x2(other.x2), y2(other.y2) { }
inline TRect(T x, T y, const TSize<T>& size) : x1(x), y1(y), x2(x+size.width()-1), y2(y+size.height()-1) { }
inline TRect(const TPoint<T>& topLeft, const TSize<T>& size) : x1(topLeft.x), y1(topLeft.y), x2(x1+size.width()-1), y2(y1+size.height()-1) { }
inline bool isNull() const { return x2 == x1 - 1 && y2 == y1 - 1; }
inline bool isEmpty() const { return x1 > x2 || y1 > y2; }
inline bool isValid() const { return x1 <= x2 && y1 <= y2; }
inline T left() const { return x1; }
inline T top() const { return y1; }
inline T right() const { return x2; }
inline T bottom() const { return y2; }
inline T horizontalCenter() const { return x1 + (x2 - x1)/2; }
inline T verticalCenter() const { return y1 + (y2 - y1)/2; }
inline T x() const { return x1; }
inline T y() const { return y1; }
inline TPoint<T> topLeft() const { return TPoint<T>(x1, y1); }
inline TPoint<T> bottomRight() const { return TPoint<T>(x2, y2); }
inline TPoint<T> topRight() const { return TPoint<T>(x2, y1); }
inline TPoint<T> bottomLeft() const { return TPoint<T>(x1, y2); }
inline TPoint<T> center() const { return TPoint<T>((x1+x2)/2, (y1+y2)/2); }
inline T width() const { return x2 - x1 + 1; }
inline T height() const { return y2 - y1 + 1; }
inline TSize<T> size() const { return TSize<T>(width(), height()); }
inline void setLeft(T pos) { x1 = pos; }
inline void setTop(T pos) { y1 = pos; }
inline void setRight(T pos) { x2 = pos; }
inline void setBottom(T pos) { y2 = pos; }
inline void setX(T x) { x1 = x; }
inline void setY(T y) { y1 = y; }
inline void setTopLeft(const TPoint<T> &p) { x1 = p.x; y1 = p.y; }
inline void setBottomRight(const TPoint<T> &p) { x2 = p.x; y2 = p.y; }
inline void setTopRight(const TPoint<T> &p) { x2 = p.x; y1 = p.y; }
inline void setBottomLeft(const TPoint<T> &p) { x1 = p.x; y2 = p.y; }
inline void setWidth(T width) { x2 = x1 + width - 1; }
inline void setHeight(T height) { y2 = y1 + height- 1; }
inline void setSize(T width, T height) { x2 = x1 + width; y2 = y1 + height; }
inline void setSize(const TSize<T>& size) { x2 = x1 + size.width(); y2 = y1 + size.height(); }
inline void setRect(T x, T y, T width, T height) { x1 = x; y1 = y; x2 = (x + width - 1); y2 = (y + height - 1); }
inline void setCoords(int left, int top, int right, int bottom) { x1 = left; y1 = top; x2 = right; y2 = bottom; }
inline void translate(T x, T y) { x1 += x; y1 += y; x2 += x; y2 += y; }
inline void translate(const TPoint<T> &p) { x1 += p.x; y1 += p.y; x2 += p.x; y2 += p.y; }
inline void moveTo(T x, T y) { x2 += x - x1; y2 += y - y1; x1 = x; y1 = y; }
inline void moveTo(const TPoint<T> &p) { x2 += p.x - x1; y2 += p.y - y1; x1 = p.x; y1 = p.y; }
inline void moveLeft(T pos) { x2 += (pos - x1); x1 = pos; }
inline void moveTop(T pos) { y2 += (pos - y1); y1 = pos; }
inline void moveRight(T pos) { x1 += (pos - x2); x2 = pos; }
inline void moveBottom(T pos) { y1 += (pos - y2); y2 = pos; }
inline void moveTopLeft(const TPoint<T> &p) { moveLeft(p.x); moveTop(p.y); }
inline void moveBottomRight(const TPoint<T> &p) { moveRight(p.x); moveBottom(p.y); }
inline void moveTopRight(const TPoint<T> &p) { moveRight(p.x); moveTop(p.y); }
inline void moveBottomLeft(const TPoint<T> &p) { moveLeft(p.x); moveBottom(p.y); }
inline TRect<T> translated(int x, int y) const { return TRect<T>(TPoint<T>(x1 + x, y1 + y), TPoint<T>(x2 + x, y2 + y)); }
inline TRect<T> translated(const TPoint<T> &p) const { return TRect<T>(TPoint<T>(x1 + p.x, y1 + p.y), TPoint<T>(x2 + p.x, y2 + p.y)); }
inline TRect<T> expanded(T pixels) const { return TRect<T>(TPoint<T>(x1 - pixels, y1 - pixels), TPoint<T>(x2 + pixels, y2 + pixels)); }
inline void moveCenter(const TPoint<T> &p) {
T w = x2 - x1;
T h = y2 - y1;
x1 = p.x - w/2;
y1 = p.y - h/2;
x2 = x1 + w;
y2 = y1 + h;
}
inline void moveHorizontalCenter(T x) {
T w = x2 - x1;
x1 = x - w/2;
x2 = x1 + w;
}
inline void moveVerticalCenter(T y) {
T h = y2 - y1;
y1 = y - h/2;
y2 = y1 + h;
}
inline bool contains(const TPoint<T> &p, bool insideOnly = false) const {
T l, r;
if(x2 < x1 - 1) {
l = x2;
r = x1;
} else {
l = x1;
r = x2;
}
if(insideOnly) {
if(p.x <= l || p.x >= r)
return false;
} else {
if(p.x < l || p.x > r)
return false;
}
int t, b;
if(y2 < y1 - 1) {
t = y2;
b = y1;
} else {
t = y1;
b = y2;
}
if(insideOnly) {
if(p.y <= t || p.y >= b)
return false;
} else {
if(p.y < t || p.y > b)
return false;
}
return true;
}
inline bool intersects(const TRect<T> &r) const {
if(isNull() || r.isNull())
return false;
int l1 = x1;
int r1 = x1;
if(x2 - x1 + 1 < 0)
l1 = x2;
else
r1 = x2;
int l2 = r.x1;
int r2 = r.x1;
if(r.x2 - r.x1 + 1 < 0)
l2 = r.x2;
else
r2 = r.x2;
if (l1 > r2 || l2 > r1)
return false;
int t1 = y1;
int b1 = y1;
if(y2 - y1 + 1 < 0)
t1 = y2;
else
b1 = y2;
int t2 = r.y1;
int b2 = r.y1;
if (r.y2 - r.y1 + 1 < 0)
t2 = r.y2;
else
b2 = r.y2;
if(t1 > b2 || t2 > b1)
return false;
return true;
}
inline TRect<T> united(const TRect<T> &r) const {
if(isNull() || r.isNull())
return TRect<T>();
int l1 = x1;
int r1 = x1;
if (x2 - x1 + 1 < 0)
l1 = x2;
else
r1 = x2;
int l2 = r.x1;
int r2 = r.x1;
if(r.x2 - r.x1 + 1 < 0)
l2 = r.x2;
else
r2 = r.x2;
if(l1 > r2 || l2 > r1)
return TRect<T>();
int t1 = y1;
int b1 = y1;
if(y2 - y1 + 1 < 0)
t1 = y2;
else
b1 = y2;
int t2 = r.y1;
int b2 = r.y1;
if(r.y2 - r.y1 + 1 < 0)
t2 = r.y2;
else
b2 = r.y2;
if(t1 > b2 || t2 > b1)
return TRect<T>();
TRect<T> tmp;
tmp.x1 = std::max(l1, l2);
tmp.x2 = std::min(r1, r2);
tmp.y1 = std::max(t1, t2);
tmp.y2 = std::min(b1, b2);
return tmp;
}
inline TRect<T> intersection(const TRect<T> &r) const {
if(isNull())
return r;
if(r.isNull())
return *this;
int l1 = x1;
int r1 = x1;
if(x2 - x1 + 1 < 0)
l1 = x2;
else
r1 = x2;
int l2 = r.x1;
int r2 = r.x1;
if(r.x2 - r.x1 + 1 < 0)
l2 = r.x2;
else
r2 = r.x2;
int t1 = y1;
int b1 = y1;
if(y2 - y1 + 1 < 0)
t1 = y2;
else
b1 = y2;
int t2 = r.y1;
int b2 = r.y1;
if(r.y2 - r.y1 + 1 < 0)
t2 = r.y2;
else
b2 = r.y2;
TRect<T> tmp;
tmp.x1 = std::min(l1, l2);
tmp.x2 = std::max(r1, r2);
tmp.y1 = std::min(t1, t2);
tmp.y2 = std::max(b1, b2);
return tmp;
}
inline TRect<T>& operator=(const TRect<T>& other) { x1 = other.x1; y1 = other.y1; x2 = other.x2; y2 = other.y2; return *this; }
inline bool operator==(const TRect<T>& other) const { return (x1 == other.x1 && y1 == other.y1 && x2 == other.x2 && y2 == other.y2); }
inline bool operator!=(const TRect<T>& other) const { return (x1 != other.x1 || y1 != other.y1 || x2 != other.x2 || y2 != other.y2); }
private:
T x1, y1, x2, y2;
};
typedef TRect<int> Rect;
typedef TRect<float> RectF;
template <class T>
inline void operator>>(const YAML::Node& node, TRect<T>& rect)
{
T x, y, width, height;
node[0] >> x;
node[1] >> y;
node[2] >> width;
node[3] >> height;
rect.setRect(x, y, width, height);
}
#endif // RECT_H

View File

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

123
src/framework/util/size.h Normal file
View File

@@ -0,0 +1,123 @@
/* 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 SIZE_H
#define SIZE_H
#include "prerequisites.h"
#include "point.h"
enum ESizeScaleMode {
IGNORE_ASPECT_RATIO,
KEEP_ASPECT_RATIO,
KEEP_ASPECT_RATIO_BY_EXPANDING
};
template <class T>
class TSize
{
public:
inline TSize() : wd(-1), ht(-1) {};
inline TSize(T width, T height) : wd(width), ht(height) { };
inline TSize(const TSize<T>& other) : wd(other.wd), ht(other.ht) { };
inline TPoint<T> toPoint() const { return TPoint<T>(wd, ht); }
inline bool isNull() const { return wd==0 && ht==0; }
inline bool isEmpty() const { return wd<1 || ht<1; }
inline bool isValid() const { return wd>=0 && ht>=0; }
inline int width() const { return wd; }
inline int height() const { return ht; }
inline void setSize(T w, T h) { wd = w; ht = h; }
inline void setWidth(T w) { wd = w; }
inline void setHeight(T h) { ht = h; }
inline TSize<T> operator-() const { return TSize<T>(-wd, -ht); }
inline TSize<T> operator+(const TSize<T>& other) const { return TSize<T>(wd + other.wd, ht + other.ht); }
inline TSize<T>& operator+=(const TSize<T>& other) { wd+=other.wd; ht+=other.ht; return *this; }
inline TSize<T> operator-(const TSize<T>& other) const { return TSize<T>(wd - other.wd, ht - other.ht); }
inline TSize<T>& operator-=(const TSize<T>& other) { wd-=other.wd; ht-=other.ht; return *this; }
inline TSize<T> operator*(const float v) const { return TSize<T>((T)v*wd, (T)ht*v); }
inline TSize<T>& operator*=(const float v) { wd=(T)v*wd; ht=(T)ht*v; return *this; }
inline TSize<T> operator/(const float v) const { return TSize<T>((T)wd/v, (T)ht/v); }
inline TSize<T>& operator/=(const float v) { (T)wd/=v; (T)ht/=v; return *this; }
inline bool operator<=(const TSize<T>&other) const { return wd<=other.wd || ht<=other.ht; }
inline bool operator>=(const TSize<T>&other) const { return wd>=other.wd || ht>=other.ht; }
inline bool operator<(const TSize<T>&other) const { return wd<other.wd || ht<other.ht; }
inline bool operator>(const TSize<T>&other) const { return wd>other.wd || ht>other.ht; }
inline TSize<T>& operator=(const TSize<T>& other) { wd = other.wd; ht = other.ht; return *this; }
inline bool operator==(const TSize<T>& other) const { return other.wd==wd && other.ht==ht; }
inline bool operator!=(const TSize<T>& other) const { return other.wd!=wd || other.ht!=ht; }
inline TSize<T> expandedTo(const TSize<T>& other) const { return TSize<T>(std::max(wd,other.wd), std::max(ht,other.ht)); }
inline TSize<T> boundedTo(const TSize<T>& other) const { return TSize<T>(std::min(wd,other.wd), std::min(ht,other.ht)); }
inline void scale(const TSize<T>& s, ESizeScaleMode mode) {
if(mode == IGNORE_ASPECT_RATIO || wd == 0 || ht == 0) {
wd = s.wd;
ht = s.ht;
} else {
bool useHeight;
T rw = (s.ht * wd) / ht;
if(mode == KEEP_ASPECT_RATIO)
useHeight = (rw <= s.wd);
else // mode == KEEP_ASPECT_RATIO_BY_EXPANDING
useHeight = (rw >= s.wd);
if(useHeight) {
wd = rw;
ht = s.ht;
} else {
ht = (s.wd * ht)/wd;
wd = s.wd;
}
}
}
inline void scale(int w, int h, ESizeScaleMode mode) { scale(TSize<T>(w, h)); }
inline float ratio() const { return (float)wd/ht; }
inline T area() const { return wd*ht; }
private:
T wd, ht;
};
typedef TSize<int> Size;
typedef TSize<float> SizeF;
template <class T>
inline void operator>>(const YAML::Node& node, TSize<T>& size)
{
T w, h;
node[0] >> w;
node[1] >> h;
size.setSize(w, h);
}
#endif

View File

@@ -0,0 +1,53 @@
/* 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 "util.h"
#include <cstdio>
std::string vformat(const char *format, va_list args)
{
if(!format)
return "";
int result = -1, length = 256;
char *buffer = 0;
while(result == -1) {
if(buffer)
delete [] buffer;
buffer = new char [length + 1];
result = vsnprintf(buffer, length, format, args);
length *= 2;
}
std::string s(buffer);
delete [] buffer;
return s;
}
std::string format(const char *format, ...)
{
va_list args;
va_start(args, format);
std::string s = vformat(format, args);
va_end(args);
return s;
}

51
src/framework/util/util.h Normal file
View File

@@ -0,0 +1,51 @@
/* 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 UTIL_H
#define UTIL_H
#include "logger.h"
#include <stdarg.h>
#include <boost/lexical_cast.hpp>
/// Formatting like printf for std::string, va_list input version
std::string vformat(const char *format, va_list args);
/// Formatting like printf for std::string
std::string format(const char *format, ...);
/// Convert any data type through boost::lexical_cast
template<class R, class T>
R convertType(T t)
{
R ret = R();
try {
ret = boost::lexical_cast<R>(t);
} catch(boost::bad_lexical_cast bad) {
logError("Error converting type: %s", bad.what());
}
return ret;
}
#endif