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

Author Topic: Classes and SFML  (Read 3848 times)

0 Members and 1 Guest are viewing this topic.

beginner88

  • Newbie
  • *
  • Posts: 7
    • View Profile
Classes and SFML
« on: December 24, 2008, 12:12:38 am »
Hey :)

first at all, I´m new at programming and total new on programming with an API.
My first steps with SFML were very comfortable, it´s really easie to load some sprites, etc. But now I thought I could write the sf::Image, or sf::String function in a class, to make my source more clearly.

I read some post in the forum and came to a solution
Code: [Select]

class CSprite
{
public:
sf::String string;

virtual void Render(const sf::RenderWindow &Window) const
{
Window.Draw(string);
}
};


The main function

Code: [Select]

CSprite sprite;


And what now?
How should the render function look like?

Code: [Select]

sprite.Render(Window);
/* ??? */

And what is the best option to set the parameters?

Sorry for the many questions but I´ve never worked with an API befor and I spent a lot of time infront of my screen but I didn´t get a result :?

So are my ideas possible, or how would you handle this situation?

Wizzard

  • Full Member
  • ***
  • Posts: 213
    • View Profile
Classes and SFML
« Reply #1 on: December 24, 2008, 11:30:16 am »
Is that class supposed to make sense or is it for learning purposes?
Code: [Select]
// Draws the CSprite using the Render function
window.Draw(sprite);

irri

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • http://www.irri.se
Classes and SFML
« Reply #2 on: December 24, 2008, 12:05:53 pm »
Of course that is possible, but I don't see the point in doing so.
sf::Sprite, sf::Image and all those are already separate classes.

Oh, and one more thing
the sf::RenderWindow-parameter cannot be const
Code: [Select]
 void Render(sf::RenderWindow &Window) const
   {
      Window.Draw(string);
   }
2D RPG Game (School project): http://PA.irri.se/

beginner88

  • Newbie
  • *
  • Posts: 7
    • View Profile
Classes and SFML
« Reply #3 on: December 24, 2008, 12:27:11 pm »
Thank you both!
@Wizzard: It´s just to practice how to use an API and C++ :)

@irri: Ok, but I thought when I get a class, for example, CPlayer with all his attributes like Health, Amor, etc. that I could use the sf::Image, sf::Sprite function in my class, to keep everything together... of course this is just an easy example and I know that it is more difficult in a "real" game.

Quote
Code: [Select]
// Draws the CSprite using the Render function
window.Draw(sprite);

If I would call this in the main function (while(Window.IsOpen())); I would try to render the instance of the CSprite class ( :?: ).

irri

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • http://www.irri.se
Classes and SFML
« Reply #4 on: December 24, 2008, 01:10:00 pm »
Quote from: "beginner88"
Thank you both!
@irri: Ok, but I thought when I get a class, for example, CPlayer with all his attributes like Health, Amor, etc. that I could use the sf::Image, sf::Sprite function in my class, to keep everything together... of course this is just an easy example and I know that it is more difficult in a "real" game.

Yeah, of course. But wouldn't it be easier to "Window.Draw(myCSprite.string);" in the main loop, if the sf::String in your class is public?

or, if your sf::string i private:

sf::String & GetString() const
{
  return myString;
}

and then in the main loop Window.Draw(myCSprite.GetString());

they're just suggestions :P
2D RPG Game (School project): http://PA.irri.se/

beginner88

  • Newbie
  • *
  • Posts: 7
    • View Profile
Classes and SFML
« Reply #5 on: December 27, 2008, 06:02:38 pm »
Sorry that my answer comes quite late, but I used to spend the holidays with my familie and girlfriend :)

@irri: Ok, thanks for your suggestions, I think I´ll try it during the next few days...

 

anything