SFML community forums

Help => Graphics => Topic started by: SvenB on November 21, 2008, 04:19:09 pm

Title: dummy question on sf::Image::CopyScreen
Post by: SvenB on November 21, 2008, 04:19:09 pm
Hi,

after discovering sf::Image::CopyScreen I thought I could draw some text on the screen, invoke Image::CopyScreen to copy the relevant part of the screen to an image and after that to set a corresponding sprite.

Code: [Select]

sf::String Letter("B", Arial100, 100) ;
sf::Image Image1 ;
sf::Sprite Sprite1;

Letter.SetPosition( 0, 0 ) ;
Screen.Draw( Letter) ;
Image1.CopyScreen( Screen, sf::IntRect(0, 0, 100, 100) ) ;
Sprite1.SetImage( Image1 );


But this results in an 'empty' image/sprite. Thus, something must be wrong with my thinking Image::CopyScreen works.

Please, can someone enlighten me :-)

Thanx - Sven
Title: dummy question on sf::Image::CopyScreen
Post by: Laurent on November 21, 2008, 04:37:09 pm
It should work. Did you try saving the image to a file ?
Title: dummy question on sf::Image::CopyScreen
Post by: SvenB on November 21, 2008, 04:56:01 pm
Just did a SaveToFile().

Interestingly, the image/sprite is no longer empty but shows a
nice stripe pattern ;-)

BTW: SaveToFile() saves in what format? IrfanView accepts it as *.bmp...

ciao - Sven
Title: dummy question on sf::Image::CopyScreen
Post by: Laurent on November 21, 2008, 05:06:12 pm
SaveToFile deduces the format from the filename extension. So you won't get anything if you don't specify a valid one... ;)

One more test : you can insert a Screen.Display() just before the call to CopyScreen.
Title: dummy question on sf::Image::CopyScreen
Post by: SvenB on November 21, 2008, 05:44:44 pm
Hi,

I inserted a Screen.Display() before CopyScreen, this did not change anything. The saved image looks like this:
(http://hermes.psych.uni-halle.de/test1.png)

-Sven-
Title: dummy question on sf::Image::CopyScreen
Post by: Laurent on November 21, 2008, 07:21:48 pm
Ok, I'll check this as soon as possible.
Title: dummy question on sf::Image::CopyScreen
Post by: SvenB on November 21, 2008, 09:13:27 pm
Hi Laurent,

I did a bit more testing...

Code: [Select]

sf::String Letter("B", Arial100, 100) ;
sf::Image Image1, Image2 ;
sf::Sprite Sprite1, Sprite2;

Letter.SetPosition( 0, 0 ) ;
Screen.Draw( Letter) ;
Screen.Display() ;
Image1.CopyScreen( Screen, sf::IntRect(0, 0, 100, 100) ) ; // does not the copy
Image2.LoadFromFile( "testimage.bmp" ) ; // runs okay due to your fix on image loading and creation - thanks again


But:

Code: [Select]

sf::String Letter("B", Arial100, 100) ;
sf::Image Image1, Image2 ;
sf::Sprite Sprite1, Sprite2;

Letter.SetPosition( 0, 0 ) ;
Screen.Draw( Letter) ;
Screen.Display() ;
Image1.CopyScreen( Screen, sf::IntRect(0, 0, 100, 100) ) ; // does not the copy, but results in funny stripes
Image1.SaveToFile( "test.bmp" ) ; // saves the funny stripes to disk
Image2.LoadFromFile( "testimage.bmp" ) ; // crashes Windows XP


That means: a LoadFromFile is pretty okay,  but a previous SaveToFile causes LoadFromFile to crash Windows. Only a hard reset reanimates Windows. The crash results in a pixel mess at the top of the display.

-Sven-
Title: dummy question on sf::Image::CopyScreen
Post by: Laurent on November 24, 2008, 07:45:22 pm
Ok, two things :
- I fixed the crash happening after a call to SaveToFile
- CopyScreen actually works, except that the source image is flipped vertically; that's why you don't see your B.

I'll definitely have to find a good solution (not just workarounds) for this flipped coordinates issue, as it impacts multiple parts of the code.
Title: dummy question on sf::Image::CopyScreen
Post by: SvenB on November 26, 2008, 03:21:56 pm
Quote from: "Laurent"
Ok, two things :
- I fixed the crash happening after a call to SaveToFile
- CopyScreen actually works, except that the source image is flipped vertically; that's why you don't see your B.

I'll definitely have to find a good solution (not just workarounds) for this flipped coordinates issue, as it impacts multiple parts of the code.


Hi Laurent,

thanks a lot for your help. I verified that CopyScreen works and LoadFromFile no longer crashes after a call to SaveToFile.

Unfortunately, I found some other problems...

Code: [Select]
Image1.CopyScreen( Screen, sf::IntRect(x1, y1, x2, y2) ) ;
Image1.SaveToFile( "test1.jpg" ) ;


works if and only if (x2-x1) <= 1024. Otherwise, SaveToFile crashes.

And the saved files are distorted unless the image-width is a power of 2, e.g. x2-x1 = 64 = 2^6 results in a correct saved image (besides y-flipping), image width of 63 or 65 pixel gives strange results, image width of 128 is okay and so on...

HTH - Sven (thanx again for your invaluable help)
Title: dummy question on sf::Image::CopyScreen
Post by: Laurent on November 26, 2008, 03:32:54 pm
Quote
works if and only if (x2-x1) <= 1024. Otherwise, SaveToFile crashes.

1024 must be the maximum texture size supported by your graphics card ;)

