Renamed / moved / updated the SFML.Net examples (2)
git-svn-id: https://sfml.svn.sourceforge.net/svnroot/sfml/branches/sfml2@1558 4e206d99-4929-0410-ac5d-dfc041789085
This commit is contained in:
parent
177c82a197
commit
80b803fe93
34 changed files with 1747 additions and 0 deletions
250
dotnet/examples/shader/Shader.cs
Normal file
250
dotnet/examples/shader/Shader.cs
Normal file
|
@ -0,0 +1,250 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using SFML;
|
||||
using SFML.Graphics;
|
||||
using SFML.Window;
|
||||
|
||||
namespace sample_shader
|
||||
{
|
||||
/// <summary>
|
||||
/// A class to simplify shader selection
|
||||
/// </summary>
|
||||
class ShaderSelector
|
||||
{
|
||||
// Constructor
|
||||
public ShaderSelector(Dictionary<string, Shader> owner)
|
||||
{
|
||||
myOwner = owner;
|
||||
myIterator = owner.GetEnumerator();
|
||||
myIterator.MoveNext();
|
||||
}
|
||||
|
||||
// Select the next shader
|
||||
public void GotoNext()
|
||||
{
|
||||
if (myIterator.MoveNext() == false)
|
||||
{
|
||||
myIterator = myOwner.GetEnumerator();
|
||||
myIterator.MoveNext();
|
||||
}
|
||||
}
|
||||
|
||||
// Update the shader parameters
|
||||
public void Update(float x, float y)
|
||||
{
|
||||
if (myIterator.Current.Key == "blur") myIterator.Current.Value.SetParameter("offset", x * y * 0.05f);
|
||||
else if (myIterator.Current.Key == "colorize") myIterator.Current.Value.SetParameter("color", 0.3f, x, y);
|
||||
else if (myIterator.Current.Key == "fisheye") myIterator.Current.Value.SetParameter("mouse", x, y);
|
||||
else if (myIterator.Current.Key == "wave") myIterator.Current.Value.SetParameter("offset", x, y);
|
||||
else if (myIterator.Current.Key == "pixelate") myIterator.Current.Value.SetParameter("mouse", x, y);
|
||||
}
|
||||
|
||||
// Get the name of the current shader
|
||||
public string Name
|
||||
{
|
||||
get {return myIterator.Current.Key;}
|
||||
}
|
||||
|
||||
// Get the current shader
|
||||
public Shader Shader
|
||||
{
|
||||
get {return myIterator.Current.Value;}
|
||||
}
|
||||
|
||||
private Dictionary<string, Shader> myOwner;
|
||||
private Dictionary<string, Shader>.Enumerator myIterator;
|
||||
};
|
||||
|
||||
static class Program
|
||||
{
|
||||
private static Dictionary<string, Shader> shaders;
|
||||
private static ShaderSelector backgroundShader;
|
||||
private static ShaderSelector entityShader;
|
||||
private static ShaderSelector globalShader;
|
||||
private static Text shaderText;
|
||||
|
||||
/// <summary>
|
||||
/// The main entry point for the application.
|
||||
/// </summary>
|
||||
static void Main()
|
||||
{
|
||||
// Create the main window
|
||||
RenderWindow window = new RenderWindow(new VideoMode(800, 600), "SFML.Net Shader");
|
||||
|
||||
// Setup event handlers
|
||||
window.Closed += new EventHandler(OnClosed);
|
||||
window.KeyPressed += new EventHandler<KeyEventArgs>(OnKeyPressed);
|
||||
|
||||
// Check that the system can use shaders
|
||||
if (Shader.IsAvailable == false)
|
||||
{
|
||||
DisplayError(window);
|
||||
return;
|
||||
}
|
||||
|
||||
// Create the render image
|
||||
RenderImage image = new RenderImage(window.Width, window.Height);
|
||||
|
||||
// Load a background image to display
|
||||
Sprite background = new Sprite(new Image("datas/shader/background.jpg"));
|
||||
background.Image.Smooth = false;
|
||||
|
||||
// Load a sprite which we'll move into the scene
|
||||
Sprite entity = new Sprite(new Image("datas/shader/sprite.png"));
|
||||
|
||||
// Load the text font
|
||||
Font font = new Font("datas/shader/arial.ttf");
|
||||
|
||||
// Load the image needed for the wave effect
|
||||
Image waveImage = new Image("datas/shader/wave.jpg");
|
||||
|
||||
// Load all effects
|
||||
shaders = new Dictionary<string, Shader>();
|
||||
shaders["nothing"] = new Shader("datas/shader/nothing.sfx");
|
||||
shaders["blur"] = new Shader("datas/shader/blur.sfx");
|
||||
shaders["colorize"] = new Shader("datas/shader/colorize.sfx");
|
||||
shaders["fisheye"] = new Shader("datas/shader/fisheye.sfx");
|
||||
shaders["wave"] = new Shader("datas/shader/wave.sfx");
|
||||
shaders["pixelate"] = new Shader("datas/shader/pixelate.sfx");
|
||||
backgroundShader = new ShaderSelector(shaders);
|
||||
entityShader = new ShaderSelector(shaders);
|
||||
globalShader = new ShaderSelector(shaders);
|
||||
|
||||
// Do specific initializations
|
||||
shaders["nothing"].SetTexture("texture", Shader.CurrentTexture);
|
||||
shaders["blur"].SetTexture("texture", Shader.CurrentTexture);
|
||||
shaders["blur"].SetParameter("offset", 0.0F);
|
||||
shaders["colorize"].SetTexture("texture", Shader.CurrentTexture);
|
||||
shaders["colorize"].SetParameter("color", 1.0F, 1.0F, 1.0F);
|
||||
shaders["fisheye"].SetTexture("texture", Shader.CurrentTexture);
|
||||
shaders["wave"].SetTexture("texture", Shader.CurrentTexture);
|
||||
shaders["wave"].SetTexture("wave", waveImage);
|
||||
shaders["pixelate"].SetTexture("texture", Shader.CurrentTexture);
|
||||
|
||||
// Define a string for displaying current effect description
|
||||
shaderText = new Text();
|
||||
shaderText.Font = font;
|
||||
shaderText.Size = 20;
|
||||
shaderText.Position = new Vector2(5.0F, 0.0F);
|
||||
shaderText.Color = new Color(250, 100, 30);
|
||||
shaderText.DisplayedString = "Background shader: \"" + backgroundShader.Name + "\"\n" +
|
||||
"Flower shader: \"" + entityShader.Name + "\"\n" +
|
||||
"Global shader: \"" + globalShader.Name + "\"\n";
|
||||
|
||||
// Define a string for displaying help
|
||||
Text infoText = new Text();
|
||||
infoText.Font = font;
|
||||
infoText.Size = 20;
|
||||
infoText.Position = new Vector2(5.0F, 500.0F);
|
||||
infoText.Color = new Color(250, 100, 30);
|
||||
infoText.DisplayedString = "Move your mouse to change the shaders' parameters\n" +
|
||||
"Press numpad 1 to change the background shader\n" +
|
||||
"Press numpad 2 to change the flower shader\n" +
|
||||
"Press numpad 3 to change the global shader";
|
||||
|
||||
// Start the game loop
|
||||
float time = 0.0F;
|
||||
while (window.IsOpened())
|
||||
{
|
||||
// Process events
|
||||
window.DispatchEvents();
|
||||
|
||||
// TOFIX -- using window.Input together with image.Draw apparently causes a memory corruption
|
||||
// Get the mouse position in the range [0, 1]
|
||||
//float x = window.Input.GetMouseX() / (float)window.Width;
|
||||
//float y = window.Input.GetMouseY() / (float)window.Height;
|
||||
float x = (float)(Math.Cos(time * 1.3) + 1) * 0.5F;
|
||||
float y = (float)(Math.Sin(time * 0.8) + 1) * 0.5F;
|
||||
|
||||
// Update the shaders
|
||||
backgroundShader.Update(x, y);
|
||||
entityShader.Update(x, y);
|
||||
globalShader.Update(x, y);
|
||||
|
||||
// Animate the sprite
|
||||
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;
|
||||
|
||||
// Draw the background and the moving entity to the render image
|
||||
image.Draw(background, backgroundShader.Shader);
|
||||
image.Draw(entity, entityShader.Shader);
|
||||
image.Display();
|
||||
|
||||
// Draw the contents of the render image to the window
|
||||
window.Draw(new Sprite(image.Image), globalShader.Shader);
|
||||
|
||||
// Draw interface texts
|
||||
window.Draw(shaderText);
|
||||
window.Draw(infoText);
|
||||
|
||||
// Finally, display the rendered frame on screen
|
||||
window.Display();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fonction called when the post-effects are not supported ;
|
||||
/// Display an error message and wait until the user exits
|
||||
/// </summary>
|
||||
private static void DisplayError(RenderWindow window)
|
||||
{
|
||||
// Define a string for displaying the error message
|
||||
Text error = new Text("Sorry, your system doesn't support shaders");
|
||||
error.Position = new Vector2(100.0F, 250.0F);
|
||||
error.Color = new Color(200, 100, 150);
|
||||
|
||||
// Start the game loop
|
||||
while (window.IsOpened())
|
||||
{
|
||||
// Process events
|
||||
window.DispatchEvents();
|
||||
|
||||
// Clear the window
|
||||
window.Clear();
|
||||
|
||||
// Draw the error message
|
||||
window.Draw(error);
|
||||
|
||||
// Finally, display the rendered frame on screen
|
||||
window.Display();
|
||||
}
|
||||
}
|
||||
|
||||
/// <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;
|
||||
|
||||
// Escape key : exit
|
||||
if (e.Code == KeyCode.Escape)
|
||||
window.Close();
|
||||
|
||||
// Numpad : switch effect
|
||||
switch (e.Code)
|
||||
{
|
||||
case KeyCode.Numpad1 : backgroundShader.GotoNext(); break;
|
||||
case KeyCode.Numpad2 : entityShader.GotoNext(); break;
|
||||
case KeyCode.Numpad3 : globalShader.GotoNext(); break;
|
||||
}
|
||||
|
||||
// Update the text
|
||||
shaderText.DisplayedString = "Background shader: \"" + backgroundShader.Name + "\"\n" +
|
||||
"Flower shader: \"" + entityShader.Name + "\"\n" +
|
||||
"Global shader: \"" + globalShader.Name + "\"\n";
|
||||
}
|
||||
}
|
||||
}
|
60
dotnet/examples/shader/sample-shader.csproj
Normal file
60
dotnet/examples/shader/sample-shader.csproj
Normal file
|
@ -0,0 +1,60 @@
|
|||
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5">
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProductVersion>9.0.21022</ProductVersion>
|
||||
<SchemaVersion>2.0</SchemaVersion>
|
||||
<ProjectGuid>{9D4738F7-34EA-433A-A765-AF85A52A174D}</ProjectGuid>
|
||||
<OutputType>WinExe</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>sample_shader</RootNamespace>
|
||||
<AssemblyName>shader</AssemblyName>
|
||||
<FileUpgradeFlags>
|
||||
</FileUpgradeFlags>
|
||||
<OldToolsVersion>2.0</OldToolsVersion>
|
||||
<UpgradeBackupLocation>
|
||||
</UpgradeBackupLocation>
|
||||
<StartupObject>sample_shader.Program</StartupObject>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>..\bin\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>..\bin\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\Graphics\sfml-graphics.csproj">
|
||||
<Project>{46786269-57B9-48E7-AA4F-8F4D84609FE6}</Project>
|
||||
<Name>sfml-graphics</Name>
|
||||
</ProjectReference>
|
||||
<ProjectReference Include="..\..\src\Window\sfml-window.csproj">
|
||||
<Project>{D17DE83D-A592-461F-8AF2-53F9E22E1D0F}</Project>
|
||||
<Name>sfml-window</Name>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Shader.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Folder Include="Properties\" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
Other similar extension points exist, see Microsoft.Common.targets.
|
||||
<Target Name="BeforeBuild">
|
||||
</Target>
|
||||
<Target Name="AfterBuild">
|
||||
</Target>
|
||||
-->
|
||||
</Project>
|
Loading…
Add table
Add a link
Reference in a new issue