Hello
I would like to know if anyone can see why and then thoughtfully explain to me why my sprites are not redrawing in new positions. I,ve spent a fair amount of time looking into this and examining my program functionality first before i had posted.
With what I have posted, In theory should this work fine?
If anyone can see a beginner mistake in either SFML or C++ please be kind and let me know.
I have a vector container of this type of object, each instance created contains a sf::Texture and sf::Sprite
all vector oriented member functions are return by reference.
std::vector<Platformable> activeLevelPlatformables;
std::vector<Platformable>& getActiveLevelPlatformables();
std::vector<Platformable>& LevelManager::getActiveLevelPlatformables(){
return activeLevelPlatformables;
}
I then have these to member functions that get called continuously during the FPS of the main game loop of the main.cpp, what ever is the default FPS of the main game loop, no changes have been made there.
The first member function to re position all the sprites
The second member function to render all sprite position changes and draw re draw all the sprites to the screen. This member function receives the window object passed by reference.
void positionActiveLevelSprites();
void LevelManager::positionActiveLevelSprites(){
for (std::vector<Platformable>::size_type i = 0; i != getActiveLevelPlatformables().size(); i++) {
getActiveLevelPlatformableIndex(i).setSpriteX(getActiveLevelPlatformableIndex(i).getSpriteX() + 20 );
}
}
void drawActiveLevel(sf::RenderWindow &rw);
void LevelManager::drawActiveLevel(sf::RenderWindow &rw){
for (std::vector<Platformable>::size_type i = 0; i != getActiveLevelPlatformables().size(); i++) {
rw.draw(getActiveLevelPlatformableIndex(i).getSprite());
}
}
please note: The key press A event handler is just a dummy value test, no specifics have been handled.
Then the main.cpp looks like this:
#include <SFML/Graphics.hpp>
#include <LevelManager.h>
#include <Vector>
#include <iostream>
int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window"/*,sf::Style::Fullscreen*/);
LevelManager lm;
lm.loadActiveLevel();
// Start the game loop
while (window.isOpen())
{
// Process events
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed) {
window.close();
}
if (event.type == sf::Event::KeyPressed) {
if (event.key.code == sf::Keyboard::Escape) {
window.close();
}
if (event.key.code == sf::Keyboard::A) {
lm.positionActiveLevelSprites();
std::cout << "this is working" << std::endl;
}
}
}
lm.drawActiveLevel(window);
window.display();
window.clear();
}
return EXIT_SUCCESS;
}