Merge pull request #116 from RobLoach/multiple

Add "static" to all global raylib-cpp function definitions
This commit is contained in:
Rob Loach
2021-02-10 11:40:18 -05:00
committed by GitHub
6 changed files with 104 additions and 30 deletions

View File

@@ -38,5 +38,9 @@ foreach(example_source ${example_sources})
endforeach()
# Multiple Files Example
add_executable("multiple" multiple/main.cpp multiple/Player.cpp)
target_link_libraries("multiple" PUBLIC raylib-cpp raylib)
# Copy all the resources
file(COPY ${example_resources} DESTINATION "resources/")

View File

@@ -0,0 +1,32 @@
#include "raylib-cpp.hpp"
#include "./Player.hpp"
Player::Player() {
position = Rectangle{
GetScreenWidth() / 2 - 50,
GetScreenHeight() / 2 - 50,
100,
100
};
speed = 3;
}
void Player::Draw() {
position.Draw(RED);
}
void Player::Update() {
if (IsKeyDown(KEY_UP)) {
position.y -= speed;
}
if (IsKeyDown(KEY_DOWN)) {
position.y += speed;
}
if (IsKeyDown(KEY_RIGHT)) {
position.x += speed;
}
if (IsKeyDown(KEY_LEFT)) {
position.x -= speed;
}
}

View File

@@ -0,0 +1,10 @@
#include "raylib-cpp.hpp"
class Player {
public:
Player();
raylib::Rectangle position;
int speed;
void Draw();
void Update();
};

View File

@@ -0,0 +1,3 @@
# Multiple Files Example
This provides an example of including raylib-cpp.hpp in multiple files.

View File

@@ -0,0 +1,18 @@
#include "raylib-cpp.hpp"
#include "./Player.hpp"
int main() {
raylib::Window window(640, 480);
SetTargetFPS(60);
Player player;
while (!window.ShouldClose()) {
window.BeginDrawing();
ClearBackground(SKYBLUE);
player.Update();
player.Draw();
window.EndDrawing();
}
}

View File

