SFML community forums
Help => Graphics => Topic started 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.
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
-
It should work. Did you try saving the image to a file ?
-
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
-
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.
-
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-
-
Ok, I'll check this as soon as possible.
-
Hi Laurent,
I did a bit more testing...
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:
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-
-
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.
-
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...
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)
-
works if and only if (x2-x1) <= 1024. Otherwise, SaveToFile crashes.
1024 must be the maximum texture size supported by your graphics card ;)
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 ?
-
works if and only if (x2-x1) <= 1024. Otherwise, SaveToFile crashes.
1024 must be the maximum texture size supported by your graphics card ;)
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
-
Ok I see. I'm going to check this, thanks for your help :)
-
It's fixed now.
-
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-
-
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 ?
-
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