c++ - 无法使用SDL在c++中进行while循环以从一个类移动到另一个类

标签 c++ sdl

我在我的代码中遇到的问题是当游戏开始时它工作正常但是当用户想要返回主菜单时应用程序停止

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;
    }


}
}

Aide.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) 并且我想返回主菜单 (menuPrincipale.cpp) 时,应用程序停止..

最佳答案

您不应该在 Aide 的析构函数中调用 SDL_Quit()TTF_Quit()TTF_CloseFont(font)。 SDL_Quit() 应该只调用一次,与 TTF 库相同。

关于c++ - 无法使用SDL在c++中进行while循环以从一个类移动到另一个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16263323/

相关文章:

c++ - 如何连接文件路径?

c++赋值运算符=用派生类重载

尝试重载运算符和使用类模板时出现 C++ 错误

c++ - 如何在 char 数组的特定索引处获取实际值

c - 变量的值被覆盖

python - 在python3中导入sdl2时出错

python - 找不到 Sdl2-config 错误(安装 pygame_sdl2)

c# - IDE 条件 block 突出显示

c++ - std::initializer_list 构造函数用于什么,除了用值填充一些容器?

c++ - C 中的 SDL 轮询事件没有响应