Skip to content

Add constructors and operators #142

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Sep 11, 2021
Merged
2 changes: 1 addition & 1 deletion examples/core/core_world_screen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ int main() {
camera.Update(); // Update camera

// Calculate cube screen space position (with a little offset to be in top)
cubeScreenPosition = GetWorldToScreen((Vector3){cubePosition.x, cubePosition.y + 2.5f, cubePosition.z}, camera);
cubeScreenPosition = GetWorldToScreen(Vector3{cubePosition.x, cubePosition.y + 2.5f, cubePosition.z}, camera);
//----------------------------------------------------------------------------------

// Draw
Expand Down
6 changes: 3 additions & 3 deletions examples/models/models_first_person_maze.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ int main(void)

raylib::Image imMap("resources/cubicmap.png"); // Load cubicmap image (RAM)
raylib::Texture cubicmap(imMap); // Convert image to texture to display (VRAM)
Mesh mesh = raylib::Mesh::Cubicmap(imMap, (Vector3){ 1.0f, 1.0f, 1.0f });
Mesh mesh = raylib::Mesh::Cubicmap(imMap, Vector3{ 1.0f, 1.0f, 1.0f });
raylib::Model model(mesh);

// NOTE: By default each cube is mapped to one part of texture atlas
Expand Down Expand Up @@ -75,7 +75,7 @@ int main(void)
{
if ((mapPixels[y*cubicmap.width + x].r == 255) && // Collision: white pixel, only check R channel
(playerPos.CheckCollisionCircle(playerRadius,
(Rectangle){ mapPosition.x - 0.5f + x*1.0f, mapPosition.z - 0.5f + y*1.0f, 1.0f, 1.0f })))
Rectangle{ mapPosition.x - 0.5f + x*1.0f, mapPosition.z - 0.5f + y*1.0f, 1.0f, 1.0f })))
{
// Collision detected, reset camera position
camera.position = oldCamPos;
Expand All @@ -97,7 +97,7 @@ int main(void)
}
camera.EndMode();

cubicmap.Draw((Vector2){ static_cast<float>(GetScreenWidth() - cubicmap.width*4 - 20), 20 }, 0.0f, 4.0f, WHITE);
cubicmap.Draw(Vector2{ static_cast<float>(GetScreenWidth() - cubicmap.width*4 - 20), 20 }, 0.0f, 4.0f, WHITE);
DrawRectangleLines(GetScreenWidth() - cubicmap.width*4 - 20, 20, cubicmap.width*4, cubicmap.height*4, GREEN);

// Draw player position radar
Expand Down
4 changes: 2 additions & 2 deletions examples/physics/physics_demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ int main(void)

