Begin move to SDL
This commit is contained in:
145
src/basic/math.cpp
Normal file
145
src/basic/math.cpp
Normal file
@@ -0,0 +1,145 @@
|
||||
struct Vec2 {
|
||||
float x;
|
||||
float y;
|
||||
};
|
||||
|
||||
struct Rect2 {
|
||||
Vec2 min;
|
||||
Vec2 max;
|
||||
};
|
||||
|
||||
Vec2 GetSize(Rect2 r) {
|
||||
Vec2 result = {r.max.x - r.min.x, r.max.y - r.min.y};
|
||||
return result;
|
||||
}
|
||||
|
||||
Vec2 GetSizeF(Rect2I r) {
|
||||
Vec2 result = {(float)(r.max.x - r.min.x), (float)(r.max.y - r.min.y)};
|
||||
return result;
|
||||
}
|
||||
|
||||
Vec2 ToVec2(Vec2I v) { return {(float)v.x, (float)v.y}; }
|
||||
Vec2I ToVec2I(Vec2 v) { return {(Int)v.x, (Int)v.y}; }
|
||||
Rect2I ToRect2I(Rect2 r) { return {ToVec2I(r.min), ToVec2I(r.max)}; }
|
||||
|
||||
// clang-format off
|
||||
Rect2 operator-(Rect2 r, Rect2 value) { return { r.min.x - value.min.x, r.min.y - value.min.y, r.max.x - value.max.x, r.max.y - value.max.y }; }
|
||||
Rect2 operator+(Rect2 r, Rect2 value) { return { r.min.x + value.min.x, r.min.y + value.min.y, r.max.x + value.max.x, r.max.y + value.max.y }; }
|
||||
Rect2 operator*(Rect2 r, Rect2 value) { return { r.min.x * value.min.x, r.min.y * value.min.y, r.max.x * value.max.x, r.max.y * value.max.y }; }
|
||||
Rect2 operator/(Rect2 r, Rect2 value) { return { r.min.x / value.min.x, r.min.y / value.min.y, r.max.x / value.max.x, r.max.y / value.max.y }; }
|
||||
|
||||
Rect2 operator-(Rect2 r, Vec2 value) { return { r.min.x - value.x, r.min.y - value.y, r.max.x - value.x, r.max.y - value.y }; }
|
||||
Rect2 operator+(Rect2 r, Vec2 value) { return { r.min.x + value.x, r.min.y + value.y, r.max.x + value.x, r.max.y + value.y }; }
|
||||
Rect2 operator*(Rect2 r, Vec2 value) { return { r.min.x * value.x, r.min.y * value.y, r.max.x * value.x, r.max.y * value.y }; }
|
||||
Rect2 operator/(Rect2 r, Vec2 value) { return { r.min.x / value.x, r.min.y / value.y, r.max.x / value.x, r.max.y / value.y }; }
|
||||
|
||||
Rect2 operator-(Rect2 r, float value) { return { r.min.x - value, r.min.y - value, r.max.x - value, r.max.y - value }; }
|
||||
Rect2 operator+(Rect2 r, float value) { return { r.min.x + value, r.min.y + value, r.max.x + value, r.max.y + value }; }
|
||||
Rect2 operator*(Rect2 r, float value) { return { r.min.x * value, r.min.y * value, r.max.x * value, r.max.y * value }; }
|
||||
Rect2 operator/(Rect2 r, float value) { return { r.min.x / value, r.min.y / value, r.max.x / value, r.max.y / value }; }
|
||||
|
||||
Rect2 operator-=(Rect2 &r, Rect2 value) { r = r - value; return r; }
|
||||
Rect2 operator+=(Rect2 &r, Rect2 value) { r = r + value; return r; }
|
||||
Rect2 operator*=(Rect2 &r, Rect2 value) { r = r * value; return r; }
|
||||
Rect2 operator/=(Rect2 &r, Rect2 value) { r = r / value; return r; }
|
||||
Rect2 operator-=(Rect2 &r, Vec2 value) { r = r - value; return r; }
|
||||
Rect2 operator+=(Rect2 &r, Vec2 value) { r = r + value; return r; }
|
||||
Rect2 operator*=(Rect2 &r, Vec2 value) { r = r * value; return r; }
|
||||
Rect2 operator/=(Rect2 &r, Vec2 value) { r = r / value; return r; }
|
||||
Rect2 operator-=(Rect2 &r, float value) { r = r - value; return r; }
|
||||
Rect2 operator+=(Rect2 &r, float value) { r = r + value; return r; }
|
||||
Rect2 operator*=(Rect2 &r, float value) { r = r * value; return r; }
|
||||
Rect2 operator/=(Rect2 &r, float value) { r = r / value; return r; }
|
||||
|
||||
Vec2 operator-(Vec2 a, Vec2 b) { return {a.x - b.x, a.y - b.y}; }
|
||||
Vec2 operator+(Vec2 a, Vec2 b) { return {a.x + b.x, a.y + b.y}; }
|
||||
Vec2 operator*(Vec2 a, Vec2 b) { return {a.x * b.x, a.y * b.y}; }
|
||||
Vec2 operator/(Vec2 a, Vec2 b) { return {a.x / b.x, a.y / b.y}; }
|
||||
|
||||
Vec2 operator-(Vec2 a, float b) { return {a.x - b, a.y - b}; }
|
||||
Vec2 operator+(Vec2 a, float b) { return {a.x + b, a.y + b}; }
|
||||
Vec2 operator*(Vec2 a, float b) { return {a.x * b, a.y * b}; }
|
||||
Vec2 operator/(Vec2 a, float b) { return {a.x / b, a.y / b}; }
|
||||
|
||||
Vec2 operator-=(Vec2 &a, Vec2 b) { a = a - b; return a; }
|
||||
Vec2 operator+=(Vec2 &a, Vec2 b) { a = a + b; return a; }
|
||||
Vec2 operator*=(Vec2 &a, Vec2 b) { a = a * b; return a; }
|
||||
Vec2 operator/=(Vec2 &a, Vec2 b) { a = a / b; return a; }
|
||||
Vec2 operator-=(Vec2 &a, float b) { a = a - b; return a; }
|
||||
Vec2 operator+=(Vec2 &a, float b) { a = a + b; return a; }
|
||||
Vec2 operator*=(Vec2 &a, float b) { a = a * b; return a; }
|
||||
Vec2 operator/=(Vec2 &a, float b) { a = a / b; return a; }
|
||||
|
||||
// clang-format on
|
||||
|
||||
Rect2 Rect2FromSize(Vec2 pos, Vec2 size) {
|
||||
Rect2 result = {};
|
||||
result.min = pos;
|
||||
result.max = pos + size;
|
||||
return result;
|
||||
}
|
||||
|
||||
Rect2 Shrink(Rect2 result, float v) {
|
||||
result.min.x += v;
|
||||
result.max.x -= v;
|
||||
result.min.y += v;
|
||||
result.max.y -= v;
|
||||
return result;
|
||||
}
|
||||
|
||||
Vec2 GetMid(Rect2 r) {
|
||||
Vec2 size = GetSize(r);
|
||||
size.x /= 2.f;
|
||||
size.y /= 2.f;
|
||||
Vec2 result = r.min + size;
|
||||
return result;
|
||||
}
|
||||
|
||||
Rect2 CutLeft(Rect2 *r, float value) {
|
||||
float minx = r->min.x;
|
||||
r->min.x = Min(r->min.x + value, r->max.x);
|
||||
Rect2 result = {
|
||||
{ minx, r->min.y},
|
||||
{r->min.x, r->max.y}
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
Rect2 CutRight(Rect2 *r, float value) {
|
||||
float maxx = r->max.x;
|
||||
r->max.x = Max(r->min.x, r->max.x - value);
|
||||
Rect2 result = {
|
||||
{r->max.x, r->min.y},
|
||||
{ maxx, r->max.y},
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
// Y is up
|
||||
Rect2 CutBottom(Rect2 *r, float value) {
|
||||
float maxy = r->max.y;
|
||||
r->max.y = Max(r->min.y, r->max.y - value);
|
||||
Rect2 result = {
|
||||
{r->min.x, r->max.y},
|
||||
{r->max.x, maxy},
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
// Y is up
|
||||
Rect2 CutTop(Rect2 *r, float value) {
|
||||
float miny = r->min.y;
|
||||
r->min.y = Min(r->min.y + value, r->max.y);
|
||||
Rect2 result = {
|
||||
{r->min.x, miny},
|
||||
{r->max.x, r->min.y},
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
float SafeDivide(float a, float b) {
|
||||
if (b == 0.f) {
|
||||
return 0.f;
|
||||
}
|
||||
return a / b;
|
||||
}
|
||||
136
src/basic/math_int.cpp
Normal file
136
src/basic/math_int.cpp
Normal file
@@ -0,0 +1,136 @@
|
||||
struct Vec2I {
|
||||
Int x;
|
||||
Int y;
|
||||
};
|
||||
|
||||
struct Rect2I {
|
||||
Vec2I min;
|
||||
Vec2I max;
|
||||
};
|
||||
|
||||
Vec2I GetSize(Rect2I r) {
|
||||
Vec2I result = {r.max.x - r.min.x, r.max.y - r.min.y};
|
||||
return result;
|
||||
}
|
||||
|
||||
// clang-format off
|
||||
Rect2I operator-(Rect2I r, Rect2I value) { return { r.min.x - value.min.x, r.min.y - value.min.y, r.max.x - value.max.x, r.max.y - value.max.y }; }
|
||||
Rect2I operator+(Rect2I r, Rect2I value) { return { r.min.x + value.min.x, r.min.y + value.min.y, r.max.x + value.max.x, r.max.y + value.max.y }; }
|
||||
Rect2I operator*(Rect2I r, Rect2I value) { return { r.min.x * value.min.x, r.min.y * value.min.y, r.max.x * value.max.x, r.max.y * value.max.y }; }
|
||||
Rect2I operator/(Rect2I r, Rect2I value) { return { r.min.x / value.min.x, r.min.y / value.min.y, r.max.x / value.max.x, r.max.y / value.max.y }; }
|
||||
|
||||
Rect2I operator-(Rect2I r, Vec2I value) { return { r.min.x - value.x, r.min.y - value.y, r.max.x - value.x, r.max.y - value.y }; }
|
||||
Rect2I operator+(Rect2I r, Vec2I value) { return { r.min.x + value.x, r.min.y + value.y, r.max.x + value.x, r.max.y + value.y }; }
|
||||
Rect2I operator*(Rect2I r, Vec2I value) { return { r.min.x * value.x, r.min.y * value.y, r.max.x * value.x, r.max.y * value.y }; }
|
||||
Rect2I operator/(Rect2I r, Vec2I value) { return { r.min.x / value.x, r.min.y / value.y, r.max.x / value.x, r.max.y / value.y }; }
|
||||
|
||||
Rect2I operator-(Rect2I r, Int value) { return { r.min.x - value, r.min.y - value, r.max.x - value, r.max.y - value }; }
|
||||
Rect2I operator+(Rect2I r, Int value) { return { r.min.x + value, r.min.y + value, r.max.x + value, r.max.y + value }; }
|
||||
Rect2I operator*(Rect2I r, Int value) { return { r.min.x * value, r.min.y * value, r.max.x * value, r.max.y * value }; }
|
||||
Rect2I operator/(Rect2I r, Int value) { return { r.min.x / value, r.min.y / value, r.max.x / value, r.max.y / value }; }
|
||||
|
||||
Rect2I operator-=(Rect2I &r, Rect2I value) { r = r - value; return r; }
|
||||
Rect2I operator+=(Rect2I &r, Rect2I value) { r = r + value; return r; }
|
||||
Rect2I operator*=(Rect2I &r, Rect2I value) { r = r * value; return r; }
|
||||
Rect2I operator/=(Rect2I &r, Rect2I value) { r = r / value; return r; }
|
||||
Rect2I operator-=(Rect2I &r, Vec2I value) { r = r - value; return r; }
|
||||
Rect2I operator+=(Rect2I &r, Vec2I value) { r = r + value; return r; }
|
||||
Rect2I operator*=(Rect2I &r, Vec2I value) { r = r * value; return r; }
|
||||
Rect2I operator/=(Rect2I &r, Vec2I value) { r = r / value; return r; }
|
||||
Rect2I operator-=(Rect2I &r, Int value) { r = r - value; return r; }
|
||||
Rect2I operator+=(Rect2I &r, Int value) { r = r + value; return r; }
|
||||
Rect2I operator*=(Rect2I &r, Int value) { r = r * value; return r; }
|
||||
Rect2I operator/=(Rect2I &r, Int value) { r = r / value; return r; }
|
||||
|
||||
Vec2I operator-(Vec2I a, Vec2I b) { return {a.x - b.x, a.y - b.y}; }
|
||||
Vec2I operator+(Vec2I a, Vec2I b) { return {a.x + b.x, a.y + b.y}; }
|
||||
Vec2I operator*(Vec2I a, Vec2I b) { return {a.x * b.x, a.y * b.y}; }
|
||||
Vec2I operator/(Vec2I a, Vec2I b) { return {a.x / b.x, a.y / b.y}; }
|
||||
|
||||
Vec2I operator-(Vec2I a, Int b) { return {a.x - b, a.y - b}; }
|
||||
Vec2I operator+(Vec2I a, Int b) { return {a.x + b, a.y + b}; }
|
||||
Vec2I operator*(Vec2I a, Int b) { return {a.x * b, a.y * b}; }
|
||||
Vec2I operator/(Vec2I a, Int b) { return {a.x / b, a.y / b}; }
|
||||
|
||||
Vec2I operator-=(Vec2I &a, Vec2I b) { a = a - b; return a; }
|
||||
Vec2I operator+=(Vec2I &a, Vec2I b) { a = a + b; return a; }
|
||||
Vec2I operator*=(Vec2I &a, Vec2I b) { a = a * b; return a; }
|
||||
Vec2I operator/=(Vec2I &a, Vec2I b) { a = a / b; return a; }
|
||||
Vec2I operator-=(Vec2I &a, Int b) { a = a - b; return a; }
|
||||
Vec2I operator+=(Vec2I &a, Int b) { a = a + b; return a; }
|
||||
Vec2I operator*=(Vec2I &a, Int b) { a = a * b; return a; }
|
||||
Vec2I operator/=(Vec2I &a, Int b) { a = a / b; return a; }
|
||||
|
||||
// clang-format on
|
||||
|
||||
Rect2I Rect2IFromSize(Vec2I pos, Vec2I size) {
|
||||
Rect2I result = {};
|
||||
result.min = pos;
|
||||
result.max = pos + size;
|
||||
return result;
|
||||
}
|
||||
|
||||
Rect2I Shrink(Rect2I result, Int v) {
|
||||
result.min.x += v;
|
||||
result.max.x -= v;
|
||||
result.min.y += v;
|
||||
result.max.y -= v;
|
||||
return result;
|
||||
}
|
||||
|
||||
Vec2I GetMid(Rect2I r) {
|
||||
Vec2I size = GetSize(r);
|
||||
size.x /= 2;
|
||||
size.y /= 2;
|
||||
Vec2I result = r.min + size;
|
||||
return result;
|
||||
}
|
||||
|
||||
Rect2I CutLeft(Rect2I *r, Int value) {
|
||||
Int minx = r->min.x;
|
||||
r->min.x = Min(r->min.x + value, r->max.x);
|
||||
Rect2I result = {
|
||||
{ minx, r->min.y},
|
||||
{r->min.x, r->max.y}
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
Rect2I CutRight(Rect2I *r, Int value) {
|
||||
Int maxx = r->max.x;
|
||||
r->max.x = Max(r->min.x, r->max.x - value);
|
||||
Rect2I result = {
|
||||
{r->max.x, r->min.y},
|
||||
{ maxx, r->max.y},
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
// Y is up
|
||||
Rect2I CutBottom(Rect2I *r, Int value) {
|
||||
Int maxy = r->max.y;
|
||||
r->max.y = Max(r->min.y, r->max.y - value);
|
||||
Rect2I result = {
|
||||
{r->min.x, r->max.y},
|
||||
{r->max.x, maxy},
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
// Y is up
|
||||
Rect2I CutTop(Rect2I *r, Int value) {
|
||||
Int miny = r->min.y;
|
||||
r->min.y = Min(r->min.y + value, r->max.y);
|
||||
Rect2I result = {
|
||||
{r->min.x, miny},
|
||||
{r->max.x, r->min.y},
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
Int SafeDivide(Int a, Int b) {
|
||||
if (b == 0) {
|
||||
return 0;
|
||||
}
|
||||
return a / b;
|
||||
}
|
||||
110
src/basic/string16.cpp
Normal file
110
src/basic/string16.cpp
Normal file
@@ -0,0 +1,110 @@
|
||||
wchar_t ToLowerCase(wchar_t a) {
|
||||
if (a >= 'A' && a <= 'Z') a += 32;
|
||||
return a;
|
||||
}
|
||||
|
||||
wchar_t ToUpperCase(wchar_t a) {
|
||||
if (a >= 'a' && a <= 'z') a -= 32;
|
||||
return a;
|
||||
}
|
||||
|
||||
bool IsWhitespace(wchar_t w) {
|
||||
bool result = w == '\n' || w == ' ' || w == '\t' || w == '\v' || w == '\r';
|
||||
return result;
|
||||
}
|
||||
|
||||
bool IsSymbol(wchar_t w) {
|
||||
bool result = (w >= '!' && w <= '/') || (w >= ':' && w <= '@') || (w >= '[' && w <= '`') || (w >= '{' && w <= '~');
|
||||
return result;
|
||||
}
|
||||
|
||||
bool IsAlphabetic(wchar_t a) {
|
||||
bool result = (a >= 'a' && a <= 'z') || (a >= 'A' && a <= 'Z');
|
||||
return result;
|
||||
}
|
||||
|
||||
bool IsIdent(wchar_t a) {
|
||||
bool result = (a >= 'a' && a <= 'z') || (a >= 'A' && a <= 'Z') || a == '_';
|
||||
return result;
|
||||
}
|
||||
|
||||
bool IsDigit(wchar_t a) {
|
||||
bool result = a >= '0' && a <= '9';
|
||||
return result;
|
||||
}
|
||||
|
||||
bool IsAlphanumeric(wchar_t a) {
|
||||
bool result = IsDigit(a) || IsAlphabetic(a);
|
||||
return result;
|
||||
}
|
||||
|
||||
bool AreEqual(String16 a, String16 b, unsigned ignore_case = false) {
|
||||
if (a.len != b.len) return false;
|
||||
for (int64_t i = 0; i < a.len; i++) {
|
||||
wchar_t A = a.data[i];
|
||||
wchar_t B = b.data[i];
|
||||
if (ignore_case) {
|
||||
A = ToLowerCase(A);
|
||||
B = ToLowerCase(B);
|
||||
}
|
||||
if (A != B)
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
inline bool operator==(String16 a, String16 b) { return AreEqual(a, b); }
|
||||
inline bool operator!=(String16 a, String16 b) { return !AreEqual(a, b); }
|
||||
|
||||
String16 Merge(Allocator allocator, Array<String16> list, String16 separator = " ") {
|
||||
int64_t char_count = 0;
|
||||
For(list) char_count += it.len;
|
||||
if (char_count == 0) return {};
|
||||
int64_t node_count = list.len;
|
||||
|
||||
int64_t base_size = (char_count + 1);
|
||||
int64_t sep_size = (node_count - 1) * separator.len;
|
||||
int64_t size = base_size + sep_size;
|
||||
wchar_t *buff = (wchar_t *)AllocSize(allocator, sizeof(wchar_t) * (size + 1));
|
||||
String16 string = {buff, 0};
|
||||
For(list) {
|
||||
Assert(string.len + it.len <= size);
|
||||
memcpy(string.data + string.len, it.data, it.len * sizeof(wchar_t));
|
||||
string.len += it.len;
|
||||
if (!IsLast(list, it)) {
|
||||
memcpy(string.data + string.len, separator.data, separator.len * sizeof(wchar_t));
|
||||
string.len += separator.len;
|
||||
}
|
||||
}
|
||||
Assert(string.len == size - 1);
|
||||
string.data[size] = 0;
|
||||
return string;
|
||||
}
|
||||
|
||||
bool Seek(String16 string, String16 find, int64_t *index_out = NULL, SeekFlag flags = SeekFlag_None) {
|
||||
bool ignore_case = flags & SeekFlag_IgnoreCase ? true : false;
|
||||
bool result = false;
|
||||
if (flags & SeekFlag_MatchFindLast) {
|
||||
for (int64_t i = string.len; i != 0; i--) {
|
||||
int64_t index = i - 1;
|
||||
String16 substring = GetSlice(string, index, index + find.len);
|
||||
if (AreEqual(substring, find, ignore_case)) {
|
||||
if (index_out)
|
||||
*index_out = index;
|
||||
result = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (int64_t i = 0; i < string.len; i++) {
|
||||
String16 substring = GetSlice(string, i, i + find.len);
|
||||
if (AreEqual(substring, find, ignore_case)) {
|
||||
if (index_out)
|
||||
*index_out = i;
|
||||
result = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
Reference in New Issue
Block a user