C++ 游戏启动到一个空白的白色屏幕,而不是加载屏幕
试图创造一个游戏,但我遇到了我的第一个障碍。游戏启动到一个空白的白色屏幕,而不是显示红色屏幕,我无法进入加载屏幕和菜单。我已经检查了我的每个屏幕是否都使用了显示器,但我无法找出哪里出了问题。我也没有错误。下面是我的代码 game.cppC++ 游戏启动到一个空白的白色屏幕,而不是加载屏幕,c++,sfml,C++,Sfml,试图创造一个游戏,但我遇到了我的第一个障碍。游戏启动到一个空白的白色屏幕,而不是显示红色屏幕,我无法进入加载屏幕和菜单。我已经检查了我的每个屏幕是否都使用了显示器,但我无法找出哪里出了问题。我也没有错误。下面是我的代码 game.cpp #include "stdafx.h" #include "Game.h" #include "LoadScreen.h" #include "MainMenu.h" void Game::Start (void) { Game game; if(_
#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;
}
}
}
}
#包括“stdafx.h”
#包括“MainMenu.h”
主菜单::菜单选项主菜单::显示(sf::渲染窗口和窗口)
{
//从文件加载菜单图像
sf::纹理图像;
loadFromFile(“images/MainMenu.png”);
雪碧雪碧(图像);
//设置可点击区域
//播放菜单项坐标
菜单项播放按钮;
playButton.rect.top=319;
playButton.rect.height=626;
playButton.rect.left=189;
playButton.rect.width=329;
playButton.action=播放;
//选项菜单项坐标
菜单项选项按钮;
optionButton.rect.left=356;
optionButton.rect.height=596;
optionButton.rect.top=287;
optionButton.rect.width=483;
optionButton.action=选项;
//退出菜单项坐标
菜单项退出按钮;
exitButton.rect.left=554;
exitButton.rect.height=580;
exitButton.rect.top=318;
exitButton.rect.width=687;
exitButton.action=退出;
_菜单项。向后推(播放按钮);
_菜单项。推回(退出按钮);
窗口。绘制(精灵);
window.display();
返回GetMenuAction(窗口);
}
MainMenu::MenuOption MainMenu::HandleClick(int x,int y)
{
std::list::迭代器;
for(it=\u menuItems.begin();it!=\u menuItems.end();it++)
{
sf::Rect menuItemRect=(*it).Rect;
如果(菜单项直接高度>y
&&menuItemRect.topx)
{
返回(*it)。操作;
}
}
返回null;
}
主菜单::菜单选项主菜单::GetMenuAction(sf::RenderWindow和window)
{
sf::事件菜单事件;
while(true)
{
while(window.pollEvent(menueEvent))
{
if(menuEvent.type==sf::Event::MouseButtonPressed)
{
返回HandleClick(menuEvent.mouseButton.x,menuEvent.mouseButton.y);
}
if(menuEvent.type==sf::Event::Closed)
{
返回出口;
}
}
}
}
无效游戏::ShowLogoScreen()
{
游戏游戏;//void游戏::ShowLogoScreen()
{
游戏游戏;//void游戏::ShowLogoScreen()
{
游戏游戏;//void游戏::ShowLogoScreen()
{
Game Game;//确切地说,代码应该读这个->\u mainWindow
而不是谢谢大家。确切地说,代码应该读这个->\u mainWindow
而不是谢谢大家。确切地说,代码应该读这个->\u mainWindow
而不是谢谢大家。确切地说,代码应该是干练地阅读此->\u主窗口
而不是。非常感谢各位。
void Game::ShowLogoScreen()
{
Game game; // <- this creates a NEW game. You want to use your already created game.
LoadScreen loadScreen;
loadScreen.Show(game._mainWindow);
_gameState = Game::MenuWindow;
}