From 511601e5d17f9fddcf49b552493ca3a6d1bffda5 Mon Sep 17 00:00:00 2001 From: Rob Loach Date: Mon, 24 Jan 2022 01:07:47 -0500 Subject: [PATCH] Clean up default constructors and Load() methods --- .github/workflows/Tests.yml | 2 +- include/AudioDevice.hpp | 9 ++- include/AudioStream.hpp | 24 +++++- include/Font.hpp | 49 ++++++++++-- include/Image.hpp | 76 +++++++++++++++---- include/Model.hpp | 24 +++++- include/Music.hpp | 33 +++++++- include/RenderTexture.hpp | 17 ++++- include/Sound.hpp | 31 +++++++- include/Texture.hpp | 33 ++++---- include/Wave.hpp | 23 +++++- include/Window.hpp | 13 +++- tests/CMakeLists.txt | 10 +-- .../{raylib_test.cpp => raylib_cpp_test.cpp} | 0 14 files changed, 280 insertions(+), 64 deletions(-) rename tests/{raylib_test.cpp => raylib_cpp_test.cpp} (100%) diff --git a/.github/workflows/Tests.yml b/.github/workflows/Tests.yml index 04f136e..3cb362d 100644 --- a/.github/workflows/Tests.yml +++ b/.github/workflows/Tests.yml @@ -22,4 +22,4 @@ jobs: run: cmake --build build - name: Test - run: ./build/tests/raylib_test + run: ./build/tests/raylib_cpp_test diff --git a/include/AudioDevice.hpp b/include/AudioDevice.hpp index e6c5765..2c3e56d 100644 --- a/include/AudioDevice.hpp +++ b/include/AudioDevice.hpp @@ -3,6 +3,7 @@ #include "./raylib.hpp" #include "./raylib-cpp-utils.hpp" +#include "./RaylibException.hpp" namespace raylib { /** @@ -17,7 +18,9 @@ class AudioDevice { */ AudioDevice(bool lateInit = false) { if (!lateInit) { - Init(); + if (!Init()) { + throw RaylibException("Failed to initialize AudioDevice"); + } } } @@ -31,9 +34,9 @@ class AudioDevice { /** * Initialize audio device and context. */ - inline AudioDevice& Init() { + inline bool Init() { ::InitAudioDevice(); - return *this; + return IsReady(); } /** diff --git a/include/AudioStream.hpp b/include/AudioStream.hpp index dc0b77b..e682eed 100644 --- a/include/AudioStream.hpp +++ b/include/AudioStream.hpp @@ -3,6 +3,7 @@ #include "./raylib.hpp" #include "./raylib-cpp-utils.hpp" +#include "./RaylibException.hpp" namespace raylib { /** @@ -14,11 +15,20 @@ class AudioStream : public ::AudioStream { set(music); } + AudioStream(rAudioBuffer* buffer = nullptr, + unsigned int sampleRate = 0, + unsigned int sampleSize = 0, + unsigned int channels = 0) : ::AudioStream{buffer, sampleRate, sampleSize, channels} { + // Nothing. + } + /** * Init audio stream (to stream raw audio pcm data) */ AudioStream(unsigned int SampleRate, unsigned int SampleSize, unsigned int Channels = 2) { - set(LoadAudioStream(SampleRate, SampleSize, Channels)); + if (!Load(SampleRate, SampleSize, Channels)) { + throw RaylibException("Failed to create AudioStream"); + } } AudioStream(const AudioStream&) = delete; @@ -148,6 +158,18 @@ class AudioStream : public ::AudioStream { ::SetAudioStreamBufferSizeDefault(size); } + bool IsReady() { + return channels > 0; + } + + /** + * Init audio stream (to stream raw audio pcm data) + */ + bool Load(unsigned int SampleRate, unsigned int SampleSize, unsigned int Channels = 2) { + set(::LoadAudioStream(SampleRate, SampleSize, Channels)); + return IsReady(); + } + private: inline void set(const ::AudioStream& stream) { buffer = stream.buffer; diff --git a/include/Font.hpp b/include/Font.hpp index 9caf0c6..60a09af 100644 --- a/include/Font.hpp +++ b/include/Font.hpp @@ -5,6 +5,7 @@ #include "./raylib.hpp" #include "./raylib-cpp-utils.hpp" +#include "./RaylibException.hpp" namespace raylib { /** @@ -12,6 +13,15 @@ namespace raylib { */ class Font : public ::Font { public: + Font(int baseSize, + int glyphCount, + int glyphPadding, + ::Texture2D texture, + ::Rectangle *recs = nullptr, + ::GlyphInfo *glyphs = nullptr) : ::Font{baseSize, glyphCount, glyphPadding, texture, recs, glyphs} { + // Nothing. + } + Font() { set(::GetFontDefault()); } @@ -21,21 +31,28 @@ class Font : public ::Font { } Font(const std::string& fileName) { - set(::LoadFont(fileName.c_str())); + if (!Load(fileName)) { + throw RaylibException("Failed to load Font from file"); + } } Font(const std::string& fileName, int fontSize, int* fontChars, int charCount) { - set(::LoadFontEx(fileName.c_str(), fontSize, fontChars, charCount)); + if (!Load(fileName, fontSize, fontChars, charCount)) { + throw RaylibException("Failed to load font from font with extras"); + } } Font(const ::Image& image, ::Color key, int firstChar) { - set(::LoadFontFromImage(image, key, firstChar)); + if (!Load(image, key, firstChar)) { + throw RaylibException("Failed to load Texture from Image"); + } } Font(const std::string& fileType, const unsigned char* fileData, int dataSize, int fontSize, int *fontChars, int charsCount) { - set(::LoadFontFromMemory(fileType.c_str(), fileData, dataSize, fontSize, fontChars, - charsCount)); + if (!Load(fileType, fileData, dataSize, fontSize, fontChars, charsCount)) { + throw RaylibException("Failed to load Texture from file data"); + } } Font(const Font&) = delete; @@ -91,6 +108,28 @@ class Font : public ::Font { return *this; } + bool Load(const std::string& fileName) { + set(::LoadFont(fileName.c_str())); + return baseSize > 0; + } + + bool Load(const std::string& fileName, int fontSize, int* fontChars, int charCount) { + set(::LoadFontEx(fileName.c_str(), fontSize, fontChars, charCount)); + return baseSize > 0; + } + + bool Load(const ::Image& image, ::Color key, int firstChar) { + set(::LoadFontFromImage(image, key, firstChar)); + return baseSize > 0; + } + + bool Load(const std::string& fileType, const unsigned char* fileData, int dataSize, int fontSize, + int *fontChars, int charsCount) { + set(::LoadFontFromMemory(fileType.c_str(), fileData, dataSize, fontSize, fontChars, + charsCount)); + return baseSize > 0; + } + /** * Draw text using font and additional parameters. */ diff --git a/include/Image.hpp b/include/Image.hpp index a5de864..d330841 100644 --- a/include/Image.hpp +++ b/include/Image.hpp @@ -15,41 +15,75 @@ namespace raylib { */ class Image : public ::Image { public: + Image(void* data = nullptr, + int width = 0, + int height = 0, + int mipmaps = 0, + int format = 0) : ::Image{data, width, height, mipmaps, format} { + // Nothing. + } + Image(const ::Image& image) { set(image); } + /** + * Load an image from the given file. + * + * @throws raylib::RaylibException Thrown if the image failed to load from the file. + * + * @see Load() + */ Image(const std::string& fileName) { - Load(fileName); - if (!IsReady()) { + if (!Load(fileName)) { throw RaylibException(TextFormat("Failed to load Image from file: %s", fileName.c_str())); } } + /** + * Load a raw image from the given file, with the provided width, height, and formats. + * + * @throws raylib::RaylibException Thrown if the image failed to load from the file. + * + * @see LoadRaw() + */ Image(const std::string& fileName, int width, int height, int format, int headerSize) { - LoadRaw(fileName, width, height, format, headerSize); - if (!IsReady()) { + if (!Load(fileName, width, height, format, headerSize)) { throw RaylibException(TextFormat("Failed to load Image from file: %s", fileName.c_str())); } } + /** + * Load an animation image from the given file. + * + * @throws raylib::RaylibException Thrown if the image failed to load from the file. + * + * @see LoadAnim() + */ Image(const std::string& fileName, int* frames) { - LoadAnim(fileName, frames); - if (!IsReady()) { + if (!Load(fileName, frames)) { throw RaylibException(TextFormat("Failed to load Image from animation: %s", fileName.c_str())); } } + /** + * Load an image from the given file. + * + * @throws raylib::RaylibException Thrown if the image failed to load from the file. + */ Image(const std::string& fileType, const unsigned char* fileData, int dataSize) { - LoadFromMemory(fileType, fileData, dataSize); - if (!IsReady()) { + if (!Load(fileType, fileData, dataSize)) { throw RaylibException("Failed to load Image from memory"); } } + /** + * Load an image from the given file. + * + * @throws raylib::RaylibException Thrown if the image failed to load from the file. + */ Image(const ::Texture2D& texture) { - set(::LoadImageFromTexture(texture)); - if (!IsReady()) { + if (!Load(texture)) { throw RaylibException("Failed to load Image from Texture"); } } @@ -189,32 +223,46 @@ class Image : public ::Image { /** * Load image from file into CPU memory (RAM) */ - void Load(const std::string& fileName) { + bool Load(const std::string& fileName) { set(::LoadImage(fileName.c_str())); + return IsReady(); } /** * Load image from RAW file data. */ - void LoadRaw(const std::string& fileName, int width, int height, int format, int headerSize) { + bool Load(const std::string& fileName, int width, int height, int format, int headerSize) { set(::LoadImageRaw(fileName.c_str(), width, height, format, headerSize)); + return IsReady(); } /** * Load image sequence from file (frames appended to image.data). */ - void LoadAnim(const std::string& fileName, int* frames) { + bool Load(const std::string& fileName, int* frames) { set(::LoadImageAnim(fileName.c_str(), frames)); + return IsReady(); } /** * Load image from memory buffer, fileType refers to extension: i.e. "png". */ - void LoadFromMemory( + bool Load( const std::string& fileType, const unsigned char *fileData, int dataSize) { set(::LoadImageFromMemory(fileType.c_str(), fileData, dataSize)); + return IsReady(); + } + + /** + * Load an image from the given file. + * + * @return True or false depending on whether or not the image was loaded from the texture. + */ + bool Load(const ::Texture2D& texture) { + set(::LoadImageFromTexture(texture)); + return IsReady(); } /** diff --git a/include/Model.hpp b/include/Model.hpp index 9f146f1..8d6fc10 100644 --- a/include/Model.hpp +++ b/include/Model.hpp @@ -6,6 +6,7 @@ #include "./raylib.hpp" #include "./raylib-cpp-utils.hpp" #include "./Mesh.hpp" +#include "./RaylibException.hpp" namespace raylib { /** @@ -18,11 +19,15 @@ class Model : public ::Model { } Model(const std::string& fileName) { - set(::LoadModel(fileName.c_str())); + if (!Load(fileName)) { + throw RaylibException("Failed to load Model from filename"); + } } Model(const ::Mesh& mesh) { - set(::LoadModelFromMesh(mesh)); + if (!Load(mesh)) { + throw RaylibException("Failed to load Model from Mesh"); + } } ~Model() { @@ -188,6 +193,20 @@ class Model : public ::Model { return ::GetModelBoundingBox(*this); } + bool IsReady() { + return meshCount > 0 || materialCount > 0 || boneCount > 0; + } + + bool Load(const std::string& fileName) { + set(::LoadModel(fileName.c_str())); + return IsReady(); + } + + bool Load(const ::Mesh& mesh) { + set(::LoadModelFromMesh(mesh)); + return IsReady(); + } + private: inline void set(const ::Model& model) { transform = model.transform; @@ -203,6 +222,7 @@ class Model : public ::Model { bindPose = model.bindPose; } }; + } // namespace raylib #endif // RAYLIB_CPP_INCLUDE_MODEL_HPP_ diff --git a/include/Music.hpp b/include/Music.hpp index 9b34479..e3975bc 100644 --- a/include/Music.hpp +++ b/include/Music.hpp @@ -13,6 +13,17 @@ namespace raylib { */ class Music : public ::Music { public: + /** + * Default Music constructor to build an empty Music object. + */ + Music() { + ctxType = 0; + ctxData = nullptr; + looping = false; + frameCount = 0; + stream.buffer = nullptr; + } + Music(const ::Music& music) { set(music); } @@ -21,8 +32,7 @@ class Music : public ::Music { * Load music stream from file */ Music(const std::string& fileName) { - set(::LoadMusicStream(fileName.c_str())); - if (!IsReady()) { + if (!Load(fileName)) { throw RaylibException(TextFormat("Failed to load Music from file: %s", fileName.c_str())); } } @@ -31,8 +41,7 @@ class Music : public ::Music { * Load music stream from memory */ Music(const std::string& fileType, unsigned char* data, int dataSize) { - set(::LoadMusicStreamFromMemory(fileType.c_str(), data, dataSize)); - if (!IsReady()) { + if (!Load(fileType, data, dataSize)) { throw RaylibException(TextFormat("Failed to load Music from %s file", fileType.c_str())); } } @@ -178,6 +187,22 @@ class Music : public ::Music { return ::GetMusicTimePlayed(*this); } + /** + * Load music stream from file + */ + bool Load(const std::string& fileName) { + set(::LoadMusicStream(fileName.c_str())); + return IsReady(); + } + + /** + * Load music stream from memory + */ + bool Load(const std::string& fileType, unsigned char* data, int dataSize) { + set(::LoadMusicStreamFromMemory(fileType.c_str(), data, dataSize)); + return IsReady(); + } + /** * Retrieve whether or not the Music has been loaded. * diff --git a/include/RenderTexture.hpp b/include/RenderTexture.hpp index 3e03c2d..2eceda5 100644 --- a/include/RenderTexture.hpp +++ b/include/RenderTexture.hpp @@ -3,6 +3,7 @@ #include "./raylib.hpp" #include "./raylib-cpp-utils.hpp" +#include "./RaylibException.hpp" namespace raylib { /** @@ -10,6 +11,13 @@ namespace raylib { */ class RenderTexture : public ::RenderTexture { public: + /** + * Default constructor to build an empty RenderTexture. + */ + RenderTexture() { + id = 0; + } + RenderTexture(const ::RenderTexture& renderTexture) { set(renderTexture); } @@ -17,7 +25,9 @@ class RenderTexture : public ::RenderTexture { RenderTexture(unsigned int id, ::Texture texture, ::Texture depth) : ::RenderTexture{id, texture, depth} {} RenderTexture(int width, int height) { - set(LoadRenderTexture(width, height)); + if (!Load(width, height)) { + throw RaylibException("Failed to create RenderTexture"); + } } RenderTexture(const RenderTexture&) = delete; @@ -80,6 +90,11 @@ class RenderTexture : public ::RenderTexture { return *this; } + bool Load(int width, int height) { + set(::LoadRenderTexture(width, height)); + return id != 0; + } + private: inline void set(const ::RenderTexture& renderTexture) { id = renderTexture.id; diff --git a/include/Sound.hpp b/include/Sound.hpp index 40eb05f..81a4d1f 100644 --- a/include/Sound.hpp +++ b/include/Sound.hpp @@ -21,6 +21,15 @@ class Sound : public ::Sound { Sound(const Sound&) = delete; Sound& operator=(const Sound&) = delete; + Sound() { + frameCount = 0; + stream.buffer = nullptr; + } + + Sound(::AudioStream stream, unsigned int frameCount) : ::Sound{stream, frameCount} { + // Nothing. + } + Sound(Sound&& other) { set(other); @@ -29,15 +38,13 @@ class Sound : public ::Sound { } Sound(const std::string& fileName) { - set(LoadSound(fileName.c_str())); - if (!IsReady()) { + if (!Load(fileName)) { throw RaylibException(TextFormat("Failed to load Sound from file: %s", fileName.c_str())); } } Sound(const ::Wave& wave) { - set(LoadSoundFromWave(wave)); - if (!IsReady()) { + if (!Load(wave)) { throw RaylibException("Failed to load Sound from Wave"); } } @@ -156,6 +163,22 @@ class Sound : public ::Sound { return *this; } + /** + * Load a sound from the given file. + * + * @return True or false depending on loading worked. + */ + bool Load(const std::string& fileName) { + set(LoadSound(fileName.c_str())); + return IsReady(); + } + + bool Load(const ::Wave& wave) { + set(LoadSoundFromWave(wave)); + return IsReady(); + } + + /** * Retrieve whether or not the Sound buffer is loaded. * diff --git a/include/Texture.hpp b/include/Texture.hpp index ae5ff7b..d8aba2c 100644 --- a/include/Texture.hpp +++ b/include/Texture.hpp @@ -18,7 +18,11 @@ class Texture : public ::Texture { /** * Default constructor to create an empty Texture object. */ - Texture() : ::Texture{0, 0, 0, 0, 0} { + Texture(unsigned int id = 0, + int width = 0, + int height = 0, + int mipmaps = 0, + int format = 0) : ::Texture{id, width, height, mipmaps, format} { // Nothing. } @@ -35,8 +39,7 @@ class Texture : public ::Texture { * @throws raylib::RaylibException Throws if failed to create the texture from the given image. */ Texture(const ::Image& image) { - LoadFromImage(image); - if (!IsReady()) { + if (!Load(image)) { throw RaylibException("Failed to load Texture from Image"); } } @@ -49,8 +52,7 @@ class Texture : public ::Texture { * @see LoadTextureCubemap() */ Texture(const ::Image& image, int layout) { - LoadCubemap(image, layout); - if (!IsReady()) { + if (!Load(image, layout)) { throw RaylibException("Failed to load Texture from Cubemap"); } } @@ -61,8 +63,7 @@ class Texture : public ::Texture { * @throws raylib::RaylibException Throws if failed to create the texture from the given file. */ Texture(const std::string& fileName) { - Load(fileName); - if (!IsReady()) { + if (!Load(fileName)) { throw RaylibException(TextFormat("Failed to load Texture from file: %s", fileName.c_str())); } } @@ -123,22 +124,25 @@ class Texture : public ::Texture { /** * Load texture from image data */ - void LoadFromImage(const ::Image& image) { + bool Load(const ::Image& image) { set(::LoadTextureFromImage(image)); + return IsReady(); } /** * Load cubemap from image, multiple image cubemap layouts supported */ - void LoadCubemap(const ::Image& image, int layoutType) { + bool Load(const ::Image& image, int layoutType) { set(::LoadTextureCubemap(image, layoutType)); + return IsReady(); } /** * Load texture from file into GPU memory (VRAM) */ - void Load(const std::string& fileName) { + bool Load(const std::string& fileName) { set(::LoadTexture(fileName.c_str())); + return IsReady(); } /** @@ -202,17 +206,10 @@ class Texture : public ::Texture { return *this; } - /** - * Draws the texture at the top left corner of the screen. - */ - inline Texture& Draw() { - return Draw(0, 0); - } - /** * Draw a Texture2D */ - inline Texture& Draw(int posX, int posY, ::Color tint = {255, 255, 255, 255}) { + inline Texture& Draw(int posX = 0, int posY = 0, ::Color tint = {255, 255, 255, 255}) { ::DrawTexture(*this, posX, posY, tint); return *this; } diff --git a/include/Wave.hpp b/include/Wave.hpp index 9a14a61..76805f4 100644 --- a/include/Wave.hpp +++ b/include/Wave.hpp @@ -28,8 +28,7 @@ class Wave : public ::Wave { * Load wave data from file */ Wave(const std::string& fileName) { - set(::LoadWave(fileName.c_str())); - if (!IsReady()) { + if (!Load(fileName)) { throw RaylibException(TextFormat("Failed to load Wave from file: %s", fileName.c_str())); } } @@ -38,8 +37,7 @@ class Wave : public ::Wave { * Load wave from memory buffer, fileType refers to extension: i.e. "wav" */ Wave(const std::string& fileType, const unsigned char *fileData, int dataSize) { - set(::LoadWaveFromMemory(fileType.c_str(), fileData, dataSize)); - if (!IsReady()) { + if (!Load(fileType, fileData, dataSize)) { throw RaylibException("Failed to load Wave from memory"); } } @@ -181,6 +179,22 @@ class Wave : public ::Wave { return LoadSound(); } + /** + * Load wave data from file + */ + bool Load(const std::string& fileName) { + set(::LoadWave(fileName.c_str())); + return IsReady(); + } + + /** + * Load wave from memory buffer, fileType refers to extension: i.e. "wav" + */ + bool Load(const std::string& fileType, const unsigned char *fileData, int dataSize) { + set(::LoadWaveFromMemory(fileType.c_str(), fileData, dataSize)); + return IsReady(); + } + /** * Retrieve whether or not the Wave data has been loaded. * @@ -199,6 +213,7 @@ class Wave : public ::Wave { data = wave.data; } }; + } // namespace raylib #endif // RAYLIB_CPP_INCLUDE_WAVE_HPP_ diff --git a/include/Window.hpp b/include/Window.hpp index e8d6313..dd488a8 100644 --- a/include/Window.hpp +++ b/include/Window.hpp @@ -4,6 +4,7 @@ #include #include "./raylib.hpp" +#include "./RaylibException.hpp" namespace raylib { /** @@ -17,7 +18,9 @@ class Window { Window(int width = 800, int height = 450, const std::string& title = "raylib", bool lateInit = false) { if (!lateInit) { - Init(width, height, title); + if (!Init(width, height, title)) { + throw RaylibException("Failed to create Window"); + } } } @@ -28,8 +31,14 @@ class Window { Close(); } - void Init(int width = 800, int height = 450, const std::string& title = "raylib") { + /** + * Initializes the window. + * + * @return True or false, depending on if the Window initialized properly. + */ + bool Init(int width = 800, int height = 450, const std::string& title = "raylib") { ::InitWindow(width, height, title.c_str()); + return IsWindowReady(); } /** diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index c2d327f..30e113a 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -4,16 +4,16 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CTEST_CUSTOM_TESTS_IGNORE pkg-config--static) # Executable -add_executable(raylib_test raylib_test.cpp) +add_executable(raylib_cpp_test raylib_cpp_test.cpp) if (MSVC) - target_compile_options(raylib_test PRIVATE /Wall /W4) + target_compile_options(raylib_cpp_test PRIVATE /Wall /W4) else() - target_compile_options(raylib_test PRIVATE -Wall -Wextra -Wconversion -Wsign-conversion) + target_compile_options(raylib_cpp_test PRIVATE -Wall -Wextra -Wconversion -Wsign-conversion) endif() -target_link_libraries(raylib_test raylib_cpp raylib) +target_link_libraries(raylib_cpp_test raylib_cpp raylib) # Test -add_test(NAME raylib_test COMMAND raylib_test) +add_test(NAME raylib_cpp_test COMMAND raylib_cpp_test) # Copy all the resources file(COPY resources/ DESTINATION "resources/") diff --git a/tests/raylib_test.cpp b/tests/raylib_cpp_test.cpp similarity index 100% rename from tests/raylib_test.cpp rename to tests/raylib_cpp_test.cpp