replaced log.h with common.h, added easier shorthand for int types

This commit is contained in:
Noel Berry
2021-03-20 17:33:04 -07:00
parent 9f9ed08007
commit d73241e8fe
58 changed files with 416 additions and 408 deletions

View File

@ -43,16 +43,6 @@ Vec2 Calc::approach(const Vec2& t, const Vec2& target, float delta)
return t + (target - t).normal() * delta;
}
float Calc::clamp(float t, float min, float max)
{
return t < min ? min : (t > max ? max : t);
}
int Calc::clamp_int(int t, int min, int max)
{
return t < min ? min : (t > max ? max : t);
}
float Calc::map(float t, float old_min, float old_max, float new_min, float new_max)
{
return new_min + ((t - old_min) / (old_max - old_min)) * (new_max - new_min);

View File

@ -10,21 +10,21 @@ Color::Color()
: r(0), g(0), b(0), a(0) {}
Color::Color(int rgb) :
r((uint8_t)((rgb & 0xFF0000) >> 16)),
g((uint8_t)((rgb & 0x00FF00) >> 8)),
b((uint8_t)(rgb & 0x0000FF)),
r((u8)((rgb & 0xFF0000) >> 16)),
g((u8)((rgb & 0x00FF00) >> 8)),
b((u8)(rgb & 0x0000FF)),
a(255) {}
Color::Color(int rgb, float alpha) :
r((int)(((uint8_t)((rgb & 0xFF0000) >> 16)) * alpha)),
g((int)(((uint8_t)((rgb & 0x00FF00) >> 8)) * alpha)),
b((int)(((uint8_t)(rgb & 0x0000FF)) * alpha)),
r((int)(((u8)((rgb & 0xFF0000) >> 16)) * alpha)),
g((int)(((u8)((rgb & 0x00FF00) >> 8)) * alpha)),
b((int)(((u8)(rgb & 0x0000FF)) * alpha)),
a((int)(255 * alpha)) {}
Color::Color(uint8_t r, uint8_t g, uint8_t b)
Color::Color(u8 r, u8 g, u8 b)
: r(r), g(g), b(b), a(255) {}
Color::Color(uint8_t r, uint8_t g, uint8_t b, uint8_t a)
Color::Color(u8 r, u8 g, u8 b, u8 a)
: r(r), g(g), b(b), a(a) {}
Color::Color(const Vec4& vec4)
@ -60,13 +60,13 @@ void Color::premultiply()
b = b * a / 255;
}
uint32_t Color::to_rgba() const
u32 Color::to_rgba() const
{
return
((uint32_t)r << 24) |
((uint32_t)g << 16) |
((uint32_t)b << 8) |
(uint32_t)a;
((u32)r << 24) |
((u32)g << 16) |
((u32)b << 8) |
(u32)a;
}
Vec4 Color::to_vec4() const
@ -110,24 +110,24 @@ String Color::to_hex_rgb() const
return str;
}
Color Color::from_rgba(uint32_t value)
Color Color::from_rgba(u32 value)
{
return
{
(uint8_t)((value & 0xFF000000) >> 24),
(uint8_t)((value & 0x00FF0000) >> 16),
(uint8_t)((value & 0x0000FF00) >> 8),
(uint8_t)((value & 0x000000FF))
(u8)((value & 0xFF000000) >> 24),
(u8)((value & 0x00FF0000) >> 16),
(u8)((value & 0x0000FF00) >> 8),
(u8)((value & 0x000000FF))
};
}
Color Color::from_rgb(uint32_t value)
Color Color::from_rgb(u32 value)
{
return
{
(uint8_t)((value & 0xFF0000) >> 16),
(uint8_t)((value & 0x00FF00) >> 8),
(uint8_t)((value & 0x0000FF))
(u8)((value & 0xFF0000) >> 16),
(u8)((value & 0x00FF00) >> 8),
(u8)((value & 0x0000FF))
};
}
@ -137,10 +137,10 @@ Color Color::lerp(Color a, Color b, float amount)
if (amount > 1) amount = 1;
return Color(
(uint8_t)(a.r + (b.r - a.r) * amount),
(uint8_t)(a.g + (b.g - a.g) * amount),
(uint8_t)(a.b + (b.b - a.b) * amount),
(uint8_t)(a.a + (b.a - a.a) * amount)
(u8)(a.r + (b.r - a.r) * amount),
(u8)(a.g + (b.g - a.g) * amount),
(u8)(a.b + (b.b - a.b) * amount),
(u8)(a.a + (b.a - a.a) * amount)
);
}
@ -155,9 +155,9 @@ Color Color::operator*(float multiply) const
Color& Color::operator=(const int rgb)
{
r = (uint8_t)((rgb & 0xFF0000) >> 16);
g = (uint8_t)((rgb & 0x00FF00) >> 8);
b = (uint8_t)(rgb & 0x0000FF);
r = (u8)((rgb & 0xFF0000) >> 16);
g = (u8)((rgb & 0x00FF00) >> 8);
b = (u8)(rgb & 0x0000FF);
a = 255;
return *this;
}

View File

@ -31,7 +31,7 @@ Vec2 Line::closest_point(const Vec2& pt) const
Vec2 v = b - a;
Vec2 w = pt - a;
float t = Vec2::dot(w, v) / Vec2::dot(v, v);
t = Calc::clamp(t, 0, 1);
t = Calc::clamp(t, 0.0f, 1.0f);
return v * t + a;
}

View File

@ -1,5 +1,5 @@
#include <blah/math/mat4x4.h>
#include <blah/core/log.h>
#include <blah/core/common.h>
using namespace Blah;

View File

@ -14,13 +14,13 @@ void Stopwatch::reset()
start_time = std::chrono::duration_cast<std::chrono::microseconds>(system_clock::now().time_since_epoch()).count();
}
uint64_t Stopwatch::milliseconds()
u64 Stopwatch::milliseconds()
{
return microseconds() / 1000;
}
uint64_t Stopwatch::microseconds()
u64 Stopwatch::microseconds()
{
return std::chrono::duration_cast<std::chrono::microseconds>(system_clock::now().time_since_epoch()).count() - start_time;
}