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.
|
|
|
|
//
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include "RenderWindow.hpp"
|
|
|
|
#include "Image.hpp"
|
2009-02-25 18:40:38 +08:00
|
|
|
#include "Window.hpp"
|
2009-02-28 07:50:02 +08:00
|
|
|
#include "Color.hpp"
|
|
|
|
#include "Drawable.hpp"
|
2009-02-25 18:40:38 +08:00
|
|
|
|
|
|
|
#include "compat.hpp"
|
2009-01-29 00:18:34 +08:00
|
|
|
|
2009-02-25 18:40:38 +08:00
|
|
|
#include <SFML/Window/WindowStyle.hpp>
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
|
|
|
|
extern PyTypeObject PySfViewType;
|
|
|
|
extern PyTypeObject PySfWindowType;
|
2009-02-28 07:50:02 +08:00
|
|
|
extern PyTypeObject PySfRenderWindowType;
|
|
|
|
extern PyTypeObject PySfColorType;
|
|
|
|
extern PyTypeObject PySfDrawableType;
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
PySfRenderWindow_dealloc(PySfRenderWindow* self)
|
2009-05-21 21:52:28 +08:00
|
|
|
{
|
|
|
|
Py_CLEAR(self->View);
|
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 *
|
|
|
|
PySfRenderWindow_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
|
|
|
{
|
|
|
|
PySfRenderWindow *self;
|
2009-03-15 06:28:16 +08:00
|
|
|
self = (PySfRenderWindow *)type->tp_alloc(type, 0);
|
2009-05-21 21:52:28 +08:00
|
|
|
if (self != NULL)
|
|
|
|
{
|
|
|
|
self->obj = new sf::RenderWindow();
|
|
|
|
self->View = NULL;
|
|
|
|
}
|
2009-01-29 00:18:34 +08:00
|
|
|
return (PyObject *)self;
|
2009-03-15 06:28:16 +08:00
|
|
|
}
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
PySfRenderWindow_Capture(PySfRenderWindow *self)
|
|
|
|
{
|
|
|
|
PySfImage *Image;
|
|
|
|
|
|
|
|
Image = GetNewPySfImage();
|
|
|
|
Image->obj = new sf::Image(self->obj->Capture());
|
|
|
|
return (PyObject *)Image;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
PySfRenderWindow_ConvertCoords(PySfRenderWindow *self, PyObject *args)
|
|
|
|
{
|
|
|
|
unsigned int WindowX, WindowY;
|
|
|
|
PySfView *PyTargetView = NULL;
|
|
|
|
sf::View *TargetView = NULL;
|
|
|
|
sf::Vector2f Vect;
|
|
|
|
|
2009-02-25 18:40:38 +08:00
|
|
|
if (!PyArg_ParseTuple(args, "II|O!:RenderWindow.ConvertCoords", &WindowX, &WindowY, &PySfViewType, &PyTargetView))
|
2009-01-29 00:18:34 +08:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (PyTargetView)
|
|
|
|
TargetView = PyTargetView->obj;
|
|
|
|
|
|
|
|
Vect = self->obj->ConvertCoords(WindowX, WindowY, TargetView);
|
|
|
|
return Py_BuildValue("ff", Vect.x, Vect.y);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
PySfRenderWindow_DrawObject(PySfRenderWindow *RenderWindow, PySfDrawable *Obj)
|
|
|
|
{
|
|
|
|
if (PyObject_TypeCheck((PyObject *)Obj, &PySfDrawableType))
|
|
|
|
{
|
2009-03-15 06:28:16 +08:00
|
|
|
if (Obj->IsCustom)
|
2009-01-29 00:18:34 +08:00
|
|
|
{
|
2009-03-06 23:23:07 +08:00
|
|
|
Py_CLEAR(Obj->obj->RenderWindow);
|
|
|
|
Py_INCREF(RenderWindow);
|
2009-02-28 07:50:02 +08:00
|
|
|
Obj->obj->RenderWindow = RenderWindow;
|
2009-01-29 00:18:34 +08:00
|
|
|
}
|
2009-03-15 06:28:16 +08:00
|
|
|
RenderWindow->obj->Draw(*(Obj->obj));
|
2009-01-29 00:18:34 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
PySfRenderWindow_Draw(PySfRenderWindow *self, PyObject *args)
|
|
|
|
{
|
2009-02-28 01:57:39 +08:00
|
|
|
if (args == NULL)
|
2009-01-29 00:18:34 +08:00
|
|
|
return NULL;
|
|
|
|
if (!PySfRenderWindow_DrawObject(self, (PySfDrawable *)args))
|
|
|
|
{
|
|
|
|
PyObject *iterator = PyObject_GetIter(args);
|
|
|
|
PyObject *item;
|
2009-02-28 01:57:39 +08:00
|
|
|
PyErr_Clear();
|
2009-01-29 00:18:34 +08:00
|
|
|
if (iterator == NULL)
|
|
|
|
{
|
|
|
|
PyErr_SetString(PyExc_TypeError, "Argument to Draw method is neither a Drawable nor an iterable.");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
while ((item = PyIter_Next(iterator)))
|
|
|
|
{
|
|
|
|
if (!PySfRenderWindow_DrawObject(self, (PySfDrawable *)item))
|
|
|
|
{
|
|
|
|
PyErr_SetString(PyExc_TypeError, "Object in iterable not a Drawable.");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
Py_DECREF(item);
|
|
|
|
}
|
|
|
|
Py_DECREF(iterator);
|
2009-02-28 01:57:39 +08:00
|
|
|
}
|
|
|
|
if (PyErr_Occurred())
|
|
|
|
return NULL;
|
|
|
|
Py_RETURN_NONE;
|
2009-02-28 07:50:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
PySfRenderWindow_Clear(PySfRenderWindow *self, PyObject *args)
|
|
|
|
{
|
2009-03-15 06:28:16 +08:00
|
|
|
PySfColor *Color = NULL;
|
|
|
|
if (!PyArg_ParseTuple(args, "|O!:RenderWindow.Clear", &PySfColorType, &Color))
|
|
|
|
return NULL;
|
|
|
|
if (Color == NULL) self->obj->Clear(sf::Color::Black);
|
|
|
|
else
|
2009-02-28 07:50:02 +08:00
|
|
|
{
|
|
|
|
PySfColorUpdate(Color);
|
|
|
|
self->obj->Clear(*(Color->obj));
|
|
|
|
}
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
PySfRenderWindow_GetView(PySfRenderWindow *self)
|
2009-05-21 21:52:28 +08:00
|
|
|
{
|
|
|
|
if (self->View != NULL)
|
|
|
|
{
|
|
|
|
Py_INCREF(self->View);
|
|
|
|
return (PyObject *)(self->View);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
PySfView *View;
|
|
|
|
|
|
|
|
View = GetNewPySfView();
|
|
|
|
View->Owner = false;
|
|
|
|
View->obj = (sf::View *)&(self->obj->GetView());
|
|
|
|
Py_INCREF(View);
|
|
|
|
self->View = View;
|
|
|
|
return (PyObject *)View;
|
|
|
|
}
|
2009-02-28 01:57:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
PySfRenderWindow_PreserveOpenGLStates(PySfRenderWindow *self, PyObject *args)
|
|
|
|
{
|
|
|
|
self->obj->PreserveOpenGLStates(PyBool_AsBool(args));
|
2009-01-29 00:18:34 +08:00
|
|
|
Py_RETURN_NONE;
|
2009-02-28 07:50:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
PySfRenderWindow_SetView(PySfRenderWindow* self, PyObject *args)
|
|
|
|
{
|
|
|
|
PySfView *View = (PySfView *)args;
|
|
|
|
if (!PyObject_TypeCheck(View, &PySfViewType))
|
|
|
|
{
|
|
|
|
PyErr_SetString(PyExc_TypeError, "RenderWindow.SetView() Argument is not a sf.View");
|
|
|
|
return NULL;
|
2009-05-21 21:52:28 +08:00
|
|
|
}
|
|
|
|
Py_CLEAR(self->View);
|
|
|
|
Py_INCREF(View);
|
|
|
|
self->View = View;
|
|
|
|
self->obj->SetView(*(View->obj));
|
2009-02-28 07:50:02 +08:00
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
PySfRenderWindow_GetDefaultView(PySfRenderWindow *self)
|
|
|
|
{
|
|
|
|
PySfView *View;
|
|
|
|
|
|
|
|
View = GetNewPySfView();
|
|
|
|
View->Owner = false;
|
|
|
|
View->obj = &(self->obj->GetDefaultView());
|
|
|
|
|
|
|
|
return (PyObject *)View;
|
2009-01-29 00:18:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyMethodDef PySfRenderWindow_methods[] = {
|
2009-02-28 07:50:02 +08:00
|
|
|
{"Clear", (PyCFunction)PySfRenderWindow_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)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."},
|
|
|
|
{"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)."},
|
2009-02-28 01:57:39 +08:00
|
|
|
{"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"},
|
2009-01-29 00:18:34 +08:00
|
|
|
{"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\
|
|
|
|
Convert a point in window coordinates into view coordinates. Returns a tuple of two floats.\n\
|
|
|
|
WindowX : X coordinate of the point to convert, relative to the window\n\
|
|
|
|
WindowY : Y coordinate of the point to convert, relative to the window\n\
|
|
|
|
TargetView : Target view to convert the point to (NULL by default -- uses the current view)."},
|
|
|
|
{NULL} /* Sentinel */
|
|
|
|
};
|
|
|
|
|
|
|
|
PyTypeObject PySfRenderWindowType = {
|
2009-02-25 18:40:38 +08:00
|
|
|
head_init
|
2009-01-29 00:18:34 +08:00
|
|
|
"RenderWindow", /*tp_name*/
|
|
|
|
sizeof(PySfRenderWindow), /*tp_basicsize*/
|
|
|
|
0, /*tp_itemsize*/
|
|
|
|
(destructor)PySfRenderWindow_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*/
|
2009-02-25 18:40:38 +08:00
|
|
|
"Simple wrapper for sf.Window that allows easy 2D rendering.\n\
|
|
|
|
Default constructor : sf.RenderWindow()\n\
|
2009-03-29 21:36:24 +08:00
|
|
|
Construct a new window : sf.RenderWindow(Mode, Title, Style::Resize|Style::Close, Params = WindowSettings())\n\
|
2009-02-25 18:40:38 +08:00
|
|
|
Mode : Video mode to use\n\
|
|
|
|
Title : Title of the window\n\
|
|
|
|
WindowStyle : Window style (Resize | Close by default)\n\
|
2009-03-29 21:36:24 +08:00
|
|
|
Params : Creation parameters (see default constructor for default values)\n\
|
|
|
|
Construct the window from an existing control : sf.RenderWindow(Handle, Params)\n\
|
|
|
|
Handle : handle of the control (long integer)\n\
|
2009-02-25 18:40:38 +08:00
|
|
|
Params : Creation parameters (see default constructor for default values)", /* 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 */
|
|
|
|
PySfRenderWindow_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 */
|
2009-02-28 07:50:02 +08:00
|
|
|
&PySfWindowType, /* tp_base */
|
2009-01-29 00:18:34 +08:00
|
|
|
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-02-28 07:50:02 +08:00
|
|
|
PySfRenderWindow_new, /* tp_new */
|
2009-01-29 00:18:34 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|