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-02-26 20:36:06 +08:00
# include "VideoMode.hpp"
# include <structmember.h>
# include "offsetof.hpp"
# include "compat.hpp"
2009-01-29 00:18:34 +08:00
static PyMemberDef PySfVideoMode_members [ ] = {
{ ( char * ) " Width " , T_UINT , offsetof ( PySfVideoMode , Width ) , 0 , ( char * ) " Video mode width, in pixels. " } ,
{ ( char * ) " Height " , T_UINT , offsetof ( PySfVideoMode , Height ) , 0 , ( char * ) " Video mode height, in pixels. " } ,
{ ( char * ) " BitsPerPixel " , T_UINT , offsetof ( PySfVideoMode , BitsPerPixel ) , 0 , ( char * ) " Video mode pixel depth, in bits per pixels. " } ,
{ NULL } /* Sentinel */
} ;
static void
PySfVideoMode_dealloc ( PySfVideoMode * self )
{
delete self - > obj ;
2009-02-26 20:36:06 +08:00
free_object ( self ) ;
2009-01-29 00:18:34 +08:00
}
static PyObject *
PySfVideoMode_new ( PyTypeObject * type , PyObject * args , PyObject * kwds )
{
2009-03-15 06:28:16 +08:00
const char * kwlist [ ] = { " Width " , " Height " , " BitsPerPixel " , NULL } ;
2009-01-29 00:18:34 +08:00
PySfVideoMode * self ;
self = ( PySfVideoMode * ) type - > tp_alloc ( type , 0 ) ;
if ( self ! = NULL )
{
self - > BitsPerPixel = 32 ;
2009-05-22 20:56:25 +08:00
if ( ! PyArg_ParseTupleAndKeywords ( args , kwds , " II|I:VideoMode.__new__ " , ( char * * ) kwlist , & self - > Width , & self - > Height , & self - > BitsPerPixel ) )
2009-03-15 06:28:16 +08:00
return NULL ;
self - > obj = new sf : : VideoMode ( self - > Width , self - > Height , self - > BitsPerPixel ) ;
2009-01-29 00:18:34 +08:00
}
return ( PyObject * ) self ;
}
static PyObject *
PySfVideoMode_IsValid ( PySfVideoMode * self )
{
return PyBool_FromLong ( self - > obj - > IsValid ( ) ) ;
}
static PyObject *
PySfVideoMode_GetDesktopMode ( PySfVideoMode * self )
{
PySfVideoMode * VideoMode ;
VideoMode = GetNewPySfVideoMode ( ) ;
2009-03-15 06:28:16 +08:00
VideoMode - > obj = new sf : : VideoMode ( sf : : VideoMode : : GetDesktopMode ( ) ) ;
2009-01-29 00:18:34 +08:00
VideoMode - > Width = VideoMode - > obj - > Width ;
VideoMode - > Height = VideoMode - > obj - > Height ;
VideoMode - > BitsPerPixel = VideoMode - > obj - > BitsPerPixel ;
return ( PyObject * ) VideoMode ;
}
static PyObject *
PySfVideoMode_GetMode ( PySfVideoMode * self , PyObject * args )
{
std : : size_t index ;
PySfVideoMode * VideoMode ;
2009-02-26 20:36:06 +08:00
index = ( std : : size_t ) PyLong_AsLong ( args ) ;
2009-01-29 00:18:34 +08:00
VideoMode = GetNewPySfVideoMode ( ) ;
2009-03-15 06:28:16 +08:00
VideoMode - > obj = new sf : : VideoMode ( sf : : VideoMode : : GetMode ( index ) ) ;
2009-01-29 00:18:34 +08:00
VideoMode - > Width = VideoMode - > obj - > Width ;
VideoMode - > Height = VideoMode - > obj - > Height ;
VideoMode - > BitsPerPixel = VideoMode - > obj - > BitsPerPixel ;
return ( PyObject * ) VideoMode ;
}
static PyObject *
PySfVideoMode_GetModesCount ( PySfVideoMode * self )
{
2009-02-26 20:36:06 +08:00
return PyLong_FromLong ( sf : : VideoMode : : GetModesCount ( ) ) ;
2009-01-29 00:18:34 +08:00
}
static PyMethodDef PySfVideoMode_methods [ ] = {
{ " IsValid " , ( PyCFunction ) PySfVideoMode_IsValid , METH_NOARGS , " IsValid() \n Tell whether or not the video mode is supported. " } ,
{ " GetDesktopMode " , ( PyCFunction ) PySfVideoMode_GetDesktopMode , METH_STATIC | METH_NOARGS , " GetDesktopMode() \n Get the current desktop video mode. " } ,
{ " GetMode " , ( PyCFunction ) PySfVideoMode_GetMode , METH_STATIC | METH_O , " GetMode(Index) \n Get a valid video mode. Index must be in range [0, GetModesCount()[ Modes are sorted from best to worst. \n Index : Index of video mode to get " } ,
{ " GetModesCount " , ( PyCFunction ) PySfVideoMode_GetModesCount , METH_STATIC | METH_NOARGS , " GetModesCount() \n Get valid video modes count. " } ,
{ NULL } /* Sentinel */
} ;
2009-05-22 20:56:25 +08:00
PyObject *
PySfVideoMode_richcompare ( PyObject * o1 , PyObject * o2 , int op )
2009-01-29 00:18:34 +08:00
{
2009-05-22 20:56:25 +08:00
if ( * ( ( ( PySfVideoMode * ) o1 ) - > obj ) = = * ( ( ( PySfVideoMode * ) o2 ) - > obj ) )
{
if ( op = = Py_EQ )
Py_RETURN_TRUE ;
if ( op = = Py_NE )
Py_RETURN_FALSE ;
}
else
{
if ( op = = Py_EQ )
Py_RETURN_FALSE ;
if ( op = = Py_NE )
Py_RETURN_TRUE ;
}
PyErr_SetString ( PyExc_TypeError , " VideoMode comparison : only == and != make sens. " ) ;
return NULL ;
}
int
PySfVideoMode_setattro ( PyObject * self , PyObject * attr_name , PyObject * v )
{
int result = PyObject_GenericSetAttr ( self , attr_name , v ) ;
PySfVideoMode * Mode = ( PySfVideoMode * ) self ;
Mode - > obj - > Width = Mode - > Width ;
Mode - > obj - > Height = Mode - > Height ;
Mode - > obj - > BitsPerPixel = Mode - > BitsPerPixel ;
return result ;
2009-01-29 00:18:34 +08:00
}
PyTypeObject PySfVideoModeType = {
2009-02-26 20:36:06 +08:00
head_init
2009-01-29 00:18:34 +08:00
" VideoMode " , /*tp_name*/
sizeof ( PySfVideoMode ) , /*tp_basicsize*/
0 , /*tp_itemsize*/
( destructor ) PySfVideoMode_dealloc , /*tp_dealloc*/
0 , /*tp_print*/
0 , /*tp_getattr*/
0 , /*tp_setattr*/
2009-05-22 20:56:25 +08:00
0 , /*tp_compare*/
2009-01-29 00:18:34 +08:00
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*/
" sf.VideoMode defines a video mode (width, height, bpp, frequency) and provides functions for getting modes supported by the display device \n \
Default constructor : VideoMode ( ) \ n \
Construct the video mode with its attributes : VideoMode ( ModeWidth , ModeHeight , ModeBpp = 32 ) \ n ModeWidth : Width in pixels \ n ModeHeight : Height in pixels \ n ModeBpp : Pixel depths in bits per pixel ( 32 by default ) " , /* tp_doc */
0 , /* tp_traverse */
0 , /* tp_clear */
2009-05-22 20:56:25 +08:00
PySfVideoMode_richcompare , /* tp_richcompare */
2009-01-29 00:18:34 +08:00
0 , /* tp_weaklistoffset */
0 , /* tp_iter */
0 , /* tp_iternext */
PySfVideoMode_methods , /* tp_methods */
PySfVideoMode_members , /* tp_members */
0 , /* tp_getset */
0 , /* 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 */
PySfVideoMode_new , /* tp_new */
} ;
PySfVideoMode *
GetNewPySfVideoMode ( )
{
2009-03-15 06:28:16 +08:00
return PyObject_New ( PySfVideoMode , & PySfVideoModeType ) ;
2009-01-29 00:18:34 +08:00
}