Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Help with drawing on RenderTexture  (Read 13778 times)

0 Members and 1 Guest are viewing this topic.

Gayle

  • Newbie
  • *
  • Posts: 2
    • View Profile
Help with drawing on RenderTexture
« on: July 12, 2015, 06:26:39 am »
Basically i'm trying to draw to a RenderTexture and then use that texture in a sprite. But I can't get it to work.

txt = sf.Text('This is a test!')
txt.color = sf.Color.RED
txt.font = sf.Font.from_file('ProggyCleanSZ.ttf')
tex = sf.RenderTexture(200,200)
tex.clear(sf.Color.BLACK)
tex.draw(txt)
tex.display()
self.sprite.texture = tex.texture
 

The sprite is then drawn to the screen. The sprite shows up as a white box with no text, even though it was cleared with black. If I draw the text to the screen directly, there is no problem.

Even copying and pasting the example code from the documention doesn't work.

# create a new render-window
window = sf.RenderWindow(sf.VideoMode(800, 600), "pySFML - RenderWindow")

# create a new render-texture
texture = sf.RenderTexture(500, 500) #The example says RenderTexture.create(500,500) but that method doesn't exist.

# the main loop
while window.is_open:

   # ...

   # clear the whole texture with red color
   texture.clear(sf.Color.RED)

   # draw stuff to the texture
   text = sf.Text('Teenage Mutant Ninja Turtles')

   texture.draw(text)

   # we're done drawing to the texture
   texture.display()

   # now we start rendering to the window, clear it first
   window.clear()

   # draw the texture
   sprite = sf.Sprite(texture.texture)
   window.draw(sprite)

   # end the current frame and display its content on screen
   window.display()

This results in a red box on a black background and no text. No clue why this one actually clears with the right color.

I'm completely lost.

FrozenTux

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: Help with drawing on RenderTexture
« Reply #1 on: July 12, 2015, 02:45:08 pm »
Hello,

I tried both examples and they work fine (you just forgot to load a font in the second one) on my side, as you can see on this image : https://monosnap.com/file/AEn0kRJDZ8itWCbUasdy04DtMt75L3.png.

Have you tried to use another font ? What version of python and pysfml are you using ?
PySFML begginer - Sorry for my english, I am french.

Gayle

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Help with drawing on RenderTexture
« Reply #2 on: July 12, 2015, 06:51:40 pm »
Hello,

I tried both examples and they work fine (you just forgot to load a font in the second one) on my side, as you can see on this image : https://monosnap.com/file/AEn0kRJDZ8itWCbUasdy04DtMt75L3.png.

Have you tried to use another font ? What version of python and pysfml are you using ?

Thanks for the help. I thought I'd read that a default font would be used if one wasn't provided. The example code does work once one is provided. My own code (the first bit) didn't work because I didn't pay attention to the documentation.

Quote from: 'pySfml Docs'
It is important to note that the Sprite instance doesn’t copy the texture that it uses, it only keeps a reference to it. Thus, an Texture must not be destroyed while it is used by an Sprite.

My code was in the __init__ method of an object (which I failed to mention), so it was destroyed as soon as the method finished. When draw was called on the sprite, the texture it had a reference to no longer existed. Using something like self.tex instead works.

FrozenTux

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: Help with drawing on RenderTexture
« Reply #3 on: July 12, 2015, 07:05:18 pm »
I thought I'd read that a default font would be used if one wasn't provided.

Yes, it's written in the documentation that there is a default font but it has never worked for me. Not sure if it's a bug or if the documentation is just outdated.
PySFML begginer - Sorry for my english, I am french.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Help with drawing on RenderTexture
« Reply #4 on: July 12, 2015, 07:46:41 pm »
In C++ SFML, there was a default font once, but that's years ago. Nowadays the user must provide a font. So I assume the Python binding's documentation is outdated in this part.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

FrozenTux

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: Help with drawing on RenderTexture
« Reply #5 on: July 12, 2015, 10:50:50 pm »
Indeed, after checking the pysfml website against the docs on github, it appears that the doc on github has been updated to the latest changes (for instance the default font and the create method on RenderTexture have both been removed), but not the website.

In order to get an up-to-date copy of the doc, you can just clone the git repository and run 'make.bat dirhtml' in the doc/ directory. I have uploaded an up-to-date version on http://frozentux.online.fr/pysfml-doc/.
PySFML begginer - Sorry for my english, I am french.

Tank

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1486
    • View Profile
    • Blog
    • Email
Re: Help with drawing on RenderTexture
« Reply #6 on: July 16, 2015, 02:15:21 pm »
Sonkun: Any chance to update it?

 

anything