2009-01-29 00:18:34 +08:00
using System ;
using System.Collections.Generic ;
using SFML ;
using SFML.Graphics ;
using SFML.Window ;
namespace sample_postfx
{
static class Program
{
2009-07-17 16:11:03 +08:00
private static Dictionary < string , PostFx > effects ;
private static Dictionary < string , PostFx > . Enumerator currentEffect ;
private static String2D curFXStr ;
2009-01-29 00:18:34 +08:00
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main ( )
{
// Create the main window
2009-07-17 16:11:03 +08:00
RenderWindow window = new RenderWindow ( new VideoMode ( 800 , 600 ) , "SFML.Net PostFX" ) ;
2009-01-29 00:18:34 +08:00
// Setup event handlers
2009-07-17 16:11:03 +08:00
window . Closed + = new EventHandler ( OnClosed ) ;
window . KeyPressed + = new EventHandler < KeyEventArgs > ( OnKeyPressed ) ;
2009-01-29 00:18:34 +08:00
// Check that the system can use post effects
if ( PostFx . CanUsePostFX = = false )
{
2009-07-17 16:11:03 +08:00
DisplayError ( window ) ;
2009-01-29 00:18:34 +08:00
return ;
}
2009-02-06 21:54:39 +08:00
// Load a background image to display
2009-07-17 16:11:03 +08:00
Sprite background = new Sprite ( new Image ( "datas/post-fx/background.jpg" ) ) ;
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-17 16:11:03 +08:00
Sprite entity = new Sprite ( new Image ( "datas/post-fx/sprite.png" ) ) ;
2009-02-06 21:54:39 +08:00
2009-01-29 00:18:34 +08:00
// Load the text font
2009-07-17 16:11:03 +08:00
Font cheeseburger = new Font ( "datas/post-fx/cheeseburger.ttf" ) ;
2009-01-29 00:18:34 +08:00
// Load the image needed for the wave effect
2009-07-17 16:11:03 +08:00
Image waveImage = new Image ( "datas/post-fx/wave.jpg" ) ;
2009-01-29 00:18:34 +08:00
// Load all effects
2009-07-17 16:11:03 +08:00
effects = new Dictionary < string , PostFx > ( ) ;
effects [ "nothing" ] = new PostFx ( "datas/post-fx/nothing.sfx" ) ;
effects [ "blur" ] = new PostFx ( "datas/post-fx/blur.sfx" ) ;
effects [ "colorize" ] = new PostFx ( "datas/post-fx/colorize.sfx" ) ;
effects [ "fisheye" ] = new PostFx ( "datas/post-fx/fisheye.sfx" ) ;
effects [ "wave" ] = new PostFx ( "datas/post-fx/wave.sfx" ) ;
effects [ "pixelate" ] = new PostFx ( "datas/post-fx/pixelate.sfx" ) ;
currentEffect = effects . GetEnumerator ( ) ;
currentEffect . MoveNext ( ) ;
2009-01-29 00:18:34 +08:00
// Do specific initializations
2009-07-17 16:11:03 +08:00
effects [ "nothing" ] . SetTexture ( "framebuffer" , null ) ;
effects [ "blur" ] . SetTexture ( "framebuffer" , null ) ;
effects [ "blur" ] . SetParameter ( "offset" , 0.0F ) ;
effects [ "colorize" ] . SetTexture ( "framebuffer" , null ) ;
effects [ "colorize" ] . SetParameter ( "color" , 1.0F , 1.0F , 1.0F ) ;
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-17 16:11:03 +08:00
curFXStr = new String2D ( ) ;
curFXStr . Text = "Current effect is \"" + currentEffect . Current . Key + "\"" ;
curFXStr . Font = cheeseburger ;
curFXStr . Position = new Vector2 ( 20.0F , 0.0F ) ;
curFXStr . Color = new Color ( 150 , 70 , 110 ) ;
2009-01-29 00:18:34 +08:00
// Define a string for displaying help
2009-07-17 16:11:03 +08:00
String2D infoStr = new String2D ( ) ;
infoStr . Text = "Move your mouse to change the effect parameters\nPress numpad + to change effect\nWarning : some effects may not work\ndepending on your graphics card" ;
infoStr . Font = cheeseburger ;
infoStr . Position = new Vector2 ( 20.0F , 460.0F ) ;
infoStr . Color = new Color ( 200 , 100 , 150 ) ;
2009-01-29 00:18:34 +08:00
// Start the game loop
2009-07-17 16:11:03 +08:00
float time = 0.0F ;
while ( window . IsOpened ( ) )
2009-01-29 00:18:34 +08:00
{
// Process events
2009-07-17 16:11:03 +08:00
window . DispatchEvents ( ) ;
2009-01-29 00:18:34 +08:00
// Get the mouse position in the range [0, 1]
2009-07-17 16:11:03 +08:00
float x = window . Input . GetMouseX ( ) / ( float ) window . Width ;
float y = window . Input . GetMouseY ( ) / ( float ) window . Height ;
2009-01-29 00:18:34 +08:00
// Update the current effect
2009-07-17 16:11:03 +08:00
if ( currentEffect . Current . Key = = "blur" ) currentEffect . Current . Value . SetParameter ( "offset" , x * y * 0.1f ) ;
else if ( currentEffect . Current . Key = = "colorize" ) currentEffect . Current . Value . SetParameter ( "color" , 0.3f , x , y ) ;
else if ( currentEffect . Current . Key = = "fisheye" ) currentEffect . Current . Value . SetParameter ( "mouse" , x , 1.0F - y ) ;
else if ( currentEffect . Current . Key = = "wave" ) currentEffect . Current . Value . SetParameter ( "offset" , x , y ) ;
else if ( currentEffect . Current . Key = = "pixelate" ) currentEffect . Current . Value . SetParameter ( "mouse" , x , y ) ;
2009-02-06 21:54:39 +08:00
// Animate the sprite
2009-07-17 16:11:03 +08:00
time + = window . GetFrameTime ( ) ;
float entityX = ( float ) ( Math . Cos ( time * 1.3 ) + 1.2 ) * 300 ;
float entityY = ( float ) ( Math . Cos ( time * 0.8 ) + 1.2 ) * 200 ;
entity . Position = new Vector2 ( entityX , entityY ) ;
entity . Rotation = time * 100 ;
2009-01-29 00:18:34 +08:00
// Clear the window
2009-07-17 16:11:03 +08:00
window . Clear ( ) ;
2009-01-29 00:18:34 +08:00
2009-02-06 21:54:39 +08:00
// Draw background, the sprite and apply the post-fx
2009-07-17 16:11:03 +08:00
window . Draw ( background ) ;
window . Draw ( entity ) ;
window . Draw ( currentEffect . Current . Value ) ;
2009-01-29 00:18:34 +08:00
// Draw interface strings
2009-07-17 16:11:03 +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-17 16:11:03 +08:00
window . Display ( ) ;
2009-01-29 00:18:34 +08:00
}
}
/// <summary>
/// Fonction called when the post-effects are not supported ;
/// Display an error message and wait until the user exits
/// </summary>
2009-07-17 16:11:03 +08:00
private static void DisplayError ( RenderWindow window )
2009-01-29 00:18:34 +08:00
{
// Define a string for displaying the error message
2009-07-17 16:11:03 +08:00
String2D errorStr = new String2D ( "Sorry, your system doesn't support post-effects" ) ;
errorStr . Position = new Vector2 ( 100.0F , 250.0F ) ;
errorStr . Color = new Color ( 200 , 100 , 150 ) ;
2009-01-29 00:18:34 +08:00
// Start the game loop
2009-07-17 16:11:03 +08:00
while ( window . IsOpened ( ) )
2009-01-29 00:18:34 +08:00
{
// Process events
2009-07-17 16:11:03 +08:00
window . DispatchEvents ( ) ;
2009-01-29 00:18:34 +08:00
// Clear the window
2009-07-17 16:11:03 +08:00
window . Clear ( ) ;
2009-01-29 00:18:34 +08:00
// Draw the error message
2009-07-17 16:11:03 +08:00
window . Draw ( errorStr ) ;
2009-01-29 00:18:34 +08:00
// Finally, display the rendered frame on screen
2009-07-17 16:11:03 +08:00
window . Display ( ) ;
2009-01-29 00:18:34 +08:00
}
}
/// <summary>
/// Function called when the window is closed
/// </summary>
static void OnClosed ( object sender , EventArgs e )
{
RenderWindow window = ( RenderWindow ) sender ;
window . Close ( ) ;
}
/// <summary>
/// Function called when a key is pressed
/// </summary>
static void OnKeyPressed ( object sender , KeyEventArgs e )
{
RenderWindow window = ( RenderWindow ) sender ;
if ( e . Code = = KeyCode . Escape )
{
// Close the window
window . Close ( ) ;
}
else if ( e . Code = = KeyCode . Add )
{
// Advance to the next effect
2009-07-17 16:11:03 +08:00
if ( currentEffect . MoveNext ( ) = = false )
2009-01-29 00:18:34 +08:00
{
2009-07-17 16:11:03 +08:00
currentEffect = effects . GetEnumerator ( ) ;
currentEffect . MoveNext ( ) ;
2009-01-29 00:18:34 +08:00
}
2009-07-17 16:11:03 +08:00
curFXStr . Text = "Current effect is \"" + currentEffect . Current . Key + "\"" ;
2009-01-29 00:18:34 +08:00
}
}
}
}