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

Author Topic: [SOLVED]Render Sprite to Image without drawing to screen?  (Read 6892 times)

0 Members and 1 Guest are viewing this topic.

Ashenwraith

  • Sr. Member
  • ****
  • Posts: 270
    • View Profile
[SOLVED]Render Sprite to Image without drawing to screen?
« on: April 17, 2010, 07:49:33 am »
Hi, I need to transform (scale) an image that will then be saved, but I don't want to draw the sprite and copy the screen pixels.

Is there a way to scale/transform an image without drawing it?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
[SOLVED]Render Sprite to Image without drawing to screen?
« Reply #1 on: April 17, 2010, 10:15:06 am »
Only in SFML 2.
Laurent Gomila - SFML developer

Ashenwraith

  • Sr. Member
  • ****
  • Posts: 270
    • View Profile
[SOLVED]Render Sprite to Image without drawing to screen?
« Reply #2 on: April 17, 2010, 01:25:01 pm »
Great, that's what I'm using.

Is that Sprite::GetPixel?

Does Sprite need a GetPixelPtr?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
[SOLVED]Render Sprite to Image without drawing to screen?
« Reply #3 on: April 17, 2010, 01:45:23 pm »
You have to use sf::RenderImage. It's a kind of RenderWindow that draws to an image.
Laurent Gomila - SFML developer

Ashenwraith

  • Sr. Member
  • ****
  • Posts: 270
    • View Profile
[SOLVED]Render Sprite to Image without drawing to screen?
« Reply #4 on: April 17, 2010, 01:53:29 pm »
How do I attach it to a sprite without a screen/window?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
[SOLVED]Render Sprite to Image without drawing to screen?
« Reply #5 on: April 17, 2010, 01:59:01 pm »
Have you read the documentation first? :P
Laurent Gomila - SFML developer

Ashenwraith

  • Sr. Member
  • ****
  • Posts: 270
    • View Profile
[SOLVED]Render Sprite to Image without drawing to screen?
« Reply #6 on: April 17, 2010, 05:22:50 pm »
Yes, the doc told me:

Code: [Select]
sf::Image img;
static_cast<sf::RenderImage>(img);

:wink:


I've just been looking in the src, I have to rebuild the doc with something. I thought the doc was not complete?

What's wrong with this:

Code: [Select]
sf::Image img(w,h,sf::Color(0,0,0,0));
sf::Sprite sprt(img);

sf::RenderTarget RT;

sf::Renderer rendr(RT);
rendr.SetTexture(img);

sf::RenderImage RI;
RI.create(w,h,32);

sprt.Render(RT,rendr);

img=sprt.GetImage();
img.SaveToFile("img.png");

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
[SOLVED]Render Sprite to Image without drawing to screen?
« Reply #7 on: April 17, 2010, 06:15:20 pm »
Quote from: "Ashenwraith"
Yes, the doc told me:

Code: [Select]
sf::Image img;
static_cast<sf::RenderImage>(img);

:wink:

Hum... I really don't think so.

Quote
I've just been looking in the src, I have to rebuild the doc with something. I thought the doc was not complete?

The documentation for the sf::RenderImage class is complete. You don't have to build it with doxygen, you can just read it in the header file RenderImage.hpp.

Quote
What's wrong with this

Almost everything :lol:

This is the example which is in the documentation:
Code: [Select]
// First of all: make sure that rendering to image is supported
if (!sf::RenderImage::IsAvailable())
   return -1;

// Create a new render-window
sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");

// Create a new render-image
sf::RenderImage image;
if (!image.Create(500, 500))
    return -1

// The main loop
while (window.IsOpened())
{
   // Event processing
   // ...

   // Clear the whole image with red color
   image.Clear(sf::Color::Red);

   // Draw stuff to the image
   image.Draw(sprite);  // sprite is a sf::Sprite
   image.Draw(shape);   // shape is a sf::Shape
   image.Draw(text);    // text is a sf::Text

   // We're done drawing to the image
   image.Display();

   // Now we start rendering to the window, clear it first
   window.Clear();

   // Draw the image
   sf::Sprite sprite(image.GetImage());
   window.Draw(sprite);

   // End the current frame and display its contents on screen
  window.Display();
}
Laurent Gomila - SFML developer

Ashenwraith

  • Sr. Member
  • ****
  • Posts: 270
    • View Profile
[SOLVED]Render Sprite to Image without drawing to screen?
« Reply #8 on: April 17, 2010, 06:53:51 pm »
I didn't know you hid tutorials in the headers...


Thanks man, got it workin no prob after that!  :D

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
[SOLVED]Render Sprite to Image without drawing to screen?
« Reply #9 on: April 17, 2010, 09:24:51 pm »
Quote
I didn't know you hid tutorials in the headers...

This is just the API documentation. The tutorials for SFML 2 are not written yet ;)
Laurent Gomila - SFML developer

Ashenwraith

  • Sr. Member
  • ****
  • Posts: 270
    • View Profile
[SOLVED]Render Sprite to Image without drawing to screen?
« Reply #10 on: April 18, 2010, 12:28:02 am »
It's kind of hard to find which headers/files have example documentation

Can you create a link section or highlight the color a certain way maybe?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
[SOLVED]Render Sprite to Image without drawing to screen?
« Reply #11 on: April 18, 2010, 10:56:36 am »
Quote
It's kind of hard to find which headers/files have example documentation

You don't have to "find" anything. When you use a class you just have to look at its header to see its documentation. All SFML public classes are documented (except drawable classes that I'm currently updating). There's one block of comments for evey function, and one big block for the global documentation of the class at the bottom of the header file. That's all.

And if you're lost in the headers you can still generate a clean HTML/CHM output with doxygen.

Quote
Can you create a link section or highlight the color a certain way maybe?

Sorry I don't understand. What do you mean?
Laurent Gomila - SFML developer

 

anything