main.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #include <SDL.h>
  2. #include "savepng.h" // includes <png.h>, you must link with -lpng
  3. int main( int argc, char* args[] )
  4. {
  5. SDL_Surface *screen;
  6. SDL_Surface *shot;
  7. Uint32 color;
  8. if (SDL_Init(SDL_INIT_VIDEO) < 0 || (screen = SDL_SetVideoMode(640, 480, 0, 0)) == 0)
  9. {
  10. fprintf(stderr, "Video initialization failed: %s\n", SDL_GetError());
  11. exit(-1);
  12. }
  13. /* Fill screen with random color */
  14. srand(time(NULL));
  15. color = SDL_MapRGB(screen->format,
  16. rand() % 255,
  17. rand() % 255,
  18. rand() % 255) | 0xFF000000;
  19. SDL_FillRect(screen, NULL, color);
  20. /*
  21. SDL_Surface *bmp = SDL_LoadBMP("image.bmp"); // load a bitmap file
  22. SDL_SavePNG(bmp, "image.png"); // save it as png
  23. SDL_Rect dest = { 0, 0, bmp->w, bmp->h }; // blit it to screen, just to make screen more interesting
  24. SDL_BlitSurface(bmp, NULL, screen, &dest);
  25. */
  26. /* Update screen, just so we can see it */
  27. SDL_Delay(100);
  28. SDL_Flip(screen);
  29. SDL_Delay(1000);
  30. /* Save screen as PNG */
  31. shot = SDL_PNGFormatAlpha(screen); /* SDL_PNGFormatAlpha is optional, but might be necessary for SCREEN surfaces */
  32. SDL_SavePNG(shot, "screen.png");
  33. SDL_FreeSurface(shot);
  34. SDL_Quit();
  35. return 0;
  36. }