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

Author Topic: Can't get the map tiles' image to draw  (Read 2313 times)

0 Members and 1 Guest are viewing this topic.

Trijames

  • Newbie
  • *
  • Posts: 6
    • View Profile
Can't get the map tiles' image to draw
« on: May 07, 2011, 03:03:23 pm »
Hi there,
I'm a hobbyist programmer and SFML beginner, I've done some easy project in the past like pong, now I'm trying to write a map system for more advanced games.
I'm just started with it but have already encountered a problem: apparently the tile sprites are drawn but not the image I set to them.

I have a Load() function which works like a constructor for my Map class, basically it should fill the "tiles" vector with stuff for testing since I haven't done a map reading/writing system yet. In this function I have an Image var and I set it with Image.LoadFromFile() like this:
Code: [Select]
sf::Image Image;
if (!Image.LoadFromFile("../img.png")) {
std::cout << "Could not load file: ../img.png" << std::endl;
return false;
}


then I have the loop which "creates" the sprites and sets them to the tiles vector, like this:
Code: [Select]
for (int i = 0; i < 255; i++) {
sf::Sprite Sprite;
Sprite.SetImage(Image);
Sprite.SetPosition(x * tileWidth, y * tileHeight);
Sprite.SetColor(sf::Color(200,100,100,255));

tiles.push_back(Sprite);
}


I have this which I call in the main loop between RenderWindow.Clear() and RenderWindow.Display() to draw stuff on screen:
Code: [Select]
void Map::Draw(sf::RenderWindow &window) {
for (int tile = 0; tile < tiles.size(); tile++) {
window.Draw(tiles[tile]);
}
}


For some reason the tiles are displayed as pink-ish squares (the 200,100,100 RGB color I set in the code) but the image is not displayed.

I'm using Microsoft Visual C++ 2010 and have read this thread but it seems not to be my case.

What am I doing wrong and can anyone reccomend me any kind of reference for a project like this? Thank you.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Can't get the map tiles' image to draw
« Reply #1 on: May 07, 2011, 03:12:31 pm »
The sf::Image instance that you use doesn't exist anymore when the sprites are drawn, that's why it doesn't show.

You must keep the image as a member of your class, so that it's kept alive as long as the sprites use it.
Laurent Gomila - SFML developer

Trijames

  • Newbie
  • *
  • Posts: 6
    • View Profile
Can't get the map tiles' image to draw
« Reply #2 on: May 07, 2011, 03:27:57 pm »
Quote from: "Laurent"
The sf::Image instance that you use doesn't exist anymore when the sprites are drawn, that's why it doesn't show.

You must keep the image as a member of your class, so that it's kept alive as long as the sprites use it.

Thanks for the quick reply,
I'm not sure to get what you mean.

I do have a declaration of the Image object in my class header file:
Code: [Select]
class Map {
   //....
   private:
         //...
         sf::Image Image;
         //...
}


So the sf::Image object should be stored as a member of the class and available when it's drawn later... or not?

Hope this makes sense.

**** Disregard that, I set the Image object as static in my Load() function and the images are displaying now :D .

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Can't get the map tiles' image to draw
« Reply #3 on: May 07, 2011, 03:36:14 pm »
Code: [Select]
  sf::Image Image;
   if (!Image.LoadFromFile("../img.png")) {
      std::cout << "Could not load file: ../img.png" << std::endl;
      return false;
   }

Here you declare a new sf::Image instance, you don't use the one that is member of your class.

Quote
Disregard that, I set the Image object as static in my Load() function and the images are displaying now

It works but it's really not a clean solution ;)
Laurent Gomila - SFML developer

Trijames

  • Newbie
  • *
  • Posts: 6
    • View Profile
Can't get the map tiles' image to draw
« Reply #4 on: May 07, 2011, 03:49:12 pm »
You're right, I think that is because I used the same name for both the objects, do you think that doing this makes sense?

Code: [Select]
// in Load()
sf::Image Img;
if (!Img.LoadFromFile("../img.png")) {
std::cout << "Could not load file: ../img.png" << std::endl;
return false;
}
//set the class member Image to the newly declared Img
Image = Img;
// destroy Img after this


This works, but I'm not sure if it makes sense to do something like this. i'm sure there is something I'm missing.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Can't get the map tiles' image to draw
« Reply #5 on: May 07, 2011, 04:13:02 pm »
Why don't you use the class member directly? You don't need to create another sf::Image instance.

Code: [Select]
  if (!Image.LoadFromFile("../img.png")) {
      std::cout << "Could not load file: ../img.png" << std::endl;
      return false;
   }
Laurent Gomila - SFML developer

Trijames

  • Newbie
  • *
  • Posts: 6
    • View Profile
Can't get the map tiles' image to draw
« Reply #6 on: May 07, 2011, 04:28:03 pm »
Quote from: "Laurent"
Why don't you use the class member directly? You don't need to create another sf::Image instance.

Code: [Select]
  if (!Image.LoadFromFile("../img.png")) {
      std::cout << "Could not load file: ../img.png" << std::endl;
      return false;
   }


Ehrrr. I feel so stupid for not having thought of that :lol: works great, thanks. Au revoir!

 

anything