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

Author Topic: Drawing Multiple Sprites  (Read 1882 times)

0 Members and 1 Guest are viewing this topic.

GarryS

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • http://www.maximize-traffic.net
Drawing Multiple Sprites
« on: January 02, 2012, 09:18:23 pm »
Hello.  I am new to SFML and using version 2.  I am able to create and texture a sprite but when I try to create any more than 1,  I get an segmentation fault.  Here is how I am trying to display the sprites:

Code: [Select]

int i, x, tile = 0;
  for(i = 0; i < WINDOW_W; i += TILE_W) {
    for(x = 0; x < WINDOW_H; x += TILE_H) {
      sprite[tile].SetPosition(i, x);
      window.Draw(sprite[tile]);
    }
  }


Thank you for any suggestions!

Mario

  • SFML Team
  • Hero Member
  • *****
  • Posts: 879
    • View Profile
Drawing Multiple Sprites
« Reply #1 on: January 02, 2012, 09:57:08 pm »
You should post the code where you create those sprites.

Also, just as a hint, you should use logical names for your variables. nothing against using i or x, but if you're talking about positions, you should ensure x is indeed the x coordinate - not the y one.

GarryS

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • http://www.maximize-traffic.net
Drawing Multiple Sprites
« Reply #2 on: January 02, 2012, 10:07:51 pm »
Thanks Mario.  That is where the error was:

Code: [Select]

  if(!texture.LoadFromFile("cobblestone.png"))
    // return error
    for(int i = 0; i < sizeof(sprite) / sizeof(sprite[0]); i++) {
      sprite[i].SetTexture(texture);
    }


The version that was not working is was using
Code: [Select]

 i < sizeof(sprite);

 :roll:

I am going to implement your other suggestions now.  Thanks again!

 

anything