diff --git a/include/FileData.hpp b/include/FileData.hpp new file mode 100644 index 00000000..ea51004f --- /dev/null +++ b/include/FileData.hpp @@ -0,0 +1,56 @@ +#ifndef RAYLIB_CPP_INCLUDE_FILEDATA_HPP_ +#define RAYLIB_CPP_INCLUDE_FILEDATA_HPP_ + +#include +#include + +#include "./raylib.hpp" +#include "./raylib-cpp-utils.hpp" + +namespace raylib { + +class FileData { +public: + FileData() = default; + FileData(const FileData&) = delete; + FileData(FileData&& other) noexcept : data(other.data), bytesRead(other.bytesRead) { + other.data = nullptr; + other.bytesRead = 0; + } + FileData& operator=(const FileData&) = delete; + FileData& operator=(FileData&& other) noexcept { + std::swap(data, other.data); + std::swap(bytesRead, other.bytesRead); + return *this; + } + ~FileData() { Unload(); } + + explicit FileData(const std::string& fileName) { + Load(fileName); + } + + GETTER(const unsigned char*, Data, data) + GETTER(int, BytesRead, bytesRead) + + void Load(const std::string& fileName) { Load(fileName.c_str()); } + void Load(const char* fileName) { + data = ::LoadFileData(fileName, &bytesRead); + } + + void Unload() { + if (data != nullptr) { + ::UnloadFileData(data); + data = nullptr; + } + } + +private: + unsigned char* data{nullptr}; + int bytesRead{0}; +}; + +} // namespace raylib + +using RFileData = raylib::FileData; + +#endif // RAYLIB_CPP_INCLUDE_FILEDATA_HPP_ diff --git a/include/FileText.hpp b/include/FileText.hpp new file mode 100644 index 00000000..f8f52a0f --- /dev/null +++ b/include/FileText.hpp @@ -0,0 +1,65 @@ +#ifndef RAYLIB_CPP_INCLUDE_FILETEXT_HPP_ +#define RAYLIB_CPP_INCLUDE_FILETEXT_HPP_ + +#include +#include + +#include "./raylib.hpp" +#include "./raylib-cpp-utils.hpp" + +namespace raylib { + +class FileText { +public: + FileText() = default; + FileText(const FileText&) = delete; + FileText(FileText&& other) noexcept : data(other.data), length(other.length) { + other.data = nullptr; + other.length = 0; + } + FileText& operator=(const FileText&) = delete; + FileText& operator=(FileText&& other) noexcept { + std::swap(data, other.data); + std::swap(length, other.length); + return *this; + } + ~FileText() { Unload(); } + + explicit FileText(const std::string& fileName) { + Load(fileName); + } + + GETTER(const char*, Data, data) + GETTER(unsigned int, Length, length) + + [[nodiscard]] const char* c_str() const { return data; } + + [[nodiscard]] std::string ToString() const { return data; } + explicit operator std::string() const { + return data; + } + + void Load(const std::string& fileName) { Load(fileName.c_str()); } + void Load(const char* fileName) { + data = ::LoadFileText(fileName); + length = ::TextLength(data); + } + + void Unload() { + if (data != nullptr) { + ::UnloadFileText(data); + data = nullptr; + length = 0; + } + } + +private: + char* data{nullptr}; + unsigned int length{0}; +}; + +} // namespace raylib + +using RFileText = raylib::FileText; + +#endif // RAYLIB_CPP_INCLUDE_FILETEXT_HPP_ diff --git a/include/raylib-cpp-utils.hpp b/include/raylib-cpp-utils.hpp index 4eee8329..78330e04 100644 --- a/include/raylib-cpp-utils.hpp +++ b/include/raylib-cpp-utils.hpp @@ -19,4 +19,17 @@ void Set##method(type value) { name = value; } #endif +#ifndef GETTER +/** + * A utility to build get methods on top of a property. + * + * @param type The type of the property. + * @param method The human-readable name for the method. + * @param name The machine-readable name of the property. + */ +#define GETTER(type, method, name) \ + /** Retrieves the name value for the object. @return The name value of the object. */ \ + type Get##method() const { return name; } +#endif + #endif // RAYLIB_CPP_INCLUDE_RAYLIB_CPP_UTILS_HPP_ diff --git a/include/raylib-cpp.hpp b/include/raylib-cpp.hpp index b2feba0c..1f45d66d 100644 --- a/include/raylib-cpp.hpp +++ b/include/raylib-cpp.hpp @@ -38,6 +38,8 @@ #include "./Camera2D.hpp" #include "./Camera3D.hpp" #include "./Color.hpp" +#include "./FileData.hpp" +#include "./FileText.hpp" #include "./Font.hpp" #include "./Functions.hpp" #include "./Gamepad.hpp" diff --git a/tests/raylib_cpp_test.cpp b/tests/raylib_cpp_test.cpp index 375eff46..58c358ff 100644 --- a/tests/raylib_cpp_test.cpp +++ b/tests/raylib_cpp_test.cpp @@ -127,6 +127,19 @@ int main(int argc, char *argv[]) { Assert(passed, "Expected to have a RaylibException to be thrown"); } + // Load FileData + { + raylib::FileData file(path + "/resources/weird.wav"); + Assert(file.GetBytesRead() > 0, "Expected file to be loaded correctly"); + } + + // Load FileText + { + raylib::FileText text(path + "/resources/lorem.txt"); + Assert(text.GetLength() > 0, "Expected file to be loaded correctly"); + AssertEqual(text.ToString().substr(0, 5), "Lorem"); + } + TraceLog(LOG_INFO, "TEST: raylib-cpp test"); TraceLog(LOG_INFO, "---------------------"); return 0; diff --git a/tests/resources/lorem.txt b/tests/resources/lorem.txt new file mode 100644 index 00000000..4c738ee8 --- /dev/null +++ b/tests/resources/lorem.txt @@ -0,0 +1,4 @@ +Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. +Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. +Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. +Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. \ No newline at end of file