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
This commit is contained in:
tankbo 2010-02-11 15:32:08 +00:00
parent 239071c0fa
commit 0a7d28b687
5 changed files with 74 additions and 145 deletions

View File

@ -20,7 +20,7 @@ wnd.UseVerticalSync( True )
# Load a fancy font. # Load a fancy font.
cheese = sf.Font() cheese = sf.Font()
cheese.LoadFromFile( "data/cheeseburger.ttf", 50 ) cheese.LoadFromFile( "data/cheeseburger.ttf" )
# Create a text. # Create a text.
text = sf.Text( u"Hello SFML from Python!", cheese, 50 ) text = sf.Text( u"Hello SFML from Python!", cheese, 50 )

View File

@ -73,7 +73,6 @@ def main():
# Draw background # Draw background
App.Draw(Background) App.Draw(Background)
App.Flush()
# Active window to be able to perform OpenGL commands. # Active window to be able to perform OpenGL commands.
App.SetActive() App.SetActive()

View File

@ -51,103 +51,36 @@ PySfFont_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
} }
static PyObject * static PyObject *
PySfFont_LoadFromFile(PySfFont* self, PyObject *args, PyObject *kwds) PySfFont_LoadFromFile( PySfFont* self, PyObject *args ) {
{ char* Filename;
const char *kwlist[] = {"Filename", "Charsize", "Charset", NULL}; bool result;
unsigned int Charsize=30;
char *Filename; if( PyArg_ParseTuple( args, "s:Font.LoadFromFile", &Filename ) ) {
char *Charset=NULL, *EncodingStr; result = self->obj->LoadFromFile(Filename);
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
} }
else if (PyArg_ParseTupleAndKeywords(args, kwds, "s|Is#s:Font.LoadFromFile", (char **)kwlist, &Filename, &Charsize, &Charset, &Length, &EncodingStr)) else {
{
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
{
PyErr_BadArgument(); PyErr_BadArgument();
return NULL; return NULL;
} }
return PyBool_FromLong(result);
return PyBool_FromLong( result );
} }
static PyObject * static PyObject *
PySfFont_LoadFromMemory(PySfFont* self, PyObject *args, PyObject *kwds) PySfFont_LoadFromMemory( PySfFont* self, PyObject *args ) {
{ unsigned int Size;
const char *kwlist[] = {"Data", "Charsize", "Charset", NULL}; char* Data;
unsigned int Charsize=30, Size; bool result;
char *Data;
char *Charset=NULL, *EncodingStr; if( PyArg_ParseTuple( args, "s#:Font.LoadFromMemory", &Data, &Size ) ) {
int Length; result = self->obj->LoadFromMemory( Data, Size );
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
} }
else if (PyArg_ParseTupleAndKeywords(args, kwds, "s#|Is#s:Font.LoadFromMemory", (char **)kwlist, &Data, &Size, &Charsize, &Charset, &Length, &EncodingStr)) else {
{
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
{
PyErr_BadArgument(); PyErr_BadArgument();
return NULL; return NULL;
} }
return PyBool_FromLong(result);
return PyBool_FromLong( result );
} }
static PyObject * static PyObject *
@ -160,56 +93,59 @@ PySfFont_GetDefaultFont(PySfFont* self, PyObject *args)
} }
static PyObject * static PyObject *
PySfFont_GetCharacterSize(PySfFont* self) PySfFont_GetGlyph(PySfFont* self, PyObject *args) {
{ unsigned int codepoint( 0 );
return PyLong_FromUnsignedLong(self->obj->GetCharacterSize()); 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<sf::Glyph*>( &( self->obj->GetGlyph( codepoint, charsize, bold ) ) );
glyph->Rectangle->obj = &glyph->obj->Rectangle;
glyph->TexCoords->obj = &glyph->obj->TexCoords;
PySfGlyphUpdateSelf( glyph );
return reinterpret_cast<PyObject*>( glyph );
} }
static PyObject * static PyObject *
PySfFont_GetGlyph(PySfFont* self, PyObject *args) PySfFont_GetImage( PySfFont* self, PyObject* args ) {
{ PySfImage* image( GetNewPySfImage() );
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;
}
static PyObject * image->obj = new sf::Image( self->obj->GetImage( PyLong_AsUnsignedLong( args ) ) );
PySfFont_GetImage(PySfFont* self)
{ return reinterpret_cast<PyObject*>( image );
PySfImage *Image;
Image = GetNewPySfImage();
Image->obj = new sf::Image(self->obj->GetImage());
return (PyObject *)Image;
} }
static PyMethodDef PySfFont_methods[] = { 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\ Load the font from a file. Returns True if loading was successful.\n\
Filename : Font file to load\n\ Filename : Font file to load"},
CharSize : Size of characters in bitmap - the bigger, the higher quality (30 by default)\n\ {"LoadFromMemory", (PyCFunction)PySfFont_LoadFromMemory, METH_VARARGS, "LoadFromMemory(Data)\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\
Load the font from a file in memory. Returns True if loading was successful.\n\ Load the font from a file in memory. Returns True if loading was successful.\n\
Data : data to load\n\ Data : data to load"},
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)"},
{"GetDefaultFont", (PyCFunction)PySfFont_GetDefaultFont, METH_NOARGS | METH_STATIC, "GetDefaultFont()\n\ {"GetDefaultFont", (PyCFunction)PySfFont_GetDefaultFont, METH_NOARGS | METH_STATIC, "GetDefaultFont()\n\
Get the SFML default built-in font (Arial)."}, Get the SFML default built-in font (Arial)."},
{"GetImage", (PyCFunction)PySfFont_GetImage, METH_NOARGS, "GetImage()\n\ {"GetImage", (PyCFunction)PySfFont_GetImage, METH_O, "GetImage(characterSize)\n\
Get the image containing the rendered characters (glyphs)."}, Get the image containing the rendered characters (glyphs).\n\
{"GetCharacterSize", (PyCFunction)PySfFont_GetCharacterSize, METH_NOARGS, "GetCharacterSize()\n\ characterSize: Character size."},
Get the base size of characters in the font; All glyphs dimensions are based on this value"}, {"GetGlyph", (PyCFunction)PySfFont_GetGlyph, METH_VARARGS, "GetGlyph(codePoint, characterSize, bold)\n\
{"GetGlyph", (PyCFunction)PySfFont_GetGlyph, METH_O, "GetGlyph(CodePoint)\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\
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 code point value of the character to get.\n\
CodePoint : Unicode value of the character to get."}, characterSize: Size of character\n\
bold: Bold character"},
{NULL} /* Sentinel */ {NULL} /* Sentinel */
}; };

