0
我正在使用SDL創建測驗遊戲程序。代碼編譯得很好,但是當我運行輸出可執行文件時,出現了分段錯誤。我正試圖將一個按鈕閃爍到屏幕上。這裏是我的代碼:C++ Debian SDL分段錯誤
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include <stdio.h>
#undef main
void ablit(SDL_Surface* source, SDL_Surface* destination, int x, int y){
SDL_Rect offset;
offset.x = x;
offset.y = y;
SDL_BlitSurface(source, NULL, destination, &offset);
}
void acreatebutton(int x, int y, int w, int h, SDL_Color fill, SDL_Surface*
screenSurface, const char* buttontext, int fontsize, SDL_Color textfill){
SDL_Rect* buttonrect;
buttonrect->x = x;
buttonrect->y = y;
buttonrect->w = w;
buttonrect->h = h;
int fillint = SDL_MapRGB(screenSurface -> format, fill.r, fill.g,
fill.b);
SDL_FillRect(screenSurface, buttonrect, fillint);
TTF_Font* font = TTF_OpenFont("/usr/share/fonts/truetype/droid/DroidSansMono.ttf", fontsize);
SDL_Surface* buttontextsurface = TTF_RenderText_Solid(font, buttontext, textfill);
ablit(buttontextsurface, screenSurface, 300, 300);
TTF_CloseFont(font);
}
int main(int argc, char** argv){
SDL_Init(SDL_INIT_EVERYTHING);
TTF_Init();
SDL_Window* screen = SDL_CreateWindow("Quiz Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 500, 400,SDL_WINDOW_RESIZABLE);
SDL_Surface* screenSurface = SDL_GetWindowSurface(screen);
SDL_FillRect (screenSurface, NULL, SDL_MapRGB(screenSurface->format, 0, 0, 255));
SDL_Color black = {0, 0, 0};
TTF_Font* afont = TTF_OpenFont("/usr/share/fonts/truetype/droid/DroidSansMono.ttf", 35);
SDL_Surface* aQuiz_Game = TTF_RenderText_Solid(afont, "Quiz Game", black);
ablit(aQuiz_Game, screenSurface, 150, 50);
acreatebutton(175, 350, 200, 50, black, screenSurface, "Take Quiz", 35, black);
SDL_UpdateWindowSurface(screen);
SDL_Event windowEvent;
while (true){
if (SDL_PollEvent(&windowEvent))
{
if (windowEvent.type == SDL_KEYUP &&
windowEvent.key.keysym.sym == SDLK_ESCAPE) break;
}
SDL_GL_SwapWindow(screen);
}
TTF_CloseFont(afont);
SDL_Quit();
TTF_Quit();
return 0;
}
th ablit功能用於blitting,而abutton功能用於創建按鈕圖像。
嘗試使用調試器來了解SegFault何時發生,以及如果仍然無法解決問題,請在此處輸出調試器的輸出。另一件事是,你永遠不會檢查函數的返回值,所以如果某些表面或字體爲空,則不能被警告。 – Lovy