mirror of
https://github.com/edubart/otclient.git
synced 2025-10-18 21:43:26 +02:00
rewrite and reoganize tools functions
* create stdext namespace which contains additional C++ algorithms * organize stdext in string, math, cast and exception utilities
This commit is contained in:
@@ -20,17 +20,18 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef UTF8_H
|
||||
#define UTF8_H
|
||||
|
||||
#include "types.h"
|
||||
#include <string>
|
||||
|
||||
namespace Fw {
|
||||
|
||||
char utf8CharToLatin1(uchar *utf8, int *read);
|
||||
std::string utf8StringToLatin1(uchar *utf8);
|
||||
#ifndef BOOLEAN_H
|
||||
#define BOOLEAN_H
|
||||
|
||||
/// Boolean with default value
|
||||
template<bool def>
|
||||
struct Boolean {
|
||||
Boolean() : v(def) { }
|
||||
operator bool &() { return v; }
|
||||
operator bool const &() const { return v; }
|
||||
bool& operator=(const bool& o) { v = o; return v; }
|
||||
private:
|
||||
bool v;
|
||||
};
|
||||
|
||||
#endif
|
@@ -20,40 +20,25 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef __COMPILER_H__
|
||||
#define __COMPILER_H__
|
||||
#include "color.h"
|
||||
|
||||
#if !(__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
|
||||
#error "sorry, you need gcc 4.6 or greater to compile"
|
||||
#endif
|
||||
|
||||
#if !defined(__GXX_EXPERIMENTAL_CXX0X__)
|
||||
#error "sorry, you must enable C++0x to compile"
|
||||
#endif
|
||||
|
||||
// hack to enable std::thread on mingw32 4.6
|
||||
/*
|
||||
#if !defined(_GLIBCXX_HAS_GTHREADS) && defined(__GNUG__)
|
||||
#include <boost/thread/thread.hpp>
|
||||
#include <boost/thread/mutex.hpp>
|
||||
#include <boost/thread/recursive_mutex.hpp>
|
||||
#include <boost/thread/locks.hpp>
|
||||
#include <boost/thread/condition_variable.hpp>
|
||||
namespace std {
|
||||
using boost::thread;
|
||||
|
||||
using boost::mutex;
|
||||
using boost::timed_mutex;
|
||||
using boost::recursive_mutex;
|
||||
using boost::recursive_timed_mutex;
|
||||
|
||||
using boost::lock_guard;
|
||||
using boost::unique_lock;
|
||||
|
||||
using boost::condition_variable;
|
||||
using boost::condition_variable_any;
|
||||
}
|
||||
#endif
|
||||
*/
|
||||
|
||||
#endif
|
||||
// NOTE: AABBGGRR order
|
||||
const Color Color::alpha = 0x00000000;
|
||||
const Color Color::white = 0xffffffff;
|
||||
const Color Color::black = 0xff000000;
|
||||
const Color Color::red = 0xff0000ff;
|
||||
const Color Color::darkRed = 0xff000080;
|
||||
const Color Color::green = 0xff00ff00;
|
||||
const Color Color::darkGreen = 0xff008000;
|
||||
const Color Color::blue = 0xffff0000;
|
||||
const Color Color::darkBlue = 0xff800000;
|
||||
const Color Color::pink = 0xffff00ff;
|
||||
const Color Color::darkPink = 0xff800080;
|
||||
const Color Color::yellow = 0xff00ffff;
|
||||
const Color Color::darkYellow = 0xff008080;
|
||||
const Color Color::teal = 0xffffff00;
|
||||
const Color Color::darkTeal = 0xff808000;
|
||||
const Color Color::gray = 0xffa0a0a0;
|
||||
const Color Color::darkGray = 0xff808080;
|
||||
const Color Color::lightGray = 0xffc0c0c0;
|
||||
const Color Color::orange = 0xffff8c00;
|
187
src/framework/util/color.h
Normal file
187
src/framework/util/color.h
Normal file
@@ -0,0 +1,187 @@
|
||||
/*
|
||||
* Copyright (c) 2010-2012 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 "../stdext/types.h"
|
||||
#include "../stdext/cast.h"
|
||||
#include "../const.h"
|
||||
|
||||
class Color
|
||||
{
|
||||
public:
|
||||
Color() : m_r(1.0f), m_g(1.0f), m_b(1.0f), m_a(1.0f) { }
|
||||
Color(uint32 rgba) { setRGBA(rgba); }
|
||||
Color(uint8 r, uint8 g, uint8 b, uint8 a = 0xFF) : m_r(r/255.0f), m_g(g/255.0f), m_b(b/255.0f), m_a(a/255.0f) { }
|
||||
Color(int r, int g, int b, int a = 0xFF) : m_r(r/255.0f), m_g(g/255.0f), m_b(b/255.0f), m_a(a/255.0f) { }
|
||||
Color(float r, float g, float b, float a = 1.0f) : m_r(r), m_g(g), m_b(b), m_a(a) { }
|
||||
|
||||
uint8 a() const { return m_a*255.0f; }
|
||||
uint8 b() const { return m_b*255.0f; }
|
||||
uint8 g() const { return m_g*255.0f; }
|
||||
uint8 r() const { return m_r*255.0f; }
|
||||
|
||||
float aF() const { return m_a; }
|
||||
float bF() const { return m_b; }
|
||||
float gF() const { return m_g; }
|
||||
float rF() const { return m_r; }
|
||||
|
||||
uint32 rgba() const { return uint32(a() | b() << 8 | g() << 16 | r() << 24); }
|
||||
|
||||
void setRed(int r) { m_r = uint8(r)/255.0f; }
|
||||
void setGreen(int g) { m_g = uint8(g)/255.0f; }
|
||||
void setBlue(int b) { m_b = uint8(b)/255.0f; }
|
||||
void setAlpha(int a) { m_a = uint8(a)/255.0f; }
|
||||
|
||||
void setRed(float r) { m_r = r; }
|
||||
void setGreen(float g) { m_g = g; }
|
||||
void setBlue(float b) { m_b = b; }
|
||||
void setAlpha(float a) { m_a = a; }
|
||||
|
||||
void setRGBA(uint8 r, uint8 g, uint8 b, uint8 a = 0xFF) { m_r = r/255.0f; m_g = g/255.0f; m_b = b/255.0f; m_a = a/255.0f; }
|
||||
void setRGBA(uint32 rgba) { setRGBA((rgba >> 0) & 0xff, (rgba >> 8) & 0xff, (rgba >> 16) & 0xff, (rgba >> 24) & 0xff); }
|
||||
|
||||
Color& operator=(uint32_t rgba) { setRGBA(rgba); return *this; }
|
||||
bool operator==(uint32_t rgba) const { return this->rgba() == rgba; }
|
||||
|
||||
Color& operator=(const Color& other) { m_r = other.m_r; m_g = other.m_g; m_b = other.m_b; m_a = other.m_a; return *this; }
|
||||
bool operator==(const Color& other) const { return other.rgba() == rgba(); }
|
||||
bool operator!=(const Color& other) const { return other.rgba() != rgba(); }
|
||||
|
||||
static Color from8bit(int color) {
|
||||
if(color >= 216 || color <= 0)
|
||||
return Color(0, 0, 0);
|
||||
|
||||
int r = int(color / 36) % 6 * 51;
|
||||
int g = int(color / 6) % 6 * 51;
|
||||
int b = color % 6 * 51;
|
||||
return Color(r, g, b);
|
||||
}
|
||||
|
||||
static const Color alpha;
|
||||
static const Color white;
|
||||
static const Color black;
|
||||
static const Color red;
|
||||
static const Color darkRed;
|
||||
static const Color green;
|
||||
static const Color darkGreen;
|
||||
static const Color blue;
|
||||
static const Color darkBlue;
|
||||
static const Color pink;
|
||||
static const Color darkPink;
|
||||
static const Color yellow;
|
||||
static const Color darkYellow;
|
||||
static const Color teal;
|
||||
static const Color darkTeal;
|
||||
static const Color gray;
|
||||
static const Color darkGray;
|
||||
static const Color lightGray;
|
||||
static const Color orange;
|
||||
|
||||
private:
|
||||
float m_r;
|
||||
float m_g;
|
||||
float m_b;
|
||||
float m_a;
|
||||
};
|
||||
|
||||
inline std::ostream& operator<<(std::ostream& out, const Color& color)
|
||||
{
|
||||
using namespace std;
|
||||
out << "#" << hex << setfill('0')
|
||||
<< setw(2) << (int)color.r()
|
||||
<< setw(2) << (int)color.g()
|
||||
<< setw(2) << (int)color.b()
|
||||
<< setw(2) << (int)color.a();
|
||||
out << dec << setfill(' ');
|
||||
return out;
|
||||
}
|
||||
|
||||
inline std::istream& operator>>(std::istream& in, Color& color)
|
||||
{
|
||||
using namespace std;
|
||||
std::string tmp;
|
||||
|
||||
if(in.get() == '#') {
|
||||
in >> tmp;
|
||||
|
||||
if(tmp.length() == 6 || tmp.length() == 8) {
|
||||
color.setRed((uint8)stdext::hex_to_dec(tmp.substr(0, 2)));
|
||||
color.setGreen((uint8)stdext::hex_to_dec(tmp.substr(2, 2)));
|
||||
color.setBlue((uint8)stdext::hex_to_dec(tmp.substr(4, 2)));
|
||||
if(tmp.length() == 8)
|
||||
color.setAlpha((uint8)stdext::hex_to_dec(tmp.substr(6, 2)));
|
||||
else
|
||||
color.setAlpha(255);
|
||||
} else
|
||||
in.seekg(-tmp.length()-1, ios_base::cur);
|
||||
} else {
|
||||
in.unget();
|
||||
in >> tmp;
|
||||
|
||||
if(tmp == "alpha") {
|
||||
color = Color::alpha;
|
||||
} else if(tmp == "black") {
|
||||
color = Color::black;
|
||||
} else if(tmp == "white") {
|
||||
color = Color::white;
|
||||
} else if(tmp == "red") {
|
||||
color = Color::red;
|
||||
} else if(tmp == "darkRed") {
|
||||
color = Color::darkRed;
|
||||
} else if(tmp == "green") {
|
||||
color = Color::green;
|
||||
} else if(tmp == "darkGreen") {
|
||||
color = Color::darkGreen;
|
||||
} else if(tmp == "blue") {
|
||||
color = Color::blue;
|
||||
} else if(tmp == "darkBlue") {
|
||||
color = Color::darkBlue;
|
||||
} else if(tmp == "pink") {
|
||||
color = Color::pink;
|
||||
} else if(tmp == "darkPink") {
|
||||
color = Color::darkPink;
|
||||
} else if(tmp == "yellow") {
|
||||
color = Color::yellow;
|
||||
} else if(tmp == "darkYellow") {
|
||||
color = Color::darkYellow;
|
||||
} else if(tmp == "teal") {
|
||||
color = Color::teal;
|
||||
} else if(tmp == "darkTeal") {
|
||||
color = Color::darkTeal;
|
||||
} else if(tmp == "gray") {
|
||||
color = Color::gray;
|
||||
} else if(tmp == "darkGray") {
|
||||
color = Color::darkGray;
|
||||
} else if(tmp == "lightGray") {
|
||||
color = Color::lightGray;
|
||||
} else if(tmp == "orange") {
|
||||
color = Color::orange;
|
||||
} else {
|
||||
in.seekg(-tmp.length(), ios_base::cur);
|
||||
}
|
||||
}
|
||||
return in;
|
||||
}
|
||||
|
||||
#endif
|
@@ -1,41 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2010-2012 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 EXCEPTION_H
|
||||
#define EXCEPTION_H
|
||||
|
||||
#include <exception>
|
||||
|
||||
class Exception : public std::exception
|
||||
{
|
||||
public:
|
||||
Exception() { }
|
||||
Exception(const std::string& what) : m_what(what) { }
|
||||
virtual ~Exception() throw() { };
|
||||
|
||||
virtual const char* what() const throw() { return m_what.c_str(); }
|
||||
|
||||
protected:
|
||||
std::string m_what;
|
||||
};
|
||||
|
||||
#endif
|
211
src/framework/util/matrix.h
Normal file
211
src/framework/util/matrix.h
Normal file
@@ -0,0 +1,211 @@
|
||||
/*
|
||||
* Copyright (c) 2010-2012 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 MATRIX_H
|
||||
#define MATRIX_H
|
||||
|
||||
#include <array>
|
||||
#include <cstring>
|
||||
#include <initializer_list>
|
||||
#include <sstream>
|
||||
|
||||
template<int N, int M, typename T = float>
|
||||
class Matrix
|
||||
{
|
||||
public:
|
||||
Matrix() { setIdentity(); }
|
||||
Matrix(const Matrix<N,M,T>& other) = default;
|
||||
template<typename U>
|
||||
Matrix(const std::initializer_list<U>& values) { *this = values; }
|
||||
template<typename U>
|
||||
Matrix(const U *values) { *this = values; }
|
||||
|
||||
void setIdentity();
|
||||
bool isIdentity() const;
|
||||
void fill(T value);
|
||||
|
||||
Matrix<M,N,T> transposed() const;
|
||||
typename std::enable_if<N==M>::type transpose() { *this = transposed(); }
|
||||
|
||||
T *data() { return m[0]; }
|
||||
const T *data() const { return m[0]; }
|
||||
|
||||
T& operator()(int row, int column) { return m[row-1][column-1]; }
|
||||
T operator()(int row, int column) const { return m[row-1][column-1]; }
|
||||
|
||||
Matrix<N,M,T>& operator=(const Matrix<N,M,T>& other) = default;
|
||||
template<typename U>
|
||||
Matrix<N,M,T>& operator=(const std::initializer_list<U>& values);
|
||||
template<typename U>
|
||||
Matrix<N,M,T>& operator=(const U *values);
|
||||
Matrix<N,M,T>& operator+=(const Matrix<N,M,T>& other);
|
||||
Matrix<N,M,T>& operator-=(const Matrix<N,M,T>& other);
|
||||
Matrix<N,M,T>& operator*=(T factor);
|
||||
Matrix<N,M,T>& operator/=(T divisor);
|
||||
bool operator==(const Matrix<N,M,T>& other) const;
|
||||
bool operator!=(const Matrix<N,M,T>& other) const;
|
||||
|
||||
private:
|
||||
Matrix(int) {} // construct without initializing identity matrix
|
||||
T m[N][M];
|
||||
};
|
||||
|
||||
template<int N, int M, typename T>
|
||||
void Matrix<N,M,T>::setIdentity() {
|
||||
for(int i=0;i<N;++i) {
|
||||
for(int j=0;j<M;++j) {
|
||||
if(i==j)
|
||||
m[i][j] = 1.0f;
|
||||
else
|
||||
m[i][j] = 0.0f;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template<int N, int M, typename T>
|
||||
bool Matrix<N,M,T>::isIdentity() const {
|
||||
for(int i=0;i<N;++i)
|
||||
for(int j=0;j<M;++j)
|
||||
if((i==j && m[i][j] != 1.0f) || (i!=j && m[i][j] != 0.0f))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
template<int N, int M, typename T>
|
||||
void Matrix<N,M,T>::fill(T value) {
|
||||
for(int i=0;i<N;++i)
|
||||
for(int j=0;j<M;++j)
|
||||
m[i][j] = value;
|
||||
}
|
||||
|
||||
template<int N, int M, typename T>
|
||||
Matrix<M,N,T> Matrix<N,M,T>::transposed() const {
|
||||
Matrix<M,N,T> result(1);
|
||||
for(int i=0;i<N;++i)
|
||||
for(int j=0;j<M;++j)
|
||||
result.m[j][i] = m[i][j];
|
||||
return result;
|
||||
}
|
||||
|
||||
template<int N, int M, typename T>
|
||||
template<typename U>
|
||||
Matrix<N,M,T>& Matrix<N,M,T>::operator=(const std::initializer_list<U>& values) {
|
||||
auto it = values.begin();
|
||||
for(int i=0;i<N;++i)
|
||||
for(int j=0;j<M;++j)
|
||||
m[i][j] = *(it++);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<int N, int M, typename T>
|
||||
template<typename U>
|
||||
Matrix<N,M,T>& Matrix<N,M,T>::operator=(const U *values) {
|
||||
for(int i=0;i<N;++i)
|
||||
for(int j=0;j<M;++j)
|
||||
m[i][j] = values[i*N + j];
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<int N, int M, typename T>
|
||||
Matrix<N,M,T>& Matrix<N,M,T>::operator+=(const Matrix<N,M,T>& other) {
|
||||
for(int i=0;i<N;++i)
|
||||
for(int j=0;j<M;++j)
|
||||
m[i][j] += other.m[i][j];
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<int N, int M, typename T>
|
||||
Matrix<N,M,T>& Matrix<N,M,T>::operator-=(const Matrix<N,M,T>& other) {
|
||||
for(int i=0;i<N;++i)
|
||||
for(int j=0;j<M;++j)
|
||||
m[i][j] -= other.m[i][j];
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<int N, int M, typename T>
|
||||
Matrix<N,M,T>& Matrix<N,M,T>::operator*=(T factor) {
|
||||
for(int i=0;i<N;++i)
|
||||
for(int j=0;j<M;++j)
|
||||
m[i][j] *= factor;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<int N, int M, typename T>
|
||||
Matrix<N,M,T>& Matrix<N,M,T>::operator/=(T divisor) {
|
||||
for(int i=0;i<N;++i)
|
||||
for(int j=0;j<M;++j)
|
||||
m[i][j] /= divisor;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<int N, int M, typename T>
|
||||
bool Matrix<N,M,T>::operator==(const Matrix<N,M,T>& other) const
|
||||
{
|
||||
for(int i=0;i<N;++i)
|
||||
for(int j=0;j<M;++j)
|
||||
if(m[i][j] != other.m[i][j])
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
template<int N, int M, typename T>
|
||||
bool Matrix<N,M,T>::operator!=(const Matrix<N,M,T>& other) const
|
||||
{
|
||||
for(int i=0;i<N;++i)
|
||||
for(int j=0;j<M;++j)
|
||||
if(m[i][j] != other.m[i][j])
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
template<int N, int M, typename T>
|
||||
std::ostream& operator<<(std::ostream& out, const Matrix<N,M,T>& mat)
|
||||
{
|
||||
for(int i=0;i<N;++i) {
|
||||
for(int j=0;j<M;++j) {
|
||||
out << mat(i,j);
|
||||
if(j+1 != M)
|
||||
out << " ";
|
||||
}
|
||||
out << "\n";
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
template<int N, int M, typename T>
|
||||
std::istream& operator>>(std::istream& in, Matrix<N,M,T>& mat)
|
||||
{
|
||||
for(int i=0;i<N;++i)
|
||||
for(int j=0;j<M;++j)
|
||||
in >> mat(i,j);
|
||||
return in;
|
||||
}
|
||||
|
||||
typedef Matrix<4,4> Matrix4x4;
|
||||
typedef Matrix<3,3> Matrix3x3;
|
||||
typedef Matrix<2,2> Matrix2x2;
|
||||
|
||||
typedef Matrix4x4 Matrix4;
|
||||
typedef Matrix3x3 Matrix3;
|
||||
typedef Matrix2x2 Matrix2;
|
||||
|
||||
#endif
|
101
src/framework/util/point.h
Normal file
101
src/framework/util/point.h
Normal file
@@ -0,0 +1,101 @@
|
||||
/*
|
||||
* Copyright (c) 2010-2012 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 "../stdext/types.h"
|
||||
#include <sstream>
|
||||
#include <cmath>
|
||||
|
||||
template<class T>
|
||||
class TSize;
|
||||
|
||||
template<class T>
|
||||
class TPoint
|
||||
{
|
||||
public:
|
||||
TPoint() : x(0), y(0) {}
|
||||
TPoint(T x, T y) : x(x), y(y) { }
|
||||
TPoint(const TPoint<T>& other) : x(other.x), y(other.y) { }
|
||||
|
||||
bool isNull() const { return x==0 && y==0; }
|
||||
TSize<T> toSize() const { return TSize<T>(x, y); }
|
||||
|
||||
TPoint<T> operator-() const { return TPoint<T>(-x, -y); }
|
||||
|
||||
TPoint<T> operator+(const TPoint<T>& other) const { return TPoint<T>(x + other.x, y + other.y); }
|
||||
TPoint<T>& operator+=(const TPoint<T>& other) { x+=other.x; y+=other.y; return *this; }
|
||||
TPoint<T> operator-(const TPoint<T>& other) const { return TPoint<T>(x - other.x, y - other.y); }
|
||||
TPoint<T>& operator-=(const TPoint<T>& other) { x-=other.x; y-=other.y; return *this; }
|
||||
TPoint<T> operator*(const TPoint<T>& other) const { return TPoint<T>(x * other.x, y * other.y); }
|
||||
TPoint<T>& operator*=(const TPoint<T>& other) { x*=other.x; y*=other.y; return *this; }
|
||||
TPoint<T> operator/(const TPoint<T>& other) const { return TPoint<T>(x/other.x, y/other.y); }
|
||||
TPoint<T>& operator/=(const TPoint<T>& other) { x/=other.x; y/=other.y; return *this; }
|
||||
|
||||
TPoint<T> operator+(T other) const { return TPoint<T>(x + other, y + other); }
|
||||
TPoint<T>& operator+=(T other) { x+=other; y+=other; return *this; }
|
||||
TPoint<T> operator-(T other) const { return TPoint<T>(x - other, y - other); }
|
||||
TPoint<T>& operator-=(T other) { x-=other; y-=other; return *this; }
|
||||
TPoint<T> operator*(float v) const { return TPoint<T>(x*v, y*v); }
|
||||
TPoint<T>& operator*=(float v) { x*=v; y*=v; return *this; }
|
||||
TPoint<T> operator/(float v) const { return TPoint<T>(x/v, y/v); }
|
||||
TPoint<T>& operator/=(float v) { x/=v; y/=v; return *this; }
|
||||
|
||||
bool operator<=(const TPoint<T>&other) const { return x<=other.x && y<=other.y; }
|
||||
bool operator>=(const TPoint<T>&other) const { return x>=other.x && y>=other.y; }
|
||||
bool operator<(const TPoint<T>&other) const { return x<other.x && y<other.y; }
|
||||
bool operator>(const TPoint<T>&other) const { return x>other.x && y>other.y; }
|
||||
|
||||
TPoint<T>& operator=(const TPoint<T>& other) { x = other.x; y = other.y; return *this; }
|
||||
bool operator==(const TPoint<T>& other) const { return other.x==x && other.y==y; }
|
||||
bool operator!=(const TPoint<T>& other) const { return other.x!=x || other.y!=y; }
|
||||
|
||||
float length() const { return sqrt((float)(x*x + y*y)); }
|
||||
T manhattanLength() const { return std::abs(x) + std::abs(y); }
|
||||
|
||||
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>
|
||||
std::ostream& operator<<(std::ostream& out, const TPoint<T>& point)
|
||||
{
|
||||
out << point.x << " " << point.y;
|
||||
return out;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
std::istream& operator>>(std::istream& in, TPoint<T>& point)
|
||||
{
|
||||
in >> point.x;
|
||||
in >> point.y;
|
||||
return in;
|
||||
}
|
||||
|
||||
#endif
|
308
src/framework/util/rect.h
Normal file
308
src/framework/util/rect.h
Normal file
@@ -0,0 +1,308 @@
|
||||
/*
|
||||
* Copyright (c) 2010-2012 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 "../stdext/types.h"
|
||||
#include <sstream>
|
||||
|
||||
template<class T>
|
||||
class TPoint;
|
||||
|
||||
template<class T>
|
||||
class TSize;
|
||||
|
||||
template<class T>
|
||||
class TRect
|
||||
{
|
||||
public:
|
||||
TRect() : x1(0), y1(0), x2(-1), y2(-1) { }
|
||||
TRect(T x, T y, T width, T height) : x1(x), y1(y), x2(x+width-1), y2(y+height-1) { }
|
||||
TRect(const TPoint<T>& topLeft, const TPoint<T>& bottomRight) : x1(topLeft.x), y1(topLeft.y), x2(bottomRight.x), y2(bottomRight.y) { }
|
||||
TRect(const TRect<T>& other) : x1(other.x1), y1(other.y1), x2(other.x2), y2(other.y2) { }
|
||||
TRect(T x, T y, const TSize<T>& size) : x1(x), y1(y), x2(x+size.width()-1), y2(y+size.height()-1) { }
|
||||
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) { }
|
||||
TRect(const TPoint<T>& topLeft, int width, int height) : x1(topLeft.x), y1(topLeft.y), x2(x1+width-1), y2(y1+height-1) { }
|
||||
|
||||
bool isNull() const { return x2 == x1 - 1 && y2 == y1 - 1; }
|
||||
bool isEmpty() const { return x1 > x2 || y1 > y2; }
|
||||
bool isValid() const { return x1 <= x2 && y1 <= y2; }
|
||||
|
||||
T left() const { return x1; }
|
||||
T top() const { return y1; }
|
||||
T right() const { return x2; }
|
||||
T bottom() const { return y2; }
|
||||
T horizontalCenter() const { return x1 + (x2 - x1)/2; }
|
||||
T verticalCenter() const { return y1 + (y2 - y1)/2; }
|
||||
T x() const { return x1; }
|
||||
T y() const { return y1; }
|
||||
TPoint<T> topLeft() const { return TPoint<T>(x1, y1); }
|
||||
TPoint<T> bottomRight() const { return TPoint<T>(x2, y2); }
|
||||
TPoint<T> topRight() const { return TPoint<T>(x2, y1); }
|
||||
TPoint<T> bottomLeft() const { return TPoint<T>(x1, y2); }
|
||||
TPoint<T> center() const { return TPoint<T>((x1+x2)/2, (y1+y2)/2); }
|
||||
T width() const { return x2 - x1 + 1; }
|
||||
T height() const { return y2 - y1 + 1; }
|
||||
TSize<T> size() const { return TSize<T>(width(), height()); }
|
||||
void reset() { x1 = y1 = 0; x2 = y2 = -1; }
|
||||
void clear() { x2 = x1 - 1; y2 = y1 - 1; }
|
||||
|
||||
void setLeft(T pos) { x1 = pos; }
|
||||
void setTop(T pos) { y1 = pos; }
|
||||
void setRight(T pos) { x2 = pos; }
|
||||
void setBottom(T pos) { y2 = pos; }
|
||||
void setX(T x) { x1 = x; }
|
||||
void setY(T y) { y1 = y; }
|
||||
void setTopLeft(const TPoint<T> &p) { x1 = p.x; y1 = p.y; }
|
||||
void setBottomRight(const TPoint<T> &p) { x2 = p.x; y2 = p.y; }
|
||||
void setTopRight(const TPoint<T> &p) { x2 = p.x; y1 = p.y; }
|
||||
void setBottomLeft(const TPoint<T> &p) { x1 = p.x; y2 = p.y; }
|
||||
void setWidth(T width) { x2 = x1 + width - 1; }
|
||||
void setHeight(T height) { y2 = y1 + height- 1; }
|
||||
void setSize(const TSize<T>& size) { x2 = x1 + size.width() - 1; y2 = y1 + size.height() - 1; }
|
||||
void setRect(T x, T y, T width, T height) { x1 = x; y1 = y; x2 = (x + width - 1); y2 = (y + height - 1); }
|
||||
void setCoords(int left, int top, int right, int bottom) { x1 = left; y1 = top; x2 = right; y2 = bottom; }
|
||||
|
||||
void expandLeft(T add) { x1 -= add; }
|
||||
void expandTop(T add) { y1 -= add; }
|
||||
void expandRight(T add) { x2 += add; }
|
||||
void expandBottom(T add) { y2 += add; }
|
||||
void expand(T top, T right, T bottom, T left) { x1 -= left; y1 -= top; x2 += right; y2 += bottom; }
|
||||
void expand(T add) { x1 -= add; y1 -= add; x2 += add; y2 += add; }
|
||||
|
||||
void translate(T x, T y) { x1 += x; y1 += y; x2 += x; y2 += y; }
|
||||
void translate(const TPoint<T> &p) { x1 += p.x; y1 += p.y; x2 += p.x; y2 += p.y; }
|
||||
void resize(const TSize<T>& size) { x2 = x1 + size.width() - 1; y2 = y1 + size.height() - 1; }
|
||||
void resize(T width, T height) { x2 = x1 + width - 1; y2 = y1 + height - 1; }
|
||||
void move(T x, T y) { x2 += x - x1; y2 += y - y1; x1 = x; y1 = y; }
|
||||
void move(const TPoint<T> &p) { x2 += p.x - x1; y2 += p.y - y1; x1 = p.x; y1 = p.y; }
|
||||
void moveLeft(T pos) { x2 += (pos - x1); x1 = pos; }
|
||||
void moveTop(T pos) { y2 += (pos - y1); y1 = pos; }
|
||||
void moveRight(T pos) { x1 += (pos - x2); x2 = pos; }
|
||||
void moveBottom(T pos) { y1 += (pos - y2); y2 = pos; }
|
||||
void moveTopLeft(const TPoint<T> &p) { moveLeft(p.x); moveTop(p.y); }
|
||||
void moveBottomRight(const TPoint<T> &p) { moveRight(p.x); moveBottom(p.y); }
|
||||
void moveTopRight(const TPoint<T> &p) { moveRight(p.x); moveTop(p.y); }
|
||||
void moveBottomLeft(const TPoint<T> &p) { moveLeft(p.x); moveBottom(p.y); }
|
||||
|
||||
TRect<T> translated(int x, int y) const { return TRect<T>(TPoint<T>(x1 + x, y1 + y), TPoint<T>(x2 + x, y2 + y)); }
|
||||
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)); }
|
||||
|
||||
TRect<T> expanded(T add) const { return TRect<T>(TPoint<T>(x1 - add, y1 - add), TPoint<T>(x2 + add, y2 + add)); }
|
||||
|
||||
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;
|
||||
}
|
||||
void moveHorizontalCenter(T x) {
|
||||
T w = x2 - x1;
|
||||
x1 = x - w/2;
|
||||
x2 = x1 + w;
|
||||
}
|
||||
void moveVerticalCenter(T y) {
|
||||
T h = y2 - y1;
|
||||
y1 = y - h/2;
|
||||
y2 = y1 + h;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
T 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;
|
||||
}
|
||||
|
||||
bool contains(const TRect<T> &r, bool insideOnly = false) const {
|
||||
if(contains(r.topLeft(), insideOnly) && contains(r.bottomRight(), insideOnly))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
TRect<T> united(const TRect<T> &r) const {
|
||||
TRect<T> tmp;
|
||||
tmp.x1 = std::min(x1, r.x1);
|
||||
tmp.x2 = std::max(x2, r.x2);
|
||||
tmp.y1 = std::min(y1, r.y1);
|
||||
tmp.y2 = std::max(y2, r.y2);
|
||||
return tmp;
|
||||
}
|
||||
|
||||
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::max(l1, l2);
|
||||
tmp.x2 = std::min(r1, r2);
|
||||
tmp.y1 = std::max(t1, t2);
|
||||
tmp.y2 = std::min(b1, b2);
|
||||
return tmp;
|
||||
}
|
||||
|
||||
void bind(const TRect<T> &r) {
|
||||
if(isNull() || r.isNull())
|
||||
return;
|
||||
|
||||
if(right() > r.right())
|
||||
moveRight(r.right());
|
||||
if(bottom() > r.bottom())
|
||||
moveBottom(r.bottom());
|
||||
if(left() < r.left())
|
||||
moveLeft(r.left());
|
||||
if(top() < r.top())
|
||||
moveTop(r.top());
|
||||
}
|
||||
|
||||
TRect<T>& operator=(const TRect<T>& other) { x1 = other.x1; y1 = other.y1; x2 = other.x2; y2 = other.y2; return *this; }
|
||||
bool operator==(const TRect<T>& other) const { return (x1 == other.x1 && y1 == other.y1 && x2 == other.x2 && y2 == other.y2); }
|
||||
bool operator!=(const TRect<T>& other) const { return (x1 != other.x1 || y1 != other.y1 || x2 != other.x2 || y2 != other.y2); }
|
||||
|
||||
TRect<T>& operator|=(const TRect<T>& other) { *this = united(other); return *this; }
|
||||
TRect<T>& operator&=(const TRect<T>& other) { *this = intersection(other); return *this; }
|
||||
|
||||
private:
|
||||
T x1, y1, x2, y2;
|
||||
};
|
||||
|
||||
typedef TRect<int> Rect;
|
||||
typedef TRect<float> RectF;
|
||||
|
||||
template<class T>
|
||||
std::ostream& operator<<(std::ostream& out, const TRect<T>& rect)
|
||||
{
|
||||
out << rect.left() << " " << rect.top() << " " << rect.width() << " " << rect.height();
|
||||
return out;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
std::istream& operator>>(std::istream& in, TRect<T>& rect)
|
||||
{
|
||||
T x, y , w, h;
|
||||
in >> x >> y >> w >> h;
|
||||
rect.setRect(x,y,w,h);
|
||||
return in;
|
||||
}
|
||||
|
||||
#endif
|
126
src/framework/util/size.h
Normal file
126
src/framework/util/size.h
Normal file
@@ -0,0 +1,126 @@
|
||||
/*
|
||||
* Copyright (c) 2010-2012 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 "point.h"
|
||||
#include "../const.h"
|
||||
|
||||
template<class T>
|
||||
class TSize
|
||||
{
|
||||
public:
|
||||
TSize() : wd(-1), ht(-1) {};
|
||||
TSize(T width, T height) : wd(width), ht(height) { };
|
||||
TSize(const TSize<T>& other) : wd(other.wd), ht(other.ht) { };
|
||||
|
||||
TPoint<T> toPoint() const { return TPoint<T>(wd, ht); }
|
||||
|
||||
bool isNull() const { return wd==0 && ht==0; }
|
||||
bool isEmpty() const { return wd<1 || ht<1; }
|
||||
bool isValid() const { return wd>=0 && ht>=0; }
|
||||
|
||||
int width() const { return wd; }
|
||||
int height() const { return ht; }
|
||||
|
||||
void resize(T w, T h) { wd = w; ht = h; }
|
||||
void setWidth(T w) { wd = w; }
|
||||
void setHeight(T h) { ht = h; }
|
||||
|
||||
TSize<T> operator-() const { return TSize<T>(-wd, -ht); }
|
||||
TSize<T> operator+(const TSize<T>& other) const { return TSize<T>(wd + other.wd, ht + other.ht); }
|
||||
TSize<T>& operator+=(const TSize<T>& other) { wd+=other.wd; ht+=other.ht; return *this; }
|
||||
TSize<T> operator-(const TSize<T>& other) const { return TSize<T>(wd - other.wd, ht - other.ht); }
|
||||
TSize<T>& operator-=(const TSize<T>& other) { wd-=other.wd; ht-=other.ht; return *this; }
|
||||
TSize<T> operator*(const TSize<T>& other) const { return TSize<T>((T)other.wd*wd, (T)ht*other.ht); }
|
||||
TSize<T>& operator*=(const TSize<T>& other) { wd=(T)other.wd*wd; ht=(T)ht*other.ht; return *this; }
|
||||
TSize<T> operator/(const TSize<T>& other) const { return TSize<T>((T)wd/other.wd, (T)ht/other.ht); }
|
||||
TSize<T>& operator/=(const TSize<T>& other) { (T)wd/=other.wd; (T)ht/=other.ht; return *this; }
|
||||
TSize<T> operator*(const float v) const { return TSize<T>((T)wd*v, (T)ht*v); }
|
||||
TSize<T>& operator*=(const float v) { wd=(T)wd*v; ht=(T)ht*v; return *this; }
|
||||
TSize<T> operator/(const float v) const { return TSize<T>((T)wd/v, (T)ht/v); }
|
||||
TSize<T>& operator/=(const float v) { wd/=v; ht/=v; return *this; }
|
||||
|
||||
bool operator<=(const TSize<T>&other) const { return wd<=other.wd || ht<=other.ht; }
|
||||
bool operator>=(const TSize<T>&other) const { return wd>=other.wd || ht>=other.ht; }
|
||||
bool operator<(const TSize<T>&other) const { return wd<other.wd || ht<other.ht; }
|
||||
bool operator>(const TSize<T>&other) const { return wd>other.wd || ht>other.ht; }
|
||||
|
||||
TSize<T>& operator=(const TSize<T>& other) { wd = other.wd; ht = other.ht; return *this; }
|
||||
bool operator==(const TSize<T>& other) const { return other.wd==wd && other.ht==ht; }
|
||||
bool operator!=(const TSize<T>& other) const { return other.wd!=wd || other.ht!=ht; }
|
||||
|
||||
TSize<T> expandedTo(const TSize<T>& other) const { return TSize<T>(std::max(wd,other.wd), std::max(ht,other.ht)); }
|
||||
TSize<T> boundedTo(const TSize<T>& other) const { return TSize<T>(std::min(wd,other.wd), std::min(ht,other.ht)); }
|
||||
|
||||
void scale(const TSize<T>& s, Fw::AspectRatioMode mode) {
|
||||
if(mode == Fw::IgnoreAspectRatio || wd == 0 || ht == 0) {
|
||||
wd = s.wd;
|
||||
ht = s.ht;
|
||||
} else {
|
||||
bool useHeight;
|
||||
T rw = (s.ht * wd) / ht;
|
||||
|
||||
if(mode == Fw::KeepAspectRatio)
|
||||
useHeight = (rw <= s.wd);
|
||||
else // mode == Fw::KeepAspectRatioByExpanding
|
||||
useHeight = (rw >= s.wd);
|
||||
|
||||
if(useHeight) {
|
||||
wd = rw;
|
||||
ht = s.ht;
|
||||
} else {
|
||||
ht = (s.wd * ht)/wd;
|
||||
wd = s.wd;
|
||||
}
|
||||
}
|
||||
}
|
||||
void scale(int w, int h, Fw::AspectRatioMode mode) { scale(TSize<T>(w, h)); }
|
||||
|
||||
float ratio() const { return (float)wd/ht; }
|
||||
T area() const { return wd*ht; }
|
||||
|
||||
private:
|
||||
T wd, ht;
|
||||
};
|
||||
|
||||
typedef TSize<int> Size;
|
||||
typedef TSize<float> SizeF;
|
||||
|
||||
template<class T>
|
||||
std::ostream& operator<<(std::ostream& out, const TSize<T>& size)
|
||||
{
|
||||
out << size.width() << " " << size.height();
|
||||
return out;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
std::istream& operator>>(std::istream& in, TSize<T>& size)
|
||||
{
|
||||
T w, h;
|
||||
in >> w >> h;
|
||||
size.resize(w, h);
|
||||
return in;
|
||||
}
|
||||
|
||||
#endif
|
@@ -1,350 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2010-2012 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 TOOLS_H
|
||||
#define TOOLS_H
|
||||
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include <exception>
|
||||
#include <cxxabi.h>
|
||||
#include <vector>
|
||||
#include <boost/algorithm/string.hpp>
|
||||
#include "types.h"
|
||||
#include "exception.h"
|
||||
|
||||
namespace Fw {
|
||||
|
||||
// read utilities for istream
|
||||
inline uint8 getU8(std::istream& in) {
|
||||
uint8 tmp;
|
||||
in.read((char*)&tmp, 1);
|
||||
return tmp;
|
||||
}
|
||||
|
||||
inline uint16 getU16(std::istream& in) {
|
||||
uint16 tmp;
|
||||
in.read((char*)&tmp, 2);
|
||||
return tmp;
|
||||
}
|
||||
|
||||
inline uint32 getU32(std::istream& in) {
|
||||
uint32 tmp;
|
||||
in.read((char*)&tmp, 4);
|
||||
return tmp;
|
||||
}
|
||||
|
||||
inline uint16 readLE16(uchar *addr) { return (uint16)addr[1] << 8 | addr[0]; }
|
||||
inline uint32 readLE32(uchar *addr) { return (uint32)readLE16(addr + 2) << 16 | readLE16(addr); }
|
||||
inline uint64 readLE64(uchar *addr) { return (uint64)readLE32(addr + 4) << 32 | readLE32(addr); }
|
||||
|
||||
inline void writeLE16(uchar *addr, uint16 value) { addr[1] = value >> 8; addr[0] = (uint8)value; }
|
||||
inline void writeLE32(uchar *addr, uint32 value) { writeLE16(addr + 2, value >> 16); writeLE16(addr, (uint16)value); }
|
||||
inline void writeLE64(uchar *addr, uint64 value) { writeLE16(addr + 4, value >> 32); writeLE32(addr, (uint32)value); }
|
||||
|
||||
// fills an ostream by concatenating args
|
||||
inline void fillOstream(std::ostringstream&) { }
|
||||
template<class T, class... Args>
|
||||
void fillOstream(std::ostringstream& stream, const T& first, const Args&... rest) {
|
||||
stream << first;
|
||||
fillOstream(stream, rest...);
|
||||
}
|
||||
|
||||
// makes a std::string by concatenating args
|
||||
template<class... T>
|
||||
std::string mkstr(const T&... args) {
|
||||
std::ostringstream buf;
|
||||
fillOstream(buf, args...);
|
||||
return buf.str();
|
||||
}
|
||||
|
||||
// throw a generic expcetion
|
||||
template<typename... T>
|
||||
void throwException(const T&... args) {
|
||||
throw Exception(mkstr(args...));
|
||||
}
|
||||
|
||||
// used by dumper
|
||||
struct dump_util {
|
||||
~dump_util() { std::cout << std::endl; }
|
||||
template<class T>
|
||||
dump_util& operator<<(const T& v) {
|
||||
std::cout << v << " ";
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
|
||||
// utility for dumping variables
|
||||
struct dumper {
|
||||
dumper() { }
|
||||
template<class T>
|
||||
dump_util operator<<(const T& v) const {
|
||||
dump_util d;
|
||||
d << v;
|
||||
return d;
|
||||
}
|
||||
};
|
||||
|
||||
// utility for printing messages into stdout
|
||||
template<class... T>
|
||||
void print(const T&... args) {
|
||||
std::ostringstream buf;
|
||||
fillOstream(buf, args...);
|
||||
std::cout << buf.str();
|
||||
}
|
||||
|
||||
template<class... T>
|
||||
void println(const T&... args) {
|
||||
print(args...);
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
// useful std::string version of sprintf :)
|
||||
template<typename... Args>
|
||||
std::string formatString(const std::string& format, Args... args) {
|
||||
int n, size = 1024;
|
||||
std::string str;
|
||||
while(true) {
|
||||
str.resize(size);
|
||||
n = snprintf(&str[0], size, format.c_str(), args...);
|
||||
assert(n != -1);
|
||||
if(n < size) {
|
||||
str.resize(n);
|
||||
return str;
|
||||
}
|
||||
size *= 2;
|
||||
}
|
||||
}
|
||||
|
||||
// demangle names for GNU g++ compiler
|
||||
inline std::string demangleName(const char* name) {
|
||||
size_t len;
|
||||
int status;
|
||||
std::string ret;
|
||||
char* demangled = abi::__cxa_demangle(name, 0, &len, &status);
|
||||
if(demangled) {
|
||||
ret = demangled;
|
||||
free(demangled);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
// returns the name of a type
|
||||
template<typename T>
|
||||
std::string demangleType() {
|
||||
return demangleName(typeid(T).name());
|
||||
}
|
||||
|
||||
// cast a type to another type
|
||||
template<typename T, typename R>
|
||||
bool cast(const T& in, R& out) {
|
||||
std::stringstream ss;
|
||||
ss << in;
|
||||
ss >> out;
|
||||
return !!ss && ss.eof();
|
||||
}
|
||||
|
||||
// cast a type to string
|
||||
template<typename T>
|
||||
bool cast(const T& in, std::string& out) {
|
||||
std::stringstream ss;
|
||||
ss << in;
|
||||
out = ss.str();
|
||||
return true;
|
||||
}
|
||||
|
||||
// cast string to string
|
||||
template<>
|
||||
inline bool cast(const std::string& in, std::string& out) {
|
||||
out = in;
|
||||
return true;
|
||||
}
|
||||
|
||||
// special cast from string to boolean
|
||||
template<>
|
||||
inline bool cast(const std::string& in, bool& b) {
|
||||
static std::string validNames[2][4] = {{"true","yes","on","1"}, {"false","no","off","0"}};
|
||||
bool ret = false;
|
||||
for(int i=0;i<4;++i) {
|
||||
if(in == validNames[0][i]) {
|
||||
b = true;
|
||||
ret = true;
|
||||
break;
|
||||
} else if(in == validNames[1][i]) {
|
||||
b = false;
|
||||
ret = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
// special cast from boolean to string
|
||||
template<>
|
||||
inline bool cast(const bool& in, std::string& out) {
|
||||
out = (in ? "true" : "false");
|
||||
return true;
|
||||
}
|
||||
|
||||
// used by safe_cast
|
||||
class cast_exception : public Exception {
|
||||
public:
|
||||
virtual ~cast_exception() throw() { }
|
||||
template<class T, class R>
|
||||
void setWhat() {
|
||||
m_what = mkstr("failed to cast value of type '", demangleType<T>(),
|
||||
"' to type '", demangleType<R>(), "'");
|
||||
}
|
||||
virtual const char* what() { return m_what.c_str(); }
|
||||
private:
|
||||
std::string m_what;
|
||||
};
|
||||
|
||||
// cast a type to another type, any error throws a cast_exception
|
||||
template<typename R, typename T>
|
||||
R safeCast(const T& t) {
|
||||
R r;
|
||||
if(!cast(t, r)) {
|
||||
cast_exception e;
|
||||
e.setWhat<T,R>();
|
||||
throw e;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
// cast a type to another type, cast errors are ignored
|
||||
template<typename R, typename T>
|
||||
R unsafeCast(const T& t, R def = R()) {
|
||||
try {
|
||||
return safeCast<R,T>(t);
|
||||
} catch(cast_exception& e) {
|
||||
println("CAST ERROR: ", e.what());
|
||||
return def;
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
std::string tostring(const T& t) {
|
||||
return unsafeCast<std::string, T>(t);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T fromstring(const std::string& str, T def = T()) {
|
||||
return unsafeCast<T, std::string>(str, def);
|
||||
}
|
||||
|
||||
inline std::string dec2hex(unsigned int num) {
|
||||
std::string str;
|
||||
std::ostringstream o;
|
||||
o << std::hex << num;
|
||||
str = o.str();
|
||||
return str;
|
||||
}
|
||||
|
||||
inline unsigned int hex2dec(const std::string& str) {
|
||||
unsigned int num;
|
||||
std::istringstream i(str);
|
||||
i >> std::hex >> num;
|
||||
return num;
|
||||
}
|
||||
|
||||
inline std::string ip2str(uint32 ip) {
|
||||
char host[16];
|
||||
sprintf(host, "%d.%d.%d.%d", (uint8)ip, (uint8)(ip >> 8), (uint8)(ip >> 16), (uint8)(ip >> 24));
|
||||
return std::string(host);
|
||||
}
|
||||
|
||||
template<typename T = std::string>
|
||||
std::vector<T> split(const std::string& str, const std::string& separators = " ") {
|
||||
std::vector<std::string> splitted;
|
||||
boost::split(splitted, str, boost::is_any_of(std::string(separators)));
|
||||
std::vector<T> results(splitted.size());
|
||||
for(uint i=0;i<splitted.size();++i)
|
||||
results[i] = safeCast<T>(splitted[i]);
|
||||
return results;
|
||||
}
|
||||
|
||||
inline std::string resolvePath(const std::string& file, std::string sourcePath) {
|
||||
if(boost::starts_with(file, "/"))
|
||||
return file;
|
||||
if(!boost::ends_with(sourcePath, "/")) {
|
||||
std::size_t slashPos = sourcePath.find_last_of("/");
|
||||
if(slashPos == std::string::npos)
|
||||
throwException("invalid source path '", sourcePath, "' for file '", file, "'");
|
||||
sourcePath = sourcePath.substr(0, slashPos + 1);
|
||||
}
|
||||
return sourcePath + file;
|
||||
}
|
||||
|
||||
inline std::string dateTimeString() {
|
||||
char date[32];
|
||||
std::time_t tnow;
|
||||
std::time(&tnow);
|
||||
std::tm *ts = std::localtime(&tnow);
|
||||
std::strftime(date, 32, "%b %d %Y %H:%M:%S", ts);
|
||||
return std::string(date);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T randomRange(T min, T max);
|
||||
|
||||
template<>
|
||||
inline int randomRange<int>(int min, int max) {
|
||||
static std::random_device rd;
|
||||
static std::mt19937 gen(rd());
|
||||
static std::uniform_int_distribution<int> dis(0, 2147483647);
|
||||
return min + (dis(gen) % (max - min + 1));
|
||||
}
|
||||
|
||||
template<>
|
||||
inline float randomRange<float>(float min, float max) {
|
||||
static std::random_device rd;
|
||||
static std::mt19937 gen(rd());
|
||||
static std::uniform_real_distribution<float> dis(0.0, 1.0);
|
||||
return min + (max - min)*dis(gen);
|
||||
}
|
||||
|
||||
inline uint32 getAdlerChecksum(uint8* buffer, uint16 size) {
|
||||
register uint32 a = 1, b = 0, tlen;
|
||||
while(size > 0) {
|
||||
tlen = size > 5552 ? 5552 : size;
|
||||
size -= tlen;
|
||||
do {
|
||||
a += *buffer++;
|
||||
b += a;
|
||||
} while (--tlen);
|
||||
|
||||
a %= 65521;
|
||||
b %= 65521;
|
||||
}
|
||||
return (b << 16) | a;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// shortcut for Fw::dump
|
||||
const static Fw::dumper dump;
|
||||
|
||||
#endif
|
@@ -1,60 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2010-2012 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 TYPES_H
|
||||
#define TYPES_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <functional>
|
||||
|
||||
// easy handwriting types
|
||||
typedef unsigned char uchar;
|
||||
typedef unsigned short ushort;
|
||||
typedef unsigned int uint;
|
||||
typedef unsigned long ulong;
|
||||
typedef uint64_t uint64;
|
||||
typedef uint32_t uint32;
|
||||
typedef uint16_t uint16;
|
||||
typedef uint8_t uint8;
|
||||
typedef int64_t int64;
|
||||
typedef int32_t int32;
|
||||
typedef int16_t int16;
|
||||
typedef int8_t int8;
|
||||
|
||||
// note that on 32 bit platforms the max ticks will overflow for values above 2,147,483,647
|
||||
// thus this means that the app may cause unknown behavior after running 24 days without restarting
|
||||
typedef long ticks_t;
|
||||
|
||||
typedef std::function<void()> SimpleCallback;
|
||||
|
||||
// boolean with default value initializer
|
||||
template<bool def>
|
||||
struct Boolean {
|
||||
Boolean() : v(def) { }
|
||||
operator bool &() { return v; }
|
||||
operator bool const &() const { return v; }
|
||||
bool& operator=(const bool& o) { v = o; return v; }
|
||||
private:
|
||||
bool v;
|
||||
};
|
||||
|
||||
#endif
|
@@ -1,58 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2010-2012 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 "utf8.h"
|
||||
|
||||
#include <string>
|
||||
#include <cstring>
|
||||
|
||||
char Fw::utf8CharToLatin1(uchar *utf8, int *read)
|
||||
{
|
||||
char c = '?';
|
||||
uchar opt1 = utf8[0];
|
||||
*read = 1;
|
||||
if(opt1 == 0xc3) {
|
||||
*read = 2;
|
||||
uchar opt2 = utf8[1];
|
||||
c = 64 + opt2;
|
||||
} else if(opt1 == 0xc2) {
|
||||
*read = 2;
|
||||
uchar opt2 = utf8[1];
|
||||
if(opt2 > 0xa1 && opt2 < 0xbb)
|
||||
c = opt2;
|
||||
} else if(opt1 < 0xc2) {
|
||||
c = opt1;
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
std::string Fw::utf8StringToLatin1(uchar *utf8) {
|
||||
std::string out;
|
||||
int len = strlen((char*)utf8);
|
||||
for(int i=0; i<len;) {
|
||||
int read = 0;
|
||||
uchar *utf8char = &utf8[i];
|
||||
out += Fw::utf8CharToLatin1(utf8char, &read);
|
||||
i += read;
|
||||
}
|
||||
return out;
|
||||
}
|
Reference in New Issue
Block a user