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

Author Topic: Sprite doesnt show the correct Image/-size  (Read 1389 times)

0 Members and 2 Guests are viewing this topic.

niccnacc

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Sprite doesnt show the correct Image/-size
« on: January 15, 2013, 01:35:24 pm »
Hi!

I'm quite new to sfml and not the master at c++ (coming from java).
Started a LevelEditor but i bonked right away:
I've showed sprites with pics (huge ones too like 1900x..) and everything went well.

The problem is with:

        sf::Image* img = new sf::Image;
        sf::Image Image1(10, 200, sf::Color(0, 255, 0));
               
        ASprite* MenuBackground = new ASprite;
        MenuBackground->SetImage(Image1);
        MenuBackground->SetPosition(100, 100);
 

I'd expect a sprite 10 broad and 200 high. But what I get is a weird white rectangle with about 500 width and 100 height. The position is right, as I set it, but the color & dimensions not. Same happens, if I do the code/or without dynamic all. in the main method...
Could someone explain this phenomenon to me? Just trying to get a nice "background" for a vertical slice of the screen to show a list of sprites, wich then can be placed at the gamefield.
Thanks in advance!

masskiller

  • Sr. Member
  • ****
  • Posts: 284
  • Pointers to Functions rock!
    • MSN Messenger - kyogre_jb@hotmail.com
    • View Profile
    • Email
Re: Sprite doesnt show the correct Image/-size
« Reply #1 on: January 15, 2013, 02:34:09 pm »
More code should help to understand, as there is nothing wrong with the code there other than the unnecessary use of heap allocations. If you are going to allocate them there anyway (once again, it's rarely necessary in something so small), then use RAII, it will make your life easier and cut down the possibilities of a memory leak.

sf::Image* img = new sf::Image;

This line does nothing (at least in your small snippet) and makes a dynamic allocation out of the blue, this should always be avoided in C++ unless you have a reason for it.

Also, you are using ASprite (your own?) and not sf::Sprite, that may be a possible cause for it.

There's also the fact that SFML 1.6 hasn't been touched at all in the last 2 years and it had some ugly bugs that SFML 2.0 doesn't have. It also has useful new classes such as sf::Vertex, sf::VertexArray, sf::RenderTexture, that give you more freedom on what you want to do. Overall it's best to just forget about 1.6 and focus on 2.0.

« Last Edit: January 15, 2013, 02:35:55 pm by masskiller »
Programmer, Artist, Composer and Storyline/Script Writer of "Origin of Magic". If all goes well this could turn into a commercial project!

Finally back into the programming world!

niccnacc

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: Sprite doesnt show the correct Image/-size
« Reply #2 on: January 15, 2013, 02:52:36 pm »
I just facepalmed myself, because this was so obvious, but I didn't get to it.
Thanks, yes! Sure, it was my ASprite class, which had no constructor for my "new" use^^.

Thank you for the hint with sfml 2.0. At least this post had some sense.