examples/example_sdl.c
/*shows image with SDL. Returns 1 if user wants to fully quit, 0 if user wants to see next image.*/
shows image with SDL. Returns 1 if user wants to fully quit, 0 if user wants to see next image.
shows image with SDL . Returns 1 if user wants to fully quit 0 if user wants to see next image .
int show(const char* filename) {
  unsigned error;
  unsigned char* image;
  unsigned w, h, x, y;
  SDL_Surface* scr;
  SDL_Event event;
  int done;
  size_t jump = 1;
  printf("showing %s\n", filename);
  error = lodepng_decode32_file(&image, &w, &h, filename);
  if(error) {
    printf("decoder error %u: %s\n", error, lodepng_error_text(error));
    return 0;
  }
  if(w / 1024 >= jump) jump = w / 1024 + 1;
  if(h / 1024 >= jump) jump = h / 1024 + 1;
  if(SDL_Init(SDL_INIT_VIDEO) < 0) {
    printf("Error, SDL video init failed\n");
    return 0;
  }
  scr = SDL_SetVideoMode(w / jump, h / jump, 32, SDL_HWSURFACE);
  if(!scr) {
    printf("Error, no SDL screen\n");
    return 0;
  }
  SDL_WM_SetCaption(filename, NULL); 
  for(y = 0; y + jump - 1 < h; y += jump)
  for(x = 0; x + jump - 1 < w; x += jump) {
    int checkerColor;
    Uint32* bufp;
    Uint32 r, g, b, a;
    r = image[4 * y * w + 4 * x + 0]; 
    g = image[4 * y * w + 4 * x + 1]; 
    b = image[4 * y * w + 4 * x + 2]; 
    a = image[4 * y * w + 4 * x + 3]; 
    checkerColor = 191 + 64 * (((x / 16) % 2) == ((y / 16) % 2));
    r = (a * r + (255 - a) * checkerColor) / 255;
    g = (a * g + (255 - a) * checkerColor) / 255;
    b = (a * b + (255 - a) * checkerColor) / 255;
    bufp = (Uint32 *)scr->pixels + (y * scr->pitch / 4) / jump + (x / jump);
    *bufp = 65536 * r + 256 * g + b;
  }
  done = 0;
  while(done == 0) {
    while(SDL_PollEvent(&event)) {
      if(event.type == SDL_QUIT) done = 2;
      else if(SDL_GetKeyState(NULL)[SDLK_ESCAPE]) done = 2;
      else if(event.type == SDL_KEYDOWN) done = 1; 
    }
    SDL_UpdateRect(scr, 0, 0, 0, 0); 
    SDL_Delay(5); 
  }
  free(image);
  SDL_Quit();
  return done == 2 ? 1 : 0;
}
int show ( const char * filename ) { unsigned error ; unsigned char * image ; unsigned w , h , x , y ; SDL_Surface * scr ; SDL_Event event ; int done ; size_t jump = 1 ; printf ( " \n " , filename ) ; error = lodepng_decode32_file ( & image , & w , & h , filename ) ; if ( error ) { printf ( " \n " , error , lodepng_error_text ( error ) ) ; return 0 ; } if ( w / 1024 >= jump ) jump = w / 1024 + 1 ; if ( h / 1024 >= jump ) jump = h / 1024 + 1 ; if ( SDL_Init ( SDL_INIT_VIDEO ) < 0 ) { printf ( " \n " ) ; return 0 ; } scr = SDL_SetVideoMode ( w / jump , h / jump , 32 , SDL_HWSURFACE ) ; if ( ! scr ) { printf ( " \n " ) ; return 0 ; } SDL_WM_SetCaption ( filename , NULL ) ; for ( y = 0 ; y + jump - 1 < h ; y += jump ) for ( x = 0 ; x + jump - 1 < w ; x += jump ) { int checkerColor ; Uint32 * bufp ; Uint32 r , g , b , a ; r = image [ 4 * y * w + 4 * x + 0 ] ; g = image [ 4 * y * w + 4 * x + 1 ] ; b = image [ 4 * y * w + 4 * x + 2 ] ; a = image [ 4 * y * w + 4 * x + 3 ] ; checkerColor = 191 + 64 * ( ( ( x / 16 ) % 2 ) == ( ( y / 16 ) % 2 ) ) ; r = ( a * r + ( 255 - a ) * checkerColor ) / 255 ; g = ( a * g + ( 255 - a ) * checkerColor ) / 255 ; b = ( a * b + ( 255 - a ) * checkerColor ) / 255 ; bufp = ( Uint32 * ) scr -> pixels + ( y * scr -> pitch / 4 ) / jump + ( x / jump ) ; * bufp = 65536 * r + 256 * g + b ; } done = 0 ; while ( done == 0 ) { while ( SDL_PollEvent ( & event ) ) { if ( event . type == SDL_QUIT ) done = 2 ; else if ( SDL_GetKeyState ( NULL ) [ SDLK_ESCAPE ] ) done = 2 ; else if ( event . type == SDL_KEYDOWN ) done = 1 ; } SDL_UpdateRect ( scr , 0 , 0 , 0 , 0 ) ; SDL_Delay ( 5 ) ; } free ( image ) ; SDL_Quit ( ) ; return done == 2 ? 1 : 0 ; }
shows image with SDL.
shows image with SDL .
/*load the PNG in one function call*/ /*stop if there is an error*/ /*avoid too large window size by downscaling large image*/ /*init SDL*/ /*set window caption*/ /*plot the pixels of the PNG file*/ /*get RGBA components*/ /*red*/ /*green*/ /*blue*/ /*alpha*/ /*make translucency visible by placing checkerboard pattern behind image*/ /*give the color value to the pixel of the screenbuffer*/ /*pause until you press escape and meanwhile redraw screen*/ /*press any other key for next image*/ /*redraw screen*/ /*pause 5 ms so it consumes less processing power*/ /*cleanup*/