2009-01-29 00:18:34 +08:00
////////////////////////////////////////////////////////////
//
// PySFML - Python binding for SFML (Simple and Fast Multimedia Library)
// Copyright (C) 2007, 2008 Rémi Koenig (remi.k2620@gmail.com)
//
// 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.
//
// 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:
//
// 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.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
2009-12-07 19:53:38 +08:00
# include "Shader.hpp"
2009-02-25 18:40:38 +08:00
# include "Drawable.hpp"
# include "Image.hpp"
# include "compat.hpp"
2009-01-29 00:18:34 +08:00
extern PyTypeObject PySfImageType ;
2009-02-25 18:40:38 +08:00
extern PyTypeObject PySfDrawableType ;
2009-01-29 00:18:34 +08:00
static void
2009-12-07 19:53:38 +08:00
PySfShader_dealloc ( PySfShader * self )
2009-01-29 00:18:34 +08:00
{
delete self - > obj ;
2009-02-25 18:40:38 +08:00
free_object ( self ) ;
2009-01-29 00:18:34 +08:00
}
static PyObject *
2009-12-07 19:53:38 +08:00
PySfShader_new ( PyTypeObject * type , PyObject * args , PyObject * kwds ) ;
2009-01-29 00:18:34 +08:00
static PyObject *
2009-12-07 19:53:38 +08:00
PySfShader_LoadFromFile ( PySfShader * self , PyObject * args )
2009-02-25 18:40:38 +08:00
{
2009-02-26 20:36:06 +08:00
load_from_file ( self , args ) ;
2009-01-29 00:18:34 +08:00
}
static PyObject *
2009-12-07 19:53:38 +08:00
PySfShader_LoadFromMemory ( PySfShader * self , PyObject * args )
2009-02-25 18:40:38 +08:00
{
2009-02-26 20:36:06 +08:00
char * effect ;
2009-02-25 18:40:38 +08:00
# ifdef IS_PY3K
PyObject * string = PyUnicode_AsUTF8String ( args ) ;
if ( string = = NULL )
return NULL ;
2009-02-26 20:36:06 +08:00
effect = PyBytes_AsString ( string ) ;
2009-02-25 18:40:38 +08:00
# else
2009-02-26 20:36:06 +08:00
effect = PyString_AsString ( args ) ;
2009-02-25 18:40:38 +08:00
# endif
2009-02-26 20:36:06 +08:00
bool result = self - > obj - > LoadFromMemory ( effect ) ;
2009-02-25 18:40:38 +08:00
# ifdef IS_PY3K
Py_DECREF ( string ) ;
# endif
return PyBool_FromLong ( result ) ;
2009-01-29 00:18:34 +08:00
}
2009-12-07 19:53:38 +08:00
static PyObject *
PySfShader_SetParameter ( PySfShader * self , PyObject * args )
{
char * Name ;
float X , Y , Z , W ;
int size = PyTuple_Size ( args ) ;
if ( ! PyArg_ParseTuple ( args , " sf|fff:Shader.SetParameter " , & Name , & X , & Y , & Z , & W ) )
return NULL ;
2009-01-29 00:18:34 +08:00
switch ( size )
{
case 2 :
self - > obj - > SetParameter ( Name , X ) ;
break ;
case 3 :
self - > obj - > SetParameter ( Name , X , Y ) ;
break ;
case 4 :
self - > obj - > SetParameter ( Name , X , Y , Z ) ;
break ;
case 5 :
self - > obj - > SetParameter ( Name , X , Y , Z , W ) ;
break ;
default :
break ;
}
Py_RETURN_NONE ;
}
static PyObject *
2009-12-07 19:53:38 +08:00
PySfShader_SetTexture ( PySfShader * self , PyObject * args )
2009-01-29 00:18:34 +08:00
{
PySfImage * Image = NULL ;
char * Name ;
2009-12-07 19:53:38 +08:00
if ( ! PyArg_ParseTuple ( args , " sO " , & Name , & Image ) )
{
2009-01-29 00:18:34 +08:00
return NULL ;
2009-12-07 19:53:38 +08:00
}
if ( ! PyObject_TypeCheck ( Image , & PySfImageType ) )
2009-01-29 00:18:34 +08:00
{
2009-12-07 19:53:38 +08:00
PyErr_SetString ( PyExc_TypeError , " Shader.SetTexture() Argument 2 must be an sf.Image instance. " ) ;
return NULL ;
2009-01-29 00:18:34 +08:00
}
2009-12-07 19:53:38 +08:00
self - > obj - > SetTexture ( Name , * Image - > obj ) ;
Py_RETURN_NONE ;
2009-01-29 00:18:34 +08:00
}
static PyObject *
2009-12-07 19:53:38 +08:00
PySfShader_IsAvailable ( PySfShader * self , PyObject * args )
2009-01-29 00:18:34 +08:00
{
2009-12-07 19:53:38 +08:00
return PyBool_FromLong ( sf : : Shader : : IsAvailable ( ) ) ;
2009-01-29 00:18:34 +08:00
}
2009-12-07 19:53:38 +08:00
static PyMethodDef PySfShader_methods [ ] = {
{ " LoadFromFile " , ( PyCFunction ) PySfShader_LoadFromFile , METH_O , " LoadFromFile(Filename) \n Load the effect from a file. " } ,
{ " LoadFromMemory " , ( PyCFunction ) PySfShader_LoadFromMemory , METH_O , " LoadFromMemory(Effect) \n Load the effect from a text in memory. " } ,
{ " SetParameter " , ( PyCFunction ) PySfShader_SetParameter , METH_VARARGS , " SetParameter(X), SetParameter(X, Y), SetParameter(X, Y, Z), SetParameter(X, Y, Z, W) \n Change a parameter of the effect. \n \
2009-01-29 00:18:34 +08:00
Name : Parameter name in the effect \ n \
X , Y , Z , W : Values to assign . " },
2009-12-07 19:53:38 +08:00
{ " SetTexture " , ( PyCFunction ) PySfShader_SetTexture , METH_VARARGS , " SetTexture(Name, Texture) \n Set a texture parameter. \n \
2009-01-29 00:18:34 +08:00
Name : Texture name in the effect \ n \
2009-12-07 19:53:38 +08:00
Texture : Image to set ( pass sf . Shader . CurrentTexture to use content of current framebuffer ) " },
{ " IsAvailable " , ( PyCFunction ) PySfShader_IsAvailable , METH_STATIC | METH_NOARGS , " IsAvailable() \n Tell wether or not the system supports post-effects. " } ,
2009-01-29 00:18:34 +08:00
{ NULL } /* Sentinel */
} ;
2009-12-07 19:53:38 +08:00
PyTypeObject PySfShaderType = {
2009-02-25 18:40:38 +08:00
head_init
2009-12-07 19:53:38 +08:00
" Shader " , /*tp_name*/
sizeof ( PySfShader ) , /*tp_basicsize*/
2009-01-29 00:18:34 +08:00
0 , /*tp_itemsize*/
2009-12-07 19:53:38 +08:00
( destructor ) PySfShader_dealloc , /*tp_dealloc*/
2009-01-29 00:18:34 +08:00
0 , /*tp_print*/
0 , /*tp_getattr*/
0 , /*tp_setattr*/
0 , /*tp_compare*/
0 , /*tp_repr*/
0 , /*tp_as_number*/
0 , /*tp_as_sequence*/
0 , /*tp_as_mapping*/
0 , /*tp_hash */
0 , /*tp_call*/
0 , /*tp_str*/
0 , /*tp_getattro*/
0 , /*tp_setattro*/
0 , /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE , /*tp_flags*/
2009-12-07 19:53:38 +08:00
" sf.Shader is used to apply a post effect to a window. \n \
Default constructor : sf . Shader ( ) \ n \
Copy constructor : sf . Shader ( Copy ) where Copy is a sf . Shader instance . " , /* tp_doc */
2009-01-29 00:18:34 +08:00
0 , /* tp_traverse */
0 , /* tp_clear */
0 , /* tp_richcompare */
0 , /* tp_weaklistoffset */
0 , /* tp_iter */
0 , /* tp_iternext */
2009-12-07 19:53:38 +08:00
PySfShader_methods , /* tp_methods */
2009-02-25 18:40:38 +08:00
0 , /* tp_members */
2009-01-29 00:18:34 +08:00
0 , /* tp_getset */
& PySfDrawableType , /* tp_base */
0 , /* tp_dict */
0 , /* tp_descr_get */
0 , /* tp_descr_set */
0 , /* tp_dictoffset */
2009-03-15 06:28:16 +08:00
0 , /* tp_init */
2009-01-29 00:18:34 +08:00
0 , /* tp_alloc */
2009-12-07 19:53:38 +08:00
PySfShader_new , /* tp_new */
2009-01-29 00:18:34 +08:00
} ;
2009-03-15 06:28:16 +08:00
static PyObject *
2009-12-07 19:53:38 +08:00
PySfShader_new ( PyTypeObject * type , PyObject * args , PyObject * kwds )
2009-01-29 00:18:34 +08:00
{
2009-12-07 19:53:38 +08:00
PySfShader * self ;
self = ( PySfShader * ) type - > tp_alloc ( type , 0 ) ;
2009-03-15 06:28:16 +08:00
if ( self ! = NULL )
2009-01-29 00:18:34 +08:00
{
2009-12-07 19:53:38 +08:00
PySfShader * Copy = NULL ;
2009-03-15 06:28:16 +08:00
self - > IsCustom = false ;
2009-12-07 19:53:38 +08:00
if ( ! PyArg_ParseTuple ( args , " |O! " , & PySfShaderType , & Copy ) )
2009-03-15 06:28:16 +08:00
return NULL ;
2009-12-07 19:53:38 +08:00
if ( Copy ) self - > obj = new sf : : Shader ( * ( Copy - > obj ) ) ;
else self - > obj = new sf : : Shader ( ) ;
2009-03-15 06:28:16 +08:00
}
return ( PyObject * ) self ;
2009-01-29 00:18:34 +08:00
}