c++ - 字体渲染函数 C++ SDL 中未处理的异常

标签 c++ class exception sdl

我正在制作 PONG 克隆版,而且只是在标题屏幕上。我有一个类来处理状态机的标题屏幕循环,它使用的很少,只有一个 sprite 和一个 true type 字体消息。但是当我调用函数将消息呈现到 SDL_Surface 上时,它使我的程序陷入困境。我收到的错误是 Pong.exe 中 0x6F4C2A9D (SDL_ttf.dll) 的未处理异常:0xC0000005:访问冲突读取位置 0x00000000。 通常这意味着我没有初始化某些东西或没有定义它在类定义之类的东西中,但这一切似乎都是有序的。所以我会把代码贴在这里,希望有人能看到渲染函数或它周围的位是怎么回事。

为了完全清楚,异常是在这一行抛出的:

Title_Message = TTF_RenderText_Solid(font, "PONG", color);

//start code
/*CLASSES*/

class GameState
{
public:
    virtual void events() = 0;
    virtual void logic() = 0;
    virtual void render() = 0;
    virtual ~GameState(){};
};

class Button
{
public:
    SDL_Rect button_clip[2];
    SDL_Rect button;
    SDL_Surface *button_sprite = NULL;
    Button();
};

class Title : public GameState
{
private:
    SDL_Surface *Title_Message = NULL;
    SDL_Rect *clip;
    Button Title_Button;
public:
    void events();
    void logic();
    void render();
    Title();
    ~Title();
};

/*FONTS*/
SDL_Color color = { 255, 255, 255 };
TTF_Font *font = NULL;

bool init()
{
    //initialize all SDL subsystems
    if (SDL_Init(SDL_INIT_EVERYTHING) == -1)
    {
        return false;
    }
    //set up screen
    screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);
    //check screen
    if (screen == NULL)
    {
        return false;
    }
    //init TTF
    if (TTF_Init() == -1)
    {
        return false;
    }
    //set window caption
    SDL_WM_SetCaption("PONG", NULL);
    //if evetything worked
    return true;
}

//load files
bool load_files()
{
    font = TTF_OpenFont("PIXELITE.ttf", 45);
    if (font == NULL)
    {
        return false;
    }
    return true;
}

/*CLASS DEFINITIONS*/

Button::Button()
{

}

Title::Title()
{
    Title_Message = TTF_RenderText_Solid(font, "PONG", color);
    Title_Button.button_sprite = load_image("Start.png");
    Title_Button.button.x = 200;
    Title_Button.button.y = 350;
    Title_Button.button.w = 100;
    Title_Button.button.h = 50;
    //clips not hover
    Title_Button.button_clip[0].x = 0;
    Title_Button.button_clip[0].y = 0;
    Title_Button.button_clip[0].w = 100;
    Title_Button.button_clip[0].h = 50;
    //clips hover
    Title_Button.button_clip[1].x = 0;
    Title_Button.button_clip[1].y = 50;
    Title_Button.button_clip[1].w = 100;
    Title_Button.button_clip[1].h = 50;
}

Title::~Title()
{
    SDL_FreeSurface(Title_Message);
    SDL_FreeSurface(Title_Button.button_sprite);
}

void Title::events()
{
    int x = 0;
    int y = 0;
    while (SDL_PollEvent(&event))
    {
        if (event.type == SDL_MOUSEMOTION)
        {
            x = event.motion.x;
            y = event.motion.y;
            if ((x > Title_Button.button.x) && (x < (Title_Button.button.x + Title_Button.button.w)) && (y > Title_Button.button.y) && (y < (Title_Button.button.y + Title_Button.button.h)))
            {
                clip = &Title_Button.button_clip[1];
            }
            else
            {
                clip = &Title_Button.button_clip[0];
            }
        }
        if (event.type == SDL_QUIT)
        {
            quit = true;
        }
    }
}

void Title::logic()
{

}

void Title::render()
{
    apply_surface(Title_Button.button.x, Title_Button.button.y, Title_Button.button_sprite, screen, clip);
    apply_surface((SCREEN_WIDTH - Title_Message->w) / 2, 100, Title_Message, screen);
}

有人知道吗?谢谢!

最佳答案

我将从评论中提出我的建议作为实际答案:

引起麻烦的线路Title_Message = TTF_RenderText_Solid(font, "PONG", color);指的是全局变量font类型 TTF_Font* .该行也是类 Title 的构造函数的一部分。 .

main看起来像这样:

int main(int argc, char* args[])
{
    //init SDL
    init();
    //load everything
    load_files();
    currentState = new Title;
    //...

font初始化为 NULL在声明时,实际对象仅在 load_files() 中分配在 main 的开头执行之前 Title是第一次实例化。

所以 load_files()必须分配一个指向 font 的有效指针, 否则下一行 main将导致访问冲突。

load_files()根据创建和分配此对象是否成功提供返回值。然而main从不检查此值,因此不能保证 font是一个有效的指针。

正如 knefcy 所指出的,问题是 load_files() 中的错误文件名。 .

关于c++ - 字体渲染函数 C++ SDL 中未处理的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20821587/

相关文章:

python - 如何将基本脚本中的代码转换为带有实例的类?

c++ - 在 C++ 中定义自己的异常类

c# - 如何处理 NullReference 异常 C#

c++ - 将元组转换为 "triangular"元组

c++ - 函数调用发生在哪里?

c++ - "cannot convert ' 这个从 'const hand' 到 'hand &' 的指针是什么意思? (C++)

python - doctest 中的链式异常

c++ - 返回对类的引用

c# - 实例化的类对于每个用户来说都是唯一的吗?

C++ 已弃用从字符串常量到 'char*' 的转换 []