From 0a7d28b6870b5d6d0b587e6a102a4e78749de207 Mon Sep 17 00:00:00 2001 From: tankbo Date: Thu, 11 Feb 2010 15:32:08 +0000 Subject: [PATCH] Python: Modified Text, Font and RenderWindow to build with latest revision of SFML2. Adjusted samples. git-svn-id: https://sfml.svn.sourceforge.net/svnroot/sfml/branches/sfml2@1403 4e206d99-4929-0410-ac5d-dfc041789085 --- python/samples/hellosfml.py | 2 +- python/samples/opengl.py | 1 - python/src/Font.cpp | 186 ++++++++++++------------------------ python/src/RenderWindow.cpp | 16 +--- python/src/Text.cpp | 14 +-- 5 files changed, 74 insertions(+), 145 deletions(-) diff --git a/python/samples/hellosfml.py b/python/samples/hellosfml.py index d6e9ecaca..d52c99c57 100644 --- a/python/samples/hellosfml.py +++ b/python/samples/hellosfml.py @@ -20,7 +20,7 @@ wnd.UseVerticalSync( True ) # Load a fancy font. cheese = sf.Font() -cheese.LoadFromFile( "data/cheeseburger.ttf", 50 ) +cheese.LoadFromFile( "data/cheeseburger.ttf" ) # Create a text. text = sf.Text( u"Hello SFML from Python!", cheese, 50 ) diff --git a/python/samples/opengl.py b/python/samples/opengl.py index cbddaaa77..93ce04fe5 100644 --- a/python/samples/opengl.py +++ b/python/samples/opengl.py @@ -73,7 +73,6 @@ def main(): # Draw background App.Draw(Background) - App.Flush() # Active window to be able to perform OpenGL commands. App.SetActive() diff --git a/python/src/Font.cpp b/python/src/Font.cpp index 979474fb7..f2475ce37 100644 --- a/python/src/Font.cpp +++ b/python/src/Font.cpp @@ -51,103 +51,36 @@ PySfFont_new(PyTypeObject *type, PyObject *args, PyObject *kwds) } static PyObject * -PySfFont_LoadFromFile(PySfFont* self, PyObject *args, PyObject *kwds) -{ - const char *kwlist[] = {"Filename", "Charsize", "Charset", NULL}; - unsigned int Charsize=30; - char *Filename; - char *Charset=NULL, *EncodingStr; - int Length; - bool result; - std::string Encoding; - if (PyArg_ParseTuple(args, "s|I:Font.LoadFromFile", &Filename, &Charsize)) - result = self->obj->LoadFromFile(Filename, Charsize); - else if (PyArg_ParseTupleAndKeywords(args, kwds, "s|Iu:Font.LoadFromFile", (char **)kwlist, &Filename, &Charsize, &Charset)) - { - PyErr_Clear(); -#if Py_UNICODE_SIZE == 4 - result = self->obj->LoadFromFile(Filename, Charsize, (sf::Uint32 *)Charset); -#else - result = self->obj->LoadFromFile(Filename, Charsize, (sf::Uint16 *)Charset); -#endif +PySfFont_LoadFromFile( PySfFont* self, PyObject *args ) { + char* Filename; + bool result; + + if( PyArg_ParseTuple( args, "s:Font.LoadFromFile", &Filename ) ) { + result = self->obj->LoadFromFile(Filename); } - else if (PyArg_ParseTupleAndKeywords(args, kwds, "s|Is#s:Font.LoadFromFile", (char **)kwlist, &Filename, &Charsize, &Charset, &Length, &EncodingStr)) - { - PyErr_Clear(); - if (EncodingStr == NULL) - result = self->obj->LoadFromFile(Filename, Charsize, Charset); - else - { - Encoding.assign(EncodingStr); - if (Encoding == "utf8" || Encoding == "") - result = self->obj->LoadFromFile(Filename, Charsize, Charset); - else if (Encoding == "utf16") - result = self->obj->LoadFromFile(Filename, Charsize, Charset+2); - else if (Encoding == "utf32") - result = self->obj->LoadFromFile(Filename, Charsize, Charset+4); - else - { - PyErr_Format(PyExc_TypeError, "Font.LoadFromFile() Encoding %s not supported", EncodingStr); - return NULL; - } - } - } - else - { + else { PyErr_BadArgument(); return NULL; } - return PyBool_FromLong(result); + + return PyBool_FromLong( result ); } static PyObject * -PySfFont_LoadFromMemory(PySfFont* self, PyObject *args, PyObject *kwds) -{ - const char *kwlist[] = {"Data", "Charsize", "Charset", NULL}; - unsigned int Charsize=30, Size; - char *Data; - char *Charset=NULL, *EncodingStr; - int Length; - bool result; - std::string Encoding; - if (PyArg_ParseTuple(args, "s#|I:Font.LoadFromMemory", &Data, &Size, &Charsize)) - result = self->obj->LoadFromMemory(Data, Size, Charsize); - else if (PyArg_ParseTupleAndKeywords(args, kwds, "s#|Iu:Font.LoadFromMemory", (char **)kwlist, &Data, &Size, &Charsize, &Charset)) - { - PyErr_Clear(); -#if Py_UNICODE_SIZE == 4 - result = self->obj->LoadFromMemory(Data, Size, Charsize, (sf::Uint32 *)Charset); -#else - result = self->obj->LoadFromMemory(Data, Size, Charsize, (sf::Uint16 *)Charset); -#endif +PySfFont_LoadFromMemory( PySfFont* self, PyObject *args ) { + unsigned int Size; + char* Data; + bool result; + + if( PyArg_ParseTuple( args, "s#:Font.LoadFromMemory", &Data, &Size ) ) { + result = self->obj->LoadFromMemory( Data, Size ); } - else if (PyArg_ParseTupleAndKeywords(args, kwds, "s#|Is#s:Font.LoadFromMemory", (char **)kwlist, &Data, &Size, &Charsize, &Charset, &Length, &EncodingStr)) - { - PyErr_Clear(); - if (EncodingStr == NULL) - result = self->obj->LoadFromMemory(Data, Size, Charsize, Charset); - else - { - Encoding.assign(EncodingStr); - if (Encoding == "utf8") - result = self->obj->LoadFromMemory(Data, Size, Charsize, Charset); - else if (Encoding == "utf16") - result = self->obj->LoadFromMemory(Data, Size, Charsize, Charset+2); - else if (Encoding == "utf32") - result = self->obj->LoadFromMemory(Data, Size, Charsize, Charset+4); - else - { - PyErr_Format(PyExc_TypeError, "Font.LoadFromMemory() Encoding %s not supported", EncodingStr); - return NULL; - } - } - } - else - { + else { PyErr_BadArgument(); return NULL; } - return PyBool_FromLong(result); + + return PyBool_FromLong( result ); } static PyObject * @@ -160,56 +93,59 @@ PySfFont_GetDefaultFont(PySfFont* self, PyObject *args) } static PyObject * -PySfFont_GetCharacterSize(PySfFont* self) -{ - return PyLong_FromUnsignedLong(self->obj->GetCharacterSize()); +PySfFont_GetGlyph(PySfFont* self, PyObject *args) { + unsigned int codepoint( 0 ); + unsigned int charsize( 0 ); + bool bold( false ); + + if( !PyArg_ParseTuple( args, "IIb:Font.LoadFromFile", &codepoint, &charsize, &bold ) ) { + PyErr_BadArgument(); + return NULL; + } + + PySfGlyph* glyph( GetNewPySfGlyph() ); + + glyph->Owner = false; + glyph->Rectangle = GetNewPySfIntRect(); + glyph->Rectangle->Owner = false; + glyph->TexCoords = GetNewPySfFloatRect(); + glyph->TexCoords->Owner = false; + + glyph->obj = const_cast( &( self->obj->GetGlyph( codepoint, charsize, bold ) ) ); + glyph->Rectangle->obj = &glyph->obj->Rectangle; + glyph->TexCoords->obj = &glyph->obj->TexCoords; + + PySfGlyphUpdateSelf( glyph ); + + return reinterpret_cast( glyph ); } static PyObject * -PySfFont_GetGlyph(PySfFont* self, PyObject *args) -{ - PySfGlyph *Glyph = GetNewPySfGlyph(); - Glyph->Owner = false; - Glyph->Rectangle = GetNewPySfIntRect(); - Glyph->Rectangle->Owner = false; - Glyph->TexCoords = GetNewPySfFloatRect(); - Glyph->TexCoords->Owner = false; - Glyph->obj = (sf::Glyph *) &(self->obj->GetGlyph(PyLong_AsUnsignedLong(args))); - Glyph->Rectangle->obj = &(Glyph->obj->Rectangle); - Glyph->TexCoords->obj = &(Glyph->obj->TexCoords); - PySfGlyphUpdateSelf(Glyph); - return (PyObject *)Glyph; -} +PySfFont_GetImage( PySfFont* self, PyObject* args ) { + PySfImage* image( GetNewPySfImage() ); -static PyObject * -PySfFont_GetImage(PySfFont* self) -{ - PySfImage *Image; - Image = GetNewPySfImage(); - Image->obj = new sf::Image(self->obj->GetImage()); - return (PyObject *)Image; + image->obj = new sf::Image( self->obj->GetImage( PyLong_AsUnsignedLong( args ) ) ); + + return reinterpret_cast( image ); } static PyMethodDef PySfFont_methods[] = { - {"LoadFromFile", (PyCFunction)PySfFont_LoadFromFile, METH_VARARGS | METH_KEYWORDS, "LoadFromFile(Filename, CharSize, UnicodeCharset) or LoadFromFile(Filename, CharSize, Charset, Encoding='utf8')\n\ + {"LoadFromFile", (PyCFunction)PySfFont_LoadFromFile, METH_VARARGS, "LoadFromFile(Filename))\n\ Load the font from a file. Returns True if loading was successful.\n\ - Filename : Font file to load\n\ - CharSize : Size of characters in bitmap - the bigger, the higher quality (30 by default)\n\ - Charset : Characters set to generate (by default, contains the ISO-8859-1 printable characters)"}, - {"LoadFromMemory", (PyCFunction)PySfFont_LoadFromMemory, METH_VARARGS | METH_KEYWORDS, "LoadFromMemory(Data, CharSize, UnicodeCharset) or LoadFromMemory(Data, CharSize, Charset, Encoding='utf8')\n\ + Filename : Font file to load"}, + {"LoadFromMemory", (PyCFunction)PySfFont_LoadFromMemory, METH_VARARGS, "LoadFromMemory(Data)\n\ Load the font from a file in memory. Returns True if loading was successful.\n\ - Data : data to load\n\ - CharSize : Size of characters in bitmap - the bigger, the higher quality (30 by default)\n\ - Charset : Characters set to generate (by default, contains the ISO-8859-1 printable characters)"}, + Data : data to load"}, {"GetDefaultFont", (PyCFunction)PySfFont_GetDefaultFont, METH_NOARGS | METH_STATIC, "GetDefaultFont()\n\ Get the SFML default built-in font (Arial)."}, - {"GetImage", (PyCFunction)PySfFont_GetImage, METH_NOARGS, "GetImage()\n\ -Get the image containing the rendered characters (glyphs)."}, - {"GetCharacterSize", (PyCFunction)PySfFont_GetCharacterSize, METH_NOARGS, "GetCharacterSize()\n\ -Get the base size of characters in the font; All glyphs dimensions are based on this value"}, - {"GetGlyph", (PyCFunction)PySfFont_GetGlyph, METH_O, "GetGlyph(CodePoint)\n\ -Get the description of a glyph (character) given by its unicode value. Returns glyph's visual settings, or an invalid glyph if character not found.\n\ - CodePoint : Unicode value of the character to get."}, + {"GetImage", (PyCFunction)PySfFont_GetImage, METH_O, "GetImage(characterSize)\n\ +Get the image containing the rendered characters (glyphs).\n\ + characterSize: Character size."}, + {"GetGlyph", (PyCFunction)PySfFont_GetGlyph, METH_VARARGS, "GetGlyph(codePoint, characterSize, bold)\n\ +Get the description of a glyph (character) given by its code point, character size and boldness. Returns glyph's visual settings, or an invalid glyph if character not found.\n\ + codePoint : Unicode code point value of the character to get.\n\ + characterSize: Size of character\n\ + bold: Bold character"}, {NULL} /* Sentinel */ }; diff --git a/python/src/RenderWindow.cpp b/python/src/RenderWindow.cpp index cb15e4bfc..d167b42d3 100644 --- a/python/src/RenderWindow.cpp +++ b/python/src/RenderWindow.cpp @@ -158,13 +158,6 @@ PySfRenderWindow_SetActive(PySfRenderWindow *self, PyObject *args) Py_RETURN_NONE; } -static PyObject * -PySfRenderWindow_Flush(PySfRenderWindow *self, PyObject *args) -{ - self->obj->Flush(); - Py_RETURN_NONE; -} - static PyObject * PySfRenderWindow_GetView(PySfRenderWindow *self) { @@ -208,8 +201,11 @@ PySfRenderWindow_GetDefaultView(PySfRenderWindow *self) PySfView *View; View = GetNewPySfView(); - View->Owner = false; - View->obj = &(self->obj->GetDefaultView()); + View->Owner = false; + + // Python doesn't know anything about 'const', so cast away. Be careful with + // not changing the default view! + View->obj = const_cast( &( self->obj->GetDefaultView() ) ); return (PyObject *)View; } @@ -218,8 +214,6 @@ static PyMethodDef PySfRenderWindow_methods[] = { {"SetActive", (PyCFunction)PySfRenderWindow_SetActive, METH_VARARGS, "SetActive(Active)\n\ Activate or deactivate the window as the current target for OpenGL rendering.\n\ Active : True to activate window. (default: True)"}, - {"Flush", (PyCFunction)PySfRenderWindow_Flush, METH_VARARGS, "Flush()\n\ -Make sure that what has been drawn so far is rendered."}, {"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."}, diff --git a/python/src/Text.cpp b/python/src/Text.cpp index 95d60c5b7..84529c160 100644 --- a/python/src/Text.cpp +++ b/python/src/Text.cpp @@ -116,16 +116,16 @@ PySfText_SetFont(PySfText* self, PyObject *args) } static PyObject * -PySfText_SetSize(PySfText* self, PyObject *args) +PySfText_SetCharacterSize(PySfText* self, PyObject *args) { - self->obj->SetSize(PyFloat_AsDouble(args)); + self->obj->SetCharacterSize(PyFloat_AsDouble(args)); Py_RETURN_NONE; } static PyObject * -PySfText_GetSize(PySfText* self) +PySfText_GetCharacterSize(PySfText* self) { - return PyFloat_FromDouble(self->obj->GetSize()); + return PyFloat_FromDouble(self->obj->GetCharacterSize()); } static PyObject * @@ -223,7 +223,7 @@ PySfText_init(PySfText *self, PyObject *args, PyObject *kwds) } } if (Font) PySfText_SetFont(self, (PyObject *)Font); - self->obj->SetSize(Size); + self->obj->SetCharacterSize(Size); return 0; } @@ -236,8 +236,8 @@ Return the visual position (a tuple of two floats) of the Index-th character of {"GetString", (PyCFunction)PySfText_GetString, METH_NOARGS, "GetString()\nGet the text as an unicode string."}, {"SetFont", (PyCFunction)PySfText_SetFont, METH_O, "SetFont(Font)\nSet the font of the string.\n Font : font to use"}, {"GetFont", (PyCFunction)PySfText_GetFont, METH_NOARGS, "GetFont()\nGet the font used by the string."}, - {"SetSize", (PyCFunction)PySfText_SetSize, METH_O, "SetSize(Size)\nSet the size of the string.\n Size : New size, in pixels"}, - {"GetSize", (PyCFunction)PySfText_GetSize, METH_NOARGS, "GetSize()\nGet the size of the characters."}, + {"SetCharacterSize", (PyCFunction)PySfText_SetCharacterSize, METH_O, "SetCharacterSize(Size)\nSet the size of the text.\n Size : New size, in pixels"}, + {"GetCharacterSize", (PyCFunction)PySfText_GetCharacterSize, METH_NOARGS, "GetCharacterSize()\nGet the size of the characters."}, {"SetStyle", (PyCFunction)PySfText_SetStyle, METH_O, "SetStyle(TextSize)\nSet the style of the text. The default style is Regular.\n TextSize : New text style, (combination of Style values)"}, {"GetStyle", (PyCFunction)PySfText_GetStyle, METH_NOARGS, "GetStyle()\nGet the style of the text."}, {"GetRect", (PyCFunction)PySfText_GetRect, METH_NOARGS, "GetRect()\nGet the string rectangle on screen."},