#include<SDL2/SDL.h>
#include<stdio.h>
#include<stdbool.h>
bool init(char *title, int width, int height);
void close();
SDL_Window *window = NULL;
SDL_Surface *screen = NULL;
bool init(char *title, int width, int height){
bool success = true;
// SDL_Init 0 on success and returns negative on failure
if( SDL_Init(SDL_INIT_EVERYTHING) != 0 ){
SDL_Log("Couldn't initialize SDL: %s",SDL_GetError());
success = false;
}
// creating Window and Surface
window = SDL_CreateWindow(title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_SHOWN );
// SDL_CreateWindow returns window on creation and NULL on failure
if ( !window ){
SDL_Log("Couldn't create window: %s",SDL_GetError());
success = false;
} else {
// get window surface
screen = SDL_GetWindowSurface( window );
if ( screen == NULL ){
SDL_Log("Couldn't get window surface: %s",SDL_GetError());
success = false;
}
}
return success;
}
void close(){
// deallocate surface
SDL_FreeSurface( screen );
// destroy window
SDL_DestroyWindow( window );
// SDL_Quit();
SDL_Quit();
}
int main(){
bool running = true;
SDL_Event event;
if( init("My Window", 800, 600) ){
printf("Congrats\n");
} else {
printf("Sorry :(\n");
}
while( running ) {
// start = SDL_GetTicks();
while ( SDL_PollEvent( &event ) ){
switch( event.type ){
case SDL_QUIT:
running = false;
break;
}
SDL_UpdateWindowSurface( window );
}
}
close();
}
我尝试创建一个单独的函数来初始化SDL Window,然后尝试将曲面附加到它,但它给了Segmentation fault (core dumped)
。有时它也会给出错误Info: invalid window
。
如果我不创建单独的函数并仅在main函数中运行此代码,它就可以工作!
答案 0 :(得分:0)
您忘记实现一个关键功能,即:
SDL_BlitSurface(SDL_Surface *,const SDL_Rect *,SDL_Surface *,SDL_Rect *);
此功能可以使表面变白并将数据(图形信息)加载到SDL_Surface Structure中。
如果您想了解有关此功能的更多详细信息,可以查看链接。