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

Author Topic: class MyPicture - Copying Image to Sprite...  (Read 4776 times)

0 Members and 1 Guest are viewing this topic.

JeZ-l-Lee

  • Jr. Member
  • **
  • Posts: 80
    • ICQ Messenger - 223180991
    • MSN Messenger - JeZLee@Live.com
    • AOL Instant Messenger - SLNTHERO@aol.com
    • View Profile
    • http://www.SilentHeroProductions.com
    • Email
class MyPicture - Copying Image to Sprite...
« on: February 10, 2009, 04:35:25 pm »
class MyPicture - Copying Image to Sprite...


Hi,

I am trying to use this class to copy an Image to a Sprite:

Quote
You have to be particularly careful when manipulating images. A sf::Image instance is a resource which is slow to load, heavy to copy and uses a lot of memory.

A lot of people, especially beginners, will just put an instance of sf::Image wherever they have an instance of sf::Sprite, because it may seem the simplest way to draw something. The fact is that it's generally a bad idea. The most obvious problem is when copying such objects (just putting them into an array generates copies) : the sprites will most likely appear white. The reason is that a sprite only points to an external image it doesn't own one, so when the image is copied the sprite has to be updated to point to the new copy of the image. This is quite easy to handle, you just have to define a copy constructor for such classes holding (or deriving from) a sprite and an image :


Code: [Select]
class MyPicture
{
public :

    // This is the copy constructor
    MyPicture(const MyPicture& Copy) : Image(Copy.Image), Sprite(Copy.Sprite)
    {
        // This is the trick : we setup the sprite
        // to use our image, instead of the one of Copy
        Sprite.SetImage(Image);
    }

    // ... a lot of useful functions ...

private :

    sf::Image  Image;
    sf::Sprite Sprite;
};


I am a beginner C++ coder and do not know how to properly call:
MyPicture(const MyPicture& Copy) : Image(Copy.Image), Sprite(Copy.Sprite)

Any help you can offer me would be appreciated!
Thanks...
JeZ+Lee
Silent Hero Productions(R)
Video Game Design Studio

http://www.SilentHeroProductions.com

irri

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • http://www.irri.se
class MyPicture - Copying Image to Sprite...
« Reply #1 on: February 10, 2009, 05:01:30 pm »
I think you're using a copy constructor for the wrong reason here.

Quote from: "JeZ-l-Lee"
I am a beginner C++ coder...

I can see that. If you want to learn good C++ from the beginning, I can recommend you the C++ book by Sthepen Prata. I read it last summer, and I really learned a lot :-)
Quote from: "JeZ-l-Lee"
...and do not know how to properly call:
MyPicture(const MyPicture& Copy) : Image(Copy.Image), Sprite(Copy.Sprite)

You can't.
The constructor you have takes a object of another "MyPicture"-object as argument. How are you going to create the first object of the class if you have no other constructor?

The point of having a Copy Constructors (or Constructors with Deep Copying) is for copying the data pointers points to, and not the address the pointer is pointing at. And since you're not using any pointers or are allocating any memory you don't need such a constructor.

And what are your class really gonna do?? I mean, isn't it just easier if you'll:
sf::Image img;
img.LoadFromFile("bajs.png");
sf::Sprite spr;
spr.SetImage(img);
2D RPG Game (School project): http://PA.irri.se/

JeZ-l-Lee

  • Jr. Member
  • **
  • Posts: 80
    • ICQ Messenger - 223180991
    • MSN Messenger - JeZLee@Live.com
    • AOL Instant Messenger - SLNTHERO@aol.com
    • View Profile
    • http://www.SilentHeroProductions.com
    • Email
class MyPicture - Copying Image to Sprite...
« Reply #2 on: February 10, 2009, 05:39:43 pm »
Quote from: "irri"
And what are your class really gonna do?? I mean, isn't it just easier if you'll:
sf::Image img;
img.LoadFromFile("bajs.png");
sf::Sprite spr;
spr.SetImage(img);

Hi,

Thanks for the reply.

The above code does not work with when trying to center the Sprite.
(for scaling and rotation)
Anyone have any ideas about my original post?
JeZ+Lee
Silent Hero Productions(R)
Video Game Design Studio

http://www.SilentHeroProductions.com

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
class MyPicture - Copying Image to Sprite...
« Reply #3 on: February 10, 2009, 05:50:58 pm »
Quote
And what are your class really gonna do?? I mean, isn't it just easier if you'll:
sf::Image img;
img.LoadFromFile("bajs.png");
sf::Sprite spr;
spr.SetImage(img);

This class is taken from the sprite tutorial. You should read the corresponding paragraph to understand what it's used for ;)

Quote
The above code does not work with when trying to center the Sprite.

What code?
Laurent Gomila - SFML developer

JeZ-l-Lee

  • Jr. Member
  • **
  • Posts: 80
    • ICQ Messenger - 223180991
    • MSN Messenger - JeZLee@Live.com
    • AOL Instant Messenger - SLNTHERO@aol.com
    • View Profile
    • http://www.SilentHeroProductions.com
    • Email
class MyPicture - Copying Image to Sprite...
« Reply #4 on: February 10, 2009, 06:06:06 pm »
Quote
This class is taken from the sprite tutorial. You should read the corresponding paragraph to understand what it's used for ;)



Hi,

I've wasted about 10 hours on trying to load a Sprite Image and then setting the center so that scaling and rotations will be centered.

