SFML community forums

Help => Graphics => Topic started by: StormWingDelta on February 22, 2015, 09:54:00 pm

Title: Constructing a texture or image from what is drawn on the screen?
Post by: StormWingDelta on February 22, 2015, 09:54:00 pm
I forget how to do this.  I know there is a way to build a texture or image from things drawn on the screen but forget how to do it.  Say a black square acts as what will be transparent and there are many green triangles draw connected together with either Shape or Vertex.  I'm working on a unit builder/editor with this and I'm going to save the created image in a database or in memory for use later in a game I'm working on.  The database would only care about the points needed to remake the image later so the only real issue is the creation of the image itself.

The idea is a player uses the editor to make some unit or structure with some shapes that gets saved as a new image for use by that unit or structure later.  In game it would be a single texture or image but in the editor it is the many smaller textures or images used to make it.

Any ideas?
Title: Re: Constructing a texture or image from what is drawn on the screen?
Post by: FRex on February 22, 2015, 11:41:11 pm
(I kinda listed them in the order of their speed, but 1. is useless on its' own if you want a subrect, since there is no SFML pure way to get a subrect of Texture into new one, I don't know if there is a GL way to do that even, but there might be. If you do it only on 'unit save' then speed shouldn't matter anyway so I'd say go for 3.)
1. Texture has update method that you can use to download entire window into it (but you probably want a subrect...).
2. You can use RenderTexture for the image editing space itself and copy its' internal texture when you are done and then throw away the RenderTexture itself.
3. RenderWindow has capture method which will get the window's content as an Image and then you can load (a part of) that to Texture using loadFromImage with custom subrect.

1. is pure GPU (but useless if you want a subrect), 2. and 3. take a trip through CPU.
Title: Re: Constructing a texture or image from what is drawn on the screen?
Post by: binary1248 on February 23, 2015, 12:02:11 am
I don't know if there is a GL way to do that even, but there might be.
glCopyImageSubData (https://www.opengl.org/wiki/GLAPI/glCopyImageSubData) ;D

But... like FRex said, the best way to do it using only SFML is to draw to a RenderTexture and draw that RenderTexture to the screen. This way you have access to its framebuffer ("screen") contents through the SFML-only API via getTexture() which you can either process on the GPU or copy to the CPU as an Image.
Title: Re: Constructing a texture or image from what is drawn on the screen?
Post by: StormWingDelta on February 23, 2015, 01:17:25 am
hmm sounds like it might be fun to get working.
Title: Re: Constructing a texture or image from what is drawn on the screen?
Post by: Glocke on February 23, 2015, 02:11:27 pm
Does Image sf::RenderWindow::capture() const (http://www.sfml-dev.org/documentation/2.2/classsf_1_1RenderWindow.php#a9bd8655d0bac83145bfc329ea7a6d538) cover your problem?