Skip to content

Commit

Permalink
add raybuff.hpp
Browse files Browse the repository at this point in the history
- Add raybuff.hpp - a header-only (or at least for now) thing to
empover raylib's structs with power of C++ operator overloading.
- Move existing operator overloads from utility.hpp -> raybuff.hpp
- Add operator overloads for operator+ and operator-.
  • Loading branch information
moonburnt committed May 28, 2022
1 parent b2322d1 commit 95af442
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 19 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ add_library(engine STATIC
engine/utility.hpp
engine/storage.cpp
engine/storage.hpp
engine/raybuff.hpp
)

target_include_directories(engine PUBLIC
Expand Down
47 changes: 47 additions & 0 deletions engine/raybuff.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#pragma once

#include "raylib.h"
// For clamp
#include <algorithm>

// Overloads to fuel raylib's C structs with power of C++.
// Header-only for now, coz its kinda smol.

// Equality operator.
bool operator==(const Color& c1, const Color& c2) {
return c1.r == c2.r && c1.g == c2.g && c1.b == c2.b && c1.a == c2.a;
}

bool operator==(const Vector2& v1, const Vector2& v2) {
return v1.x == v2.x && v1.y == v2.y;
}

bool operator==(const Rectangle& r1, const Rectangle& r2) {
return r1.x == r2.x && r1.y == r2.y && r1.width == r2.width && r1.height == r2.height;
}

// Addition operator. Iirc these should always return new objects.
Color operator+(const Color& c1, const Color& c2) {
return {
std::clamp((c1.r + c2.r), 0, 255),
std::clamp((c1.g + c2.g), 0, 255),
std::clamp((c1.b + c2.b), 0, 255),
std::clamp((c1.a + c2.a), 0, 255)};
}

Vector2 operator+(const Vector2& v1, const Vector2& v2) {
return {v1.x + v2.x, v1.y + v2.y};
}

// Subtraction operator.
Color operator-(const Color& c1, const Color& c2) {
return {
std::clamp((c1.r - c2.r), 0, 255),
std::clamp((c1.g - c2.g), 0, 255),
std::clamp((c1.b - c2.b), 0, 255),
std::clamp((c1.a - c2.a), 0, 255)};
}

Vector2 operator-(const Vector2& v1, const Vector2& v2) {
return {v1.x - v2.x, v1.y - v2.y};
}
13 changes: 0 additions & 13 deletions engine/utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,3 @@ void FrameCounter::update() {
void FrameCounter::draw() {
DrawText(TextFormat(format, fps_value), pos.x, pos.y, size, color);
}

// Operator overloads
bool operator==(const Color& c1, const Color& c2) {
return c1.r == c2.r && c1.g == c2.g && c1.b == c2.b && c1.a == c2.a;
}

bool operator==(const Vector2& v1, const Vector2& v2) {
return v1.x == v2.x && v1.y == v2.y;
}

bool operator==(const Rectangle& r1, const Rectangle& r2) {
return r1.x == r2.x && r1.y == r2.y && r1.width == r2.width && r1.height == r2.height;
}
6 changes: 0 additions & 6 deletions engine/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,3 @@ class FrameCounter : public Node {
void update() override;
void draw() override;
};

// Various overloads to default raylib's structs.
// Idk where else to put these.
bool operator==(const Color& c1, const Color& c2);
bool operator==(const Vector2& v1, const Vector2& v2);
bool operator==(const Rectangle& r1, const Rectangle& r2);

0 comments on commit 95af442

Please sign in to comment.