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

Author Topic: sprite.SetImage efficiency?  (Read 3434 times)

0 Members and 1 Guest are viewing this topic.

Reiv

  • Newbie
  • *
  • Posts: 13
    • View Profile
sprite.SetImage efficiency?
« on: June 20, 2011, 05:10:40 pm »
Hi, I am working on character animation and I want to know if its good to:

sprite.SetImage(sf::Image animation[direction][frame]...);
App.Draw(sprite);
in game loop,

or its better to have array of sprites and do App.Draw(sprite[direc.][frame]...)?
I find the first option better, because then I can GetPosition and SetPosition etc.. only for one sprite, but the tutorial says that manipulating Images is slow, so dont I lose some efficiency?

thePyro_13

  • Full Member
  • ***
  • Posts: 156
    • View Profile
sprite.SetImage efficiency?
« Reply #1 on: June 20, 2011, 05:29:00 pm »
You probably want to do SetSubRect() on a spritesheet, rather than an array of images.

While manipulating images is slow, AFAIK all you're doing is manipulating references to images, so your should't need to worry about performance too much.

You want to have one sprite per entity, or one sprite derived or containing class per entity. as sprites manage rotation and position for themselves, using different sprites per animation frame will become very inefficient and require a lot of duplication of data.

Your first option is vastly superior to your second option. Though you should look into SetSubRect() as it may make your code and file structure simpler(ie. one spritesheet per characterr type rather than a messy folder per character type).

Reiv

  • Newbie
  • *
  • Posts: 13
    • View Profile
sprite.SetImage efficiency?
« Reply #2 on: June 20, 2011, 06:09:03 pm »
Thanks, I will look at it.

But could anyone tell me if there is something wrong with this code? It gives me Access Violation:

sf::Image *img;

img = (sf::Image *) malloc(sizeof(sf::Image) * SOME_CONST);
if (img == NULL)
{
    print and exit;
}

img[0].LoadFromFile("name.png"); << Access violation reading location at blah

*I know that name.png and number in [] is correct.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
sprite.SetImage efficiency?
« Reply #3 on: June 20, 2011, 06:11:41 pm »
malloc is for the C world, it doesn't call constructors of C++ classes. Use new instead.
Laurent Gomila - SFML developer

slotdev

  • Sr. Member
  • ****
  • Posts: 385
    • View Profile
sprite.SetImage efficiency?
« Reply #4 on: June 20, 2011, 06:13:01 pm »
Just for reference, I use a load of animations in my game, and I use a std::map with a pointer to my animation instances, and with 10 animation running (each circa 200px x 200px) and a full screen redraw each frame at 60fps, I am getting ~6% CPU usage.

Which is pretty impressive, I think. MUCH better than SDL, anyway  :P
SFML 2.1

Reiv

  • Newbie
  • *
  • Posts: 13
    • View Profile
sprite.SetImage efficiency?
« Reply #5 on: June 20, 2011, 06:16:46 pm »
Quote from: "Laurent"
malloc is for the C world, it doesn't call constructors of C++ classes. Use new instead.


yeah I figure it out and tried new too, but it gives me the same exception :/

edit: I tried try and catch(...) and now it works  :shock: nevermind then

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
sprite.SetImage efficiency?
« Reply #6 on: June 20, 2011, 06:47:09 pm »
btw, if you have something like malloc(sizeof(X) * Y) you should use calloc instead because the multiplication can overflow.
SFML / OS X developer

 

anything