所以我正在使用SDL2和C ++创建一个2D Pong游戏(我对此完全陌生!)一切顺利,直到我遇到这个错误:
Exception thrown at 0x71002A85 (SDL2_ttf.dll) in 2D Game.exe: 0xC0000005: Access violation reading location 0x00000000.
代码断开:
SDL_Surface * surf = TTF_RenderText_Blended(font,message.c_str(),color);
#include "utilities.h"
#include <SDL.h>
#include <SDL_ttf.h>
void renderTexture(SDL_Texture* tex,
SDL_Renderer* ren, SDL_Rect dst, SDL_Rect *clip) {
SDL_RenderCopy(ren, tex, clip, &dst);
}
void renderTexture(SDL_Texture* tex,
SDL_Renderer* ren, int x, int y, SDL_Rect* clip) {
SDL_Rect dst;
dst.x = x;
dst.y = y;
if (clip != nullptr) {
dst.w = clip->w;
dst.h = clip->h;
} else {
SDL_QueryTexture(tex, nullptr, nullptr, &dst.w, &dst.h);
}
renderTexture(tex, ren, dst, clip);
}
SDL_Texture* renderText(const std::string &message,
const std::string &fontFile, SDL_Color color,
int fontSize, SDL_Renderer* renderer) {
TTF_Font* font = TTF_OpenFont(fontFile.c_str(), fontSize);
SDL_Surface* surf = TTF_RenderText_Blended(font, message.c_str(), color);
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surf);
SDL_FreeSurface(surf);
TTF_CloseFont(font);
return texture;
}
答案 0 :(得分:0)
您应该检查之前返回的PCL
对象为null。如果font
无法打开给定的字体文件,则TTF_OpenFont
很可能返回null。
TTF_Font* font = TTF_OpenFont(fontFile.c_str(), fontSize);
if(!font) {
... //some action here, perhaps throw exception
}
SDL_Surface* surf = TTF_RenderText_Blended(font, message.c_str(), color);