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

Author Topic: Error with calling sf::Sprite  (Read 5413 times)

0 Members and 1 Guest are viewing this topic.

Devero

  • Newbie
  • *
  • Posts: 11
    • View Profile
Error with calling sf::Sprite
« on: December 18, 2011, 07:06:01 am »
Hey Guys,
So I'm in the process of trying to optimize my game/project a bit-- trying to split things up from one giant, cluttered main loop to a main loop with a LoadMedia() function which defines my Sprites, Music, Fonts, etc. A Menu() function which runs the game menu loop, and the Game() function that runs the main game loop.

I'm passing the Sprites defined in the LoadMedia function to the Menu functions; however, I'm encountering some errors.

Any help would be greatly appreciated.

Code: [Select]

int LoadMedia();
int Menu(sf::Sprite*);
int Game();

int LoadMedia() {
    sf::Image ITitle;
    if(!ITitle.LoadFromFile("media/images/UI/title.png")) {return EXIT_FAILURE;}

    sf::Sprite Title(ITitle);
    Title.SetCenter(221,51.5);
}

int Menu (sf::Sprite* Title) {
    Title.SetPosition(Menu.GetWidth()/2,100);
}

int Game() {
//The usual stuff
}

int main() {
  LoadMedia();
  Menu(Title);
  Game();

  return EXIT_SUCCESS;
}


The errors I am getting are related to any sprite functions that I call in the Menu class. The one specifically related to the .SetPosition function is here:
Code: [Select]

error: request for member 'SetPosition' in 'Title', which is of non-class type 'sf::Sprite*'


Thanks in advance for any and all help!

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Error with calling sf::Sprite
« Reply #1 on: December 18, 2011, 10:03:22 am »
You need to dereference the pointer before you access the sf::Sprite members. There exists a short syntax with -> for this purpose.
Code: [Select]
(*Title).SetPosition(...);
Title->SetPosition(...);

The alternative is to use references. They are dereferenced automatically, and you can just use the . operator.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Devero

  • Newbie
  • *
  • Posts: 11
    • View Profile
Error with calling sf::Sprite
« Reply #2 on: December 18, 2011, 05:55:19 pm »
Thanks!
Unfortunately after trying both methods, I'm now receiving:

Code: [Select]

error: Title was not declared in this scope


referring to where I call the Menu(Title) function in my main.

Anata

  • Jr. Member
  • **
  • Posts: 77
    • View Profile
Error with calling sf::Sprite
« Reply #3 on: December 18, 2011, 07:17:39 pm »
try with

Code: [Select]
int Menu (sf::Sprite &Title)

Tex Killer

  • Full Member
  • ***
  • Posts: 242
    • View Profile
Error with calling sf::Sprite
« Reply #4 on: December 18, 2011, 07:19:38 pm »
Your Title variable is local to LoadMedia, so after it is executed Title gets destroyed. You can make it global, or you can return it to the main function. If you choose to return it to main, you must use dynamic allocation... making a pointer and constructing it with new.

Devero

  • Newbie
  • *
  • Posts: 11
    • View Profile
Error with calling sf::Sprite
« Reply #5 on: December 18, 2011, 07:49:04 pm »
Anata
Unfortunately, that didn't work. Thanks for the input though.

Tex Killer
I'm not entirely sure how to do that, do you know of any good tutorials or are you willing to give an example?

Thanks guys!

Tex Killer

  • Full Member
  • ***
  • Posts: 242
    • View Profile
Re: Error with calling sf::Sprite
« Reply #6 on: December 18, 2011, 07:58:49 pm »
Code: [Select]

sf::Sprite* LoadMedia();
int Menu(sf::Sprite* Title);
int Game();

sf::Sprite* LoadMedia() {
    sf::Image* ITitle = new sf::Image();
    if(!ITitle->LoadFromFile("media/images/UI/title.png")) {return EXIT_FAILURE;}

    sf::Sprite* Title = new sf::Sprite(ITitle);
    Title->SetCenter(221,51.5);
    return Title;
}

int Menu (sf::Sprite* Title) {
    Title->SetPosition(Menu.GetWidth()/2,100);
}

int Game() {
//The usual stuff
}

int main() {
  sf::Sprite* Title = LoadMedia();
  Menu(Title);
  Game();

  return EXIT_SUCCESS;
}

Devero

  • Newbie
  • *
  • Posts: 11
    • View Profile
Error with calling sf::Sprite
« Reply #7 on: December 18, 2011, 08:28:53 pm »
That makes a bit more sense, but if I have multiple unique sprites along with music, and fonts in my LoadMedia() function, how would I handle that?

I realize I should have put that into the first bit of code, but I thought it would be a standardized solution, not handling each one separately.

