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

Author Topic: White Screen when game is Launched.  (Read 4404 times)

0 Members and 1 Guest are viewing this topic.

rowntrees

  • Newbie
  • *
  • Posts: 3
    • View Profile
White Screen when game is Launched.
« on: March 24, 2014, 09:05:56 pm »
I've been following the pang tutorial from http://www.gamefromscratch.com/page/Game-From-Scratch-CPP-Edition.aspx.My code has no error apart from a few warnings which i've read that I should ignore. However when i launch the program  I just see a blank white screen, instead of my logo, and menu screens. My code is as below.




game.cpp
#include "stdafx.h"
#include "Game.h"
#include "LoadScreen.h"
#include "MainMenu.h"


void Game::Start (void)
{
        Game game;
if(_gameState != Unlaunched)
        return;
game._mainWindow.create(sf::VideoMode(1024,768,32),"Code Matrix");
_gameState = Game::LogoScreen;

while (!Exists())
{
        GameLoop();
}
game._mainWindow.close();
}

bool Game::Exists()
{
        if(_gameState == Game::Exiting)
                return true;
        else
                return false;
}

void Game::GameLoop()
{
        Game game;
        sf::Event currentEvent;
        while (game._mainWindow.pollEvent(currentEvent))
        {
                switch (_gameState)
                {
                case Game::MenuWindow:
                        {
                                ShowMenu();
                                break;
                        }
                case Game::LogoScreen:
                        {
                                ShowLogoScreen();
                                break;
                        }
               
                case Game::Playing:
                        {
                                sf::Event currentEvent;
                                while (game._mainWindow.pollEvent(currentEvent))
                                {
                                game._mainWindow.clear(sf::Color(255,0,0));
                                game._mainWindow.display();

                                if(currentEvent.type == sf::Event::Closed)
                                        _gameState = Game::Exiting;
                                if(currentEvent.type == sf::Event::KeyPressed)
                                {
                                        if(currentEvent.key.code == sf::Keyboard::Escape) ShowMenu();
                                }
                                }
                                break;
                        }
                }
        }
}

void Game::ShowLogoScreen()
{
        Game game;
        LoadScreen loadScreen;
        loadScreen.Show(game._mainWindow);
        _gameState = Game::MenuWindow;
}

void Game::ShowMenu()
{
Game game;
MainMenu mainMenu;
MainMenu::MenuOption option = mainMenu.show(game._mainWindow);
switch(option)
{
case MainMenu::Exit:
        _gameState = Game::Exiting;
        break;
case MainMenu::Play:
        _gameState = Game::Playing;
        break;
}
}
Game::GameState Game::_gameState = Unlaunched;

LoadScreen.cpp
#include "stdafx.h"
#include "LoadScreen.h"

void LoadScreen::Show(sf::RenderWindow & renderWindow)
{
        sf::Texture image;
        if (image.loadFromFile("images/LoadingScreen.png") !=true)
        {
                return;
        }

        sf::Sprite sprite (image);

        renderWindow.draw(sprite);
        renderWindow.display();

        sf::Event event;
        while (true)
        {
                while (renderWindow.pollEvent(event))
                {
                        if(event.type == sf::Event::EventType::KeyPressed
                                ||event.type == sf::Event::EventType::MouseButtonPressed
                                || event.type == sf::Event::EventType::Closed)
                        {
                                return;
                }
        }
}
}
 

MainMenu.cpp

 #include "stdafx.h"
  #include "MainMenu.h"
 
 
  MainMenu::MenuOption MainMenu::show(sf::RenderWindow& window)
  {
 
    //Load menu image from file
    sf::Texture image;
   image.loadFromFile("images/MainMenu.png");
   sf::Sprite sprite(image);
 
   //Setup clickable regions
   //Play menu item coordinates
   MenuItem playButton;
   playButton.rect.top= 319;
   playButton.rect.height = 626;
   playButton.rect.left = 189;
   playButton.rect.width = 329;
   playButton.action = Play;
 
   //Options menu item coordinates
   MenuItem optionButton;
   optionButton.rect.left = 356;
   optionButton.rect.height = 596;
   optionButton.rect.top = 287;
   optionButton.rect.width = 483;
   optionButton.action = Options;
   
   //Exit menu item coordinates
   MenuItem exitButton;
   exitButton.rect.left = 554;
   exitButton.rect.height = 580;
   exitButton.rect.top = 318;
   exitButton.rect.width = 687;
   exitButton.action = Exit;


 
   _menuItems.push_back(playButton);
   _menuItems.push_back(exitButton);
 
   window.draw(sprite);
   window.display();
 
   return GetMenuAction(window);
 }
 
 MainMenu::MenuOption MainMenu::HandleClick(int x, int y)
 {
   std::list<MenuItem>::iterator it;
 
   for ( it = _menuItems.begin(); it != _menuItems.end(); it++)
   {
     sf::Rect<int> menuItemRect = (*it).rect;
         if( menuItemRect.height > y
       && menuItemRect.top < y
           && menuItemRect.left < x
           && menuItemRect.width > x)
       {
         return (*it).action;
       }
   }
 
   return null;
 }
 MainMenu::MenuOption  MainMenu::GetMenuAction(sf::RenderWindow& window)
 {
   sf::Event menuEvent;
 
   while(true)
   {
 
     while(window.pollEvent(menuEvent))
     {
                 if(menuEvent.type == sf::Event::MouseButtonPressed)
       {
                   return HandleClick(menuEvent.mouseButton.x,menuEvent.mouseButton.y);
       }
       if(menuEvent.type == sf::Event::Closed)
       {
         return Exit;
       }
     }
   }
 }