I read the paragraph and do not understand.

Please help!
JeZ+Lee
Silent Hero Productions(R)
Video Game Design Studio

http://www.SilentHeroProductions.com

Daazku

  • Hero Member
  • *****
  • Posts: 896
    • View Profile
class MyPicture - Copying Image to Sprite...
« Reply #5 on: February 10, 2009, 06:28:20 pm »
Code: [Select]
sf::Image img;
img.LoadFromFile("bajs.png");
sf::Sprite spr;
spr.SetImage(img);
spr.SetCenter(img.GetWidth()/2, img.GetHeight()/2);


Like that? (for the rotation)
Pensez à mettre le tag [Résolu] une fois la réponse à votre question trouvée.
Remember to add the tag [Solved] when you got an answer to your question.

JeZ-l-Lee

  • Jr. Member
  • **
  • Posts: 80
    • ICQ Messenger - 223180991
    • MSN Messenger - JeZLee@Live.com
    • AOL Instant Messenger - SLNTHERO@aol.com
    • View Profile
    • http://www.SilentHeroProductions.com
    • Email
class MyPicture - Copying Image to Sprite...
« Reply #6 on: February 10, 2009, 06:39:03 pm »


My friend write this demo that show that a Sprite with a loaded Image is not centered on screen properly when scaling and rotating!


Code: [Select]
#include <SFML/Graphics.hpp>

int main(int argc, char **argv)
{
sf::RenderWindow App(sf::VideoMode(640, 640), "Sprite Image Rotation Scaling");

sf::Image Image;
Image.LoadFromFile("test.bmp");
Image.CreateMaskFromColor(sf::Color(0, 255, 0, 255), 0);

sf::Sprite Sprite;
Sprite.SetImage(Image);
// Sprite.Resize(50, 50);
// Sprite.SetColor(sf::Color::Red);
Sprite.SetPosition(320.f, 320.f);
Sprite.SetCenter(.5,.5);

bool Running = true;

while(Running)
{
sf::Event Event;
while(App.GetEvent(Event))
{
switch (Event.Type)
{
case sf::Event::Closed:
Running = false;
break;
case sf::Event::KeyPressed:
switch (Event.Key.Code)
{
case sf::Key::Escape:
case sf::Key::Q:
Running = false;
break;
case sf::Key::Up:
Sprite.Move( 0,-10);
break;
case sf::Key::Down:
Sprite.Move( 0, 10);
break;
case sf::Key::Right:
Sprite.Move( 10, 0);
break;
case sf::Key::Left:
Sprite.Move(-10, 0);
break;
case sf::Key::PageUp:
Sprite.Scale(1.25,1.25);
break;
case sf::Key::PageDown:
Sprite.Scale(.75,.75);
break;
case sf::Key::Add:
Sprite.Rotate(5);
break;
case sf::Key::Subtract:
Sprite.Rotate(-5);
break;
default:
break;
}
default:
break;
}
}

App.Clear(sf::Color::Blue);
App.Draw(Sprite);
App.Display();
sf::Sleep(.04);
}

App.Close();

return 0;
}
JeZ+Lee
Silent Hero Productions(R)
Video Game Design Studio

http://www.SilentHeroProductions.com

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
class MyPicture - Copying Image to Sprite...
« Reply #7 on: February 10, 2009, 07:19:43 pm »
First, set the center to the actual center of the sprite
Code: [Select]
sprite.SetCenter(sprite.GetSize() / 2.f);
Now all your transformations (rotation, translation and scaling) will have this point as the origin, which certainly helps when you're dealing with centering ;)

Then, if you want to center it on the window, just do this
Code: [Select]
sprite.SetPosition(window.GetWidth() / 2.f, window.GetHeight() / 2.f);

Is it still confusing, or is it clearer now?
Laurent Gomila - SFML developer

JeZ-l-Lee

  • Jr. Member
  • **
  • Posts: 80
    • ICQ Messenger - 223180991
    • MSN Messenger - JeZLee@Live.com
    • AOL Instant Messenger - SLNTHERO@aol.com
    • View Profile
    • http://www.SilentHeroProductions.com
    • Email
class MyPicture - Copying Image to Sprite...
« Reply #8 on: February 10, 2009, 07:23:16 pm »
Got it working!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Thanks
JeZ+Lee
Silent Hero Productions(R)
Video Game Design Studio

http://www.SilentHeroProductions.com

quasius

  • Full Member
  • ***
  • Posts: 166
    • View Profile
class MyPicture - Copying Image to Sprite...
« Reply #9 on: February 10, 2009, 07:38:13 pm »
Quote from: "JeZ-l-Lee"
Quote
This class is taken from the sprite tutorial. You should read the corresponding paragraph to understand what it's used for ;)



Hi,

I've wasted about 10 hours on trying to load a Sprite Image and then setting the center so that scaling and rotations will be centered.

I read the paragraph and do not understand.

Please help!


I think you're in the wrong thread.  Laurent already explained in the other thread that the center point does not take into account scaling and rotation.  So will need set the center point at the center of the untransformed sprite to get the effect you want.
Also, as other people have mentioned, you're probably biting off too much by trying to make an SFML-powered game without learning basic C++ first.  You might figure it out eventually, but you're doing it the hard way.  Either way, good luck.

Edit:  I guess that's what happens when I reply to a post over the course of several hours.

 

anything