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:
LaurentGom 2009-11-03 09:04:40 +00:00
parent 63e07cec84
commit d7bd00afc0
125 changed files with 1606 additions and 2348 deletions

View file

@ -0,0 +1,238 @@
using System;
using System.Runtime.InteropServices;
using System.Security;
using System.Collections.Generic;
namespace SFML
{
namespace Graphics
{
////////////////////////////////////////////////////////////
/// <summary>
/// Wrapper for pixel shaders
/// </summary>
////////////////////////////////////////////////////////////
public class Shader : ObjectBase
{
////////////////////////////////////////////////////////////
/// <summary>
/// Default constructor (invalid shader)
/// </summary>
/// <exception cref="LoadingFailedException" />
////////////////////////////////////////////////////////////
public Shader() :
base(sfShader_Create())
{
if (This == IntPtr.Zero)
throw new LoadingFailedException("shader");
}
////////////////////////////////////////////////////////////
/// <summary>
/// Load the shader from a file
/// </summary>
/// <param name="filename">Path of the shader file to load</param>
/// <exception cref="LoadingFailedException" />
////////////////////////////////////////////////////////////
public Shader(string filename) :
base(sfShader_CreateFromFile(filename))
{
if (This == IntPtr.Zero)
throw new LoadingFailedException("shader", filename);
}
////////////////////////////////////////////////////////////
/// <summary>
/// Load the shader from a text in memory
/// </summary>
/// <param name="shader">String containing the shader code</param>
/// <exception cref="LoadingFailedException" />
////////////////////////////////////////////////////////////
void LoadFromString(string shader)
{
SetThis(sfShader_CreateFromMemory(shader));
if (This == IntPtr.Zero)
throw new LoadingFailedException("shader");
}
////////////////////////////////////////////////////////////
/// <summary>
/// Change a vector2 parameter of the shader
/// </summary>
/// <param name="name">Name of the parameter in the shader</param>
/// <param name="v">Value of the parameter</param>
////////////////////////////////////////////////////////////
public void SetParameter(string name, Vector2 v)
{
SetParameter(name, v.X, v.Y);
}
////////////////////////////////////////////////////////////
/// <summary>
/// Change a 1-component parameter of the shader
/// </summary>
/// <param name="name">Name of the parameter in the shader</param>
/// <param name="x">Value of the parameter</param>
////////////////////////////////////////////////////////////
public void SetParameter(string name, float x)
{
sfShader_SetParameter1(This, name, x);
}
////////////////////////////////////////////////////////////
/// <summary>
/// Change a 2-component parameter of the shader
/// </summary>
/// <param name="name">Name of the parameter in the shader</param>
/// <param name="x">X component of the value</param>
/// <param name="y">Y component of the value</param>
////////////////////////////////////////////////////////////
public void SetParameter(string name, float x, float y)
{
sfShader_SetParameter2(This, name, x, y);
}
////////////////////////////////////////////////////////////
/// <summary>
/// Change a 3-component parameter of the shader
/// </summary>
/// <param name="name">Name of the parameter in the shader</param>
/// <param name="x">X component of the value</param>
/// <param name="y">Y component of the value</param>
/// <param name="z">Z component of the value</param>
////////////////////////////////////////////////////////////
public void SetParameter(string name, float x, float y, float z)
{
sfShader_SetParameter3(This, name, x, y, z);
}
////////////////////////////////////////////////////////////
/// <summary>
/// Change a 4-component parameter of the shader
/// </summary>
/// <param name="name">Name of the parameter in the shader</param>
/// <param name="x">X component of the value</param>
/// <param name="y">Y component of the value</param>
/// <param name="z">Z component of the value</param>
/// <param name="w">W component of the value</param>
////////////////////////////////////////////////////////////
public void SetParameter(string name, float x, float y, float z, float w)
{
sfShader_SetParameter4(This, name, x, y, z, w);
}
////////////////////////////////////////////////////////////
/// <summary>
/// Set a texture parameter
/// </summary>
/// <param name="name">Name of the texture in the shader</param>
/// <param name="texture">Image to set (pass null to use the texture of the object being drawn)</param>
////////////////////////////////////////////////////////////
public void SetTexture(string name, Image texture)
{
myTextures[name] = texture;
sfShader_SetTexture(This, name, texture != null ? texture.This : IntPtr.Zero);
}
////////////////////////////////////////////////////////////
/// <summary>
/// Bind the shader for rendering
/// </summary>
////////////////////////////////////////////////////////////
public void Bind()
{
sfShader_Bind(This);
}
////////////////////////////////////////////////////////////
/// <summary>
/// Unbind the shader
/// </summary>
////////////////////////////////////////////////////////////
public void Unbind()
{
sfShader_Unbind(This);
}
////////////////////////////////////////////////////////////
/// <summary>
/// Tell whether or not the system supports shaders
/// </summary>
////////////////////////////////////////////////////////////
public static bool IsAvailable
{
get {return sfShader_IsAvailable();}
}
////////////////////////////////////////////////////////////
/// <summary>
/// Special image representing the texture used by the object being drawn
/// </summary>
////////////////////////////////////////////////////////////
public static Image CurrentTexture
{
get {return null;}
}
////////////////////////////////////////////////////////////
/// <summary>
/// Handle the destruction of the object
/// </summary>
/// <param name="disposing">Is the GC disposing the object, or is it an explicit call ?</param>
////////////////////////////////////////////////////////////
protected override void Destroy(bool disposing)
{
if (!disposing)
Context.Global.SetActive(true);
myTextures.Clear();
sfShader_Destroy(This);
if (!disposing)
Context.Global.SetActive(false);
}
Dictionary<string, Image> myTextures = new Dictionary<string, Image>();
#region Imports
[DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity]
static extern IntPtr sfShader_Create();
[DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity]
static extern IntPtr sfShader_CreateFromFile(string Filename);
[DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity]
static extern IntPtr sfShader_CreateFromMemory(string Shader);
[DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity]
static extern void sfShader_Destroy(IntPtr Shader);
[DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity]
static extern void sfShader_SetParameter1(IntPtr Shader, string Name, float X);
[DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity]
static extern void sfShader_SetParameter2(IntPtr Shader, string Name, float X, float Y);
[DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity]
static extern void sfShader_SetParameter3(IntPtr Shader, string Name, float X, float Y, float Z);
[DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity]
static extern void sfShader_SetParameter4(IntPtr Shader, string Name, float X, float Y, float Z, float W);
[DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity]
static extern void sfShader_SetTexture(IntPtr Shader, string Name, IntPtr Texture);
[DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity]
static extern void sfShader_Bind(IntPtr Shader);
[DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity]
static extern void sfShader_Unbind(IntPtr Shader);
[DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity]
static extern bool sfShader_IsAvailable();
#endregion
}
}
}