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

Author Topic: Little surface class  (Read 4333 times)

0 Members and 1 Guest are viewing this topic.

BloodGod

  • Newbie
  • *
  • Posts: 10
    • View Profile
Little surface class
« on: May 22, 2008, 05:26:25 pm »
Hi =)

I've coded a little surface class, ok it's a class that stores a set of sf:Sprites  :roll: :

Code: [Select]

#include <vector>
#include <SFML/Graphics.hpp>

class Surface
{
      std::vector<sf::Sprite> m_sprites;
   public:
      void Add(sf::Sprite &s);
      void Clear();
      void Display(sf::RenderWindow& rw) const;
      Surface();
      Surface(int reserve);
};

Surface::Surface()
{
}

Surface::Surface(int reserve)
{
   m_sprites.reserve(reserve);
}

void Surface::Add(sf::Sprite &s)
{
   m_sprites.push_back(s);
}
void Surface::Clear()
{
   m_sprites.clear();
}

void Surface::Display(sf::RenderWindow& rw) const
{
   for(unsigned int i = 0;i < m_sprites.size();i++)
      rw.Draw(m_sprites[i]);

   rw.Display();
}


usage:
Code: [Select]

     sf::Image Image;
     if (!Image.LoadFromFile("image.jpg"))
         return EXIT_FAILURE;
     sf::Sprite Sprite(Image);
     Surface surf;

     surf.Add(Sprite);
/*......*/
     surf.Display(App); //App -> sf::RenderWindow


I thought it will might be useful for someone. Be careful, the sprites are copied. That makes it possible to "draw" 1 Sprite onto different surfaces...

Maybe it would be a nice idea to make real surfaces :/
Sicerely,
BloodGod
Verlässt du dich auf andere, bist du selbst verlassen ;)

zarka

  • Jr. Member
  • **
  • Posts: 81
    • View Profile
Little surface class
« Reply #1 on: May 23, 2008, 09:31:01 am »
a few tips on how you might want to change this.

change the Add method to take in a const sf::Sprite &s this is good since you avoid making several unnecessary copy constructor calls. (and also makes it more const correct)

you could also remove the member iterator m_it and use the push_back method in the vector.

to make this even more const correct you could make your Display function const to (since it does not change any data in the class)
Code: [Select]
void Display(const sf::RenderWindow& rw) const;
//Zzzarka

BloodGod

  • Newbie
  • *
  • Posts: 10
    • View Profile
Little surface class
« Reply #2 on: May 23, 2008, 04:51:27 pm »
Hi =)

Thanks for the tip(I've changed the first post), but
Code: [Select]
void Surface::Display([color=red]const[/color] sf::RenderWindow& rw) const

does not work:
Code: [Select]

error: no matching function for call to `sf::RenderWindow::Display() const'


this works:
Code: [Select]
void Surface::Display(sf::RenderWindow& rw) const

Did I oversight something?

Sincerely,
BloodGod[/code]
Verlässt du dich auf andere, bist du selbst verlassen ;)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Little surface class
« Reply #3 on: May 24, 2008, 05:40:29 am »
sf::RenderWindow is not const, so you can't call it if you have a const RenderWindow.

However, I'm not sure it's a good idea to call RenderWindow::Display in this function, as it's supposed to be called once per frame. This means you would use only one Surface instance in your program.
Laurent Gomila - SFML developer

zarka

  • Jr. Member
  • **
  • Posts: 81
    • View Profile
Little surface class
« Reply #4 on: May 30, 2008, 10:58:32 am »
Quote from: "BloodGod"

does not work:
Code: [Select]

error: no matching function for call to `sf::RenderWindow::Display() const'


this works:
Code: [Select]
void Surface::Display(sf::RenderWindow& rw) const
[/code]


aah i see. my bad i didn't really test the code before i submitted it... but ofcource if 6he const version does not work go with non const :)
//Zzzarka

 

anything