Jump to content

  • Log In with Google      Sign In   
  • Create Account

#ActualCryusaki

Posted 17 August 2013 - 10:26 PM

After about a half hour of intense trouble shooting I have found out my simple error, sorry for this now useless bump :P


#1Cryusaki

Posted 17 August 2013 - 09:57 PM

Sorry to beat a dead horse but when I read your explaination it all made sense and I thought my issue was simple but what went on in my head and what actually happened had different results

I just dont understand what I'm doing wrong, the BackgroundSprite and BackgroundTexture are in the public variables. I initialize the class so the Constructor must have ran and I did it at the beginning of the program. The destructor was never called so the BackgroundSprite must still be alive but when I try and draw it to the screen I just can't get it to be in scope. How did it get out of scope?!

 

Main.cpp

#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include "include\SpriteHandler.h"

#include <iostream>

int main()
{
        // Creates an 800x600 Window
    sf::RenderWindow window(sf::VideoMode(800, 600), "Pong Clone");

    SpriteHandler SpriteManager; // Create Class Object

        // Init Clock Variables
    float frameCounter = 0, switchFrame = 100, frameSpeed = 500;

        // Disable Repeating Keys
    window.setKeyRepeatEnabled(false);

        // Initialize Clock and Time Variables
    sf::Clock clock;
    sf::Time time;

        // Game Loop
    while (window.isOpen())
    {
            // Event Init and Loop
        sf::Event Event;
        while (window.pollEvent(Event))
        {
            if (Event.type == sf::Event::Closed)
                window.close();
        }

        // Event Detector
    switch(Event.type)
    {
        //case sf::Event::KeyPressed:
            //if(Event.key.code == sf::Keyboard::Up)
                // Check if Up Key was Pressed
    }

            // Clock Work
        frameCounter += clock.restart().asSeconds();
        if(frameCounter >= switchFrame)
            frameCounter = 0;

            // Drawing Screen
        window.clear();

            // Draw Sprites to Window
        window.draw(BackgroundSprite);

            // Display Window
        window.display();
    }

    return 0;
}

SpriteHandler.h

#ifndef SPRITEHANDLER_H
#define SPRITEHANDLER_H

#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>

class SpriteHandler
{
    public:
        SpriteHandler();

        sf::Texture BackgroundTexture;
        sf::Sprite BackgroundSprite;

    protected:
    private:
};

#endif // SPRITEHANDLER_H

SpriteHandler.cpp

#include "..\include\SpriteHandler.h"

#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>

#include <iostream>

SpriteHandler::SpriteHandler()
{
    if(!BackgroundTexture.loadFromFile("PongField.png"))
        std::cout << "Could not load Background." << std::endl;
    BackgroundSprite.setTexture(BackgroundTexture);
}

main.cpp|52|error: 'BackgroundSprite' was not declared in this scope|

PARTNERS