FS#25 - Change sf::PostFx to a more general purpose pixel shader class (sf::Shader)
Updated the PostFx sample, renamed to Shader Renamed all the static X::CanUseX() functions to X::IsAvailable() to make the API more consistent Moved .def files from /build/VC200X to /src in CSFML Minors fixes in CSFML git-svn-id: https://sfml.svn.sourceforge.net/svnroot/sfml/branches/sfml2@1258 4e206d99-4929-0410-ac5d-dfc041789085
This commit is contained in:
parent
63e07cec84
commit
d7bd00afc0
125 changed files with 1606 additions and 2348 deletions
|
@ -1,16 +0,0 @@
|
|||
texture framebuffer
|
||||
float offset
|
||||
|
||||
effect
|
||||
{
|
||||
vec2 offx = vec2(offset, 0.0);
|
||||
vec2 offy = vec2(0.0, offset);
|
||||
|
||||
vec4 c0 = framebuffer(_in);
|
||||
vec4 c1 = framebuffer(_in - offy);
|
||||
vec4 c2 = framebuffer(_in + offy);
|
||||
vec4 c3 = framebuffer(_in - offx);
|
||||
vec4 c4 = framebuffer(_in + offx);
|
||||
|
||||
_out = c0 * 0.2 + c1 * 0.2 + c2 * 0.2 + c3 * 0.2 + c4 * 0.2;
|
||||
}
|
Binary file not shown.
|
@ -1,10 +0,0 @@
|
|||
texture framebuffer
|
||||
vec3 color
|
||||
|
||||
effect
|
||||
{
|
||||
vec4 pixel = framebuffer(_in);
|
||||
float gray = pixel.r * 0.39 + pixel.g * 0.50 + pixel.b * 0.11;
|
||||
|
||||
_out = vec4(gray * color, 1.0) * 0.6 + pixel * 0.4;
|
||||
}
|
|
@ -1,12 +0,0 @@
|
|||
texture framebuffer
|
||||
vec2 mouse
|
||||
|
||||
effect
|
||||
{
|
||||
float len = distance(_in, mouse) * 7.0;
|
||||
|
||||
if (len < 1.0)
|
||||
_out = framebuffer(_in + (_in - mouse) * len);
|
||||
else
|
||||
_out = framebuffer(_in);
|
||||
}
|
|
@ -1,6 +0,0 @@
|
|||
texture framebuffer
|
||||
|
||||
effect
|
||||
{
|
||||
_out = framebuffer(_in);
|
||||
}
|
|
@ -1,11 +0,0 @@
|
|||
texture framebuffer
|
||||
vec2 mouse
|
||||
|
||||
effect
|
||||
{
|
||||
float factor = 5 + 100 * length(mouse);
|
||||
|
||||
vec2 pos = floor(_in * factor) / factor;
|
||||
|
||||
_out = framebuffer(pos);
|
||||
}
|
|
@ -1,12 +0,0 @@
|
|||
texture framebuffer
|
||||
texture wave
|
||||
vec2 offset
|
||||
|
||||
effect
|
||||
{
|
||||
vec2 texoffset = wave(_in * offset).xy;
|
||||
texoffset -= vec2(0.5, 0.5);
|
||||
texoffset *= 0.05;
|
||||
|
||||
_out = framebuffer(_in + texoffset);
|
||||
}
|
BIN
samples/bin/datas/shader/arial.ttf
Normal file
BIN
samples/bin/datas/shader/arial.ttf
Normal file
Binary file not shown.
Before Width: | Height: | Size: 52 KiB After Width: | Height: | Size: 52 KiB |
16
samples/bin/datas/shader/blur.sfx
Normal file
16
samples/bin/datas/shader/blur.sfx
Normal file
|
@ -0,0 +1,16 @@
|
|||
uniform sampler2D texture;
|
||||
uniform float offset;
|
||||
|
||||
void main()
|
||||
{
|
||||
vec2 offx = vec2(offset, 0.0);
|
||||
vec2 offy = vec2(0.0, offset);
|
||||
|
||||
vec4 c0 = texture2D(texture, gl_TexCoord[0].xy);
|
||||
vec4 c1 = texture2D(texture, gl_TexCoord[0].xy - offy);
|
||||
vec4 c2 = texture2D(texture, gl_TexCoord[0].xy + offy);
|
||||
vec4 c3 = texture2D(texture, gl_TexCoord[0].xy - offx);
|
||||
vec4 c4 = texture2D(texture, gl_TexCoord[0].xy + offx);
|
||||
|
||||
gl_FragColor = gl_Color * (c0 * 0.2 + c1 * 0.2 + c2 * 0.2 + c3 * 0.2 + c4 * 0.2);
|
||||
}
|
11
samples/bin/datas/shader/colorize.sfx
Normal file
11
samples/bin/datas/shader/colorize.sfx
Normal file
|
@ -0,0 +1,11 @@
|
|||
uniform sampler2D texture;
|
||||
uniform vec3 color;
|
||||
|
||||
void main()
|
||||
{
|
||||
vec4 pixel = texture2D(texture, gl_TexCoord[0].xy) * gl_Color;
|
||||
float gray = pixel.r * 0.39 + pixel.g * 0.50 + pixel.b * 0.11;
|
||||
|
||||
gl_FragColor = vec4(gray * color, 1.0) * 0.6 + pixel * 0.4;
|
||||
gl_FragColor.a = pixel.a;
|
||||
}
|
13
samples/bin/datas/shader/fisheye.sfx
Normal file
13
samples/bin/datas/shader/fisheye.sfx
Normal file
|
@ -0,0 +1,13 @@
|
|||
uniform sampler2D texture;
|
||||
uniform vec2 mouse;
|
||||
|
||||
void main()
|
||||
{
|
||||
float len = distance(gl_TexCoord[0].xy, mouse) * 7.0;
|
||||
|
||||
vec2 coords = gl_TexCoord[0].xy;
|
||||
if (len < 1.0)
|
||||
coords += (gl_TexCoord[0].xy - mouse) * len;
|
||||
|
||||
gl_FragColor = texture2D(texture, coords) * gl_Color;
|
||||
}
|
6
samples/bin/datas/shader/nothing.sfx
Normal file
6
samples/bin/datas/shader/nothing.sfx
Normal file
|
@ -0,0 +1,6 @@
|
|||
uniform sampler2D texture;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_FragColor = texture2D(texture, gl_TexCoord[0].xy) * gl_Color;
|
||||
}
|
10
samples/bin/datas/shader/pixelate.sfx
Normal file
10
samples/bin/datas/shader/pixelate.sfx
Normal file
|
@ -0,0 +1,10 @@
|
|||
uniform sampler2D texture;
|
||||
uniform vec2 mouse;
|
||||
|
||||
void main()
|
||||
{
|
||||
float factor = 5 + 100 * length(mouse);
|
||||
vec2 pos = floor(gl_TexCoord[0].xy * factor + 0.5) / factor;
|
||||
|
||||
gl_FragColor = texture2D(texture, pos) * gl_Color;
|
||||
}
|
Before Width: | Height: | Size: 6.1 KiB After Width: | Height: | Size: 6.1 KiB |
Before Width: | Height: | Size: 23 KiB After Width: | Height: | Size: 23 KiB |
12
samples/bin/datas/shader/wave.sfx
Normal file
12
samples/bin/datas/shader/wave.sfx
Normal file
|
@ -0,0 +1,12 @@
|
|||
uniform sampler2D texture;
|
||||
uniform sampler2D wave;
|
||||
uniform vec2 offset;
|
||||
|
||||
void main()
|
||||
{
|
||||
vec2 texoffset = texture2D(wave, (gl_TexCoord[0].xy * offset).xy);
|
||||
texoffset -= vec2(0.5, 0.5);
|
||||
texoffset *= 0.05;
|
||||
|
||||
gl_FragColor = texture2D(texture, gl_TexCoord[0].xy + texoffset) * gl_Color;
|
||||
}
|
|
@ -2,13 +2,13 @@
|
|||
<CodeBlocks_project_file>
|
||||
<FileVersion major="1" minor="6" />
|
||||
<Project>
|
||||
<Option title="postfx" />
|
||||
<Option title="shader" />
|
||||
<Option pch_mode="2" />
|
||||
<Option compiler="gcc" />
|
||||
<Build>
|
||||
<Target title="Debug_Win32">
|
||||
<Option output="..\..\bin\postfx-d" prefix_auto="1" extension_auto="1" />
|
||||
<Option object_output="..\..\..\Temp\postfx\Debug_Win32" />
|
||||
<Option output="..\..\bin\shader-d" prefix_auto="1" extension_auto="1" />
|
||||
<Option object_output="..\..\..\Temp\shader\Debug_Win32" />
|
||||
<Option type="0" />
|
||||
<Option compiler="gcc" />
|
||||
<Compiler>
|
||||
|
@ -31,8 +31,8 @@
|
|||
</Linker>
|
||||
</Target>
|
||||
<Target title="Release_Win32">
|
||||
<Option output="..\..\bin\postfx" prefix_auto="1" extension_auto="1" />
|
||||
<Option object_output="..\..\..\Temp\postfx\Release_Win32" />
|
||||
<Option output="..\..\bin\shader" prefix_auto="1" extension_auto="1" />
|
||||
<Option object_output="..\..\..\Temp\shader\Release_Win32" />
|
||||
<Option type="0" />
|
||||
<Option compiler="gcc" />
|
||||
<Compiler>
|
||||
|
@ -57,7 +57,7 @@
|
|||
<Compiler>
|
||||
<Add directory="..\..\..\include" />
|
||||
</Compiler>
|
||||
<Unit filename="..\..\post-fx\PostFX.cpp" />
|
||||
<Unit filename="..\..\shader\Shader.cpp" />
|
||||
<Extensions>
|
||||
<code_completion />
|
||||
<envvars />
|
|
@ -2,9 +2,9 @@
|
|||
<VisualStudioProject
|
||||
ProjectType="Visual C++"
|
||||
Version="8,00"
|
||||
Name="postfx"
|
||||
Name="shader"
|
||||
ProjectGUID="{E8B7727D-2308-4ADC-90AE-D3F46798447D}"
|
||||
RootNamespace="postfx"
|
||||
RootNamespace="shader"
|
||||
Keyword="Win32Proj"
|
||||
>
|
||||
<Platforms>
|
||||
|
@ -180,7 +180,7 @@
|
|||
</References>
|
||||
<Files>
|
||||
<File
|
||||
RelativePath="..\..\post-fx\PostFX.cpp"
|
||||
RelativePath="..\..\shader\Shader.cpp"
|
||||
>
|
||||
</File>
|
||||
</Files>
|
|
@ -2,9 +2,9 @@
|
|||
<VisualStudioProject
|
||||
ProjectType="Visual C++"
|
||||
Version="9,00"
|
||||
Name="postfx"
|
||||
Name="shader"
|
||||
ProjectGUID="{E8B7727D-2308-4ADC-90AE-D3F46798447D}"
|
||||
RootNamespace="postfx"
|
||||
RootNamespace="shader"
|
||||
Keyword="Win32Proj"
|
||||
TargetFrameworkVersion="131072"
|
||||
>
|
||||
|
@ -179,7 +179,7 @@
|
|||
</References>
|
||||
<Files>
|
||||
<File
|
||||
RelativePath="..\..\post-fx\PostFX.cpp"
|
||||
RelativePath="..\..\shader\Shader.cpp"
|
||||
>
|
||||
</File>
|
||||
</Files>
|
|
@ -41,7 +41,7 @@ int main()
|
|||
|
||||
// Load the text font
|
||||
sf::Font font;
|
||||
if (!font.LoadFromFile("datas/post-fx/cheeseburger.ttf"))
|
||||
if (!font.LoadFromFile("datas/pong/cheeseburger.ttf"))
|
||||
return EXIT_FAILURE;
|
||||
|
||||
// Initialize the end text
|
||||
|
|
|
@ -1,204 +0,0 @@
|
|||
|
||||
////////////////////////////////////////////////////////////
|
||||
// Headers
|
||||
////////////////////////////////////////////////////////////
|
||||
#include <SFML/Graphics.hpp>
|
||||
#include <map>
|
||||
#include <math.h>
|
||||
|
||||
void DisplayError();
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Entry point of application
|
||||
///
|
||||
/// \return Application exit code
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
int main()
|
||||
{
|
||||
// Check that the system can use post effects
|
||||
if (sf::PostFX::CanUsePostFX() == false)
|
||||
{
|
||||
DisplayError();
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
// Create the main window
|
||||
sf::RenderWindow window(sf::VideoMode(800, 600), "SFML PostFX");
|
||||
|
||||
// Load a background image to display
|
||||
sf::Image backgroundImage;
|
||||
if (!backgroundImage.LoadFromFile("datas/post-fx/background.jpg"))
|
||||
return EXIT_FAILURE;
|
||||
sf::Sprite background(backgroundImage);
|
||||
|
||||
// Load a sprite which we'll move into the scene
|
||||
sf::Image entityImage;
|
||||
if (!entityImage.LoadFromFile("datas/post-fx/sprite.png"))
|
||||
return EXIT_FAILURE;
|
||||
sf::Sprite entity(entityImage);
|
||||
|
||||
// Load the text font
|
||||
sf::Font font;
|
||||
if (!font.LoadFromFile("datas/post-fx/cheeseburger.ttf"))
|
||||
return EXIT_FAILURE;
|
||||
|
||||
// Load the image needed for the wave effect
|
||||
sf::Image waveImage;
|
||||
if (!waveImage.LoadFromFile("datas/post-fx/wave.jpg"))
|
||||
return EXIT_FAILURE;
|
||||
|
||||
// Load all effects
|
||||
std::map<std::string, sf::PostFX> effects;
|
||||
if (!effects["nothing"].LoadFromFile("datas/post-fx/nothing.sfx")) return EXIT_FAILURE;
|
||||
if (!effects["blur"].LoadFromFile("datas/post-fx/blur.sfx")) return EXIT_FAILURE;
|
||||
if (!effects["colorize"].LoadFromFile("datas/post-fx/colorize.sfx")) return EXIT_FAILURE;
|
||||
if (!effects["fisheye"].LoadFromFile("datas/post-fx/fisheye.sfx")) return EXIT_FAILURE;
|
||||
if (!effects["wave"].LoadFromFile("datas/post-fx/wave.sfx")) return EXIT_FAILURE;
|
||||
if (!effects["pixelate"].LoadFromFile("datas/post-fx/pixelate.sfx")) return EXIT_FAILURE;
|
||||
std::map<std::string, sf::PostFX>::iterator currentEffect = effects.find("nothing");
|
||||
|
||||
// Do specific initializations
|
||||
effects["nothing"].SetTexture("framebuffer", NULL);
|
||||
effects["blur"].SetTexture("framebuffer", NULL);
|
||||
effects["blur"].SetParameter("offset", 0.f);
|
||||
effects["colorize"].SetTexture("framebuffer", NULL);
|
||||
effects["colorize"].SetParameter("color", 1.f, 1.f, 1.f);
|
||||
effects["fisheye"].SetTexture("framebuffer", NULL);
|
||||
effects["wave"].SetTexture("framebuffer", NULL);
|
||||
effects["wave"].SetTexture("wave", &waveImage);
|
||||
effects["pixelate"].SetTexture("framebuffer", NULL);
|
||||
|
||||
// Define a string for displaying current effect description
|
||||
sf::String curFXStr;
|
||||
curFXStr.SetText("Current effect is \"" + currentEffect->first + "\"");
|
||||
curFXStr.SetFont(font);
|
||||
curFXStr.SetPosition(20.f, 0.f);
|
||||
curFXStr.SetColor(sf::Color(150, 70, 110));
|
||||
|
||||
// Define a string for displaying help
|
||||
sf::String infoStr;
|
||||
infoStr.SetText("Move your mouse to change the effect parameters\nPress numpad + and - to change effect\nWarning : some effects may not work\ndepending on your graphics card");
|
||||
infoStr.SetFont(font);
|
||||
infoStr.SetPosition(20.f, 460.f);
|
||||
infoStr.SetColor(sf::Color(200, 100, 150));
|
||||
|
||||
// Create a clock to measure the total time elapsed
|
||||
sf::Clock clock;
|
||||
|
||||
// Start the game loop
|
||||
while (window.IsOpened())
|
||||
{
|
||||
// Process events
|
||||
sf::Event event;
|
||||
while (window.GetEvent(event))
|
||||
{
|
||||
// Close window : exit
|
||||
if (event.Type == sf::Event::Closed)
|
||||
window.Close();
|
||||
|
||||
if (event.Type == sf::Event::KeyPressed)
|
||||
{
|
||||
// Escape key : exit
|
||||
if (event.Key.Code == sf::Key::Escape)
|
||||
window.Close();
|
||||
|
||||
// Add key : next effect
|
||||
if (event.Key.Code == sf::Key::Add)
|
||||
{
|
||||
currentEffect++;
|
||||
if (currentEffect == effects.end())
|
||||
currentEffect = effects.begin();
|
||||
curFXStr.SetText("Current effect is \"" + currentEffect->first + "\"");
|
||||
}
|
||||
|
||||
// Subtract key : previous effect
|
||||
if (event.Key.Code == sf::Key::Subtract)
|
||||
{
|
||||
if (currentEffect == effects.begin())
|
||||
currentEffect = effects.end();
|
||||
currentEffect--;
|
||||
curFXStr.SetText("Current effect is \"" + currentEffect->first + "\"");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Get the mouse position in the range [0, 1]
|
||||
float mouseX = window.GetInput().GetMouseX() / static_cast<float>(window.GetWidth());
|
||||
float mouseY = window.GetInput().GetMouseY() / static_cast<float>(window.GetHeight());
|
||||
|
||||
// Update the current effect
|
||||
if (currentEffect->first == "blur") currentEffect->second.SetParameter("offset", mouseX * mouseY * 0.05f);
|
||||
else if (currentEffect->first == "colorize") currentEffect->second.SetParameter("color", 0.3f, mouseX, mouseY);
|
||||
else if (currentEffect->first == "fisheye") currentEffect->second.SetParameter("mouse", mouseX, 1.f - mouseY);
|
||||
else if (currentEffect->first == "wave") currentEffect->second.SetParameter("offset", mouseX, mouseY);
|
||||
else if (currentEffect->first == "pixelate") currentEffect->second.SetParameter("mouse", mouseX, mouseY);
|
||||
|
||||
// Animate the sprite
|
||||
float entityX = (cos(clock.GetElapsedTime() * 1.3f) + 1.2f) * 300;
|
||||
float entityY = (cos(clock.GetElapsedTime() * 0.8f) + 1.2f) * 200;
|
||||
entity.SetPosition(entityX, entityY);
|
||||
entity.Rotate(window.GetFrameTime() * 100);
|
||||
|
||||
// Clear the window
|
||||
window.Clear();
|
||||
|
||||
// Draw background, sprite and apply the post-fx
|
||||
window.Draw(background);
|
||||
window.Draw(entity);
|
||||
window.Draw(currentEffect->second);
|
||||
|
||||
// Draw interface strings
|
||||
window.Draw(curFXStr);
|
||||
window.Draw(infoStr);
|
||||
|
||||
// Finally, display the rendered frame on screen
|
||||
window.Display();
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Fonction called when the post-effects are not supported ;
|
||||
/// Display an error message and wait until the user exits
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
void DisplayError()
|
||||
{
|
||||
// Create the main window
|
||||
sf::RenderWindow window(sf::VideoMode(800, 600), "SFML PostFX");
|
||||
|
||||
// Define a string for displaying the error message
|
||||
sf::String error("Sorry, your system doesn't support post-effects");
|
||||
error.SetPosition(100.f, 250.f);
|
||||
error.SetColor(sf::Color(200, 100, 150));
|
||||
|
||||
// Start the game loop
|
||||
while (window.IsOpened())
|
||||
{
|
||||
// Process events
|
||||
sf::Event event;
|
||||
while (window.GetEvent(event))
|
||||
{
|
||||
// Close window : exit
|
||||
if (event.Type == sf::Event::Closed)
|
||||
window.Close();
|
||||
|
||||
// Escape key : exit
|
||||
if ((event.Type == sf::Event::KeyPressed) && (event.Key.Code == sf::Key::Escape))
|
||||
window.Close();
|
||||
}
|
||||
|
||||
// Clear the window
|
||||
window.Clear();
|
||||
|
||||
// Draw the error message
|
||||
window.Draw(error);
|
||||
|
||||
// Finally, display the rendered frame on screen
|
||||
window.Display();
|
||||
}
|
||||
}
|
|
@ -1,5 +1,5 @@
|
|||
EXEC = post-fx
|
||||
OBJ = PostFX.o
|
||||
EXEC = shader
|
||||
OBJ = Shader.o
|
||||
|
||||
all: $(EXEC)
|
||||
|
277
samples/shader/Shader.cpp
Normal file
277
samples/shader/Shader.cpp
Normal file
|
@ -0,0 +1,277 @@
|
|||
|
||||
////////////////////////////////////////////////////////////
|
||||
// Headers
|
||||
////////////////////////////////////////////////////////////
|
||||
#include <SFML/Graphics.hpp>
|
||||
#include <map>
|
||||
#include <math.h>
|
||||
|
||||
|
||||
void DisplayError();
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// A class to simplify shader selection
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
class ShaderSelector
|
||||
{
|
||||
public :
|
||||
|
||||
// Constructor
|
||||
ShaderSelector(std::map<std::string, sf::Shader>& owner, const std::string& shader) :
|
||||
myOwner (&owner),
|
||||
myIterator(owner.find(shader))
|
||||
{
|
||||
}
|
||||
|
||||
// Select the previous shader
|
||||
void GotoPrevious()
|
||||
{
|
||||
if (myIterator == myOwner->begin())
|
||||
myIterator = myOwner->end();
|
||||
myIterator--;
|
||||
}
|
||||
|
||||
// Select the next shader
|
||||
void GotoNext()
|
||||
{
|
||||
myIterator++;
|
||||
if (myIterator == myOwner->end())
|
||||
myIterator = myOwner->begin();
|
||||
}
|
||||
|
||||
// Update the shader parameters
|
||||
void Update(float x, float y)
|
||||
{
|
||||
if (myIterator->first == "blur") myIterator->second.SetParameter("offset", x * y * 0.05f);
|
||||
else if (myIterator->first == "colorize") myIterator->second.SetParameter("color", 0.3f, x, y);
|
||||
else if (myIterator->first == "fisheye") myIterator->second.SetParameter("mouse", x, y);
|
||||
else if (myIterator->first == "wave") myIterator->second.SetParameter("offset", x, y);
|
||||
else if (myIterator->first == "pixelate") myIterator->second.SetParameter("mouse", x, y);
|
||||
}
|
||||
|
||||
// Get the name of the current shader
|
||||
const std::string& GetName() const
|
||||
{
|
||||
return myIterator->first;
|
||||
}
|
||||
|
||||
// Get the current shader
|
||||
const sf::Shader& GetShader() const
|
||||
{
|
||||
return myIterator->second;
|
||||
}
|
||||
|
||||
private :
|
||||
|
||||
std::map<std::string, sf::Shader>* myOwner;
|
||||
std::map<std::string, sf::Shader>::iterator myIterator;
|
||||
};
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Entry point of application
|
||||
///
|
||||
/// \return Application exit code
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
int main()
|
||||
{
|
||||
// Check that the system can use shaders
|
||||
if (sf::Shader::IsAvailable() == false)
|
||||
{
|
||||
DisplayError();
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
// Create the main window
|
||||
sf::RenderWindow window(sf::VideoMode(800, 600), "SFML Shader");
|
||||
|
||||
// Create the render image
|
||||
sf::RenderImage image;
|
||||
if (!image.Create(window.GetWidth(), window.GetHeight()))
|
||||
return EXIT_FAILURE;
|
||||
|
||||
// Load a background image to display
|
||||
sf::Image backgroundImage;
|
||||
if (!backgroundImage.LoadFromFile("datas/shader/background.jpg"))
|
||||
return EXIT_FAILURE;
|
||||
sf::Sprite background(backgroundImage);
|
||||
backgroundImage.SetSmooth(false);
|
||||
|
||||
// Load a sprite which we'll move into the scene
|
||||
sf::Image entityImage;
|
||||
if (!entityImage.LoadFromFile("datas/shader/sprite.png"))
|
||||
return EXIT_FAILURE;
|
||||
sf::Sprite entity(entityImage);
|
||||
|
||||
// Load the text font
|
||||
sf::Font font;
|
||||
if (!font.LoadFromFile("datas/shader/arial.ttf", 20))
|
||||
return EXIT_FAILURE;
|
||||
|
||||
// Load the image needed for the wave shader
|
||||
sf::Image waveImage;
|
||||
if (!waveImage.LoadFromFile("datas/shader/wave.jpg"))
|
||||
return EXIT_FAILURE;
|
||||
|
||||
// Load all shaders
|
||||
std::map<std::string, sf::Shader> shaders;
|
||||
if (!shaders["nothing"].LoadFromFile("datas/shader/nothing.sfx")) return EXIT_FAILURE;
|
||||
if (!shaders["blur"].LoadFromFile("datas/shader/blur.sfx")) return EXIT_FAILURE;
|
||||
if (!shaders["colorize"].LoadFromFile("datas/shader/colorize.sfx")) return EXIT_FAILURE;
|
||||
if (!shaders["fisheye"].LoadFromFile("datas/shader/fisheye.sfx")) return EXIT_FAILURE;
|
||||
if (!shaders["wave"].LoadFromFile("datas/shader/wave.sfx")) return EXIT_FAILURE;
|
||||
if (!shaders["pixelate"].LoadFromFile("datas/shader/pixelate.sfx")) return EXIT_FAILURE;
|
||||
ShaderSelector backgroundShader(shaders, "nothing");
|
||||
ShaderSelector entityShader(shaders, "nothing");
|
||||
ShaderSelector globalShader(shaders, "nothing");
|
||||
|
||||
// Do specific initializations
|
||||
shaders["nothing"].SetTexture("texture", sf::Shader::CurrentTexture);
|
||||
shaders["blur"].SetTexture("texture", sf::Shader::CurrentTexture);
|
||||
shaders["blur"].SetParameter("offset", 0.f);
|
||||
shaders["colorize"].SetTexture("texture", sf::Shader::CurrentTexture);
|
||||
shaders["colorize"].SetParameter("color", 1.f, 1.f, 1.f);
|
||||
shaders["fisheye"].SetTexture("texture", sf::Shader::CurrentTexture);
|
||||
shaders["wave"].SetTexture("texture", sf::Shader::CurrentTexture);
|
||||
shaders["wave"].SetTexture("wave", waveImage);
|
||||
shaders["pixelate"].SetTexture("texture", sf::Shader::CurrentTexture);
|
||||
|
||||
// Define a string for displaying the description of the current shader
|
||||
sf::String shaderStr;
|
||||
shaderStr.SetFont(font);
|
||||
shaderStr.SetSize(20);
|
||||
shaderStr.SetPosition(5.f, 0.f);
|
||||
shaderStr.SetColor(sf::Color(250, 100, 30));
|
||||
shaderStr.SetText("Background shader: \"" + backgroundShader.GetName() + "\"\n"
|
||||
"Flower shader: \"" + entityShader.GetName() + "\"\n"
|
||||
"Global shader: \"" + globalShader.GetName() + "\"\n");
|
||||
|
||||
// Define a string for displaying help
|
||||
sf::String infoStr;
|
||||
infoStr.SetFont(font);
|
||||
infoStr.SetSize(20);
|
||||
infoStr.SetPosition(5.f, 510.f);
|
||||
infoStr.SetColor(sf::Color(250, 100, 30));
|
||||
infoStr.SetText("Move your mouse to change the shaders' parameters\n"
|
||||
"Press numpad 1/4 to change the background shader\n"
|
||||
"Press numpad 2/5 to change the flower shader\n"
|
||||
"Press numpad 3/6 to change the global shader");
|
||||
|
||||
// Create a clock to measure the total time elapsed
|
||||
sf::Clock clock;
|
||||
|
||||
// Start the game loop
|
||||
while (window.IsOpened())
|
||||
{
|
||||
// Process events
|
||||
sf::Event event;
|
||||
while (window.GetEvent(event))
|
||||
{
|
||||
// Close window : exit
|
||||
if (event.Type == sf::Event::Closed)
|
||||
window.Close();
|
||||
|
||||
if (event.Type == sf::Event::KeyPressed)
|
||||
{
|
||||
// Escape key : exit
|
||||
if (event.Key.Code == sf::Key::Escape)
|
||||
window.Close();
|
||||
|
||||
// Numpad : switch effect
|
||||
switch (event.Key.Code)
|
||||
{
|
||||
case sf::Key::Numpad1 : backgroundShader.GotoPrevious(); break;
|
||||
case sf::Key::Numpad4 : backgroundShader.GotoNext(); break;
|
||||
case sf::Key::Numpad2 : entityShader.GotoPrevious(); break;
|
||||
case sf::Key::Numpad5 : entityShader.GotoNext(); break;
|
||||
case sf::Key::Numpad3 : globalShader.GotoPrevious(); break;
|
||||
case sf::Key::Numpad6 : globalShader.GotoNext(); break;
|
||||
}
|
||||
|
||||
// Update the text
|
||||
shaderStr.SetText("Background shader: \"" + backgroundShader.GetName() + "\"\n"
|
||||
"Entity shader: \"" + entityShader.GetName() + "\"\n"
|
||||
"Global shader: \"" + globalShader.GetName() + "\"\n");
|
||||
}
|
||||
}
|
||||
|
||||
// Get the mouse position in the range [0, 1]
|
||||
float mouseX = window.GetInput().GetMouseX() / static_cast<float>(window.GetWidth());
|
||||
float mouseY = window.GetInput().GetMouseY() / static_cast<float>(window.GetHeight());
|
||||
|
||||
// Update the shaders
|
||||
backgroundShader.Update(mouseX, mouseY);
|
||||
entityShader.Update(mouseX, mouseY);
|
||||
globalShader.Update(mouseX, mouseY);
|
||||
|
||||
// Animate the entity
|
||||
float entityX = (cos(clock.GetElapsedTime() * 1.3f) + 1.2f) * 300;
|
||||
float entityY = (cos(clock.GetElapsedTime() * 0.8f) + 1.2f) * 200;
|
||||
entity.SetPosition(entityX, entityY);
|
||||
entity.Rotate(window.GetFrameTime() * 100);
|
||||
|
||||
// Draw the background and the moving entity to the render image
|
||||
image.Clear();
|
||||
image.Draw(background, backgroundShader.GetShader());
|
||||
image.Draw(entity, entityShader.GetShader());
|
||||
image.Display();
|
||||
|
||||
// Draw the contents of the render image to the window
|
||||
sf::Sprite screen(image.GetImage());
|
||||
window.Draw(screen, globalShader.GetShader());
|
||||
|
||||
// Draw the interface texts
|
||||
window.Draw(shaderStr);
|
||||
window.Draw(infoStr);
|
||||
|
||||
// Finally, display the rendered frame on screen
|
||||
window.Display();
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Fonction called when the post-effects are not supported ;
|
||||
/// Display an error message and wait until the user exits
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
void DisplayError()
|
||||
{
|
||||
// Create the main window
|
||||
sf::RenderWindow window(sf::VideoMode(800, 600), "SFML Shader");
|
||||
|
||||
// Define a string for displaying the error message
|
||||
sf::String error("Sorry, your system doesn't support shaders");
|
||||
error.SetPosition(100.f, 250.f);
|
||||
error.SetColor(sf::Color(200, 100, 150));
|
||||
|
||||
// Start the game loop
|
||||
while (window.IsOpened())
|
||||
{
|
||||
// Process events
|
||||
sf::Event event;
|
||||
while (window.GetEvent(event))
|
||||
{
|
||||
// Close window : exit
|
||||
if (event.Type == sf::Event::Closed)
|
||||
window.Close();
|
||||
|
||||
// Escape key : exit
|
||||
if ((event.Type == sf::Event::KeyPressed) && (event.Key.Code == sf::Key::Escape))
|
||||
window.Close();
|
||||
}
|
||||
|
||||
// Clear the window
|
||||
window.Clear();
|
||||
|
||||
// Draw the error message
|
||||
window.Draw(error);
|
||||
|
||||
// Finally, display the rendered frame on screen
|
||||
window.Display();
|
||||
}
|
||||
}
|
|
@ -16,7 +16,7 @@
|
|||
int main()
|
||||
{
|
||||
// Check that the device can capture audio
|
||||
if (sf::SoundRecorder::CanCapture() == false)
|
||||
if (sf::SoundRecorder::IsAvailable() == false)
|
||||
{
|
||||
std::cout << "Sorry, audio capture is not supported by your system" << std::endl;
|
||||
return EXIT_SUCCESS;
|
||||
|
|
|
@ -63,7 +63,7 @@ private :
|
|||
void DoClient(unsigned short port)
|
||||
{
|
||||
// Check that the device can capture audio
|
||||
if (sf::SoundRecorder::CanCapture() == false)
|
||||
if (sf::SoundRecorder::IsAvailable() == false)
|
||||
{
|
||||
std::cout << "Sorry, audio capture is not supported by your system" << std::endl;
|
||||
return;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue