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

Author Topic: Drawables manager [Solved]  (Read 3452 times)

0 Members and 1 Guest are viewing this topic.

TechRogue

  • Jr. Member
  • **
  • Posts: 63
    • View Profile
Drawables manager [Solved]
« on: February 23, 2011, 06:37:56 am »
I'm trying to clean up my render loop and I thought this might work, since It works fine with a vector of Sprites/Shapes etc.:

Code: [Select]


std::vector< sf::drawable > Drawables;
Drawables.push_back(SomeSprite);
Drawables.push_back(SomeShape);
Drawables.push_back(SomeText);

...

for (unsigned int i = 0; i < Drawables.size(); i++)
{
    App.Draw(Drawables[i]);
}



However the compilation fails with these errors:
Quote

/usr/include/c++/4.4/ext/new_allocator.h||In member function ‘void __gnu_cxx::new_allocator<_Tp>::construct(_Tp*, const _Tp&) [with _Tp = sf::Drawable]’:|
/usr/include/c++/4.4/bits/stl_vector.h|737|instantiated from ‘void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = sf::Drawable, _Alloc = std::allocator<sf::Drawable>]’|
/home/jake/Projects/CodeBlocks workspaces/Meander/main.cpp|76|instantiated from here|
/usr/include/c++/4.4/ext/new_allocator.h|105|error: cannot allocate an object of abstract type ‘sf::Drawable’|
/usr/include/SFML/Graphics/Drawable.hpp|59|note:   because the following virtual functions are pure within ‘sf::Drawable’:|
/usr/include/SFML/Graphics/Drawable.hpp|341|note:    virtual void sf::Drawable::Render(sf::RenderTarget&) const|
/usr/include/c++/4.4/bits/vector.tcc||In member function ‘void std::vector<_Tp, _Alloc>::_M_insert_aux(__gnu_cxx::__normal_iterator<typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer, std::vector<_Tp, _Alloc> >, const _Tp&) [with _Tp = sf::Drawable, _Alloc = std::allocator<sf::Drawable>]’:|
/usr/include/c++/4.4/bits/stl_vector.h|741|instantiated from ‘void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = sf::Drawable, _Alloc = std::allocator<sf::Drawable>]’|
/home/jake/Projects/CodeBlocks workspaces/Meander/main.cpp|76|instantiated from here|
/usr/include/c++/4.4/bits/vector.tcc|306|error: cannot allocate an object of abstract type ‘sf::Drawable’|
/usr/include/SFML/Graphics/Drawable.hpp|59|note:   since type ‘sf::Drawable’ has pure virtual functions|
/usr/include/c++/4.4/bits/vector.tcc|306|error: cannot declare variable ‘__x_copy’ to be of abstract type ‘sf::Drawable’|
/usr/include/SFML/Graphics/Drawable.hpp|59|note:   since type ‘sf::Drawable’ has pure virtual functions|
/usr/include/c++/4.4/bits/stl_uninitialized.h|117|instantiated from ‘_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = sf::Drawable*, _ForwardIterator = sf::Drawable*]’|
/usr/include/c++/4.4/bits/stl_uninitialized.h|257|instantiated from ‘_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = sf::Drawable*, _ForwardIterator = sf::Drawable*, _Tp = sf::Drawable]’|
/usr/include/c++/4.4/bits/stl_uninitialized.h|267|instantiated from ‘_ForwardIterator std::__uninitialized_move_a(_InputIterator, _InputIterator, _ForwardIterator, _Allocator&) [with _InputIterator = sf::Drawable*, _ForwardIterator = sf::Drawable*, _Allocator = std::allocator<sf::Drawable>]’|
/usr/include/c++/4.4/bits/vector.tcc|338|instantiated from ‘void std::vector<_Tp, _Alloc>::_M_insert_aux(__gnu_cxx::__normal_iterator<typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer, std::vector<_Tp, _Alloc> >, const _Tp&) [with _Tp = sf::Drawable, _Alloc = std::allocator<sf::Drawable>]’|
/usr/include/c++/4.4/bits/stl_vector.h|741|instantiated from ‘void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = sf::Drawable, _Alloc = std::allocator<sf::Drawable>]’|
/home/jake/Projects/CodeBlocks workspaces/Meander/main.cpp|76|instantiated from here|
/usr/include/c++/4.4/bits/stl_uninitialized.h|74|error: cannot allocate an object of abstract type ‘sf::Drawable’|
/usr/include/SFML/Graphics/Drawable.hpp|59|note:   since type ‘sf::Drawable’ has pure virtual functions|


which leads me to believe that a base class with pure virtual functions cannot be added to a vector. So:

Does anyone have a suggestion on how I would implement this?

devlin

  • Full Member
  • ***
  • Posts: 128
    • View Profile
Drawables manager [Solved]
« Reply #1 on: February 23, 2011, 07:58:06 am »
You would need to use sf::drawable* (i.e. pointer) in order to store them like that.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Drawables manager [Solved]
« Reply #2 on: February 23, 2011, 10:43:06 am »
And when you use new, don't forget delete.

By the way: sf::Drawable
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

TechRogue

  • Jr. Member
  • **
  • Posts: 63
    • View Profile
Drawables manager [Solved]
« Reply #3 on: February 23, 2011, 10:40:20 pm »
Perfect! I ended up storing references instead of objects created with 'new', so when my sprites change position they update on the screen. Thanks to both of you!