#include #include #include #include SDL_Surface *load_image(char *path, SDL_Surface *screen) { SDL_Surface *surface2 = NULL; SDL_Surface *surface = NULL; surface = IMG_Load(path); if (!surface) { fprintf(stderr, "IMG_Load failed: %s\n", IMG_GetError()); goto error; } surface2 = SDL_ConvertSurface(surface, screen->format, 0); if (!surface2) { fprintf(stderr, "SDL_ConvertSurface failed: %s\n", SDL_GetError()); goto error; } error: SDL_FreeSurface(surface); return surface2; } #define NELEMS(x) (sizeof(x) / sizeof((x)[0])) int main(void) { SDL_Window *window = NULL; SDL_Surface *screen = NULL; SDL_Surface *logo = NULL; int exit_status = EXIT_FAILURE; int img_flags = IMG_INIT_PNG; uint8_t colours[][3] = { { 0xFF, 0, 0 }, { 0, 0xFF, 0 }, { 0, 0, 0xFF }, { 0, 0, 0 } }; int i; int n; if (SDL_Init(SDL_INIT_VIDEO) < 0) { fprintf(stderr, "SDL_Init failed: %s\n", SDL_GetError()); goto error; } if ((IMG_Init(img_flags) & img_flags) != img_flags) { fprintf(stderr, "IMG_Init failed: %s\n", IMG_GetError()); goto error; } window = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 0, 0, SDL_WINDOW_FULLSCREEN_DESKTOP); if (!window) { fprintf(stderr, "SDL_CreateWindow failed: %s\n", SDL_GetError()); goto error; } screen = SDL_GetWindowSurface(window); if (!screen) { fprintf(stderr, "SDL_GetWindowSurface failed: %s\n", SDL_GetError()); goto error; } n = (int)NELEMS(colours); for (i=0; iformat, c[0], c[1], c[2])) < 0) { fprintf(stderr, "SDL_FillRect failed: %s\n", SDL_GetError()); goto error; } if (SDL_UpdateWindowSurface(window) < 0) { fprintf(stderr, "SDL_UpdateWindowSurface failed: %s\n", SDL_GetError()); goto error; } SDL_Delay(100); } logo = load_image("sdl/logo.png", screen); if (!logo) { goto error; } if (SDL_BlitSurface(logo, NULL, screen, NULL) < 0) { fprintf(stderr, "SDL_BlitSurface failed: %s\n", SDL_GetError()); goto error; } if (SDL_UpdateWindowSurface(window) < 0) { fprintf(stderr, "SDL_UpdateWindowSurface failed: %s\n", SDL_GetError()); goto error; } SDL_Delay(500); exit_status = EXIT_SUCCESS; error: SDL_FreeSurface(logo); SDL_FreeSurface(screen); SDL_DestroyWindow(window); SDL_Quit(); exit(exit_status); }