if (needsReset)
{
floor = physics.CreateBodyRectangle((Vector2){ screenWidth/2, screenHeight }, 500, 100, 10);
floor = physics.CreateBodyRectangle(Vector2{ screenWidth/2, screenHeight }, 500, 100, 10);
floor->enabled = false;

circle = physics.CreateBodyCircle((Vector2){ screenWidth/2, screenHeight/2 }, 45, 10);
circle = physics.CreateBodyCircle(Vector2{ screenWidth/2, screenHeight/2 }, 45, 10);
circle->enabled = false;

needsReset = false;
Expand Down
4 changes: 2 additions & 2 deletions examples/text/text_font_loading.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ int main() {

if (!useTtf)
{
fontBm.DrawText(msg, (Vector2){ 20.0f, 100.0f }, fontBm.baseSize, 2, MAROON);
fontBm.DrawText(msg, Vector2{ 20.0f, 100.0f }, fontBm.baseSize, 2, MAROON);
DrawText("Using BMFont (Angelcode) imported", 20, GetScreenHeight() - 30, 20, GRAY);
}
else
{
fontTtf.DrawText(msg, (Vector2){ 20.0f, 100.0f }, fontTtf.baseSize, 2, LIME);
fontTtf.DrawText(msg, Vector2{ 20.0f, 100.0f }, fontTtf.baseSize, 2, LIME);
DrawText("Using TTF font generated", 20, GetScreenHeight() - 30, 20, GRAY);
}
}
Expand Down
29 changes: 29 additions & 0 deletions include/AudioStream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ class AudioStream : public ::AudioStream {
set(InitAudioStream(SampleRate, SampleSize, Channels));
}

AudioStream(const AudioStream&) = delete;

AudioStream(AudioStream&& other) {
set(other);

other.buffer = nullptr;
other.sampleRate = 0;
other.sampleSize = 0;
other.channels = 0;
}

~AudioStream() {
Close();
}
Expand All @@ -35,6 +46,24 @@ class AudioStream : public ::AudioStream {
return *this;
}

AudioStream& operator=(const AudioStream&) = delete;

AudioStream& operator=(AudioStream&& other) {
if (this == &other) {
return *this;
}

Close();
set(other);

other.buffer = nullptr;
other.sampleRate = 0;
other.sampleSize = 0;
other.channels = 0;

return *this;
}

/**
* Update audio stream buffers with data
*/
Expand Down
4 changes: 2 additions & 2 deletions include/Camera3D.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class Camera3D : public ::Camera3D {
* @param projection Camera projection: CAMERA_PERSPECTIVE or CAMERA_ORTHOGRAPHIC
*/
Camera3D(::Vector3 position,
::Vector3 target = (::Vector3){0.0f, 0.0f, 0.0f},
::Vector3 up = (::Vector3){0.0f, 1.0f, 0.0f},
::Vector3 target = ::Vector3{0.0f, 0.0f, 0.0f},
::Vector3 up = ::Vector3{0.0f, 1.0f, 0.0f},
float fovy = 0,
int projection = CAMERA_PERSPECTIVE
) : ::Camera3D{position, target, up, fovy, projection} {}
Expand Down
33 changes: 33 additions & 0 deletions include/Font.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,19 @@ class Font : public ::Font {
charsCount));
}

Font(const Font&) = delete;

Font(Font&& other) {
set(other);

other.baseSize = 0;
other.charsCount = 0;
other.charsPadding = 0;
other.texture = { 0 };
other.recs = nullptr;
other.chars = nullptr;
}

~Font() {
Unload();
}
Expand All @@ -58,6 +71,26 @@ class Font : public ::Font {
return *this;
}

Font& operator=(const Font&) = delete;

Font& operator=(Font&& other) {
if (this == &other) {
return *this;
}

Unload();
set(other);

other.baseSize = 0;
other.charsCount = 0;
other.charsPadding = 0;
other.texture = { 0 };
other.recs = nullptr;
other.chars = nullptr;

return *this;
}

/**
* Draw text using font and additional parameters.
*/
Expand Down
66 changes: 53 additions & 13 deletions include/Image.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,20 @@ class Image : public ::Image {
set(::ImageTextEx(font, text.c_str(), fontSize, spacing, tint));
}

Image(const Image& other) {
set(other.Copy());
}

Image(Image&& other) {
set(other);

other.data = nullptr;
other.width = 0;
other.height = 0;
other.mipmaps = 0;
other.format = 0;
}

