using System;
using System.Runtime.InteropServices;
using System.Security;
using System.Collections.Generic;
namespace SFML
{
namespace Graphics
{
////////////////////////////////////////////////////////////
///
/// PostFX is used to apply a post effect to a window
///
////////////////////////////////////////////////////////////
public class PostFx : ObjectBase
{
////////////////////////////////////////////////////////////
///
/// Default constructor (invalid effect)
///
///
////////////////////////////////////////////////////////////
public PostFx() :
base(sfPostFx_Create())
{
if (This == IntPtr.Zero)
throw new LoadingFailedException("post-fx");
}
////////////////////////////////////////////////////////////
///
/// Load the effect from a file
///
/// Path of the effect file to load
///
////////////////////////////////////////////////////////////
public PostFx(string filename) :
base(sfPostFX_CreateFromFile(filename))
{
if (This == IntPtr.Zero)
throw new LoadingFailedException("post-fx", filename);
}
////////////////////////////////////////////////////////////
///
/// Load the effect from a text in memory
///
/// String containing the effect code
///
////////////////////////////////////////////////////////////
void LoadFromString(string effect)
{
SetThis(sfPostFX_CreateFromMemory(effect));
if (This == IntPtr.Zero)
throw new LoadingFailedException("post-fx");
}
////////////////////////////////////////////////////////////
///
/// Change a 1-component parameter of the effect
///
/// Name of the parameter in the effect
/// Value of the parameter
////////////////////////////////////////////////////////////
public void SetParameter(string name, float x)
{
sfPostFX_SetParameter1(This, name, x);
}
////////////////////////////////////////////////////////////
///
/// Change a 2-component parameter of the effect
///
/// Name of the parameter in the effect
/// X component of the value
/// Y component of the value
////////////////////////////////////////////////////////////
public void SetParameter(string name, float x, float y)
{
sfPostFX_SetParameter2(This, name, x, y);
}
////////////////////////////////////////////////////////////
///
/// Change a 3-component parameter of the effect
///
/// Name of the parameter in the effect
/// 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)
{
sfPostFX_SetParameter3(This, name, x, y, z);
}
////////////////////////////////////////////////////////////
///
/// Change a 4-component parameter of the effect
///
/// Name of the parameter in the effect
/// 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)
{
sfPostFX_SetParameter4(This, name, x, y, z, w);
}
////////////////////////////////////////////////////////////
///
/// Set a texture parameter
///
/// Name of the texture in the effect
/// Image to set (pass null to use the contents of the screen)
////////////////////////////////////////////////////////////
public void SetTexture(string name, Image texture)
{
myTextures[name] = texture;
sfPostFX_SetTexture(This, name, texture != null ? texture.This : IntPtr.Zero);
}
////////////////////////////////////////////////////////////
///
/// Tell whether or not the system supports post-effects
///
////////////////////////////////////////////////////////////
public static bool CanUsePostFX
{
get {return sfPostFX_CanUsePostFX();}
}
////////////////////////////////////////////////////////////
///
/// 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();
sfPostFX_Destroy(This);
if (!disposing)
Context.Global.SetActive(false);
}
Dictionary myTextures = new Dictionary();
#region Imports
[DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity]
static extern IntPtr sfPostFx_Create();
[DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity]
static extern void sfPostFx_Destroy(IntPtr This);
[DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity]
static extern IntPtr sfPostFX_CreateFromFile(string Filename);
[DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity]
static extern IntPtr sfPostFX_CreateFromMemory(string Effect);
[DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity]
static extern void sfPostFX_Destroy(IntPtr PostFX);
[DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity]
static extern void sfPostFX_SetParameter1(IntPtr PostFX, string Name, float X);
[DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity]
static extern void sfPostFX_SetParameter2(IntPtr PostFX, string Name, float X, float Y);
[DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity]
static extern void sfPostFX_SetParameter3(IntPtr PostFX, string Name, float X, float Y, float Z);
[DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity]
static extern void sfPostFX_SetParameter4(IntPtr PostFX, string Name, float X, float Y, float Z, float W);
[DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity]
static extern void sfPostFX_SetTexture(IntPtr PostFX, string Name, IntPtr Texture);
[DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity]
static extern bool sfPostFX_CanUsePostFX();
#endregion
}
}
}