View File

@ -158,13 +158,6 @@ PySfRenderWindow_SetActive(PySfRenderWindow *self, PyObject *args)
Py_RETURN_NONE; Py_RETURN_NONE;
} }
static PyObject *
PySfRenderWindow_Flush(PySfRenderWindow *self, PyObject *args)
{
self->obj->Flush();
Py_RETURN_NONE;
}
static PyObject * static PyObject *
PySfRenderWindow_GetView(PySfRenderWindow *self) PySfRenderWindow_GetView(PySfRenderWindow *self)
{ {
@ -209,7 +202,10 @@ PySfRenderWindow_GetDefaultView(PySfRenderWindow *self)
View = GetNewPySfView(); View = GetNewPySfView();
View->Owner = false; View->Owner = false;
View->obj = &(self->obj->GetDefaultView());
// Python doesn't know anything about 'const', so cast away. Be careful with
// not changing the default view!
View->obj = const_cast<sf::View*>( &( self->obj->GetDefaultView() ) );
return (PyObject *)View; return (PyObject *)View;
} }
@ -218,8 +214,6 @@ static PyMethodDef PySfRenderWindow_methods[] = {
{"SetActive", (PyCFunction)PySfRenderWindow_SetActive, METH_VARARGS, "SetActive(Active)\n\ {"SetActive", (PyCFunction)PySfRenderWindow_SetActive, METH_VARARGS, "SetActive(Active)\n\
Activate or deactivate the window as the current target for OpenGL rendering.\n\ Activate or deactivate the window as the current target for OpenGL rendering.\n\
Active : True to activate window. (default: True)"}, 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", (PyCFunction)PySfRenderWindow_Clear, METH_VARARGS, "Clear(FillColor)\n\
Clear the entire target with a single color.\n\ Clear the entire target with a single color.\n\
FillColor : Color to use to clear the render target."}, FillColor : Color to use to clear the render target."},

View File

@ -116,16 +116,16 @@ PySfText_SetFont(PySfText* self, PyObject *args)
} }
static PyObject * 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; Py_RETURN_NONE;
} }
static PyObject * static PyObject *
PySfText_GetSize(PySfText* self) PySfText_GetCharacterSize(PySfText* self)
{ {
return PyFloat_FromDouble(self->obj->GetSize()); return PyFloat_FromDouble(self->obj->GetCharacterSize());
} }
static PyObject * static PyObject *
@ -223,7 +223,7 @@ PySfText_init(PySfText *self, PyObject *args, PyObject *kwds)
} }
} }
if (Font) PySfText_SetFont(self, (PyObject *)Font); if (Font) PySfText_SetFont(self, (PyObject *)Font);
self->obj->SetSize(Size); self->obj->SetCharacterSize(Size);
return 0; 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."}, {"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"}, {"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."}, {"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"}, {"SetCharacterSize", (PyCFunction)PySfText_SetCharacterSize, METH_O, "SetCharacterSize(Size)\nSet the size of the text.\n Size : New size, in pixels"},
{"GetSize", (PyCFunction)PySfText_GetSize, METH_NOARGS, "GetSize()\nGet the size of the characters."}, {"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)"}, {"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."}, {"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."}, {"GetRect", (PyCFunction)PySfText_GetRect, METH_NOARGS, "GetRect()\nGet the string rectangle on screen."},