Quote
And the saved files are distorted unless the image-width is a power of 2, e.g. x2-x1 = 64 = 2^6 results in a correct saved image (besides y-flipping), image width of 63 or 65 pixel gives strange results, image width of 128 is okay and so on...

Can you upload an example of such a distorted image ?
Title: dummy question on sf::Image::CopyScreen
Post by: SvenB on November 26, 2008, 04:18:15 pm
Quote from: "Laurent"
Quote
works if and only if (x2-x1) <= 1024. Otherwise, SaveToFile crashes.

1024 must be the maximum texture size supported by your graphics card ;)

Quote
And the saved files are distorted unless the image-width is a power of 2, e.g. x2-x1 = 64 = 2^6 results in a correct saved image (besides y-flipping), image width of 63 or 65 pixel gives strange results, image width of 128 is okay and so on...

Can you upload an example of such a distorted image ?


Okay, max texture size... I c :-)

Now to the relevant images. On screen everything is fine. The following images are the saved ones.

129 x 128 px:
(http://hermes.psych.uni-halle.de/test3.png)

128 x 128 px: that's visible on the screen
(http://hermes.psych.uni-halle.de/test2.png)

127 x 128 px:
(http://hermes.psych.uni-halle.de/test4.png)

126 x 128 px:
(http://hermes.psych.uni-halle.de/test5.png)

Another example:

127 x 110 px:
(http://hermes.psych.uni-halle.de/test2.jpg)

128 x 110 px: that's on the screen
(http://hermes.psych.uni-halle.de/test1.jpg)



Looks somehow systematic to me ;-)

Yours - Sven
Title: dummy question on sf::Image::CopyScreen
Post by: Laurent on November 26, 2008, 04:36:08 pm
Ok I see. I'm going to check this, thanks for your help :)
Title: dummy question on sf::Image::CopyScreen
Post by: Laurent on November 26, 2008, 05:57:24 pm
It's fixed now.
Title: dummy question on sf::Image::CopyScreen
Post by: SvenB on November 26, 2008, 10:07:11 pm
Thanks a lot! Great.

Now I can easily flip.x and flip.y strings, just draw the string on screen,
CopyScreen the string to an Image, make it a sprite and flip the sprite.
You made my day.

Thanx again.

-Sven-
Title: dummy question on sf::Image::CopyScreen
Post by: Laurent on November 26, 2008, 10:19:52 pm
Sorry that you still have to use such hack, I promise that true render-to-image will be implemented soon :D

By the way, why do you need to pre-render text ? Is it a performance issue ?
Title: dummy question on sf::Image::CopyScreen
Post by: SvenB on November 26, 2008, 11:12:30 pm
Hi,

no, it's not a performance issue. For the experiment at hand I simply need mirror-images from letters. Now I could prepare these letters with some graphics software, save them as images to file and load them with sfml. This requires some work especially when I decide to use a different set of letters, or digits or want to change the image size without loss of quality or want to use a different font. Thus, it is easier to render the text with sfml. Since FlipX and FlipY are not available for sfml strings I need a workaround to create the mirror images. With the help of CopyScreen (and having in mind the coordinate issue) this workaround is available now. Fine.

Thanks - Sven