我最近尝试使用sdl2_ttf打印一些文本,但是我在gdb中不断收到此错误消息:
Type "apropos word" to search for commands related to "word".
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/usr/lib/libthread_db.so.1".
[New Thread 0x7ffff7059700 (LWP 2771)]
[New Thread 0x7ffff45cf700 (LWP 2773)]
[New Thread 0x7ffff2ee5700 (LWP 2775)]
Thread 1 "U" received signal SIGSEGV, Segmentation fault.
TTF_SizeUTF8_Internal (font=font@entry=0x0, text=text@entry=0x7fffffffe600 "Hey", w=w@entry=0x7fffffffe590, h=h@entry=0x7fffffffe594, xstart=xstart@entry=0x7fffffffe588, ystart=ystart@entry=0x7fffffffe58c) at SDL_ttf.c:1185
1185 maxy = font->height;
然后仅出现一个黑色窗口。我还检查了我的字体路径是否正确或字体是否损坏,但是它都没有帮助。当我删除行SDL_DrawText(1,"../fonts/Terminus.ttf","Hey",20,Black,20,20,20,20);
时,它可以很好地工作(当然不包括绘制文本)。
另外,我的IDE在调试时总是说分段错误出现在
surface[number] = TTF_RenderText_Solid(font[number],Text,color);
我该如何解决?
代码:
#include <SDL2/SDL.h>
#include "SDL2/SDL_image.h"
#include "SDL2/SDL_ttf.h"
#include <stdio.h>
SDL_Window *window = NULL;
SDL_Renderer *renderer = NULL;
SDL_Event event;
int Window = 1;
int FriskX = 150;
int FriskY = 95;
int FriskW;
int FriskH;
SDL_Texture *texture[10000];
float C = 1;
int windowWidth = 0;
int windowHeight = 0;
SDL_Rect dest[1000];
SDL_Rect rect[1000];
SDL_Rect camera;
int Friskwidth = 80;
int Friskheight = 80;
int Fullscreen = 0;
int Room1X = 0;
int Room1Y = 0;
int ShowHitboxes = 0;
int WindowTop;
int WindowBottom;
int WindowLeft;
int WindowRight;
int MausX;
int MausY;
int rectnumber;
int Mousebuttonpressed = 1;
int MausHoldX;
int MausHoldY;
int MausPressedX;
int MausPressedY;
int Z = 0;
SDL_Rect TextRect[1000];
TTF_Font* font[1000];
SDL_Surface* surface[1000];
SDL_Texture* text[1000];
SDL_Color iWhite = {231,232,235,255};
SDL_Color Black = {0,0,0,0};
SDL_Color White = {255,255,255,255};
void SDL_DrawTexture(char *pfad, int X, int Y,int W, int H,int texturzahl)
{
texture[texturzahl] = IMG_LoadTexture(renderer, pfad);
dest[texturzahl].x = X;
dest[texturzahl].y = Y;
dest[texturzahl].w = W;
dest[texturzahl].h = H;
SDL_RenderCopy(renderer, texture[texturzahl], NULL, &dest[texturzahl]);
SDL_DestroyTexture(texture[texturzahl]);
}
void SDL_Fastrect(int rectn,Uint8 r, Uint8 q, Uint8 b, Uint8 a,int x, int y, int w, int h, int fill)
{
SDL_SetRenderDrawColor(renderer,r,q,b,a);
rect[rectn].x = x;
rect[rectn].y = y;
rect[rectn].w = w;
rect[rectn].h = h;
if(fill == 0)
{
SDL_RenderFillRect(renderer,&rect[rectn]);
}
else if (fill == 1)
{
SDL_RenderDrawRect(renderer,&rect[rectn]);
}
}
void SDL_DrawText(int number, char *ttfPath, char *Text,int TextSize ,SDL_Color color, int X, int Y, int W, int H)
{
font[number] = TTF_OpenFont(ttfPath, TextSize);
surface[number] = TTF_RenderText_Solid(font[number],Text,color); //segfault??
text[number] = SDL_CreateTextureFromSurface(renderer,surface[number]);
TextRect[number].x = X;
TextRect[number].y = Y;
TextRect[number].w = W;
TextRect[number].h = H;
SDL_RenderCopy(renderer,text[number],NULL,&TextRect[number]);
SDL_FreeSurface(surface[number]);
SDL_DestroyTexture(text[number]);
TTF_CloseFont(font[number]);
}
void u()
{
SDL_GetWindowSize(window,&windowWidth,&windowHeight);
SDL_GetMouseState(&MausX,&MausY);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
SDL_RenderClear(renderer);
SDL_Fastrect(1,255,255,255,255,0,0,windowWidth,windowHeight,0);
SDL_Fastrect(2,255,0,0,0,MausX,MausY,20,20,1);
SDL_Fastrect(3,242,242,242,255,0,0,windowWidth,23,0);
SDL_DrawText(1,"../fonts/Terminus.ttf","Hey",20,Black,20,20,20,20);
SDL_RenderPresent(renderer);
}
int main()
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_CreateWindowAndRenderer(640, 480, SDL_WINDOW_RESIZABLE, &window, &renderer);
TTF_Init();
printf("%d", windowHeight);
SDL_SetWindowTitle(window, "U");
SDL_RenderSetScale(renderer, 1, 1);
SDL_RenderSetScale(renderer, 1, 1);
while (Window == 1)
{
u();
while (SDL_PollEvent(&event) != 0)
{
if (event.type == SDL_QUIT)
{
Window = 0;
}
else if (event.type == SDL_KEYDOWN)
{
switch (event.key.keysym.sym)
{
case SDLK_RIGHT:
FriskX += 10;
C = 1;
break;
case SDLK_LEFT:
FriskX -= 10;
break;
case SDLK_UP:
FriskY -= 10;
break;
case SDLK_DOWN:
FriskY += 10;
break;
case SDLK_3:
C += 0.01;
break;
case SDLK_F2:
C -= 0.01;
break;
case SDLK_F3:
SDL_SetWindowFullscreen(window, SDL_WINDOW_MAXIMIZED);
break;
case SDLK_F4:
SDL_SetWindowFullscreen(window, SDL_WINDOW_MINIMIZED);
break;
case SDLK_F5:
ShowHitboxes += 1;
break;
default:
break;
}
}
}
}
}