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

Author Topic: Window is drawing Background of whatever is behind the window/Window draw ERROR  (Read 2425 times)

0 Members and 1 Guest are viewing this topic.

Kleath

  • Newbie
  • *
  • Posts: 7
    • View Profile
Hello,
Im making a Pong Game with SFML. But when i start the Game it just draws the Background of whatever is behind the window. If I start the Game with the Dektop behind the Game Window it just draws the Desktop-Background. But it worked previously. Is it possible that the OS is causing the problems ?

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Make sure you have clear, draw and display every cycle:
window.clear();
window.draw(sprite); // "sprite" is an example here
window.display();

It doesn't actually show anything until the "display" so you will continue to see the background that was behind it until you call it (display).
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Kleath

  • Newbie
  • *
  • Posts: 7
    • View Profile
Yes , thisis my Loop :

while (window.isOpen())
   {
      CurrentState->HandleEvents(*this);
      window.clear(sf::Color(0, 134, 194));
      CurrentState->Update(*this);
      CurrentState->Draw(*this);
      window.display();
   }

so this shouldn't be the problem.

Gambit

  • Sr. Member
  • ****
  • Posts: 283
    • View Profile
Where is the rest of your code?

Kleath

  • Newbie
  • *
  • Posts: 7
    • View Profile
Pong.cpp :
#include "Game.h"

int main()
{
        Game Pong;
        Pong.ChangeState(Game::State::MAINMENU);

        while (Pong.IsRunning())
        {
                Pong.Run();
        }

        return 0;
}



 
GameState.h :
#ifndef GAMESTATE_HPP
#define GAMESTATE_HPP

class Game;

class GameState{
public:
        virtual void HandleEvents(Game& game) = 0;
        virtual void Update(Game& game) = 0;
        virtual void Draw(Game& game) = 0;
        virtual      ~GameState() {};
};

#endif
 

Game.h :
#ifndef GAME_HPP
#define GAME_HPP

#include <SFML\Graphics.hpp>
#include "GameState.h"
#include "MainMenuState.h"
#include "PlayState.h"
#include <memory>

class Game{
public:

        enum class State{ MAINMENU, PLAY };
        sf::RenderWindow window;

        Game();
        void Run();
        void ChangeState(State);
        bool IsRunning();
        void setRunning(bool);
        ~Game();
       

       

private:
       
        std::unique_ptr<GameState> CurrentState;
       
        bool isRunning;
};

#endif
Game.cpp :
#include"Game.h"


Game::Game()
{
        window.create(sf::VideoMode(1280, 720), "Pong");
        isRunning = true;
        //std::move(std::unique_ptr<MainMenuState>(new MainMenuState));
}


void Game::Run()
{
        while (window.isOpen())
        {
                CurrentState->HandleEvents(*this);
                window.clear(sf::Color(0, 134, 194));
                CurrentState->Update(*this);
                CurrentState->Draw(*this);
                window.display();
        }
}

void Game::ChangeState(State state)
{
        switch (state)
        {
        case State::MAINMENU: CurrentState = std::move(std::unique_ptr<MainMenuState>(new MainMenuState)); break;
        case State::PLAY: CurrentState = std::move(std::unique_ptr<PlayState>(new PlayState)); break;
        }
}

bool Game::IsRunning()
{
        return isRunning;
}

void Game::setRunning(bool running)
{
        isRunning = running;
}



Game::~Game()
{

}

MainMenuState.h :
#ifndef MAINMENUSTATE_HPP
#define MAINMENUSTATE_HPP

#include "GameState.h"
#include "SFML\Graphics.hpp"

class MainMenuState : public GameState{
public:

        sf::Text StartGame;
        sf::Text QuitGame;

        sf::Font font;

        MainMenuState();
        void HandleEvents(Game &game);
        void Update(Game &game);
        void Draw(Game &game);
        ~MainMenuState();

        bool bStartGameSelected = false;
        bool bQuitGameSelected = false;

};

