From 60ebaa84d1f773c6ffbc0ee10ac045d1fe39f12c Mon Sep 17 00:00:00 2001 From: remi-k Date: Mon, 23 Feb 2009 19:01:41 +0000 Subject: [PATCH] * Implemented sf.RenderTarget; * Made sf.RenderWindow inherit from sf.RenderTarget and sf.Window git-svn-id: https://sfml.svn.sourceforge.net/svnroot/sfml/trunk@1020 4e206d99-4929-0410-ac5d-dfc041789085 --- python/setup.py | 4 +- python/src/RenderTarget.cpp | 190 ++++++++++++++++++++++++++++++++++++ python/src/RenderTarget.hpp | 41 ++++++++ python/src/RenderWindow.cpp | 83 ++-------------- python/src/main.cpp | 3 + 5 files changed, 244 insertions(+), 77 deletions(-) create mode 100644 python/src/RenderTarget.cpp create mode 100644 python/src/RenderTarget.hpp diff --git a/python/setup.py b/python/setup.py index bb05523f3..db8cb7cb0 100644 --- a/python/setup.py +++ b/python/setup.py @@ -12,8 +12,8 @@ setup(name='PySFML', license='zlib/png', ext_modules=[ Extension('PySFML.sf', \ ['src/Clock.cpp', 'src/Color.cpp', 'src/Drawable.cpp', \ - 'src/Event.cpp', 'src/Image.cpp', 'src/Input.cpp', 'src/Key.cpp', 'src/main.cpp', \ - 'src/Music.cpp', 'src/PostFX.cpp', 'src/Rect.cpp', 'src/RenderWindow.cpp', 'src/Sleep.cpp', \ + 'src/Event.cpp', 'src/Image.cpp', 'src/Input.cpp', 'src/Key.cpp', 'src/main.cpp', 'src/Music.cpp', \ + 'src/PostFX.cpp', 'src/Rect.cpp', 'src/RenderTarget.cpp', 'src/RenderWindow.cpp', 'src/Sleep.cpp', \ 'src/Sprite.cpp', 'src/String.cpp', 'src/VideoMode.cpp', 'src/View.cpp', 'src/Window.cpp', \ 'src/Joy.cpp', 'src/Mouse.cpp', 'src/WindowStyle.cpp', 'src/Blend.cpp', 'src/Sound.cpp', \ 'src/SoundBuffer.cpp', 'src/Listener.cpp', 'src/SoundRecorder.cpp', 'src/SoundBufferRecorder.cpp', \ diff --git a/python/src/RenderTarget.cpp b/python/src/RenderTarget.cpp new file mode 100644 index 000000000..8762cdaa9 --- /dev/null +++ b/python/src/RenderTarget.cpp @@ -0,0 +1,190 @@ +//////////////////////////////////////////////////////////// +// +// 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. +// +//////////////////////////////////////////////////////////// + +#include "RenderTarget.hpp" +#include "Color.hpp" +#include "View.hpp" + +extern PyTypeObject PySfColorType; +extern PyTypeObject PySfViewType; + +static PyMemberDef PySfRenderTarget_members[] = { + {NULL} /* Sentinel */ +}; + + +static void +PySfRenderTarget_dealloc(PySfRenderTarget *self) +{ + delete self->obj; + self->ob_type->tp_free((PyObject*)self); +} + +static PyObject * +PySfRenderTarget_new(PyTypeObject *type, PyObject *args, PyObject *kwds) +{ + PySfRenderTarget *self; + + self = (PySfRenderTarget *)type->tp_alloc(type, 0); + + if (self != NULL) + { + } + + return (PyObject *)self; +} + +static PyObject * +PySfRenderTarget_Clear(PySfRenderTarget *self, PyObject *args) +{ + PySfColor *Color; + int size = PyTuple_Size(args); + if (size == 1) + { + if (! PyArg_ParseTuple(args, "O!", &PySfColorType, &Color)) + { + PyErr_SetString(PyExc_TypeError, "Argument is not a sf.Color"); + return NULL; + } + PySfColorUpdate(Color); + self->obj->Clear(*(Color->obj)); + } + else if (size == 0) + { + self->obj->Clear(sf::Color::Black); + } + else + { + PyErr_SetString(PyExc_TypeError, "sf.RenderTarget.Clear takes one or zero argument"); + return NULL; + } + Py_RETURN_NONE; +} + + +static PyObject * +PySfRenderTarget_GetView(PySfRenderTarget *self) +{ + PySfView *View; + + View = GetNewPySfView(); + View->obj = new sf::View(self->obj->GetView()); + + return (PyObject *)View; +} + +static PyObject * +PySfRenderTarget_PreserveOpenGLStates(PySfRenderTarget *self, PyObject *args) +{ + bool Optimize = false; + if (PyObject_IsTrue(args)) + Optimize = true; + self->obj->PreserveOpenGLStates(Optimize); + Py_RETURN_NONE; +} + +static PyObject * +PySfRenderTarget_SetView(PySfRenderTarget* self, PyObject *args) +{ + PySfView *View = (PySfView *)args; + if (! PyObject_TypeCheck(View, &PySfViewType)) + { + PyErr_SetString(PyExc_TypeError, "Argument is not a sf.View"); + return NULL; + } + self->obj->SetView( *(View->obj)); + Py_RETURN_NONE; +} + +static PyObject * +PySfRenderTarget_GetDefaultView(PySfRenderTarget *self) +{ + PySfView *View; + + View = GetNewPySfView(); + View->Owner = false; + View->obj = &(self->obj->GetDefaultView()); + + return (PyObject *)View; +} + +static PyMethodDef PySfRenderTarget_methods[] = { + {"Clear", (PyCFunction)PySfRenderTarget_Clear, METH_VARARGS, "Clear(FillColor)\n\ +Clear the entire target with a single color.\n\ + FillColor : Color to use to clear the render target."}, + {"GetDefaultView", (PyCFunction)PySfRenderTarget_GetDefaultView, METH_NOARGS, "GetDefaultView()\n\ +Get the default view of the window for read / write (returns a sf.View instance)."}, + {"GetView", (PyCFunction)PySfRenderTarget_GetView, METH_NOARGS, "GetView()\n\ +Get the current view rectangle (returns a sf.View instance)."}, + {"PreserveOpenGLStates", (PyCFunction)PySfRenderTarget_PreserveOpenGLStates, METH_O, "PreserveOpenGLStates(Preserve)\n\ +Tell SFML to preserve external OpenGL states, at the expense of more CPU charge. Use this function if you don't want SFML to mess up your own OpenGL states (if any). Don't enable state preservation if not needed, as it will allow SFML to do internal optimizations and improve performances. This parameter is false by default\n\ + Preserve : True to preserve OpenGL states, false to let SFML optimize"}, + {"SetView", (PyCFunction)PySfRenderTarget_SetView, METH_O, "SetView(View)\n\ +Change the current active view. View must be a sf.View instance."}, + {NULL} /* Sentinel */ +}; + +PyTypeObject PySfRenderTargetType = { + PyObject_HEAD_INIT(NULL) + 0, /*ob_size*/ + "RenderTarget", /*tp_name*/ + sizeof(PySfRenderTarget), /*tp_basicsize*/ + 0, /*tp_itemsize*/ + (destructor)PySfRenderTarget_dealloc, /*tp_dealloc*/ + 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*/ + "Base class for all render targets (window, image, ...).", /* tp_doc */ + 0, /* tp_traverse */ + 0, /* tp_clear */ + 0, /* tp_richcompare */ + 0, /* tp_weaklistoffset */ + 0, /* tp_iter */ + 0, /* tp_iternext */ + PySfRenderTarget_methods, /* tp_methods */ + PySfRenderTarget_members, /* tp_members */ + 0, /* tp_getset */ + 0, /* tp_base */ + 0, /* tp_dict */ + 0, /* tp_descr_get */ + 0, /* tp_descr_set */ + 0, /* tp_dictoffset */ + 0, /* tp_init */ + 0, /* tp_alloc */ + PySfRenderTarget_new, /* tp_new */ +}; + + diff --git a/python/src/RenderTarget.hpp b/python/src/RenderTarget.hpp new file mode 100644 index 000000000..25a4ef40c --- /dev/null +++ b/python/src/RenderTarget.hpp @@ -0,0 +1,41 @@ +//////////////////////////////////////////////////////////// +// +// 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. +// +//////////////////////////////////////////////////////////// + +#ifndef __PYRENDERTARGET_H +#define __PYRENDERTARGET_H + +#include + +#include +#include + +#include + +typedef struct { + PyObject_HEAD + sf::RenderTarget *obj; +} PySfRenderTarget; + +#endif + diff --git a/python/src/RenderWindow.cpp b/python/src/RenderWindow.cpp index a251bd822..71f293e3f 100644 --- a/python/src/RenderWindow.cpp +++ b/python/src/RenderWindow.cpp @@ -42,7 +42,9 @@ extern PyTypeObject PySfColorType; extern PyTypeObject PySfWindowType; extern PyTypeObject PySfWindowSettingsType; extern PyTypeObject PySfVideoModeType; -extern PyTypeObject PySfDrawableType; +extern PyTypeObject PySfDrawableType; + +extern PyTypeObject PySfRenderTargetType; static PyMemberDef PySfRenderWindow_members[] = { {NULL} /* Sentinel */ @@ -157,72 +159,9 @@ PySfRenderWindow_Draw(PySfRenderWindow *self, PyObject *args) } } Py_RETURN_NONE; -} - -static PyObject * -PySfRenderWindow_GetDefaultView(PySfRenderWindow *self) -{ - PySfView *View; - - View = GetNewPySfView(); - View->Owner = false; - View->obj = &(self->obj->GetDefaultView()); - - return (PyObject *)View; -} - -static PyObject * -PySfRenderWindow_GetView(PySfRenderWindow *self) -{ - PySfView *View; - - View = GetNewPySfView(); - View->obj = new sf::View(self->obj->GetView()); - - return (PyObject *)View; -} - -static PyObject * -PySfRenderWindow_PreserveOpenGLStates(PySfRenderWindow *self, PyObject *args) -{ - bool Optimize = false; - if (PyObject_IsTrue(args)) - Optimize = true; - self->obj->PreserveOpenGLStates(Optimize); - Py_RETURN_NONE; -} - -static PyObject * -PySfRenderWindow_SetView(PySfRenderWindow* self, PyObject *args) -{ - PySfView *View = (PySfView *)args; - if (! PyObject_TypeCheck(View, &PySfViewType)) - { - PyErr_SetString(PyExc_TypeError, "Argument is not a sfView"); - return NULL; - } - self->obj->SetView( *(View->obj)); - Py_RETURN_NONE; -} - -static PyObject * -PySfRenderWindow_Clear(PySfRenderWindow* self, PyObject *args) -{ - PySfColor *Color = (PySfColor *)args; - if (! PyObject_TypeCheck(Color, &PySfColorType)) - { - PyErr_SetString(PyExc_TypeError, "Argument is not a sfColor"); - return NULL; - } - PySfColorUpdate(Color); - self->obj->Clear(*(Color->obj)); - Py_RETURN_NONE; } static PyMethodDef PySfRenderWindow_methods[] = { - {"Clear", (PyCFunction)PySfRenderWindow_Clear, METH_O, "Clear(FillColor)\n\ -Clear the entire target with a single color.\n\ - FillColor : Color to use to clear the render target."}, {"Capture", (PyCFunction)PySfRenderWindow_Capture, METH_NOARGS, "Capture()\n\ Save the content of the window to an image. Returns a sf.Image object."}, {"ConvertCoords", (PyCFunction)PySfRenderWindow_ConvertCoords, METH_VARARGS, "ConvertCoords(WindowX, WindowY, TargetView)\n\ @@ -232,15 +171,6 @@ Convert a point in window coordinates into view coordinates. Returns a tuple of TargetView : Target view to convert the point to (NULL by default -- uses the current view)."}, {"Draw", (PyCFunction)PySfRenderWindow_Draw, METH_O, "Draw(Drawable)\n\ Draw something on the window. The argument can be a drawable or any object supporting the iterator protocol and containing drawables (for example a tuple of drawables)."}, - {"GetDefaultView", (PyCFunction)PySfRenderWindow_GetDefaultView, METH_NOARGS, "GetDefaultView()\n\ -Get the default view of the window for read / write (returns a sf.View instance)."}, - {"GetView", (PyCFunction)PySfRenderWindow_GetView, METH_NOARGS, "GetView()\n\ -Get the current view rectangle (returns a sf.View instance)."}, - {"PreserveOpenGLStates", (PyCFunction)PySfRenderWindow_PreserveOpenGLStates, METH_O, "PreserveOpenGLStates(Preserve)\n\ -Tell SFML to preserve external OpenGL states, at the expense of more CPU charge. Use this function if you don't want SFML to mess up your own OpenGL states (if any). Don't enable state preservation if not needed, as it will allow SFML to do internal optimizations and improve performances. This parameter is false by default\n\ - Preserve : True to preserve OpenGL states, false to let SFML optimize"}, - {"SetView", (PyCFunction)PySfRenderWindow_SetView, METH_O, "SetView(View)\n\ -Change the current active view. View must be a sf.View instance."}, {NULL} /* Sentinel */ }; @@ -276,14 +206,17 @@ PyTypeObject PySfRenderWindowType = { PySfRenderWindow_methods, /* tp_methods */ PySfRenderWindow_members, /* tp_members */ 0, /* tp_getset */ - &PySfWindowType, /* tp_base */ + 0, /* tp_base */ 0, /* tp_dict */ 0, /* tp_descr_get */ 0, /* tp_descr_set */ 0, /* tp_dictoffset */ (initproc)PySfRenderWindow_init, /* tp_init */ 0, /* tp_alloc */ - PySfRenderWindow_new, /* tp_new */ + PySfRenderWindow_new, /* tp_new */ + 0, /* tp_free */ + 0, /* tp_is_gc */ + Py_BuildValue("(OO)", &PySfWindowType, &PySfRenderTargetType) /* tp_bases */ }; diff --git a/python/src/main.cpp b/python/src/main.cpp index 86f3fae89..7df0428b1 100644 --- a/python/src/main.cpp +++ b/python/src/main.cpp @@ -55,6 +55,7 @@ extern PyTypeObject PySfWindowType; extern PyTypeObject PySfWindowSettingsType; extern PyTypeObject PySfStyleType; extern PyTypeObject PySfRenderWindowType; +extern PyTypeObject PySfRenderTargetType; extern PyTypeObject PySfViewType; extern PyTypeObject PySfInputType; @@ -106,6 +107,8 @@ initsf(void) return; if (PyType_Ready(&PySfStyleType) < 0) return; + if (PyType_Ready(&PySfRenderTargetType) < 0) + return; if (PyType_Ready(&PySfRenderWindowType) < 0) return; if (PyType_Ready(&PySfVideoModeType) < 0)