我正在尝试在函数中初始化 SDL_Window 但它总是失败,除非我定义 SDL_Window*win; 作为全局变量。这是代码:
#include<SDL2/SDL.h>
#include<stdio.h>
#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600
SDL_Surface*Init(SDL_Window*win,char*title,int x,int y,int w,int h,Uint32 flags);
int main(int argc,char**argv){
SDL_Surface *screen=NULL;
SDL_Window*win=NULL;
screen=Init(win,"SDL_Test",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
SCREEN_WIDTH,
SCREEN_HEIGHT,
SDL_WINDOW_SHOWN);
SDL_FillRect(screen,NULL,SDL_MapRGB(screen->format,0xFF,0x00,0x00));
SDL_UpdateWindowSurface(win);
SDL_Delay(2000);
}
SDL_Surface*Init(SDL_Window*win,
char*title,
int x,
int y,
int w,
int h,
Uint32 flags){
SDL_Surface*temp=NULL;
if(SDL_Init(SDL_INIT_EVERYTHING)){
printf("\nError : %s",SDL_GetError());
SDL_ClearError();
return NULL;
}
win=SDL_CreateWindow(title,x,6,w,h,flags);
if(!win){
printf("\nError: %s",SDL_GetError());
SDL_ClearError();
return NULL;
}
temp=SDL_GetWindowSurface(win);
if(!temp){
printf("\nError: %s",SDL_GetError());
SDL_ClearError();
return NULL;
}
return temp;
}
如果我将 SDL_Window*win 声明为全局变量,它会显示一个红屏(应该如此),但如果我在 main() 中定义 SDL_Window*win 并将其传递给 Init(),它会显示一个白屏(错误?)。