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