我在我的代码中面临的问题是当游戏开始时它工作正常但是当用户想要返回到主菜单时应用程序停止
main.cpp----->
int main ( int argc, char** argv )
{
int x = 5;
while (1){
menuPrincipale p;
x = p.start();
if (x==2){
Aide a;
a.start();
}
}
}
menuPrincipale.cpp---->
#include "snake.h"
#include <stdlib.h>
#include <stdio.h>
#include <SDL/SDL.h>
menuPrincipale::menuPrincipale()
{
SDL_Rect positionFond;
positionFond.x = 0;
positionFond.y = 0;
SDL_Init(SDL_INIT_VIDEO);
screen = SDL_SetVideoMode(800, 400, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
SDL_WM_SetCaption("Menu Principale", NULL);
/* Chargement d'une image Bitmap dans une surface */
menu = SDL_LoadBMP("snake.bmp");
/* On blitte par-dessus l'écran */
SDL_BlitSurface(menu, NULL, screen, &positionFond);
SDL_Flip(screen);
}
menuPrincipale::~menuPrincipale()
{
TTF_CloseFont(font);
TTF_Quit();
SDL_Quit();
}
int menuPrincipale::start()
{
SDL_Event event;
while (1)
{
SDL_WaitEvent(&event);
switch(event.type)
{
case SDL_QUIT:
return 0;
case SDL_KEYDOWN:
switch(event.key.keysym.sym)
{
case SDLK_ESCAPE: // Veut arrêter le jeu
return 0;
break;
case SDLK_1: // Demande à jouer
SDL_Flip(screen);
SDL_FreeSurface(menu); /* On libère la surface */
SDL_Quit();
return 1;
break;
case SDLK_2: // afficher l'aide
SDL_Flip(screen);
SDL_FreeSurface(menu); /* On libère la surface */
SDL_Quit();
return 2;
break;
case SDLK_3: // quitter le jeux
return 0;
break;
}
break;
}
}
}
助手.cpp------>
#include "snake.h"
#include <stdlib.h>
#include <stdio.h>
#include <SDL/SDL.h>
Aide::Aide()
{
SDL_Rect positionFond;
positionFond.x = 0;
positionFond.y = 0;
SDL_Init(SDL_INIT_VIDEO);
screen = SDL_SetVideoMode(800, 400, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
SDL_WM_SetCaption("Help", NULL);
/* Chargement d'une image Bitmap dans une surface */
menu = SDL_LoadBMP("aide.bmp");
/* On blitte par-dessus l'écran */
SDL_BlitSurface(menu, NULL, screen, &positionFond);
SDL_Flip(screen);
}
Aide::~Aide()
{
TTF_CloseFont(font);
TTF_Quit();
SDL_Quit();
}
void Aide::start()
{
SDL_Event event;
while (1)
{
SDL_WaitEvent(&event);
switch(event.type)
{
case SDL_QUIT:
break;
case SDL_KEYDOWN:
switch(event.key.keysym.sym)
{
case SDLK_r: // Demande à jouer
SDL_Flip(screen);
SDL_FreeSurface(menu); /* On libère la surface */
SDL_Quit();
break;
}
}
}
}
当我显示帮助页面 (Aide.cpp) 并且我想返回到 Principal 菜单 (menuPrincipale.cpp) 时,应用程序停止..