Ok so, here is a snippet of code I just created.
#include <SFML/Graphics.hpp>
#include "ImageService.h"
#include "DisplayObject.h"
#include "Enums.h"
ImageService is;
DisplayObject obj1;
DisplayObject obj2;
DisplayObject obj3;
DisplayObject obj4;
DisplayObject obj5;
int timer = 0;
int main()
{
sf::RenderWindow window(sf::VideoMode(1024, 768), "SFML works!");
is.Images[image1].loadFromFile("Block.png");
obj1.sprite.setTexture(is.Images[obj1.spriteType]);
obj1.sprite.setColor(sf::Color(255, 0, 0));
obj1.sprite.setPosition(100,100);
obj2.sprite.setTexture(is.Images[obj2.spriteType]);
obj2.sprite.setColor(sf::Color(0, 255, 0));
obj3.sprite.setPosition(200, 100);
obj3.sprite.setTexture(is.Images[obj3.spriteType]);
obj3.sprite.setColor(sf::Color(0, 0, 255));
obj4.sprite.setPosition(300,100);
obj4.sprite.setTexture(is.Images[obj4.spriteType]);
obj4.sprite.setColor(sf::Color(127, 127, 127));
obj5.sprite.setPosition(400,100);
obj5.sprite.setTexture(is.Images[obj5.spriteType]);
std::vector<sf::Sprite> displayObjects;
displayObjects.push_back(obj1.sprite);
displayObjects.push_back(obj2.sprite);
displayObjects.push_back(obj3.sprite);
displayObjects.push_back(obj4.sprite);
displayObjects.push_back(obj5.sprite);
window.setFramerateLimit(60);
while (window.isOpen())
{
timer++;
if (timer == 60)
{
obj1.sprite.setPosition(100, 200);
obj2.sprite.setPosition(200, 200);
obj4.sprite.setPosition(400, 200);
}
for (int i = 0; i < displayObjects.size(); i++)
{
window.draw(displayObjects.at(i));
}
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
//Draw out the selected sprites
for (int i = 0; i < displayObjects.size(); i++)
{
window.draw(displayObjects.at(i));
}
window.display();
}
return 0;
}
a DisplayObject at the moment just holds a Sprite and has a Sprites, Sprites is an enum which is used for loading the correct sprite for that object.
So as you can see we have 5 display objects, these are then pushed onto the vector, now that I have updated obj1, obj2 and obj4, how would I then tell the vector to update these? I can just say at position 0, 1 and 3, but if I were to do that where would I get the numbers 0, 1 and 3 from? Would i have to do something on the lines of this?
#include <SFML/Graphics.hpp>
#include "ImageService.h"
#include "DisplayObject.h"
#include "Enums.h"
ImageService is;
DisplayObject obj1;
DisplayObject obj2;
DisplayObject obj3;
DisplayObject obj4;
DisplayObject obj5;
int timer = 0;
int main()
{
sf::RenderWindow window(sf::VideoMode(1024, 768), "SFML works!");
is.Images[image1].loadFromFile("Block.png");
obj1.sprite.setTexture(is.Images[obj1.spriteType]);
obj1.sprite.setColor(sf::Color(255, 0, 0));
obj1.sprite.setPosition(100,100);
obj2.sprite.setTexture(is.Images[obj2.spriteType]);
obj2.sprite.setColor(sf::Color(0, 255, 0));
obj2.sprite.setPosition(200, 100);
obj3.sprite.setTexture(is.Images[obj3.spriteType]);
obj3.sprite.setColor(sf::Color(0, 0, 255));
obj3.sprite.setPosition(300,100);
obj4.sprite.setTexture(is.Images[obj4.spriteType]);
obj4.sprite.setColor(sf::Color(127, 127, 127));
obj4.sprite.setPosition(400,100);
obj5.sprite.setTexture(is.Images[obj5.spriteType]);
std::vector<sf::Sprite> displayObjects;
displayObjects.push_back(obj1.sprite);
obj1.positionInVector = displayObjects.size() - 1;
displayObjects.push_back(obj2.sprite);
obj2.positionInVector = displayObjects.size() - 1;
displayObjects.push_back(obj3.sprite);
obj3.positionInVector = displayObjects.size() - 1;
displayObjects.push_back(obj4.sprite);
obj4.positionInVector = displayObjects.size() - 1;
displayObjects.push_back(obj5.sprite);
obj5.positionInVector = displayObjects.size() - 1;
window.setFramerateLimit(60);
while (window.isOpen())
{
timer++;
if (timer == 60)
{
obj1.sprite.setPosition(100, 200);
displayObjects.at(obj1.positionInVector) = obj1.sprite;
obj2.sprite.setPosition(200, 200);
displayObjects.at(obj2.positionInVector) = obj2.sprite;
obj4.sprite.setPosition(400, 200);
displayObjects.at(obj4.positionInVector) = obj4.sprite;
}
for (int i = 0; i < displayObjects.size(); i++)
{
window.draw(displayObjects.at(i));
}
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
//Draw out the selected sprites
for (int i = 0; i < displayObjects.size(); i++)
{
window.draw(displayObjects.at(i));
}
window.display();
}
return 0;
}
It does seem a little long winded, but it does work. Is that what you were thinking?
Update
dwarfman78 I do agree with you, but I don't want to get into a habit that is seen as "wrong" or "not great for performance" code.