问题:
我的 while 循环没有退出。在调试过程中,它非常明确地表示stateID
等于 3。但是,当我添加std::cout << stateID;
该值时1
,总是将值写入控制台,无论调试器告诉我什么。
在代码中,我在输入循环之后渲染屏幕。这表明,毫无疑问,输入循环按预期正确退出。此外,如果这还不够,它必须首先退出才能更改 stateID。请不要再讨论嵌套循环是问题所在。而且我还在循环之后使用了正确命中的断点。
代码:
int main()
{
stateID = 1;
GameState* state = new GameStateTitle();
sf::RenderWindow window(sf::VideoMode(1000, 600), "RPG");
//GAME LOOP//
while (stateID != 3)
{//INPUT//
sf::Event event;
while (window.pollEvent(event))
{
switch (event.type)
{
//Window closed
case sf::Event::Closed:
set_next_state(3);
break;
}
}
change_state(state);
//RENDERING//
window.clear(sf::Color::Black);
state->render(window);
window.display();
}
/////////////
window.close();
return 0;
}
void set_next_state(int new_state)
{
//Set the next state to take place
next_state = new_state;
}
void change_state(GameState *current_state)
{
//Check if the next state is null or exit
if (next_state != 0)
{
if (next_state != 3)
delete current_state;
//Set the new state
switch(next_state)
...
}
stateID = next_state;
next_state = 0;
}
}
下面是我的完整代码,以防我错过了一些重要的东西。
主文件
#include "GameState.hpp"
int main()
{
stateID = STATE_TITLE;
GameState* state = new GameStateTitle();
sf::RenderWindow window(sf::VideoMode(1000, 600), "RPG");
//GAME LOOP//
while (stateID != STATE_EXIT)
{
//INPUT//
sf::Event event;
while (window.pollEvent(event))
{
switch (event.type)
{
//Window closed
case sf::Event::Closed:
set_next_state(STATE_EXIT);
break;
}
}
///////////
////LOGIC//
state->logic();
///////////
change_state(state);
//RENDERING//
window.clear(sf::Color::Black);
state->render(window);
std::cout << stateID;
window.display();
/////////////
}
///////////////
window.close();
return 0;
}
游戏状态.hpp
#pragma once
#include <SFML/Graphics.hpp>
//DEFINITIONS ETC//
enum GAME_STATES {
STATE_NULL,
STATE_TITLE,
STATE_BATTLE,
STATE_EXIT
};
///////////////////
//Game State Class//
class GameState
{
public:
virtual void input(void) = 0;
virtual void logic(void) = 0;
virtual void render(sf::RenderWindow &window) = 0;
};
////////////////////
//FUNCTIONS//
void set_next_state(int new_state);
void change_state(GameState *current_state);
/////////////
//VARIABLES//
static int stateID;
static int next_state;
/////////////
//Title GameState Class//
class GameStateTitle : public GameState
{
private:
sf::Texture img_title;
sf::Sprite bgr_title;
public:
GameStateTitle(void);
~GameStateTitle(void);
void input(void);
void logic(void);
void render(sf::RenderWindow &window);
};
/////////////////////////
//Battle GameState Class//
class GameStateBattle : public GameState
{
private:
sf::Image img_left;
sf::Image img_right;
public:
GameStateBattle(void);
~GameStateBattle(void);
void input(void);
void logic(void);
void render(sf::RenderWindow &window);
};
//////////////////////////
游戏状态.cpp
#include "GameState.hpp"
//Game state general functions
void set_next_state(int new_state)
{
//Set the next state to take place
next_state = new_state;
}
void change_state(GameState *current_state)
{
//Check if the next state is null or exit
if (next_state != STATE_NULL)
{
if (next_state != STATE_EXIT)
delete current_state;
//Set the new state
switch(next_state)
{
case STATE_TITLE:
current_state = new GameStateTitle;
break;
case STATE_BATTLE:
current_state = new GameStateBattle;
break;
}
stateID = next_state;
next_state = STATE_NULL;
}
}
//The functions of the title state
GameStateTitle::GameStateTitle(void)
{
//Load texture
img_title.loadFromFile("title_screen.png");
//Set texture to background
bgr_title.setTexture(img_title);
}
GameStateTitle::~GameStateTitle(void)
{
}
void GameStateTitle::input(void)
{
}
void GameStateTitle::logic(void)
{
}
void GameStateTitle::render(sf::RenderWindow &window)
{
window.draw(bgr_title);
}
//The functions of the battle state
GameStateBattle::GameStateBattle(void)
{
}
GameStateBattle::~GameStateBattle(void)
{
}
void GameStateBattle::input(void)
{
}
void GameStateBattle::logic(void)
{
}
void GameStateBattle::render(sf::RenderWindow &window)
{
}