I'm working on a grid to speed up collision detection, and the way I have this set up is that a GridSquare class holds a std::vector of all the objects that are within that specific grid square (determined by using a sf::FloatRect::intersects()). Here is what I mean by this class:
class GridSquare{
public:
GridSquare(float x, float y, float px, float py); //position.x, position.y, width, height,
~GridSquare();
sf::RectangleShape &GetVisual();
sf::FloatRect &GetGridSquare();
void AddStationaryObject(sf::Transformable &object);
void AddMovableObject(sf::Transformable &object);
std::vector<sf::Transformable> &GetStationaryObjects();
std::vector<sf::Transformable> &GetMovableObjects();
std::vector<sf::Transformable> &GetGridObjects();
private:
//so in the gameloop I'll check for what objects intersect this float rect, and...
sf::FloatRect gridSquare;
//...add these objects to the respective vectors below
//stationary objects hold walls and such, movable objects would be player and projectiles
std::vector<sf::Transformable> stationaryObjects_, movableObjects_, gridObjects_;
};
The issue is that sf::RenderWindow::draw() does not take a sf::Transformable as a parameter, so when I declare the vectors above as "std::vector<sf::Transformable>" and try to loop through the vector and draw the objects, this compiler error is thrown:
IntelliSense: no instance of overloaded function "sf::RenderWindow::draw" matches the argument list
argument types are: (sf::Transformable)
object type is: sf::RenderWindow c:\Users\jordan\Documents\Visual Studio 2012\Projects\SFML Test\SFML Test\SFML Test.cpp 20
Here is a small code example to see for yourself:
int _tmain(int argc, _TCHAR* argv[]){
sf::RenderWindow window;
window.create(sf::VideoMode(500, 500, 32), "Test");
//a vector of sf::Transformable, can't be drawn on screen because a transformable isn't a drawable
std::vector<sf::Transformable> objectsToDraw;
objectsToDraw.push_back(sf::RectangleShape(sf::Vector2f(20, 20)));
while(window.isOpen()){
//just calling the draw function here, no need to worry about updating because nothing is being updated
window.clear();
window.draw(objectsToDraw[0]); //throws an error that draw() doesn't take an argument of sf::Transformable
window.display();
}
return 0;
}
So, the ideal fix would be to declare those std::vectors as std::vector<sf::Drawable>, that way their objects can be drawn, right? Well, that throws a different error:
Error 2 error C2259: 'sf::Drawable' : cannot instantiate abstract class c:\program files (x86)\microsoft visual studio 11.0\vc\include\xmemory0 617
Again, a small code example:
#include "stdafx.h"
#include "SFML\Graphics.hpp"
int _tmain(int argc, _TCHAR* argv[]){
sf::RenderWindow window;
window.create(sf::VideoMode(500, 500, 32), "Test");
//a vector of sf::Drawable, throws an error that the abstract class can't be instantiated
std::vector<sf::Drawable> objectsToDraw;
objectsToDraw.push_back(sf::RectangleShape(sf::Vector2f(20, 20)));
while(window.isOpen()){
//just calling the draw function here, no need to worry about updating because nothing is being updated
window.clear();
//window.draw(objectsToDraw[0]); //the error isn't thrown here, but above when the vector is declared
window.display();
}
return 0;
}
I don't understand this error. I just want a std::vector that will hold all types of sfml drawable objects, which I can then iterate through and draw all the objects on screen. Any suggestions?