Skip to content

Commit bf2cb77

Browse files
authored
Add shader example and clean up (#152)
* Add shader example * Remove unused shaders * Clean up
1 parent 3279571 commit bf2cb77

File tree

19 files changed

+333
-95
lines changed

19 files changed

+333
-95
lines changed

CHANGELOG.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,23 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [4.x.x] - xxxx-xx-xx
8+
### Added
9+
- `Image::IsLoaded()`
10+
- `Wave::IsLoaded()`
11+
- `Sound::IsLoaded()`
12+
- `Music::IsLoaded()`
13+
- Shader example
14+
- `raylib::DrawText()` for `std::string` support
15+
- `raylib::DrawTextEx()` for `std::string` support
16+
- `raylib::DrawTextPro()` for `std::string` support
17+
18+
### Fixed
19+
- Shader constructor
20+
21+
### Changed
22+
- Merged `Text.hpp` into `Functions.hpp`
23+
724
## [4.0.1] - 2021-11-20
825
### Changes
926
- Fix compiler warning

CMakeLists.txt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@ project (raylib-cpp
99
# Options
1010
option(BUILD_RAYLIB_CPP_EXAMPLES "Examples" ON)
1111

12-
# C++
13-
set(CMAKE_CXX_STANDARD 11)
14-
set(CMAKE_CXX_STANDARD_REQUIRED ON)
15-
set(CMAKE_CXX_EXTENSIONS OFF)
16-
1712
# Include Directory
1813
add_subdirectory(include)
1914

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ for (auto& file : files) {
212212

213213
### String Functions
214214

215-
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.
215+
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.
216216

217217
``` cpp
218218
// raylib

examples/CMakeLists.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
# Get the sources together
2-
set(example_dirs audio core models physics shapes text textures)
2+
set(example_dirs audio core models physics shaders shapes text textures)
33
set(example_sources)
44
set(example_resources)
55

6+
# C++
7+
set(CMAKE_CXX_STANDARD 11)
8+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
9+
set(CMAKE_CXX_EXTENSIONS OFF)
10+
611
# raylib
712
find_package(raylib QUIET)
813
if (NOT raylib_FOUND)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#version 100
2+
3+
precision mediump float;
4+
5+
// Input vertex attributes (from vertex shader)
6+
varying vec2 fragTexCoord;
7+
varying vec4 fragColor;
8+
9+
// Input uniform values
10+
uniform sampler2D texture0;
11+
uniform vec4 colDiffuse;
12+
13+
uniform float secondes;
14+
15+
uniform vec2 size;
16+
17+
uniform float freqX;
18+
uniform float freqY;
19+
uniform float ampX;
20+
uniform float ampY;
21+
uniform float speedX;
22+
uniform float speedY;
23+
24+
void main() {
25+
float pixelWidth = 1.0 / size.x;
26+
float pixelHeight = 1.0 / size.y;
27+
float aspect = pixelHeight / pixelWidth;
28+
float boxLeft = 0.0;
29+
float boxTop = 0.0;
30+
31+
vec2 p = fragTexCoord;
32+
p.x += cos((fragTexCoord.y - boxTop) * freqX / ( pixelWidth * 750.0) + (secondes * speedX)) * ampX * pixelWidth;
33+
p.y += sin((fragTexCoord.x - boxLeft) * freqY * aspect / ( pixelHeight * 750.0) + (secondes * speedY)) * ampY * pixelHeight;
34+
35+
gl_FragColor = texture2D(texture0, p)*colDiffuse*fragColor;
36+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#version 330
2+
3+
// Input vertex attributes (from vertex shader)
4+
in vec2 fragTexCoord;
5+
in vec4 fragColor;
6+
7+
// Input uniform values
8+
uniform sampler2D texture0;
9+
uniform vec4 colDiffuse;
10+
11+
// Output fragment color
12+
out vec4 finalColor;
13+
14+
uniform float secondes;
15+
16+
uniform vec2 size;
17+
18+
uniform float freqX;
19+
uniform float freqY;
20+
uniform float ampX;
21+
uniform float ampY;
22+
uniform float speedX;
23+
uniform float speedY;
24+
25+
void main() {
26+
float pixelWidth = 1.0 / size.x;
27+
float pixelHeight = 1.0 / size.y;
28+
float aspect = pixelHeight / pixelWidth;
29+
float boxLeft = 0.0;
30+
float boxTop = 0.0;
31+
32+
vec2 p = fragTexCoord;
33+
p.x += cos((fragTexCoord.y - boxTop) * freqX / ( pixelWidth * 750.0) + (secondes * speedX)) * ampX * pixelWidth;
34+
p.y += sin((fragTexCoord.x - boxLeft) * freqY * aspect / ( pixelHeight * 750.0) + (secondes * speedY)) * ampY * pixelHeight;
35+
36+
finalColor = texture(texture0, p)*colDiffuse*fragColor;
37+
}
21.9 KB
Loading
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*******************************************************************************************
2+
*
3+
* raylib [shaders] example - Texture Waves
4+
*
5+
* NOTE: This example requires raylib OpenGL 3.3 or ES2 versions for shaders support,
6+
* OpenGL 1.1 does not support shaders, recompile raylib to OpenGL 3.3 version.
7+
*
8+
* NOTE: Shaders used in this example are #version 330 (OpenGL 3.3), to test this example
9+
* on OpenGL ES 2.0 platforms (Android, Raspberry Pi, HTML5), use #version 100 shaders
10+
* raylib comes with shaders ready for both versions, check raylib/shaders install folder
11+
*
12+
* This example has been created using raylib 2.5 (www.raylib.com)
13+
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
14+
*
15+
* Example contributed by Anata (@anatagawa) and reviewed by Ramon Santamaria (@raysan5)
16+
*
17+
* Copyright (c) 2019 Anata (@anatagawa) and Ramon Santamaria (@raysan5)
18+
*
19+
********************************************************************************************/
20+
21+
#include "raylib-cpp.hpp"
22+
23+
#if defined(PLATFORM_DESKTOP)
24+
#define GLSL_VERSION 330
25+
#else // PLATFORM_RPI, PLATFORM_ANDROID, PLATFORM_WEB
26+
#define GLSL_VERSION 100
27+
#endif
28+
29+
int main(void)
30+
{
31+
// Initialization
32+
//--------------------------------------------------------------------------------------
33+
const int screenWidth = 800;
34+
const int screenHeight = 450;
35+
36+
raylib::Window window(screenWidth, screenHeight, "raylib-cpp [shaders] example - texture waves");
37+
38+
// Load texture texture to apply shaders
39+
raylib::Texture2D texture("resources/space.png");
40+
41+
// Load shader and setup location points and values
42+
raylib::Shader shader(0, TextFormat("resources/shaders/glsl%i/wave.fs", GLSL_VERSION));
43+
44+
int secondsLoc = shader.GetLocation("secondes");
45+
int freqXLoc = shader.GetLocation("freqX");
46+
int freqYLoc = shader.GetLocation("freqY");
47+
int ampXLoc = shader.GetLocation("ampX");
48+
int ampYLoc = shader.GetLocation("ampY");
49+
int speedXLoc = shader.GetLocation("speedX");
50+
int speedYLoc = shader.GetLocation("speedY");
51+
52+
// Shader uniform values that can be updated at any time
53+
float freqX = 25.0f;
54+
float freqY = 25.0f;
55+
float ampX = 5.0f;
56+
float ampY = 5.0f;
57+
float speedX = 8.0f;
58+
float speedY = 8.0f;
59+
60+
float screenSize[2] = { (float)window.GetWidth(), (float)window.GetHeight() };
61+
shader.SetValue(shader.GetLocation("size"), &screenSize, SHADER_UNIFORM_VEC2);
62+
shader.SetValue(freqXLoc, &freqX, SHADER_UNIFORM_FLOAT);
63+
shader.SetValue(freqYLoc, &freqY, SHADER_UNIFORM_FLOAT);
64+
shader.SetValue(ampXLoc, &ampX, SHADER_UNIFORM_FLOAT);
65+
shader.SetValue(ampYLoc, &ampY, SHADER_UNIFORM_FLOAT);
66+
shader.SetValue(speedXLoc, &speedX, SHADER_UNIFORM_FLOAT);
67+
shader.SetValue(speedYLoc, &speedY, SHADER_UNIFORM_FLOAT);
68+
69+
float seconds = 0.0f;
70+
71+
window.SetTargetFPS(60); // Set our game to run at 60 frames-per-second
72+
// -------------------------------------------------------------------------------------------------------------
73+
74+
// Main game loop
75+
while (!window.ShouldClose()) // Detect window close button or ESC key
76+
{
77+
// Update
78+
//----------------------------------------------------------------------------------
79+
seconds += GetFrameTime();
80+
81+
shader.SetValue(secondsLoc, &seconds, SHADER_UNIFORM_FLOAT);
82+
//----------------------------------------------------------------------------------
83+
84+
// Draw
85+
//----------------------------------------------------------------------------------
86+
BeginDrawing();
87+
88+
ClearBackground(RAYWHITE);
89+
90+
shader.BeginMode();
91+
92+
texture.Draw(0, 0, WHITE);
93+
texture.Draw(texture.GetWidth(), 0, WHITE);
94+
95+
EndShaderMode();
96+
97+
EndDrawing();
98+
//----------------------------------------------------------------------------------
99+
}
100+
101+
return 0;
102+
}

include/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ install(FILES
3434
RenderTexture.hpp
3535
Shader.hpp
3636
Sound.hpp
37-
Text.hpp
3837
Texture.hpp
3938
Vector2.hpp
4039
Vector3.hpp

include/Functions.hpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,50 @@ RLCPPAPI inline bool ExportImageAsCode(const Image& image, const std::string& fi
243243
return ::ExportImageAsCode(image, fileName.c_str());
244244
}
245245

246+
/**
247+
* Draw text (using default font)
248+
*/
249+
RLCPPAPI inline void DrawText(const std::string& text, int posX, int posY, int fontSize, ::Color color) {
250+
::DrawText(text.c_str(), posX, posY, fontSize, color);
251+
}
252+
253+
/**
254+
* Draw text using font and additional parameters
255+
*/
256+
RLCPPAPI inline void DrawTextEx(const Font& font, const std::string& text, Vector2 position,
257+
float fontSize, float spacing, ::Color tint) {
258+
::DrawTextEx(font, text.c_str(), position, fontSize, spacing, tint);
259+
}
260+
261+
/**
262+
* Draw text using Font and pro parameters (rotation)
263+
*/
264+
RLCPPAPI inline void DrawTextPro(const Font& font, const std::string& text, Vector2 position,
265+
Vector2 origin, float rotation, float fontSize, float spacing, ::Color tint) {
266+
::DrawTextPro(font, text.c_str(), position, origin, rotation, fontSize, spacing, tint);
267+
}
268+
269+
/**
270+
* Measure string width for default font
271+
*/
272+
RLCPPAPI inline int MeasureText(const std::string& text, int fontSize) {
273+
return ::MeasureText(text.c_str(), fontSize);
274+
}
275+
276+
/**
277+
* Check if two text string are equal
278+
*/
279+
RLCPPAPI inline bool TextIsEqual(const std::string& text1, const std::string& text2) {
280+
return ::TextIsEqual(text1.c_str(), text2.c_str());
281+
}
282+
283+
/**
284+
* Check if two text string are equal
285+
*/
286+
RLCPPAPI inline unsigned int TextLength(const std::string& text) {
287+
return ::TextLength(text.c_str());
288+
}
289+
246290
} // namespace raylib
247291

248292
#endif // RAYLIB_CPP_INCLUDE_FUNCTIONS_HPP_

0 commit comments

Comments
 (0)