mkxp-freebird/src/etc-internal.h

229 lines
3.6 KiB
C
Raw Normal View History

2013-09-01 14:27:21 +00:00
/*
** etc-internal.h
**
** This file is part of mkxp.
**
** Copyright (C) 2013 Jonas Kulla <Nyocurio@gmail.com>
**
** mkxp is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 2 of the License, or
** (at your option) any later version.
**
** mkxp is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with mkxp. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef ETC_TYPES_H
#define ETC_TYPES_H
#include "util.h"
#include <SDL_rect.h>
2013-09-01 14:27:21 +00:00
struct Vec2
{
float x, y;
Vec2()
: x(0), y(0)
{}
Vec2(float x, float y)
: x(x), y(y)
{}
bool operator==(const Vec2 &other) const
{
return (x == other.x && y == other.y);
}
};
struct Vec4
{
float x, y, z, w;
Vec4()
: x(0), y(0), z(0), w(0)
{}
Vec4(float x, float y, float z, float w)
: x(x), y(y), z(z), w(w)
{}
bool operator==(const Vec4 &other) const
{
return (x == other.x && y == other.y && z == other.z && w == other.w);
}
};
struct Vec2i
{
int x, y;
Vec2i()
: x(0), y(0)
{}
Vec2i(int x, int y)
: x(x), y(y)
{}
bool operator==(const Vec2i &other) const
2013-09-01 14:27:21 +00:00
{
return x == other.x && y == other.y;
}
2013-09-10 02:25:58 +00:00
Vec2i &operator+=(const Vec2i &other)
{
x += other.x;
y += other.y;
return *this;
}
2013-09-10 02:25:58 +00:00
operator Vec2() const
{
return Vec2(x, y);
}
2013-09-01 14:27:21 +00:00
};
struct IntRect
{
int x, y, w, h;
IntRect()
: x(0), y(0), w(0), h(0)
{}
IntRect(int x, int y, int w, int h)
: x(x), y(y), w(w), h(h)
{}
bool operator==(const IntRect &other) const
{
return (x == other.x && y == other.y &&
w == other.w && h == other.h);
}
Vec2i pos() const
{
return Vec2i(x, y);
}
Vec2i size() const
{
return Vec2i(w, h);
}
operator SDL_Rect() const
{
SDL_Rect r = { x, y, w, h };
return r;
}
2013-09-01 14:27:21 +00:00
};
struct StaticRect { float x, y, w, h; };
struct FloatRect
{
float x, y, w, h;
FloatRect()
: x(0), y(0), w(0), h(0)
{}
FloatRect(float x, float y, float w, float h)
: x(x), y(y), w(w), h(h)
{}
FloatRect(const StaticRect &d)
: x(d.x), y(d.y), w(d.w), h(d.h)
{}
FloatRect(const IntRect &r)
: x(r.x), y(r.y), w(r.w), h(r.h)
{}
operator IntRect() const
{
return IntRect(x, y, w, h);
}
2013-09-01 14:27:21 +00:00
Vec2 topLeft() const { return Vec2(x, y); }
Vec2 bottomLeft() const { return Vec2(x, y+h); }
Vec2 topRight() const { return Vec2(x+w, y); }
Vec2 bottomRight() const { return Vec2(x+w, y+h); }
void shrinkHalf()
{
x += 0.5;
y += 0.5;
w -= 1.0;
h -= 1.0;
}
FloatRect vFlipped() const
{
return FloatRect(x, y+h, w, -h);
}
FloatRect hFlipped() const
{
return FloatRect(x+w, y, -w, h);
}
Vec2 corner(int i) const
{
switch (i)
{
case 0 : return topLeft();
case 1 : return topRight();
case 2 : return bottomRight();
case 3 : return bottomLeft();
default : return Vec2();
}
}
};
/* Value between 0 and 255 with internal
* normalized representation */
struct NormValue
{
int unNorm;
float norm;
NormValue()
: unNorm(0),
norm(0)
{}
NormValue(int unNorm)
: unNorm(unNorm),
norm(unNorm / 255.0)
{}
void operator =(int value)
{
2013-09-03 13:31:29 +00:00
unNorm = clamp(value, 0, 255);
2013-09-01 14:27:21 +00:00
norm = unNorm / 255.0;
}
bool operator ==(int value) const
{
return unNorm == clamp(value, 0, 255);
}
2013-09-01 14:27:21 +00:00
operator int() const
{
return unNorm;
}
};
#endif // ETC_TYPES_H