#include "SDL.h"
#include <stdio.h>
#include <zzip.h>

int main(void) {
  SDL_Surface *screen;
  SDL_Surface *one;
  SDL_Rect rect;

  /* This is the RWops structure we'll be using */
  SDL_RWops *rw;

  /* ZZIP_FILE is the zzip equivalent of the stdio FILE.*/
  ZZIP_FILE *file;

  /* Again, we're cheating because I know how big the file is */
  Uint8 buffer[13000];

  int filesize;

  SDL_Init(SDL_INIT_VIDEO);

  atexit(SDL_Quit);

  screen = SDL_SetVideoMode(640, 480, 16, SDL_DOUBLEBUF);

  /* zzip_open does some magic here - if there is a directory called
     "penguin" and there is a "penguin.bmp" there, zzip_read (which
     we'll use later) will act just like stdio's read.  If, however,
     there is a penguins.zip that has a penguin.bmp in it, that will be
     read.  Keep in mind that zzip will prefer real files to files in
     .zip archives if there's a conflict. */
  file = zzip_open("penguins/penguin.bmp", 0);
  if(file == NULL) {
    printf("Can't load penguins/penguin.bmp\n");
    exit(1);
  }

  /* Isn't it nice how all these libraries mimic read(2) for us? */
  filesize = zzip_read(file, buffer, 13000);

  rw = SDL_RWFromMem(buffer, filesize);
  one = SDL_LoadBMP_RW(rw, 0);
  
  /* And clean up */
  SDL_FreeRW(rw);
  zzip_close(file);

  /* Haphazard way of getting stuff to the screen */
  rect.x = rect.y = 20;
  rect.w = one -> w;
  rect.y = one -> h;
  SDL_BlitSurface(one, NULL, screen, &rect);

  SDL_Flip(screen);

  SDL_Delay(3000);
}
