Creé un juego de tetris donde puedes reiniciar después de un juego terminado. Implementé esto rápido y sucio con un goto (ver código). La clase Game
se basa en destructores, ¿estos se llaman con estos goto? ¿Qué tan malo es este goto, es aceptable, o qué debo hacer en su lugar?¿Qué tan malo es este goto?
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
// initiate sdl
sdl_init();
// seed rng
srand(time(NULL));
newgame: // new game label
Game game(GAME_WIDTH, GAME_HEIGHT, 1, screen);
// keydowns
bool fastfall = false;
bool gamerunning = true;
Uint32 lastupdate = 0;
while (gamerunning && game.isalive()) {
// game running stuff here
}
// game over stuff here
while (gamerunning) {
if (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
gamerunning = false;
} else if (event.type == SDL_KEYDOWN) {
if (event.key.keysym.sym == SDLK_r) goto newgame; // yay a new game!
}
}
}
TTF_Quit();
SDL_Quit();
return 0;
}
Ewww ... si sólo fuera por el Goto –
Por supuesto, se puede utilizar un Goto, si se puede vivir con las consecuencias : http://xkcd.com/292/ – bitmask