diff --git a/CHANGELOG.md b/CHANGELOG.md index 0df7636..7a72a90 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,23 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [4.x.x] - xxxx-xx-xx +### Added +- `Image::IsLoaded()` +- `Wave::IsLoaded()` +- `Sound::IsLoaded()` +- `Music::IsLoaded()` +- Shader example +- `raylib::DrawText()` for `std::string` support +- `raylib::DrawTextEx()` for `std::string` support +- `raylib::DrawTextPro()` for `std::string` support + +### Fixed +- Shader constructor + +### Changed +- Merged `Text.hpp` into `Functions.hpp` + ## [4.0.1] - 2021-11-20 ### Changes - Fix compiler warning diff --git a/CMakeLists.txt b/CMakeLists.txt index 114134b..62d4d9d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,11 +9,6 @@ project (raylib-cpp # Options option(BUILD_RAYLIB_CPP_EXAMPLES "Examples" ON) -# C++ -set(CMAKE_CXX_STANDARD 11) -set(CMAKE_CXX_STANDARD_REQUIRED ON) -set(CMAKE_CXX_EXTENSIONS OFF) - # Include Directory add_subdirectory(include) diff --git a/README.md b/README.md index bbdee9f..27a1b3c 100644 --- a/README.md +++ b/README.md @@ -212,7 +212,7 @@ for (auto& file : files) { ### String Functions -Many of the raylib functions have `std::string`-related functions to allow calling them directly with `std::string`s to save having to use the `.c_str()` method. +Many of the raylib functions have `std::string`-related overrides to allow calling them directly with `std::string`s to save having to use the `.c_str()` method. ``` cpp // raylib diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 74b3f42..e1c5e54 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -1,8 +1,13 @@ # Get the sources together -set(example_dirs audio core models physics shapes text textures) +set(example_dirs audio core models physics shaders shapes text textures) set(example_sources) set(example_resources) +# C++ +set(CMAKE_CXX_STANDARD 11) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_CXX_EXTENSIONS OFF) + # raylib find_package(raylib QUIET) if (NOT raylib_FOUND) diff --git a/examples/shaders/resources/shaders/glsl100/wave.fs b/examples/shaders/resources/shaders/glsl100/wave.fs new file mode 100644 index 0000000..50c4e02 --- /dev/null +++ b/examples/shaders/resources/shaders/glsl100/wave.fs @@ -0,0 +1,36 @@ +#version 100 + +precision mediump float; + +// Input vertex attributes (from vertex shader) +varying vec2 fragTexCoord; +varying vec4 fragColor; + +// Input uniform values +uniform sampler2D texture0; +uniform vec4 colDiffuse; + +uniform float secondes; + +uniform vec2 size; + +uniform float freqX; +uniform float freqY; +uniform float ampX; +uniform float ampY; +uniform float speedX; +uniform float speedY; + +void main() { + float pixelWidth = 1.0 / size.x; + float pixelHeight = 1.0 / size.y; + float aspect = pixelHeight / pixelWidth; + float boxLeft = 0.0; + float boxTop = 0.0; + + vec2 p = fragTexCoord; + p.x += cos((fragTexCoord.y - boxTop) * freqX / ( pixelWidth * 750.0) + (secondes * speedX)) * ampX * pixelWidth; + p.y += sin((fragTexCoord.x - boxLeft) * freqY * aspect / ( pixelHeight * 750.0) + (secondes * speedY)) * ampY * pixelHeight; + + gl_FragColor = texture2D(texture0, p)*colDiffuse*fragColor; +} diff --git a/examples/shaders/resources/shaders/glsl330/wave.fs b/examples/shaders/resources/shaders/glsl330/wave.fs new file mode 100644 index 0000000..43efee2 --- /dev/null +++ b/examples/shaders/resources/shaders/glsl330/wave.fs @@ -0,0 +1,37 @@ +#version 330 + +// Input vertex attributes (from vertex shader) +in vec2 fragTexCoord; +in vec4 fragColor; + +// Input uniform values +uniform sampler2D texture0; +uniform vec4 colDiffuse; + +// Output fragment color +out vec4 finalColor; + +uniform float secondes; + +uniform vec2 size; + +uniform float freqX; +uniform float freqY; +uniform float ampX; +uniform float ampY; +uniform float speedX; +uniform float speedY; + +void main() { + float pixelWidth = 1.0 / size.x; + float pixelHeight = 1.0 / size.y; + float aspect = pixelHeight / pixelWidth; + float boxLeft = 0.0; + float boxTop = 0.0; + + vec2 p = fragTexCoord; + p.x += cos((fragTexCoord.y - boxTop) * freqX / ( pixelWidth * 750.0) + (secondes * speedX)) * ampX * pixelWidth; + p.y += sin((fragTexCoord.x - boxLeft) * freqY * aspect / ( pixelHeight * 750.0) + (secondes * speedY)) * ampY * pixelHeight; + + finalColor = texture(texture0, p)*colDiffuse*fragColor; +} diff --git a/examples/shaders/resources/space.png b/examples/shaders/resources/space.png new file mode 100644 index 0000000..5d016e4 Binary files /dev/null and b/examples/shaders/resources/space.png differ diff --git a/examples/shaders/shaders_texture_waves.cpp b/examples/shaders/shaders_texture_waves.cpp new file mode 100644 index 0000000..4877806 --- /dev/null +++ b/examples/shaders/shaders_texture_waves.cpp @@ -0,0 +1,102 @@ +/******************************************************************************************* +* +* raylib [shaders] example - Texture Waves +* +* NOTE: This example requires raylib OpenGL 3.3 or ES2 versions for shaders support, +* OpenGL 1.1 does not support shaders, recompile raylib to OpenGL 3.3 version. +* +* NOTE: Shaders used in this example are #version 330 (OpenGL 3.3), to test this example +* on OpenGL ES 2.0 platforms (Android, Raspberry Pi, HTML5), use #version 100 shaders +* raylib comes with shaders ready for both versions, check raylib/shaders install folder +* +* This example has been created using raylib 2.5 (www.raylib.com) +* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) +* +* Example contributed by Anata (@anatagawa) and reviewed by Ramon Santamaria (@raysan5) +* +* Copyright (c) 2019 Anata (@anatagawa) and Ramon Santamaria (@raysan5) +* +********************************************************************************************/ + +#include "raylib-cpp.hpp" + +#if defined(PLATFORM_DESKTOP) + #define GLSL_VERSION 330 +#else // PLATFORM_RPI, PLATFORM_ANDROID, PLATFORM_WEB + #define GLSL_VERSION 100 +#endif + +int main(void) +{ + // Initialization + //-------------------------------------------------------------------------------------- + const int screenWidth = 800; + const int screenHeight = 450; + + raylib::Window window(screenWidth, screenHeight, "raylib-cpp [shaders] example - texture waves"); + + // Load texture texture to apply shaders + raylib::Texture2D texture("resources/space.png"); + + // Load shader and setup location points and values + raylib::Shader shader(0, TextFormat("resources/shaders/glsl%i/wave.fs", GLSL_VERSION)); + + int secondsLoc = shader.GetLocation("secondes"); + int freqXLoc = shader.GetLocation("freqX"); + int freqYLoc = shader.GetLocation("freqY"); + int ampXLoc = shader.GetLocation("ampX"); + int ampYLoc = shader.GetLocation("ampY"); + int speedXLoc = shader.GetLocation("speedX"); + int speedYLoc = shader.GetLocation("speedY"); + + // Shader uniform values that can be updated at any time + float freqX = 25.0f; + float freqY = 25.0f; + float ampX = 5.0f; + float ampY = 5.0f; + float speedX = 8.0f; + float speedY = 8.0f; + + float screenSize[2] = { (float)window.GetWidth(), (float)window.GetHeight() }; + shader.SetValue(shader.GetLocation("size"), &screenSize, SHADER_UNIFORM_VEC2); + shader.SetValue(freqXLoc, &freqX, SHADER_UNIFORM_FLOAT); + shader.SetValue(freqYLoc, &freqY, SHADER_UNIFORM_FLOAT); + shader.SetValue(ampXLoc, &X, SHADER_UNIFORM_FLOAT); + shader.SetValue(ampYLoc, &Y, SHADER_UNIFORM_FLOAT); + shader.SetValue(speedXLoc, &speedX, SHADER_UNIFORM_FLOAT); + shader.SetValue(speedYLoc, &speedY, SHADER_UNIFORM_FLOAT); + + float seconds = 0.0f; + + window.SetTargetFPS(60); // Set our game to run at 60 frames-per-second + // ------------------------------------------------------------------------------------------------------------- + + // Main game loop + while (!window.ShouldClose()) // Detect window close button or ESC key + { + // Update + //---------------------------------------------------------------------------------- + seconds += GetFrameTime(); + + shader.SetValue(secondsLoc, &seconds, SHADER_UNIFORM_FLOAT); + //---------------------------------------------------------------------------------- + + // Draw + //---------------------------------------------------------------------------------- + BeginDrawing(); + + ClearBackground(RAYWHITE); + + shader.BeginMode(); + + texture.Draw(0, 0, WHITE); + texture.Draw(texture.GetWidth(), 0, WHITE); + + EndShaderMode(); + + EndDrawing(); + //---------------------------------------------------------------------------------- + } + + return 0; +} diff --git a/include/CMakeLists.txt b/include/CMakeLists.txt index 25be549..fb96901 100644 --- a/include/CMakeLists.txt +++ b/include/CMakeLists.txt @@ -34,7 +34,6 @@ install(FILES RenderTexture.hpp Shader.hpp Sound.hpp - Text.hpp Texture.hpp Vector2.hpp Vector3.hpp diff --git a/include/Functions.hpp b/include/Functions.hpp index 301812c..6f7209d 100644 --- a/include/Functions.hpp +++ b/include/Functions.hpp @@ -243,6 +243,50 @@ RLCPPAPI inline bool ExportImageAsCode(const Image& image, const std::string& fi return ::ExportImageAsCode(image, fileName.c_str()); } +/** + * Draw text (using default font) + */ +RLCPPAPI inline void DrawText(const std::string& text, int posX, int posY, int fontSize, ::Color color) { + ::DrawText(text.c_str(), posX, posY, fontSize, color); +} + +/** + * Draw text using font and additional parameters + */ +RLCPPAPI inline void DrawTextEx(const Font& font, const std::string& text, Vector2 position, + float fontSize, float spacing, ::Color tint) { + ::DrawTextEx(font, text.c_str(), position, fontSize, spacing, tint); +} + +/** + * Draw text using Font and pro parameters (rotation) + */ +RLCPPAPI inline void DrawTextPro(const Font& font, const std::string& text, Vector2 position, + Vector2 origin, float rotation, float fontSize, float spacing, ::Color tint) { + ::DrawTextPro(font, text.c_str(), position, origin, rotation, fontSize, spacing, tint); +} + +/** + * Measure string width for default font + */ +RLCPPAPI inline int MeasureText(const std::string& text, int fontSize) { + return ::MeasureText(text.c_str(), fontSize); +} + +/** + * Check if two text string are equal + */ +RLCPPAPI inline bool TextIsEqual(const std::string& text1, const std::string& text2) { + return ::TextIsEqual(text1.c_str(), text2.c_str()); +} + +/** + * Check if two text string are equal + */ +RLCPPAPI inline unsigned int TextLength(const std::string& text) { + return ::TextLength(text.c_str()); +} + } // namespace raylib #endif // RAYLIB_CPP_INCLUDE_FUNCTIONS_HPP_ diff --git a/include/Image.hpp b/include/Image.hpp index 0729f69..4e7e898 100644 --- a/include/Image.hpp +++ b/include/Image.hpp @@ -618,6 +618,15 @@ class Image : public ::Image { return ::GetPixelDataSize(width, height, format); } + /** + * Retrieve whether or not the Image has been loaded. + * + * @return True or false depending on whether the image has been loaded. + */ + inline bool IsLoaded() const { + return data != nullptr; + } + private: inline void set(const ::Image& image) { data = image.data; diff --git a/include/Music.hpp b/include/Music.hpp index caf0f39..5da27e2 100644 --- a/include/Music.hpp +++ b/include/Music.hpp @@ -171,6 +171,15 @@ class Music : public ::Music { return ::GetMusicTimePlayed(*this); } + /** + * Retrieve whether or not the Music has been loaded. + * + * @return True or false depending on whether the music has been loaded. + */ + inline bool IsLoaded() const { + return stream.buffer != nullptr; + } + private: inline void set(const ::Music& music) { ctxType = music.ctxType; diff --git a/include/Shader.hpp b/include/Shader.hpp index 1cc011f..9b13945 100644 --- a/include/Shader.hpp +++ b/include/Shader.hpp @@ -23,6 +23,10 @@ class Shader : public ::Shader { set(::LoadShader(vsFileName.c_str(), fsFileName.c_str())); } + Shader(const char* vsFileName, const char* fsFileName) { + set(::LoadShader(vsFileName, fsFileName)); + } + Shader(const Shader&) = delete; Shader(Shader&& other) { @@ -34,11 +38,18 @@ class Shader : public ::Shader { /** * Load shader from files and bind default locations. + * + * @see ::LoadShader */ static ::Shader Load(const std::string& vsFileName, const std::string& fsFileName) { return ::LoadShader(vsFileName.c_str(), fsFileName.c_str()); } + /** + * Load a shader from memory. + * + * @see ::LoadShaderFromMemory + */ static ::Shader LoadFromMemory(const std::string& vsCode, const std::string& fsCode) { return ::LoadShaderFromMemory(vsCode.c_str(), fsCode.c_str()); } @@ -116,8 +127,8 @@ class Shader : public ::Shader { * * @see SetShaderValue() */ - inline Shader& SetValue(int uniformLoc, const std::string& value, int uniformType) { - ::SetShaderValue(*this, uniformLoc, value.c_str(), uniformType); + inline Shader& SetValue(int uniformLoc, const void* value, int uniformType) { + ::SetShaderValue(*this, uniformLoc, value, uniformType); return *this; } @@ -126,8 +137,8 @@ class Shader : public ::Shader { * * @see SetShaderValueV() */ - inline Shader& SetValue(int uniformLoc, const std::string& value, int uniformType, int count) { - ::SetShaderValueV(*this, uniformLoc, value.c_str(), uniformType, count); + inline Shader& SetValue(int uniformLoc, const void* value, int uniformType, int count) { + ::SetShaderValueV(*this, uniformLoc, value, uniformType, count); return *this; } diff --git a/include/Sound.hpp b/include/Sound.hpp index dd9dc56..4dff368 100644 --- a/include/Sound.hpp +++ b/include/Sound.hpp @@ -149,6 +149,15 @@ class Sound : public ::Sound { return *this; } + /** + * Retrieve whether or not the Sound buffer is loaded. + * + * @return True or false depending on whether the Sound buffer is loaded. + */ + bool IsLoaded() { + return stream.buffer != nullptr; + } + private: inline void set(const ::Sound& sound) { frameCount = sound.frameCount; diff --git a/include/Text.hpp b/include/Text.hpp deleted file mode 100644 index 197d80b..0000000 --- a/include/Text.hpp +++ /dev/null @@ -1,57 +0,0 @@ -/** - * Text drawing functions - */ -#ifndef RAYLIB_CPP_INCLUDE_TEXT_HPP_ -#define RAYLIB_CPP_INCLUDE_TEXT_HPP_ - -#include - -#include "./raylib.hpp" - -/** - * Allow changing the declare type for all raylib-cpp global functions. Defaults to static. - */ -#ifndef RLCPPAPI -#define RLCPPAPI static -#endif - -namespace raylib { - -/** - * Draw text (using default font) - */ -RLCPPAPI inline void DrawText( - const std::string& title, - int posX, - int posY, - int fontSize, - ::Color color) { - ::DrawText(title.c_str(), posX, posY, fontSize, color); -} - -/** - * Measure string width for default font - */ -RLCPPAPI inline int MeasureText(const std::string& text, int fontSize) { - return ::MeasureText(text.c_str(), fontSize); -} - -/** - * Check if two text string are equal - */ -RLCPPAPI inline bool TextIsEqual(const std::string& text1, const std::string& text2) { - return ::TextIsEqual(text1.c_str(), text2.c_str()); -} - -/** - * Check if two text string are equal - */ -RLCPPAPI inline unsigned int TextLength(const std::string& text) { - return ::TextLength(text.c_str()); -} - -// TODO(RobLoach): Add remaining raylib C functions with string c_str() wrappers. - -} // namespace raylib - -#endif // RAYLIB_CPP_INCLUDE_TEXT_HPP_ diff --git a/include/Wave.hpp b/include/Wave.hpp index 79b6aa1..cb18d30 100644 --- a/include/Wave.hpp +++ b/include/Wave.hpp @@ -174,6 +174,15 @@ class Wave : public ::Wave { return LoadSound(); } + /** + * Retrieve whether or not the Wave data has been loaded. + * + * @return True or false depending on whether the wave data has been loaded. + */ + inline bool IsLoaded() const { + return data != nullptr; + } + private: inline void set(const ::Wave& wave) { frameCount = wave.frameCount; diff --git a/include/raylib-cpp.hpp b/include/raylib-cpp.hpp index c05d745..b6161ff 100644 --- a/include/raylib-cpp.hpp +++ b/include/raylib-cpp.hpp @@ -54,7 +54,6 @@ #include "./RenderTexture.hpp" #include "./Shader.hpp" #include "./Sound.hpp" -#include "./Text.hpp" #include "./Texture.hpp" #include "./Vector2.hpp" #include "./Vector3.hpp" diff --git a/package.json b/package.json index 9e07f24..3f22dc0 100644 --- a/package.json +++ b/package.json @@ -54,7 +54,6 @@ "include/RenderTexture.hpp", "include/Shader.hpp", "include/Sound.hpp", - "include/Text.hpp", "include/Texture.hpp", "include/Vector2.hpp", "include/Vector3.hpp", diff --git a/tests/raylib_test.cpp b/tests/raylib_test.cpp index b17efd3..1bfd7e8 100644 --- a/tests/raylib_test.cpp +++ b/tests/raylib_test.cpp @@ -1,44 +1,59 @@ #include -#include #include #include #include "raylib-cpp.hpp" -int main() { - // Vector - raylib::Vector2 position(50, 100); - assert(position.GetX() == position.x); - position.x = 150; - assert(position.GetX() == 150); +int main(int argc, char *argv[]) { + TraceLog(LOG_INFO, "TEST: raylib-cpp test"); - // Addition operator. - raylib::Vector2 speed(10, 10); - position += speed; - assert(position.x == 160); - assert(raylib::Window::IsReady() == false); + // Get a path to where the executable is so file loading is relative. + std::string path = ""; + if (argc > 0) { + path = GetDirectoryPath(argv[0]); + } + + // Vector + { + raylib::Vector2 position(50, 100); + assert(position.GetX() == position.x); + position.x = 150; + assert(position.GetX() == 150); + position.SetX(300); + assert(position.GetX() == 300); + + raylib::Vector2 speed(10, 10); + position += speed; + assert(position.x == 310); + assert(raylib::Window::IsReady() == false); + } // Color - raylib::Color color = RED; - assert(color.ToInt() == ::ColorToInt(RED)); - color = RAYWHITE; - assert(color.r == RAYWHITE.r); + { + raylib::Color color = RED; + assert(color.ToInt() == ::ColorToInt(RED)); + color = RAYWHITE; + assert(color.r == RAYWHITE.r); + } // Math - raylib::Vector2 direction(50, 50); - raylib::Vector2 newDirection = direction.Rotate(30); - std::cout << "Out: " << newDirection.x << std::endl; - assert((int)newDirection.x == 57); + { + raylib::Vector2 direction(50, 50); + raylib::Vector2 newDirection = direction.Rotate(30); + assert(((int)newDirection.x) == 57); + } // raylib::GetDirectoryFiles() - std::vector files = raylib::GetDirectoryFiles(::GetWorkingDirectory()); - assert(files.size() > 3); + { + std::vector files = raylib::GetDirectoryFiles(::GetWorkingDirectory()); + assert(files.size() > 3); + } // Sound { - raylib::Sound sound("resources/weird.wav"); - sound.Play(); + raylib::Wave wave(path + "/resources/weird.wav"); + assert(wave.IsLoaded()); } - std::cout << "Tests complete" << std::endl; + TraceLog(LOG_INFO, "TEST: raylib-cpp test complete"); return 0; }