c++ - Game Launches to a blank white screen instead of loadingscreen -
trying create game i've run first hurdle. game launches blank white screen instead of showing red screen , cant proceed loading screen , menu. i've checked i'm using display each of screens can't figure out i'm going wrong. no errors. below code
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 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; ( = _menuitems.begin(); != _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; } } } }
void game::showlogoscreen() { game game; // <- creates new game. want use created game. loadscreen loadscreen; loadscreen.show(game._mainwindow); _gamestate = game::menuwindow; } you create new game local logo screen. 1 not own window. should reference existing game variable.
Comments
Post a Comment