static ::Image Text(const std::string& text, int fontSize,
::Color color = {255, 255, 255, 255}) {
return ::ImageText(text.c_str(), fontSize, color);
Expand Down Expand Up @@ -132,6 +146,32 @@ class Image : public ::Image {
return *this;
}

Image& operator=(const Image& other) {
if (this == &other) {
return *this;
}

Unload();
set(other.Copy());
}

Image& operator=(Image&& other) {
if (this == &other) {
return *this;
}

Unload();
set(other);

other.data = nullptr;
other.width = 0;
other.height = 0;
other.mipmaps = 0;
other.format = 0;

return *this;
}

/**
* Load image from file into CPU memory (RAM)
*/
Expand Down Expand Up @@ -167,24 +207,24 @@ class Image : public ::Image {
* Unload image from CPU memory (RAM)
*/
inline void Unload() {
if (data != NULL) {
if (data != nullptr) {
::UnloadImage(*this);
data = NULL;
data = nullptr;
}
}

/**
* Export image data to file, returns true on success
*/
inline bool Export(const std::string& fileName) {
inline bool Export(const std::string& fileName) const {
// TODO(RobLoach): Switch to an invalid loading exception on false.
return ::ExportImage(*this, fileName.c_str());
}

/**
* Export image as code file defining an array of bytes, returns true on success
*/
inline bool ExportAsCode(const std::string& fileName) {
inline bool ExportAsCode(const std::string& fileName) const {
return ::ExportImageAsCode(*this, fileName.c_str());
}

Expand All @@ -197,21 +237,21 @@ class Image : public ::Image {
/**
* Retrieve the width and height of the image.
*/
inline ::Vector2 GetSize() {
inline ::Vector2 GetSize() const {
return {static_cast<float>(width), static_cast<float>(height)};
}

/**
* Create an image duplicate (useful for transformations)
*/
inline ::Image Copy() {
inline ::Image Copy() const {
return ::ImageCopy(*this);
}

/**
* Create an image from another image piece
*/
inline ::Image FromImage(::Rectangle rec) {
inline ::Image FromImage(::Rectangle rec) const {
return ::ImageFromImage(*this, rec);
}

Expand Down Expand Up @@ -517,35 +557,35 @@ class Image : public ::Image {
/**
* Load color data from image as a Color array (RGBA - 32bit)
*/
inline ::Color* LoadColors() {
inline ::Color* LoadColors() const {
return ::LoadImageColors(*this);
}

/**
* Load colors palette from image as a Color array (RGBA - 32bit)
*/
inline ::Color* LoadPalette(int maxPaletteSize, int *colorsCount) {
inline ::Color* LoadPalette(int maxPaletteSize, int *colorsCount) const {
return ::LoadImagePalette(*this, maxPaletteSize, colorsCount);
}

/**
* Unload color data loaded with LoadImageColors()
*/
inline void UnloadColors(::Color* colors) {
inline void UnloadColors(::Color* colors) const {
::UnloadImageColors(colors);
}

/**
* Unload colors palette loaded with LoadImagePalette()
*/
inline void UnloadPalette(::Color* colors) {
inline void UnloadPalette(::Color* colors) const {
::UnloadImagePalette(colors);
}

/**
* Load texture from image data
*/
inline ::Texture2D LoadTexture() {
inline ::Texture2D LoadTexture() const {
return ::LoadTextureFromImage(*this);
}

Expand All @@ -568,7 +608,7 @@ class Image : public ::Image {
*
* @return The pixel data size of the image.
*/
int GetPixelDataSize() {
int GetPixelDataSize() const {
return ::GetPixelDataSize(width, height, format);
}

Expand Down
30 changes: 28 additions & 2 deletions include/Material.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ class Material : public ::Material {
set(LoadMaterialDefault());
}

Material(const Material&) = delete;

Material(Material&& other) {
set(other);

other.maps = nullptr;
other.shader = { 0 };
}

~Material() {
Unload();
}
Expand All @@ -33,6 +42,7 @@ class Material : public ::Material {
*/
static std::vector<Material> Load(const std::string& fileName) {
int count = 0;
// TODO: Material::Load() possibly leaks the materials array.
::Material* materials = ::LoadMaterials(fileName.c_str(), &count);
return std::vector<Material>(materials, materials + count);
}
Expand All @@ -47,13 +57,29 @@ class Material : public ::Material {
return *this;
}

Material& operator=(const Material&) = delete;

Material& operator=(Material&& other) {
if (this != &other) {
return *this;
}

Unload();
set(other);

other.maps = nullptr;
other.shader = { 0 };

return *this;
}

/**
* Unload material from memory
*/
inline void Unload() {
if (maps != NULL) {
if (maps != nullptr) {
::UnloadMaterial(*this);
maps = NULL;
maps = nullptr;
}
}

Expand Down
Loading