@@ -30,54 +30,61 @@
#include "./raylib.hpp"
#ifndef RLCPPAPI
/**
* Allow changing the declare type for all raylib-cpp global functions. Defaults to static.
*/
#define RLCPPAPI static
#endif
namespace raylib {
/**
* Initialize window and OpenGL context
*/
inline void InitWindow(int width, int height, const std::string& title) {
RLCPPAPI inline void InitWindow(int width, int height, const std::string& title = "raylib") {
::InitWindow(width, height, title.c_str());
}
/**
* Set title for window
*/
inline void SetWindowTitle(const std::string& title) {
RLCPPAPI inline void SetWindowTitle(const std::string& title) {
::SetWindowTitle(title.c_str());
}
/**
* Get the human-readable, UTF-8 encoded name of the primary monitor
*/
inline std::string GetMonitorName(int monitor) {
RLCPPAPI inline std::string GetMonitorName(int monitor = 0) {
return ::GetMonitorName(monitor);
}
/**
* Set clipboard text content
*/
inline void SetClipboardText(const std::string& text) {
RLCPPAPI inline void SetClipboardText(const std::string& text) {
::SetClipboardText(text.c_str());
}
/**
* Get clipboard text content
*/
inline std::string GetClipboardText() {
RLCPPAPI inline std::string GetClipboardText() {
return ::GetClipboardText();
}
/**
* Takes a screenshot of current screen (saved a .png)
*/
inline void TakeScreenshot(const std::string& fileName) {
RLCPPAPI inline void TakeScreenshot(const std::string& fileName) {
::TakeScreenshot(fileName.c_str());
}
/**
* Load text data from file (read)
*/
std::string LoadFileText(const std::string& fileName) {
RLCPPAPI std::string LoadFileText(const std::string& fileName) {
char* text = ::LoadFileText(fileName.c_str());
std::string output(text);
::UnloadFileText((unsigned char*)text);
@@ -87,77 +94,77 @@ std::string LoadFileText(const std::string& fileName) {
/**
* Save text data to file (write)
*/
inline bool SaveFileText(const std::string& fileName, const std::string& text) {
RLCPPAPI inline bool SaveFileText(const std::string& fileName, const std::string& text) {
return ::SaveFileText(fileName.c_str(), const_cast<char*>(text.c_str()));
}
/**
* Check if file exists
*/
inline bool FileExists(const std::string& fileName) {
RLCPPAPI inline bool FileExists(const std::string& fileName) {
return ::FileExists(fileName.c_str());
}
/**
* Check if directory path exists
*/
inline bool DirectoryExists(const std::string& dirPath) {
RLCPPAPI 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) {
RLCPPAPI 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) {
RLCPPAPI 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) {
RLCPPAPI 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) {
RLCPPAPI 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) {
RLCPPAPI 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) {
RLCPPAPI inline std::string GetPrevDirectoryPath(const std::string& dirPath) {
return ::GetPrevDirectoryPath(dirPath.c_str());
}
/**
* Get current working directory
*/
inline std::string GetWorkingDirectory() {
RLCPPAPI inline std::string GetWorkingDirectory() {
return ::GetWorkingDirectory();
}
/**
* Get filenames in a directory path
*/
std::vector<std::string> GetDirectoryFiles(const std::string& dirPath) {
RLCPPAPI 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);
@@ -168,14 +175,14 @@ std::vector<std::string> GetDirectoryFiles(const std::string& dirPath) {
/**
* Change working directory, return true on success
*/
inline bool ChangeDirectory(const std::string& dir) {
RLCPPAPI inline bool ChangeDirectory(const std::string& dir) {
return ::ChangeDirectory(dir.c_str());
}
/**
* Get dropped files names
*/
std::vector<std::string> GetDroppedFiles() {
RLCPPAPI std::vector<std::string> GetDroppedFiles() {
int count;
char** files = ::GetDroppedFiles(&count);
std::vector<std::string> output(files, files + count);
@@ -186,51 +193,51 @@ std::vector<std::string> GetDroppedFiles() {
/**
* Get file modification time (last write time)
*/
inline long GetFileModTime(const std::string& fileName) { // NOLINT
RLCPPAPI inline long GetFileModTime(const std::string& fileName) { // NOLINT
return ::GetFileModTime(fileName.c_str());
}
/**
* Open URL with default system browser (if available)
*/
inline void OpenURL(const std::string& url) {
RLCPPAPI inline void OpenURL(const std::string& url) {
return ::OpenURL(url.c_str());
}
inline bool IsGamepadName(int gamepad, const std::string& name) {
RLCPPAPI inline bool IsGamepadName(int gamepad, const std::string& name) {
return ::IsGamepadName(gamepad, name.c_str());
}
inline void UpdateCamera(const ::Camera& camera) {
RLCPPAPI inline void UpdateCamera(const ::Camera& camera) {
::Camera* cameraPointer = (::Camera*)&camera;
::UpdateCamera(cameraPointer);
}
inline ::Image LoadImage(const std::string& fileName) {
RLCPPAPI inline ::Image LoadImage(const std::string& fileName) {
return ::LoadImage(fileName.c_str());
}
inline ::Image LoadImageRaw(const std::string& fileName,
RLCPPAPI inline ::Image LoadImageRaw(const std::string& fileName,
int width, int height,
int format, int headerSize) {
return ::LoadImageRaw(fileName.c_str(), width, height, format, headerSize);
}
inline ::Image LoadImageAnim(const std::string& fileName, int *frames) {
RLCPPAPI inline ::Image LoadImageAnim(const std::string& fileName, int *frames) {
return ::LoadImageAnim(fileName.c_str(), frames);
}
inline ::Image LoadImageFromMemory(const std::string& fileType,
RLCPPAPI inline ::Image LoadImageFromMemory(const std::string& fileType,
const unsigned char *fileData,
int dataSize) {
return ::LoadImageFromMemory(fileType.c_str(), fileData, dataSize);
}
inline bool ExportImage(const Image& image, const std::string& fileName) {
RLCPPAPI inline bool ExportImage(const Image& image, const std::string& fileName) {
return ::ExportImage(image, fileName.c_str());
}
inline bool ExportImageAsCode(const Image& image, const std::string& fileName) {
RLCPPAPI inline bool ExportImageAsCode(const Image& image, const std::string& fileName) {
return ::ExportImageAsCode(image, fileName.c_str());
}