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

Author Topic: High CPU load (was "Sprite's SetImage question")  (Read 2885 times)

0 Members and 1 Guest are viewing this topic.

fra9000

  • Newbie
  • *
  • Posts: 5
    • View Profile
High CPU load (was "Sprite's SetImage question")
« on: August 21, 2011, 09:34:19 am »
I'm pretty new to SFML coding. Programming my first little project I faced one common problem.

Basically I have a sprite with his related image and I want to change the image it displays.

Since object.SetImage() doesn't work for this case, how could I get through this problem?

Reading the tutorial I've seen the "copy-constructor-trick", so I was wondering..

is the only way to change the image displayed by the sprite, creating another object (with the copy constructor) with a relative new sprite and the new image i want to use?? and then have I to destruct the previous object too?


Thanks in advance..

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Sprite's "change-image" question
« Reply #1 on: August 21, 2011, 11:23:33 am »
Quote from: "fra9000"
Since object.SetImage() doesn't work for this case, how could I get through this problem?
What doesn't work?
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

fra9000

  • Newbie
  • *
  • Posts: 5
    • View Profile
High CPU load (was "Sprite's SetImage question")
« Reply #2 on: August 21, 2011, 01:56:19 pm »
Quote
What doesn't work?


images are of different sizes so the final result is a cutting of the 2nd image

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
High CPU load (was "Sprite's SetImage question")
« Reply #3 on: August 21, 2011, 04:34:31 pm »
I'm not sure about SFML 1.6, but I think you need to call SetSubRect(), too. Or if you reset the whole sprite, do this:
Code: [Select]
sprite = sf::Sprite(image);
In SFML 2, sf::Image::SetTexture() has a parameter adjustToNewSize.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

fra9000

  • Newbie
  • *
  • Posts: 5
    • View Profile
High CPU load (was "Sprite's SetImage question")
« Reply #4 on: August 23, 2011, 10:59:14 am »
Quote
..but I think you need to call SetSubRect(), too.


yes, It works fine for me..


Quote
In SFML 2, sf::Image::SetTexture() has a parameter adjustToNewSize.


nice and far more intuitive..I'll swap to SFML 2 when it will show enough tutorials and documentation..


btw..thank you Nexus ;)

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
High CPU load (was "Sprite's SetImage question")
« Reply #5 on: August 23, 2011, 11:21:17 pm »
Quote from: "fra9000"
nice and far more intuitive..I'll swap to SFML 2 when it will show enough tutorials and documentation..


btw..thank you Nexus ;)
The documentation is 100% up to date for SFML2, the tutorials for 1.6 work nearly 100% with SFML2 (with a few changes to account for modifications to the API. There are several threads that can help with that), and SFML 1.6 is technically VERY unstable (many bugs have been patched in SFML2, and people who use ATi Cards on Windows will most likely be unable to run your programs if they use SFML 1.x).
I use the latest build of SFML2

fra9000

  • Newbie
  • *
  • Posts: 5
    • View Profile
High CPU load (was "Sprite's SetImage question")
« Reply #6 on: September 10, 2011, 07:30:46 am »
Eventually you convinced me on switching to SFML 2 :)

After the conversion the Task Manager shows a CPU load around 35%.

Since I was testing a simple movement system of an array of objects, I'm wondering if there's a bottle-neck somewhere that I haven't noticed.

Minimal code below.


main.cpp

Code: [Select]

int main()
{

sf::Uint32 ElapsedTime;

sf::RenderWindow App(sf::VideoMode(800, 480, 32), "SFML 2 Engine");

App.SetFramerateLimit(60);
App.EnableVerticalSync(true);

sf::Texture background_texture;
if (!background_texture.LoadFromFile("background.png")) return EXIT_FAILURE;

sf::Sprite background_sprite(background_texture);
background_sprite.SetPosition(0, 0);


while (App.IsOpened())
{

        sf::Event event;
       
while (App.PollEvent(event))
        {
if (event.Type == sf::Event::Closed) App.Close();
       }

ElapsedTime = App.GetFrameTime();
ElapsedTime /= 10;

App.Clear();

App.Draw(background_sprite);

for(int i = 0; i < 8; i++) { targets[i].move_sprite(ElapsedTime, App); }

App.Display();

}

return EXIT_SUCCESS;

}



target.h
Code: [Select]

class Target {

private:

sf::Sprite sprite;


public:

Target(float x, float y);

void move_sprite(sf::Uint32 t, sf::RenderWindow &App);

};


Target::Target(float x, float y) {

static sf::Texture tex;
tex.LoadFromFile("target_1.png");
sprite.SetTexture(tex);
sprite.SetOrigin(0, 44);
sprite.SetPosition(x, y);

}


void Target::move_sprite(sf::Uint32 t, sf::RenderWindow &App) {

sprite.Move(3.f*t, 0);
if (sprite.GetPosition().x > 850) { sprite.SetPosition(-800, 240); }

App.Draw(sprite);

}


// create targets
Target Target_1(   0.f, 240.f );
Target Target_2(  60.f, 240.f );
Target Target_3( 120.f, 240.f );
Target Target_4( 180.f, 240.f );
Target Target_5( 240.f, 240.f );
Target Target_6( 300.f, 240.f );
Target Target_7( 360.f, 240.f );
Target Target_8( 420.f, 240.f );


Target targets[] = { Target_1, Target_2, Target_3, Target_4, Target_5, Target_6, Target_7, Target_8 };



There's something wrong that I'm doing which could be the cause of the unjustified cpu consumption? :(

Also, deleting the GetFrameTime() control doesn't affect the cpu consumption, but improves the smoothness of the movement (every tot frames I can observe some lag in the movement).


Thank you in advance guy, I would really appreciate any help.

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
High CPU load (was "Sprite's SetImage question")
« Reply #7 on: September 10, 2011, 08:50:56 am »
Well, first of all, don't mix VerticalSync and FramerateLimit. That's probably the problem. If not, update your drivers.
I use the latest build of SFML2

fra9000

  • Newbie
  • *
  • Posts: 5
    • View Profile
High CPU load (was "Sprite's SetImage question")
« Reply #8 on: September 10, 2011, 10:31:40 am »
@ Oni*:

Thank you for the reply,

I've tried everything with the v-sync/setframelimit/setsmooth..but nothing has changed..


OS: Xfce-Ubuntu 11.10 beta 1

CPU: AMD v105 1.2 Ghz
RAM: 4GB
VGA: ATI HD4250
Catalyst: 11.8
2D Driver: 8.88

v-sync is enabled via AMD Control Panel.


Also, with this consumption the program runs smooth if no other program is active..for example if I'm downloading a torrent I experience flickering.