将 SDL_ttf 与 SDL 和 SDL2 一起使用时出现 C++ 访问冲突

C++ Access violation when using SDL_ttf with SDL and SDL2

我正在使用 SDL2_ttf 和 SDL2(在 Visual Studio 2015 年)。当我尝试 运行 以下代码时,

#include "SDL.h"
#include "SDL_ttf.h"

int main(int argc, char* args[]) {
    SDL_Init(SDL_INIT_EVERYTHING);
    TTF_Init();

    SDL_Window* window;
    SDL_Renderer* renderer;
    SDL_CreateWindowAndRenderer(1600, 900, SDL_WINDOW_OPENGL, &window, &renderer);

    TTF_Font* font = TTF_OpenFont("comic.ttf", 12);
    SDL_Color color = { 0, 0, 0, 255 };
    SDL_Surface* textSurface = TTF_RenderText_Solid(font, "asdf", color);
    SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, textSurface);

    TTF_Quit();
    SDL_Quit();
    return 0;
}

我遇到了 "SDL.dll missing" 运行 时间错误。我将 SDL.dll、SDL2.dll、libfreetype-6.dll、SDL_ttf.dll、zlib1.dll 和其他库放在我的 system32 文件夹中,这解决了 运行时间错误,但我瞬间运行进入另一个错误:"Unhandled exception at 0x000000006C812E39 (SDL2.dll) in MCP2016.exe: 0xC0000005: Access violation reading location 0x000000010000006A."

当我决定在 Visual studio 对话框中 "Break" 告诉我这个时,它指向行

SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, textSurface);

我遇到了一个 old forum post,它表明 SDL_ttf 和 SDL2 可能会导致这样的访问冲突,因为它们不完全兼容。我认为这与我遇到的问题有关,因为它首先抱怨 SDL.dll 。有人建议用 SDL2 头文件重新编译 DLL,但恐怕这超出了我的能力范围,尤其是因为 windows 和 C++ 相处得不是很好。

我已经为此工作了大约八个小时,但我 运行 想不出办法解决这个问题。有人对此有任何想法吗?

SDL 和 SDL2 不兼容。 你要么有一个,要么有另一个。

如评论中所述,解决方法是改用 SDL2_ttf。