2010-01-07 04:25:45 +08:00
/ *
2010-01-07 04:37:29 +08:00
* DSFML - SFML Library wrapper for the D programming language .
* Copyright ( C ) 2008 Julien Dagorn ( sirjulio13 @gmail.com )
* Copyright ( C ) 2010 Andreas Hollandt
2010-01-07 04:25:45 +08:00
*
2010-01-07 04:37:29 +08:00
* This software is provided ' as - is ' , without any express or
* implied warranty . In no event will the authors be held
* liable for any damages arising from the use of this software .
2010-01-07 04:25:45 +08:00
*
2010-01-07 04:37:29 +08:00
* Permission is granted to anyone to use this software for any purpose ,
* including commercial applications , and to alter it and redistribute
* it freely , subject to the following restrictions :
2010-01-07 04:25:45 +08:00
*
2010-01-07 04:37:29 +08:00
* 1. The origin of this software must not be misrepresented ;
* you must not claim that you wrote the original software .
* If you use this software in a product , an acknowledgment
* in the product documentation would be appreciated but
* is not required .
2010-01-07 04:25:45 +08:00
*
2010-01-07 04:37:29 +08:00
* 2. Altered source versions must be plainly marked as such ,
* and must not be misrepresented as being the original software .
2010-01-07 04:25:45 +08:00
*
2010-01-07 04:37:29 +08:00
* 3. This notice may not be removed or altered from any
* source distribution .
2010-01-07 04:25:45 +08:00
* /
module dsfml.graphics.shader ;
import dsfml.graphics.image ;
import dsfml.system.common ;
import dsfml.system.exception ;
import dsfml.system.stringutil ;
/ * *
2010-01-07 04:37:29 +08:00
* Define loading methods for effect
2010-01-07 04:25:45 +08:00
* /
enum LoadingType
{
2010-01-07 04:37:29 +08:00
FROMFILE , /// string represents a file path
FROMSTRING /// string represents effect code
2010-01-07 04:25:45 +08:00
}
/ * *
2010-01-07 04:37:29 +08:00
* Shader is used to apply a post effect to a window
2010-01-07 04:25:45 +08:00
*
2010-01-07 04:37:29 +08:00
* See_Also :
* $ ( LINK2 http : //www.sfml-dev.org/tutorials/graphics-postfx.php, SFML post FX tutorial) from more informations about Post effects and GLSL fragment shaders syntax.
2010-01-07 04:25:45 +08:00
* /
class Shader : DSFMLObject
{
2010-01-08 06:00:45 +08:00
private :
Image m_texture ;
public :
2010-01-07 04:37:29 +08:00
/ * *
* construct the effect
*
* Params :
* effect = Path of a file or string containing the effect .
* type = type of the effect ( default is FROMFILE )
* /
this ( string effect , LoadingType type = LoadingType . FROMFILE )
2010-01-07 04:25:45 +08:00
{
2010-01-07 04:37:29 +08:00
if ( effect is null | | effect . length = = 0 )
throw new LoadingException ( "LoadingException : Effect is invalid." ) ;
if ( type = = LoadingType . FROMFILE )
super ( sfShader_CreateFromFile ( toStringz ( effect ) ) ) ;
else
super ( sfShader_CreateFromMemory ( toStringz ( effect ) ) ) ;
2010-01-07 04:25:45 +08:00
}
2010-01-07 04:37:29 +08:00
override void dispose ( )
2010-01-07 04:25:45 +08:00
{
sfShader_Destroy ( m_ptr ) ;
}
2010-01-07 04:37:29 +08:00
/ * *
* Change parameters of the effect
*
* Params :
* name = Parameter name in the effect
* /
void setParameter ( string name , float x )
2010-01-07 04:25:45 +08:00
{
sfShader_SetParameter1 ( m_ptr , toStringz ( name ) , x ) ;
}
2010-01-07 04:37:29 +08:00
/ * *
* ditto
* /
void setParameter ( string name , float x , float y )
2010-01-07 04:25:45 +08:00
{
sfShader_SetParameter2 ( m_ptr , toStringz ( name ) , x , y ) ;
}
2010-01-07 04:37:29 +08:00
/ * *
* ditto
* /
void setParameter ( string name , float x , float y , float z )
2010-01-07 04:25:45 +08:00
{
sfShader_SetParameter3 ( m_ptr , toStringz ( name ) , x , y , z ) ;
}
2010-01-07 04:37:29 +08:00
/ * *
* ditto
* /
void setParameter ( string name , float x , float y , float z , float w )
2010-01-07 04:25:45 +08:00
{
sfShader_SetParameter4 ( m_ptr , toStringz ( name ) , x , y , z , w ) ;
}
2010-01-07 04:37:29 +08:00
/ * *
* Set a texture parameter
*
* Params :
* name = Texture name in the effect
* texture = Image to set ( pass NULL to use content of current framebuffer )
* /
void setTexture ( string name , Image texture )
2010-01-07 04:25:45 +08:00
{
2010-01-07 04:37:29 +08:00
m_texture = texture ;
2010-01-07 04:25:45 +08:00
sfShader_SetTexture ( m_ptr , toStringz ( name ) , texture is null ? null : texture . getNativePointer ) ;
}
2010-01-07 04:37:29 +08:00
/ * *
* Tell whether or not the system supports shaders
*
* Returns :
* True if the system can use shaders
* /
static bool isAvailable ( )
2010-01-07 04:25:45 +08:00
{
return cast ( bool ) sfShader_IsAvailable ( ) ;
}
private :
2010-01-08 06:00:45 +08:00
static extern ( C )
{
void * function ( cchar * ) sfShader_CreateFromFile ;
void * function ( cchar * ) sfShader_CreateFromMemory ;
void function ( void * ) sfShader_Destroy ;
void function ( void * , cchar * , float ) sfShader_SetParameter1 ;
void function ( void * , cchar * , float , float ) sfShader_SetParameter2 ;
void function ( void * , cchar * , float , float , float ) sfShader_SetParameter3 ;
void function ( void * , cchar * , float , float , float , float ) sfShader_SetParameter4 ;
void function ( void * , cchar * , void * ) sfShader_SetTexture ;
int function ( ) sfShader_IsAvailable ;
void function ( void * ) sfShader_Bind ;
void function ( void * ) sfShader_Unbind ;
}
static this ( )
{
debug
DllLoader dll = DllLoader . load ( "csfml-graphics-d" ) ;
else
DllLoader dll = DllLoader . load ( "csfml-graphics" ) ;
mixin ( loadFromSharedLib ( "sfShader_CreateFromFile" ) ) ;
mixin ( loadFromSharedLib ( "sfShader_CreateFromMemory" ) ) ;
mixin ( loadFromSharedLib ( "sfShader_Destroy" ) ) ;
mixin ( loadFromSharedLib ( "sfShader_SetParameter1" ) ) ;
mixin ( loadFromSharedLib ( "sfShader_SetParameter2" ) ) ;
mixin ( loadFromSharedLib ( "sfShader_SetParameter3" ) ) ;
mixin ( loadFromSharedLib ( "sfShader_SetParameter4" ) ) ;
mixin ( loadFromSharedLib ( "sfShader_SetTexture" ) ) ;
mixin ( loadFromSharedLib ( "sfShader_IsAvailable" ) ) ;
mixin ( loadFromSharedLib ( "sfShader_Bind" ) ) ;
mixin ( loadFromSharedLib ( "sfShader_Unbind" ) ) ;
}
2010-01-07 04:25:45 +08:00
}