Add shader example and clean up (#152)

* Add shader example
* Remove unused shaders
* Clean up
This commit is contained in:
Rob Loach
2021-12-13 17:46:08 -05:00
committed by GitHub
parent 3279571bf4
commit bf2cb770ab
19 changed files with 334 additions and 96 deletions

View File

@@ -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

View File

@@ -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)

View File

@@ -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

View File

@@ -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)

View File

@@ -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;
}

View File

@@ -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;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

View File

@@ -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, &ampX, SHADER_UNIFORM_FLOAT);
shader.SetValue(ampYLoc, &ampY, 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;
}

View File

@@ -34,7 +34,6 @@ install(FILES
RenderTexture.hpp
Shader.hpp
Sound.hpp
Text.hpp
Texture.hpp
Vector2.hpp
Vector3.hpp

View File

@@ -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_

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;
}

View File

@@ -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;

View File

@@ -1,57 +0,0 @@
/**
* Text drawing functions
*/
#ifndef RAYLIB_CPP_INCLUDE_TEXT_HPP_
#define RAYLIB_CPP_INCLUDE_TEXT_HPP_
#include <string>
#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_

View File

@@ -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;

View File

@@ -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"

View File

@@ -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",

View File

@@ -1,44 +1,59 @@
#include <cassert>
#include <iostream>
#include <string>
#include <vector>
#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<std::string> files = raylib::GetDirectoryFiles(::GetWorkingDirectory());
assert(files.size() > 3);
{
std::vector<std::string> 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;
}