--Using your way would mean using "sf::Sprite* Title = LoadMenu();" multiple times for multiple objects. While I haven't tried it, I have a feeling that wouldn't work as intended.

For example:
Code: [Select]

int LoadMedia();
int Menu(sf::Sprite*, sf::Sprite*, sf::Sprite*, bool, sf::Music*);
int Game(sf::String*, sf::Music*, sf::Sprite*, sf::Sprite*, sf::Sprite*, sf::Sprite*);

int main() {

LoadMedia();
Menu(Title, Easy, Hard,  MouseLeftDown, MenuMusic);
Game(HealthText, GameMusic, playerSprite,  World1, World2, spacestationSprite);

}
Code: [Select]

int LoadMedia () {

//Picture Data --------------------------------------------------
    sf::Image ITitle;
    if(!ITitle.LoadFromFile("media/images/UI/title.png")) {return EXIT_FAILURE;}

    sf::Sprite Title(ITitle);
    Title.SetCenter(221,51.5);

    sf::Image difficultyImage;
    if(!difficultyImage.LoadFromFile("media/images/UI/difficulty.png")) {return EXIT_FAILURE;}

    sf::Sprite Easy(difficultyImage);               //Easy
    Easy.SetSubRect(sf::IntRect(0,0,200,50));
    Easy.SetCenter(100,25);

    sf::Sprite Hard(difficultyImage);               //Hard
    Hard.SetSubRect(sf::IntRect(0,100,200,150));
    Hard.SetCenter(100,25);

//Music Data -----------------------------------------------------------------

    sf::Music MenuMusic;
    if(!MenuMusic.OpenFromFile("media/music/The Son of Flynn.ogg")) {return EXIT_FAILURE;}

    sf::Music GameMusic;
    if(!GameMusic.OpenFromFile("media/music/Separate Ways.ogg")) {return EXIT_FAILURE;}

// Text Data -----------------------------------------------------------------

    sf::Font MyFont;
    if (!MyFont.LoadFromFile("media/Bedizen.ttf", 25)) {return EXIT_FAILURE;}

    sf::String HealthText;
    HealthText.SetFont(MyFont);
    HealthText.SetColor(sf::Color::Black);
    HealthText.SetSize(20);
}
Code: [Select]

int Menu (sf::Sprite* Title, sf::Sprite* Easy, sf::Sprite* Hard, bool MouseLeftDown, sf::Music* MenuMusic) {

    (*MenuMusic).Play();

    (*Title).SetPosition(Menu.GetWidth()/2,100);
    (*Easy).SetPosition(Menu.GetWidth()/2,340);
    (*Hard).SetPosition... yadda yadda yadda

    Menu.Draw(*Title);
    Menu.Draw(*Easy);
    Menu.Draw(*Hard);
}

Tex Killer

  • Full Member
  • ***
  • Posts: 242
    • View Profile
Error with calling sf::Sprite
« Reply #8 on: December 18, 2011, 08:51:42 pm »
You need the first solution that I've sugested earlier.
Like this:

Code: [Select]

sf::Sprite* Title;
int LoadMedia();
int Menu(sf::Sprite*);
int Game();

int LoadMedia() {
    sf::Image* ITitle = new sf::Image();
    if(!ITitle->LoadFromFile("media/images/UI/title.png")) {return EXIT_FAILURE;}

    Title = new sf::Sprite(ITitle);
    Title->SetCenter(221,51.5);
}

int Menu () {
    Title->SetPosition(Menu.GetWidth()/2,100);
}

int Game() {
//The usual stuff
}

int main() {
  LoadMedia();
  Menu();
  Game();

  return EXIT_SUCCESS;
}


You wont even need to pass them as parameters, as they'll be global.

Devero

  • Newbie
  • *
  • Posts: 11
    • View Profile
Error with calling sf::Sprite
« Reply #9 on: December 18, 2011, 09:38:53 pm »
So after changing my code to use the global sprites I'm now getting a weird error related to:
Code: [Select]

Title = new sf::Sprite(ITitle);
Errors:
Code: [Select]

no matching function for call to 'sf::Sprite::Sprite(sf::Image*&)


Its giving the exact same error for each sprite when that line is ran.

This is pretty new territory for me, so I'm sorry its taking me a while to understand =\ Thank you so much for the help though.

Tex Killer

  • Full Member
  • ***
  • Posts: 242
    • View Profile
Error with calling sf::Sprite
« Reply #10 on: December 18, 2011, 09:59:25 pm »
Sorry, it should be:

Code: [Select]
Title = new sf::Sprite(*ITitle);

Devero

  • Newbie
  • *
  • Posts: 11
    • View Profile
Error with calling sf::Sprite
« Reply #11 on: December 18, 2011, 10:17:08 pm »
Thank you so much for the help!