#endif

 
MainMenuState.cpp :
#include "MainMenuState.h"
#include "Game.h"



MainMenuState::MainMenuState()
{
       
        if(!font.loadFromFile("assets\\fonts\\HAMMERHEAD.ttf"))
        {
                printf("Unable to load font");
        }
       
        StartGame.setFont(font);
        QuitGame.setFont(font);

        StartGame.setString("Start Game");
        QuitGame.setString("Quit Game");

        StartGame.setCharacterSize(50);
        QuitGame.setCharacterSize(50);

       
        StartGame.setPosition(440.f,300.f);
        QuitGame.setPosition(440.f,350.f);

}

void MainMenuState::HandleEvents(Game &game)
{
        sf::Event event;
        while (game.window.pollEvent(event))
        {
                if (event.type == sf::Event::Closed)
                {
                        game.window.close();
                        game.setRunning(false);
                }
                if (event.type == sf::Event::MouseButtonReleased)
                {
                        if (event.mouseButton.button == 0)
                        {
                                if (bStartGameSelected)
                                {
                                        game.ChangeState(Game::State::PLAY);
                                }

                                else if (bQuitGameSelected)
                                {
                                        game.window.close();
                                        game.setRunning(false);
                                }
                        }
                }

        }

}

void MainMenuState::Update(Game &game)
{

        sf::Vector2i pixelPos = sf::Mouse::getPosition(game.window);
        sf::Vector2f worldPos = game.window.mapPixelToCoords(pixelPos);

        if (StartGame.getGlobalBounds().contains(worldPos) && StartGame.getColor() != sf::Color::Green)
        {
                StartGame.setColor(sf::Color::Green);
                bStartGameSelected = true;
        }
        else if (!StartGame.getGlobalBounds().contains(worldPos) && StartGame.getColor() == sf::Color::Green)
        {
                StartGame.setColor(sf::Color::White);
                bStartGameSelected = false;
        }



        if (QuitGame.getGlobalBounds().contains(worldPos) && QuitGame.getColor() != sf::Color::Green)
        {
                QuitGame.setColor(sf::Color::Green);
                bQuitGameSelected = true;
        }
        else if (!QuitGame.getGlobalBounds().contains(worldPos) && QuitGame.getColor() == sf::Color::Green)
        {
                QuitGame.setColor(sf::Color::White);
                bQuitGameSelected = false;
        }

}


void MainMenuState::Draw(Game &game)
{
        game.window.draw(StartGame);
        game.window.draw(QuitGame);
}

MainMenuState::~MainMenuState()
{
        printf("MainMenu Destructor called\n");
}

PlayState.h :
#ifndef PLAYSTATE_HPP
#define PLAYSTATE_HPP

#include "GameState.h"

class PlayState : public GameState{
public:

        PlayState();
        void HandleEvents(Game &game);
        void Update(Game &game);
        void Draw(Game &game);
        ~PlayState();
};

#endif

PlayState.cpp :
#include "PlayState.h"
#include "Game.h"

PlayState::PlayState()
{
        printf("PlayState-Constructor called\n");
}

void PlayState::HandleEvents(Game &game)
{
        sf::Event event;
        while (game.window.pollEvent(event))
        {
                if (event.type == sf::Event::KeyPressed)
                {
                        if (event.type == sf::Keyboard::Escape)
                        {
                                game.ChangeState(Game::State::MAINMENU);
                        }
                }

        }
       
}

void PlayState::Update(Game &game)
{

}

void PlayState::Draw(Game &game)
{

}

PlayState::~PlayState()
{

}
 

Thats my code.....

Mr_Blame

  • Full Member
  • ***
  • Posts: 192
    • View Profile
    • Email
Which OS you have?
Are OpenGL drivers up-to-date?

Update:

source is alright, works right on my Windows 10 PC, looks definetly like some OS or Driver issue.
« Last Edit: May 20, 2016, 07:13:54 pm by Mr_Blame »

 

anything