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

Author Topic: Loading the Image in a class?  (Read 3786 times)

0 Members and 1 Guest are viewing this topic.

coral

  • Newbie
  • *
  • Posts: 37
    • View Profile
Loading the Image in a class?
« on: October 18, 2008, 10:34:09 pm »
Hi!

I am trying a bit with dynamic Image loading but i can't get it to work:

Code: [Select]

#include "KillPillar.h"
#include "libs.h"

using namespace std;

class Pillar {
private:
//Pillarinfo
int number;
string name;
int layer;

//SRPITELOADING
vector<sf::Sprite> sprites;
vector<sf::Image> images;
vector<string> names;

public:

bool loadsprite (string filename, string name) {
sf::Image Image;
if (!Image.LoadFromFile(filename)) {
return 0;
}
images.push_back(Image);
sf::Sprite Sprite(Image);
sprites.push_back(Sprite);
names.push_back(name);
return 1;
}

sf::Sprite & fetchSprite (string name) {

for(int i = 0; i <= sprites.size(); i++) {
if(name == names[i]) {
  return sprites[i];
}
}
}

};


int main() {
sf::RenderWindow App(sf::VideoMode(1024, 768), "PILLAR KILLER EXTREME");
const sf::Input& Input = App.GetInput();
Pillar forsta;
if(!forsta.loadsprite("pillar.png", "start")) {
return EXIT_FAILURE;

}

while (App.IsOpened())
{

bool         LeftKeyDown     = Input.IsKeyDown(sf::Key::Left);
bool         RightKeyDown     = Input.IsKeyDown(sf::Key::Right);
bool         UpKeyDown     = Input.IsKeyDown(sf::Key::Up);
bool         DownKeyDown     = Input.IsKeyDown(sf::Key::Down);
bool EnterKeyDown = Input.IsKeyDown(sf::Key::Return);

// Process events
sf::Event Event;
while (App.GetEvent(Event))
{
// Close window : exit
if (Event.Type == sf::Event::Closed)
App.Close();
}

if(RightKeyDown) {
forsta.fetchSprite("start").Move(2,0);
}
// Clear screen
App.Clear();

// Draw the sprite
App.Draw(forsta.fetchSprite("start"));

// Update the window
App.Display();
}

return EXIT_SUCCESS;
}


The screen turns up like this:


The proportions are right of the image but it won't show the actual picture.

I am running the SVN version of SFML, built on Mac OSX 10.5.5

quasius

  • Full Member
  • ***
  • Posts: 166
    • View Profile
Loading the Image in a class?
« Reply #1 on: October 19, 2008, 02:23:34 am »
Have you debugged and traced through the code?  Are the images sucessfully loading?  Are you actually drawing the sprites?  Trace through and find the section that's not working and then ask specifically about that if you still can't figure it out.  "Here's all my code why doesn't it work?" posts are hard to answer.
Edit:  It looks like you have error-checking code for loading, so I'd look at wheer you're drawing.

Edit:  Also, iterating through every image everytime you want to draw one is terrible.  Like really, really slow.  What are you actually trying to achieve with this class?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Loading the Image in a class?
« Reply #2 on: October 19, 2008, 09:08:08 am »
The image you give to your sprite is a temporary one which is destroyed when the loadsprite function exits. You must keep the image valid as long as the sprites uses it. And giving it the image stored in your vector wouldn't solve the problem, because this image may be moved in memory when the vector grows.
Laurent Gomila - SFML developer

coral

  • Newbie
  • *
  • Posts: 37
    • View Profile
Loading the Image in a class?
« Reply #3 on: October 19, 2008, 10:10:29 am »
quasius: This is just a test, i know this is a terrible method of doing it. But i haven't figured out a dynamic way to do it that isn't horrible yet.

Laurent: Ahh, it worked too!. Thanx!

Wizzard

  • Full Member
  • ***
  • Posts: 213
    • View Profile
Loading the Image in a class?
« Reply #4 on: October 19, 2008, 10:49:20 pm »
Quote from: "Laurent"
The image you give to your sprite is a temporary one which is destroyed when the loadsprite function exits. You must keep the image valid as long as the sprites uses it. And giving it the image stored in your vector wouldn't solve the problem, because this image may be moved in memory when the vector grows.

Are vectors the only STL container that shouldn't be used with pointers?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Loading the Image in a class?
« Reply #5 on: October 20, 2008, 07:54:50 am »
Quote
Are vectors the only STL container that shouldn't be used with pointers?

It's more "don't point to an element of the container" rather than just "use with pointers". list, map and set are the only ones that don't move their elements in memory.
Laurent Gomila - SFML developer