SFML community forums

Bindings - other languages => Python => Topic started by: shackra on April 24, 2012, 04:54:52 am

Title: [Solved] Text doesn't show up (pysfml-cython)
Post by: shackra on April 24, 2012, 04:54:52 am
Hello! :D

I finally can build PySFML :), so, I started playing with it, but Oh Lord!, I can't make appears a simple text in the game window, here is the code:
Code: [Select]
#!/usr/bin/python2
#-*- coding: utf-8 -*-

import sfml

window = sfml.RenderWindow(sfml.VideoMode(640, 480),
                           "Dibujando una imagen en SFML")
window.framerate_limit = 60
running = True
texture = sfml.Texture.load_from_file("mog.png")
sprite = sfml.Sprite(texture)
letra = sfml.Font()
print letra.DEFAULT_FONT
texto = sfml.Text("Hola mundo!", letra, 50)
texto.color = sfml.Color.BLACK
print texto.string

while running:
    for event in window.iter_events():
        if event.type == sfml.Event.CLOSED:
            running = False
        if event.type == sfml.Event.KEY_PRESSED:
            if sfml.Keyboard.is_key_pressed(sfml.Keyboard.W):
                sprite.y -= 10
            if sfml.Keyboard.is_key_pressed(sfml.Keyboard.S):
                sprite.y += 10
            if sfml.Keyboard.is_key_pressed(sfml.Keyboard.A):
                sprite.x -= 10
            if sfml.Keyboard.is_key_pressed(sfml.Keyboard.D):
                sprite.x += 10

    window.clear(sfml.Color.WHITE)
    window.draw(sprite)
    window.draw(texto)
    window.display()

window.close()

what I'm doing wrong?
Title: Re: Text doesn't show up
Post by: bastien on April 24, 2012, 12:17:02 pm
It's because you use an “empty” font object. The same thing happens with C++ SFML.
If you don't specify a font at all, specify the default font (sfml.Font.DEFAULT_FONT (http://pysfml2-cython.readthedocs.org/en/latest/graphics.html#sfml.Font.DEFAULT_FONT)), or load one of your fonts with the class method sfml.Font.load_from_file() (http://pysfml2-cython.readthedocs.org/en/latest/graphics.html#sfml.Font.load_from_file), it should work.

I'm going to make the constructor raise an exception like for other classes that use class methods instead of the constructor for creating objects.
Title: Re: Text doesn't show up
Post by: shackra on April 25, 2012, 02:42:37 am
huh, I see... so, I should do letra = sfml.Font.load_from_file("sometypography.ttf") instead of what I am doing!
and yes, it works now :)

thanks!