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

Author Topic: Please have a look and try explain why Sprites not redrawing in new positions  (Read 1799 times)

0 Members and 1 Guest are viewing this topic.

S_BISHOP

  • Newbie
  • *
  • Posts: 15
    • View Profile
    • Email
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;
}


 
« Last Edit: August 27, 2014, 10:48:12 am by S_BISHOP »

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
What does getActiveLevelPlatformableIndex(i) look like?

The crystal ball says you are modifying a copy instead of the actual object.
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

S_BISHOP

  • Newbie
  • *
  • Posts: 15
    • View Profile
    • Email
What does getActiveLevelPlatformableIndex(i) look like?

The crystal ball says you are modifying a copy instead of the actual object.


Hey mate thanks for the reply I did not realize you had posted a response or anyone for that matter.
This is what the member function prototype and the member function definition body look like:


Platformable& getActiveLevelPlatformableIndex(int index);

Platformable& LevelManager::getActiveLevelPlatformableIndex(int index){
    return activeLevelPlatformables[index];
}

After you having viewed this.
My question to you. Am i really modifying only a local copy and not the passed reference ?
Please get back to me mate.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Try outputting getActiveLevelPlatformableIndex(i).getSpriteX() to see if the values are as expected.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*