I've checked to see if I am missing a .display() somewhere but I can't see. Please help thanks.

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: White Screen when game is Launched.
« Reply #1 on: March 24, 2014, 10:15:38 pm »
Have you read the official tutorials? If not then that might be a good idea.

I've only skimmed briefly through your code, but a few things caught my eye. Rough list of comments below.

The clear()/draw()/display() cycle is not optional. You must do those 3 for every frame.
I notice that in "case Game::Playing" you have a loop that does clear() followed by display() - that is suspect unless all you want is a red screen.

In LoadScreen::Show there's a draw() followed by display() but where's the clear()? Why is this stuff not centralized in a game loop that does (for each frame) clear() followed by draw_everything() followed by display()? Why is it spread across various functions like this?

bool Game::Exists() : just do
  return _gameState == Game::Exiting;

Why are you loading images from file every time you enter your functions? Why not load resources once and pass references to them to where they are needed? Look into Thor's resource management classes if you don't feel like building your own.

You could restructure the code a bit and get rid of that infinite "while (true)" loop.

Formatting seems somewhat inconsistent which doesn't make the code easier to follow. Perhaps look into clang-format.

"return null" ?? OK, sure, this could be defined somewhere and be fine, but it's definitely a surprising/confusing name. If you want a null pointer constant then use "nullptr" (or NULL or 0 if you are stuck with C++98/03 rather than C++11). If what you want is not a null pointer constant, then name it something other than "null".

Beware of symbols starting with underscore "_". Many such names are reserved for the implementation - see the C and C++ specifications for details.

"it++" - you should generally prefer the prefix version of the increment/decrement operators when you have a choice and only use the postfix versions when you really need to (so "++it").

I could comment on more, but as I said, I only read the code briefly and I'm out of time for now.
Hope the above is of use.


rowntrees

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: White Screen when game is Launched.
« Reply #2 on: March 24, 2014, 10:37:01 pm »
Hi Jesper thanks for you time. Regarding the issues you mentioned, i'm following the tutorial and trying to do everything like the original author did. The tutorial is done in parts that why the code doesn't seem structured as it should as the classes are supposed to be edited as the tutorial  goes on. I should be seeing a red screen but I keep getting a white screen and therefore cant proceed to test any of my other screens. At this moment I just want to be able to see the red screen.

eigenbom

  • Full Member
  • ***
  • Posts: 228
    • View Profile
Re: White Screen when game is Launched.
« Reply #3 on: March 25, 2014, 12:15:20 am »
This is very confusing and most likely wrong:

Code: [Select]
void Game::GameLoop()
{
    Game game;
    sf::Event currentEvent;
    while (game._mainWindow.pollEvent(currentEvent))
    {
       switch (_gameState){
       ....
    }
}

Reading that, I'm not sure of the quality of the tutorial. Looking at the other code I think you just want to do something like this:

Code: [Select]
void Game::GameLoop(){
  switch(_gameState){
    ...
  }
}

But hold it right there! Maybe you should start over and instead use the official SFML tutorials?

rowntrees

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: White Screen when game is Launched.
« Reply #4 on: March 25, 2014, 09:36:28 am »
I did the small change you suggested which prompted an error. The error was that the files for my menu and loadinscreen couldn't be opened so i rectified this by placing them in the correct directory. Upon re-launching however I still get  the white screen. I've since been reading the official tutorials and trying to compare to see where I might be going wrong.

fallahn

  • Hero Member
  • *****
  • Posts: 507
  • Buns.
    • View Profile
    • Trederia
Re: White Screen when game is Launched.
« Reply #5 on: March 25, 2014, 10:57:02 am »
I had a problem where sfml would launch to a white screen when using an AMD card with the 13.12 drivers. Reverting to 13.9 or updating to the beta drivers fixed this for me.