這個SDL程序工作正常(顯示一個窗口並在其上繪製位圖),但是,如果我在程序中聲明任何變量(如int,long等),則窗戶凍結,是白色而不是黑色,沒有畫。什麼可能造成這種情況?SDL在聲明任何標準變量時崩潰
#include <stdio.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
int main(int argc, char* argv[]) {
// line below will cause SDL crash
// int blabla = 640;
SDL_Window *window;
SDL_Init(SDL_INIT_VIDEO);
window = SDL_CreateWindow(
"A rather exceptional SDL window",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
640,
480,
SDL_WINDOW_OPENGL
);
if (window == NULL) {
printf("Could not create window: %s\n", SDL_GetError());
return 1;
}
SDL_Renderer *renderer;
SDL_Texture *texture;
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
SDL_Surface *brush;
brush = IMG_Load("brush2.png");
texture = SDL_CreateTextureFromSurface(renderer, brush);
SDL_RenderClear(renderer);
SDL_Rect *sRect, *dRect;
sRect->w = 10;
sRect->h = 4;
sRect->x = 0;
sRect->y = 0;
dRect->w = 10;
dRect->h = 4;
dRect->x = 10;
dRect->y = 10;
SDL_RenderCopy(renderer, texture, sRect, dRect);
SDL_RenderPresent(renderer);
SDL_Delay(1000);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
你在哪裏聲明瞭這個變量? – rullof
@rullof查看註釋掉的行「int blabla = 640」 - 是的,_indeed_,如果聲明瞭無關的變量(或任何不相關的變量),SDL將凍結而不繪製。它會很好地編譯,如果沒有SDL代碼,就沒有問題,但由於某些原因,如果我嘗試定義任何不相關的標準變量(例如註釋掉的行),SDL就會凍結。 – user353885
換句話說,由於「int blabla = 640」被註釋掉了,你上面看到的代碼編譯得很好。如果你取消註釋「int blabla = 640」或寫入「int bob = 44」或「long joe = 34」,它當然會編譯好,但是_SDL將凍結並且不會繪製。我希望這是有道理的。 – user353885