2009-01-29 00:18:34 +08:00
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
# include <SFML/Graphics.hpp>
# include <map>
2009-02-06 21:54:39 +08:00
# include <math.h>
2009-01-29 00:18:34 +08:00
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
2009-07-12 06:17:24 +08:00
sf : : RenderWindow window ( sf : : VideoMode ( 800 , 600 ) , " SFML PostFX " ) ;
2009-01-29 00:18:34 +08:00
2009-02-06 21:54:39 +08:00
// Load a background image to display
2009-07-12 06:17:24 +08:00
sf : : Image backgroundImage ;
if ( ! backgroundImage . LoadFromFile ( " datas/post-fx/background.jpg " ) )
2009-01-29 00:18:34 +08:00
return EXIT_FAILURE ;
2009-07-12 06:17:24 +08:00
sf : : Sprite background ( backgroundImage ) ;
2009-01-29 00:18:34 +08:00
2009-02-06 21:54:39 +08:00
// Load a sprite which we'll move into the scene
2009-07-12 06:17:24 +08:00
sf : : Image entityImage ;
if ( ! entityImage . LoadFromFile ( " datas/post-fx/sprite.png " ) )
2009-02-06 21:54:39 +08:00
return EXIT_FAILURE ;
2009-07-12 06:17:24 +08:00
sf : : Sprite entity ( entityImage ) ;
2009-02-06 21:54:39 +08:00
2009-01-29 00:18:34 +08:00
// Load the text font
2009-07-12 06:17:24 +08:00
sf : : Font font ;
if ( ! font . LoadFromFile ( " datas/post-fx/cheeseburger.ttf " ) )
2009-01-29 00:18:34 +08:00
return EXIT_FAILURE ;
// Load the image needed for the wave effect
2009-07-12 06:17:24 +08:00
sf : : Image waveImage ;
if ( ! waveImage . LoadFromFile ( " datas/post-fx/wave.jpg " ) )
2009-01-29 00:18:34 +08:00
return EXIT_FAILURE ;
// Load all effects
2009-07-12 06:17:24 +08:00
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 " ) ;
2009-01-29 00:18:34 +08:00
// Do specific initializations
2009-07-12 06:17:24 +08:00
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 ) ;
2009-01-29 00:18:34 +08:00
// Define a string for displaying current effect description
2009-07-12 06:17:24 +08:00
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 ) ) ;
2009-01-29 00:18:34 +08:00
// Define a string for displaying help
2009-07-12 06:17:24 +08:00
sf : : String infoStr ;
infoStr . SetText ( " Move your mouse to change the effect parameters \n Press numpad + and - to change effect \n Warning : some effects may not work \n depending on your graphics card " ) ;
infoStr . SetFont ( font ) ;
infoStr . SetPosition ( 20.f , 460.f ) ;
infoStr . SetColor ( sf : : Color ( 200 , 100 , 150 ) ) ;
2009-01-29 00:18:34 +08:00
2009-02-06 21:54:39 +08:00
// Create a clock to measure the total time elapsed
2009-07-12 06:17:24 +08:00
sf : : Clock clock ;
2009-02-06 21:54:39 +08:00
2009-01-29 00:18:34 +08:00
// Start the game loop
2009-07-12 06:17:24 +08:00
while ( window . IsOpened ( ) )
2009-01-29 00:18:34 +08:00
{
// Process events
2009-07-12 06:17:24 +08:00
sf : : Event event ;
while ( window . GetEvent ( event ) )
2009-01-29 00:18:34 +08:00
{
// Close window : exit
2009-07-12 06:17:24 +08:00
if ( event . Type = = sf : : Event : : Closed )
window . Close ( ) ;
2009-01-29 00:18:34 +08:00
2009-07-12 06:17:24 +08:00
if ( event . Type = = sf : : Event : : KeyPressed )
2009-01-29 00:18:34 +08:00
{
// Escape key : exit
2009-07-12 06:17:24 +08:00
if ( event . Key . Code = = sf : : Key : : Escape )
window . Close ( ) ;
2009-01-29 00:18:34 +08:00
// Add key : next effect
2009-07-12 06:17:24 +08:00
if ( event . Key . Code = = sf : : Key : : Add )
2009-01-29 00:18:34 +08:00
{
2009-07-12 06:17:24 +08:00
currentEffect + + ;
if ( currentEffect = = effects . end ( ) )
currentEffect = effects . begin ( ) ;
curFXStr . SetText ( " Current effect is \" " + currentEffect - > first + " \" " ) ;
2009-01-29 00:18:34 +08:00
}
// Subtract key : previous effect
2009-07-12 06:17:24 +08:00
if ( event . Key . Code = = sf : : Key : : Subtract )
2009-01-29 00:18:34 +08:00
{
2009-07-12 06:17:24 +08:00
if ( currentEffect = = effects . begin ( ) )
currentEffect = effects . end ( ) ;
currentEffect - - ;
curFXStr . SetText ( " Current effect is \" " + currentEffect - > first + " \" " ) ;
2009-01-29 00:18:34 +08:00
}
}
}
// Get the mouse position in the range [0, 1]
2009-07-12 06:17:24 +08:00
float mouseX = window . GetInput ( ) . GetMouseX ( ) / static_cast < float > ( window . GetWidth ( ) ) ;
float mouseY = window . GetInput ( ) . GetMouseY ( ) / static_cast < float > ( window . GetHeight ( ) ) ;
2009-01-29 00:18:34 +08:00
// Update the current effect
2009-07-12 06:17:24 +08:00
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 ) ;
2009-02-06 21:54:39 +08:00
// Animate the sprite
2009-07-12 06:17:24 +08:00
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 ) ;
2009-01-29 00:18:34 +08:00
// Clear the window
2009-07-12 06:17:24 +08:00
window . Clear ( ) ;
2009-01-29 00:18:34 +08:00
2009-02-06 21:54:39 +08:00
// Draw background, sprite and apply the post-fx
2009-07-12 06:17:24 +08:00
window . Draw ( background ) ;
window . Draw ( entity ) ;
window . Draw ( currentEffect - > second ) ;
2009-01-29 00:18:34 +08:00
// Draw interface strings
2009-07-12 06:17:24 +08:00
window . Draw ( curFXStr ) ;
window . Draw ( infoStr ) ;
2009-01-29 00:18:34 +08:00
// Finally, display the rendered frame on screen
2009-07-12 06:17:24 +08:00
window . Display ( ) ;
2009-01-29 00:18:34 +08:00
}
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
2009-07-12 06:17:24 +08:00
sf : : RenderWindow window ( sf : : VideoMode ( 800 , 600 ) , " SFML PostFX " ) ;
2009-01-29 00:18:34 +08:00
// Define a string for displaying the error message
2009-07-12 06:17:24 +08:00
sf : : String error ( " Sorry, your system doesn't support post-effects " ) ;
error . SetPosition ( 100.f , 250.f ) ;
error . SetColor ( sf : : Color ( 200 , 100 , 150 ) ) ;
2009-01-29 00:18:34 +08:00
// Start the game loop
2009-07-12 06:17:24 +08:00
while ( window . IsOpened ( ) )
2009-01-29 00:18:34 +08:00
{
// Process events
2009-07-12 06:17:24 +08:00
sf : : Event event ;
while ( window . GetEvent ( event ) )
2009-01-29 00:18:34 +08:00
{
// Close window : exit
2009-07-12 06:17:24 +08:00
if ( event . Type = = sf : : Event : : Closed )
window . Close ( ) ;
2009-01-29 00:18:34 +08:00
// Escape key : exit
2009-07-12 06:17:24 +08:00
if ( ( event . Type = = sf : : Event : : KeyPressed ) & & ( event . Key . Code = = sf : : Key : : Escape ) )
window . Close ( ) ;
2009-01-29 00:18:34 +08:00
}
// Clear the window
2009-07-12 06:17:24 +08:00
window . Clear ( ) ;
2009-01-29 00:18:34 +08:00
// Draw the error message
2009-07-12 06:17:24 +08:00
window . Draw ( error ) ;
2009-01-29 00:18:34 +08:00
// Finally, display the rendered frame on screen
2009-07-12 06:17:24 +08:00
window . Display ( ) ;
2009-01-29 00:18:34 +08:00
}
}