mirror of
https://github.com/allankoechke/raylib-cpp.git
synced 2026-12-02 09:37:59 +00:00
Merge pull request #103 from RobLoach/string
Add String Functions and vector-based returns
This commit is contained in:
@@ -10,6 +10,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
- `Model.DrawWires()`
|
||||
- `models_first_person_maze.cpp` example
|
||||
- Error correction when unloading images, materials, models and meshes
|
||||
- String override functions to use `std::string` directly instead of `const char*`
|
||||
- `std::vector<std::string>` wrapper for `GetDirectoryFiles()` and `GetDroppedFiles()`
|
||||
|
||||
## [v3.5.0-alpha1] - 2020-12-26
|
||||
### Changed
|
||||
|
||||
22
README.md
22
README.md
@@ -176,6 +176,28 @@ raylib::Vector2 speed(10, 10);
|
||||
position += speed; // Addition assignment operator override.
|
||||
```
|
||||
|
||||
### Vector-Based Returns
|
||||
|
||||
When there is a function that would return a pointer-array, there is a wrapper that allows returning a [vector](https://www.cplusplus.com/reference/vector/vector/) of objects instead.
|
||||
|
||||
``` cpp
|
||||
// raylib
|
||||
int count;
|
||||
char** files = GetDirectoryFiles(".", &count);
|
||||
printf("Count: %i\n", count);
|
||||
for (int i = 0; i < count; i++) {
|
||||
printf("File: %s\n", files[i]);
|
||||
}
|
||||
ClearDirectoryFiles();
|
||||
|
||||
// raylib-cpp
|
||||
std::vector<std::string> files = raylib::GetDirectoryFiles(".");
|
||||
std::cout << "Count: " << files.size() << std::endl;
|
||||
for (auto& file : files) {
|
||||
std::cout << "File: " << file << std::endl;
|
||||
}
|
||||
```
|
||||
|
||||
### RayMath
|
||||
|
||||
The [raymath](https://github.com/raysan5/raylib/blob/master/src/raymath.h) methods are included.
|
||||
|
||||
@@ -12,6 +12,7 @@ install(FILES
|
||||
Camera3D.hpp
|
||||
Color.hpp
|
||||
DroppedFiles.hpp
|
||||
Functions.hpp
|
||||
Font.hpp
|
||||
Gamepad.hpp
|
||||
Image.hpp
|
||||
|
||||
@@ -117,7 +117,8 @@ class Font : public ::Font {
|
||||
/**
|
||||
* Create an image from text (custom sprite font)
|
||||
*/
|
||||
inline Image ImageText(const std::string& text, float fontSize, float spacing, ::Color tint) const {
|
||||
inline Image ImageText(const std::string& text, float fontSize,
|
||||
float spacing, ::Color tint) const {
|
||||
return ::ImageTextEx(*this, text.c_str(), fontSize, spacing, tint);
|
||||
}
|
||||
|
||||
|
||||
188
include/Functions.hpp
Normal file
188
include/Functions.hpp
Normal file
@@ -0,0 +1,188 @@
|
||||
/*
|
||||
* LICENSE: zlib/libpng
|
||||
*
|
||||
* raylib-cpp is licensed under an unmodified zlib/libpng license, which is an OSI-certified,
|
||||
* BSD-like license that allows static linking with closed source software:
|
||||
*
|
||||
* Copyright (c) 2020 Rob Loach (@RobLoach)
|
||||
*
|
||||
* This software is provided "as-is", without any express or implied warranty. In no event
|
||||
* will the authors be held liable for any damages arising from the use of this software.
|
||||
*
|
||||
* Permission is granted to anyone to use this software for any purpose, including commercial
|
||||
* applications, and to alter it and redistribute it freely, subject to the following restrictions:
|
||||
*
|
||||
* 1. The origin of this software must not be misrepresented; you must not claim that you
|
||||
* wrote the original software. If you use this software in a product, an acknowledgment
|
||||
* in the product documentation would be appreciated but is not required.
|
||||
*
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be misrepresented
|
||||
* as being the original software.
|
||||
*
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifndef RAYLIB_CPP_INCLUDE_FUNCTIONS_HPP_
|
||||
#define RAYLIB_CPP_INCLUDE_FUNCTIONS_HPP_
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "./raylib.hpp"
|
||||
|
||||
namespace raylib {
|
||||
|
||||
/**
|
||||
* Get the human-readable, UTF-8 encoded name of the primary monitor
|
||||
*/
|
||||
inline std::string GetMonitorName(int monitor) {
|
||||
return ::GetMonitorName(monitor);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set clipboard text content
|
||||
*/
|
||||
inline void SetClipboardText(const std::string& text) {
|
||||
::SetClipboardText(text.c_str());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get clipboard text content
|
||||
*/
|
||||
inline std::string GetClipboardText() {
|
||||
return ::GetClipboardText();
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes a screenshot of current screen (saved a .png)
|
||||
*/
|
||||
inline void TakeScreenshot(const std::string& fileName) {
|
||||
::TakeScreenshot(fileName.c_str());
|
||||
}
|
||||
|
||||
/**
|
||||
* Load text data from file (read)
|
||||
*/
|
||||
std::string LoadFileText(const std::string& fileName) {
|
||||
char* text = ::LoadFileText(fileName.c_str());
|
||||
std::string output(text);
|
||||
::UnloadFileText((unsigned char*)text);
|
||||
return output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save text data to file (write)
|
||||
*/
|
||||
inline bool SaveFileText(const std::string& fileName, const std::string& text) {
|
||||
return ::SaveFileText(fileName.c_str(), (char*)text.c_str());
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if file exists
|
||||
*/
|
||||
inline bool FileExists(const std::string& fileName) {
|
||||
return ::FileExists(fileName.c_str());
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if directory path exists
|
||||
*/
|
||||
inline bool DirectoryExists(const std::string& dirPath) {
|
||||
return ::DirectoryExists(dirPath.c_str());
|
||||
}
|
||||
|
||||
/**
|
||||
* Check file extension (including point: .png, .wav)
|
||||
*/
|
||||
inline bool IsFileExtension(const std::string& fileName, const std::string& ext) {
|
||||
return ::IsFileExtension(fileName.c_str(), ext.c_str());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get pointer to extension for a filename string (including point: ".png")
|
||||
*/
|
||||
inline std::string GetFileExtension(const std::string& fileName) {
|
||||
return ::GetFileExtension(fileName.c_str());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get pointer to filename for a path string
|
||||
*/
|
||||
inline std::string GetFileName(const std::string& filePath) {
|
||||
return ::GetFileName(filePath.c_str());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get filename string without extension
|
||||
*/
|
||||
inline std::string GetFileNameWithoutExt(const std::string& filePath) {
|
||||
return ::GetFileNameWithoutExt(filePath.c_str());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get full path for a given fileName with path
|
||||
*/
|
||||
inline std::string GetDirectoryPath(const std::string& filePath) {
|
||||
return ::GetDirectoryPath(filePath.c_str());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get previous directory path for a given path
|
||||
*/
|
||||
inline std::string GetPrevDirectoryPath(const std::string& dirPath) {
|
||||
return ::GetPrevDirectoryPath(dirPath.c_str());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current working directory
|
||||
*/
|
||||
inline std::string GetWorkingDirectory() {
|
||||
return ::GetWorkingDirectory();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get filenames in a directory path
|
||||
*/
|
||||
std::vector<std::string> GetDirectoryFiles(const std::string& dirPath) {
|
||||
int count;
|
||||
char** files = ::GetDirectoryFiles(dirPath.c_str(), &count);
|
||||
std::vector<std::string> output(files, files + count);
|
||||
::ClearDirectoryFiles();
|
||||
return output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Change working directory, return true on success
|
||||
*/
|
||||
inline bool ChangeDirectory(const std::string& dir) {
|
||||
return ::ChangeDirectory(dir.c_str());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get dropped files names
|
||||
*/
|
||||
std::vector<std::string> GetDroppedFiles() {
|
||||
int count;
|
||||
char** files = ::GetDroppedFiles(&count);
|
||||
std::vector<std::string> output(files, files + count);
|
||||
::ClearDroppedFiles();
|
||||
return output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get file modification time (last write time)
|
||||
*/
|
||||
inline long GetFileModTime(const std::string& fileName) {
|
||||
return ::GetFileModTime(fileName.c_str());
|
||||
}
|
||||
|
||||
/**
|
||||
* Open URL with default system browser (if available)
|
||||
*/
|
||||
inline void OpenURL(const std::string& url) {
|
||||
return ::OpenURL(url.c_str());
|
||||
}
|
||||
|
||||
} // namespace raylib
|
||||
|
||||
#endif // RAYLIB_CPP_INCLUDE_FUNCTIONS_HPP_
|
||||
@@ -67,13 +67,6 @@
|
||||
#ifndef RAYLIB_CPP_INCLUDE_RAYLIB_CPP_HPP_
|
||||
#define RAYLIB_CPP_INCLUDE_RAYLIB_CPP_HPP_
|
||||
|
||||
/**
|
||||
* Provides all the classes associated with raylib-cpp.
|
||||
*/
|
||||
namespace raylib {
|
||||
// Nothing.
|
||||
} // namespace raylib
|
||||
|
||||
#include "./AudioDevice.hpp"
|
||||
#include "./AudioStream.hpp"
|
||||
#include "./BoundingBox.hpp"
|
||||
@@ -81,6 +74,7 @@ namespace raylib {
|
||||
#include "./Camera3D.hpp"
|
||||
#include "./Color.hpp"
|
||||
#include "./DroppedFiles.hpp"
|
||||
#include "./Functions.hpp"
|
||||
#include "./Font.hpp"
|
||||
#include "./Gamepad.hpp"
|
||||
#include "./Image.hpp"
|
||||
@@ -105,4 +99,11 @@ namespace raylib {
|
||||
#include "./Wave.hpp"
|
||||
#include "./Window.hpp"
|
||||
|
||||
/**
|
||||
* Provides all the classes associated with raylib-cpp.
|
||||
*/
|
||||
namespace raylib {
|
||||
// Nothing.
|
||||
} // namespace raylib
|
||||
|
||||
#endif // RAYLIB_CPP_INCLUDE_RAYLIB_CPP_HPP_
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
"include/Camera3D.hpp",
|
||||
"include/Color.hpp",
|
||||
"include/DroppedFiles.hpp",
|
||||
"include/Functions.hpp",
|
||||
"include/Font.hpp",
|
||||
"include/Gamepad.hpp",
|
||||
"include/Image.hpp",
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do this in one cpp file
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "raylib-cpp.hpp"
|
||||
#include "catch.hpp"
|
||||
|
||||
@@ -33,4 +34,9 @@ TEST_CASE("raylib_test", "[raylib_test]" ) {
|
||||
raylib::Vector2 newDirection = direction.Rotate(30);
|
||||
REQUIRE((int)newDirection.x == 18);
|
||||
}
|
||||
|
||||
SECTION("Functions") {
|
||||
std::vector<std::string> files = raylib::GetDirectoryFiles(::GetWorkingDirectory());
|
||||
REQUIRE(files.size() > 2);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user