module "/pliant/language/context.pli"
module "/pliant/SDL/SDL.pli"

function main -> ret
 arg Int ret
 var Pointer:SDL_Surface screen src #Display surfaces
 var SDL_Event event                #Event structure  

 if (SDL_Init SDL_INIT_VIDEO) < 0
  console "Could not Initialize Video!" eol
  ret := 1
  return 

 #Create an 8-bit color surface, 

 #screen :> (SDL_SetVideoMode 640 480 8 SDL_HWSURFACE .or. SDL_DOUBLEBUF)
 screen :> (SDL_SetVideoMode 640 480 8 SDL_SWSURFACE)

 if not (exists screen)
  console "Could not set video mode"
  ret := 1
  SDL_Quit #Quit SDL
  return 

 #Load sprite source image (SDL.bmp is an 8-bit bitmap file)
 #SDL.bmp can be found at http://pligame.sf.net/files/SDL.bmp 
 src :> SDL_LoadBMP "SDL.bmp"

 #Set screen's 8-bit palette to match sprite's
 SDL_SetColors screen src:format:palette:colors 0 src:format:palette:ncolors

 #Blit (fast copy) source image onto screen
 SDL_BlitSurface src null screen null
 SDL_UpdateRect screen 0 0 0 0
 #SDL_Flip screen

 #CBool is generally faster than plain Bool
 var CBool done := false

 while not done
  while (SDL_PollEvent:event > 0)#Event handling inner loop
   #Wait till key goes up before quitting. Just feels right.
   if event:type = SDL_KEYUP
    done := true

 #Free surface memory (yes, Pliant has no garbage collection)
 SDL_FreeSurface src

 #Quit SDL. Important! 
 SDL_Quit
 ret := 0
 return

main #Run the program!