module "/pliant/language/context.pli"
module "/pliant/language/compiler.pli"
module "/pliant/language/unsafe.pli"
module "/pliant/SDL/SDL_rwops.pli"

constant sdl_library "libSDL.so" #linux
#constant sdl_library "SDL.dll" #win32

public
 # Transparency definitions: These define alpha as the opacity of a surface 
 gvar uInt8 SDL_ALPHA_OPAQUE := 255
 gvar uInt8 SDL_ALPHA_TRANSPARENT := 0

 # Useful data types 

 type SDL_Rect
  packed
  field Int16 x y
  field uInt16 w h

 function 'cast Address' rect -> address
  arg SDL_Rect rect
  arg Address address
  implicit
  address := addressof:rect

 function 'cast SDL_Rect' address -> rect
  arg Address address
  arg SDL_Rect rect
  implicit
  rect := (address map SDL_Rect)

 type SDL_Color
  packed
  field uInt8 r
  field uInt8 g
  field uInt8 b
  field uInt8 unused

 type SDL_Palette
  packed
  field Int ncolors
  field Address colors # (SDL_Color *)

 # Everything in the pixel format structure is read-only 
 type SDL_PixelFormat
  packed
  field Pointer:SDL_Palette palette
  field uInt8  BitsPerPixel
  field uInt8  BytesPerPixel
  field uInt8  Rloss
  field uInt8  Gloss
  field uInt8  Bloss
  field uInt8  Aloss
  field uInt8  Rshift
  field uInt8  Gshift
  field uInt8  Bshift
  field uInt8  Ashift
  field (Array Byte 2) padding0
  field uInt32 Rmask
  field uInt32 Gmask
  field uInt32 Bmask
  field uInt32 Amask
  # RGB color key information
  field uInt32 colorkey
  # Alpha value information (per-surface alpha)
  field uInt8  alpha
  field (Array Byte 3) padding1

 # This structure should be treated as read-only, except for 'pixels',
 # which, if not NULL, contains the raw pixel data for the surface.
 type SDL_Surface
  packed
  field uInt32 flags
  field Pointer:SDL_PixelFormat format
  field uInt w h
  field uInt16 pitch
  field (Array Byte 2) padding2
  field Address pixels
  field Int offset
  field Address hwdata
  field SDL_Rect clip_rect
  field uInt32 unused
  field uInt32 locked
  field Address map
  field uInt format_version
  field Int refcount

 #function 'cast Address' surface -> address
 # arg SDL_Surface surface
 # arg Address address
 # implicit
 # address := addressof:surface

 #function 'cast SDL_Surface' address -> surface
 # arg Address address
 # arg SDL_Surface surface
 # implicit
 # surface := (address map SDL_Surface)

 # These are the currently supported flags for the SDL_surface 
 gvar uInt SDL_SWSURFACE  := 00000000h  #Surface is in System memory
 gvar uInt SDL_HWSURFACE  := 00000001h  #Surface is in Video memory
 gvar uInt SDL_ASYNCBLIT  := 00000004h  #Use asynchronous blits if possible

 # Available for SDL_SetVideoMode() 
 gvar uInt SDL_ANYFORMAT  := 10000000h  #Allow any video depth/pixel-format
 gvar uInt SDL_HWPALETTE  := 20000000h  #Surface has exclusive palette
 gvar uInt SDL_DOUBLEBUF  := 40000000h  #Set up double-buffered video-mode
 gvar uInt SDL_FULLSCREEN := 80000000h  #Surface is a full screen display
 gvar uInt SDL_OPENGL     := 00000002h  #Create an OpenGL rendering context
 gvar uInt SDL_OPENGLBLIT := 0000000Ah  #Same as above, and use it for blitting
 gvar uInt SDL_RESIZABLE  := 00000010h  #This video mode maybe resized
 gvar uInt SDL_NOFRAME    := 00000020h  #No window caption or edge on frame

 # Used internally (read-only) 
 gvar uInt SDL_HWACCEL     := 00000100h #Blit uses hardware accelaration
 gvar uInt SDL_SRCCOLORKEY := 00001000h #Private flag
 gvar uInt SDL_RLEACCELOK  := 00002000h #Surface is RLE encoded
 gvar uInt SDL_RLEACCEL    := 00004000h #Surface is RLE encoded
 gvar uInt SDL_SRCALPHA    := 00010000h #Blit uses source alpha blending
 gvar uInt SDL_PREALLOC    := 01000000h #Surface uses preallocated memory

 function SDL_MUSTLOCK surface -> z
  arg SDL_Surface surface
  arg Bool z
  z := ((surface:offset <> 0) or ((surface:flags .and. (SDL_HWSURFACE .or. SDL_ASYNCBLIT .or. SDL_RLEACCEL)) <> 0))

 type SDL_VideoInfo
  field uInt32 info_field
  field Address video_mem
  field Address SDL_PixelFormat vfmt

 method info hw_available -> i
  arg SDL_VideoInfo info ; arg Bool i
  i := (info:info_field .and. 00000001h) <> 0

 method info wm_available -> i
  arg SDL_VideoInfo info ; arg Bool i
  i := (info:info_field .and. 00000002h) <> 0

 method info blit_hw -> i
  arg SDL_VideoInfo info ; arg Bool i
  i := (info:info_field .and. 00000100h) <> 0

 method info blit_hw_CC -> i
  arg SDL_VideoInfo info ; arg Bool i
  i := (info:info_field .and. 00000200h) <> 0

 method info blit_hw_A -> i
  arg SDL_VideoInfo info ; arg Bool i
  i := (info:info_field .and. 00000400h) <> 0

 method info blit_sw -> i
  arg SDL_VideoInfo info ; arg Bool i
  i := (info:info_field .and. 00001000h) <> 0

 method info blit_sw_CC -> i
  arg SDL_VideoInfo info ; arg Bool i
  i := (info:info_field .and. 00002000h) <> 0

 method info blit_fill -> i
  arg SDL_VideoInfo info ; arg Bool i
  i := (info:info_field .and. 00004000h ) <> 0

 function 'cast Address' info -> address
  arg SDL_VideoInfo info
  arg Address address
  implicit
  address := addressof:info

 function 'cast SDL_VideoInfo' address -> info
  arg Address address
  arg SDL_VideoInfo info
  implicit
  info := (address map SDL_VideoInfo)

 #Common Overlay formats:
 #For information on color spaces, see
 #http://www.neuro.sfc.keio.ac.jp/~aly/polygon/info/color-space-faq.html

 gvar uInt SDL_YV12_OVERLAY := 32315659h #Planar mode: Y + V + U (3 planes)
 gvar uInt SDL_IYUV_OVERLAY := 56555949h #Planar mode: Y + U + V  (3 planes)
 gvar uInt SDL_YUY2_OVERLAY := 32595559h #Packed mode: Y0+U0+Y1+V0 (1 plane)
 gvar uInt SDL_UYVY_OVERLAY := 59565955h #Packed mode: U0+Y0+V0+Y1 (1 plane)
 gvar uInt SDL_YVYU_OVERLAY := 55595659h #Packed mode: Y0+V0+Y1+U0 (1 plane)

 type SDL_Overlay
  packed
  field uInt32 format
  field Int w h
  field Int planes
  field Address pitches
  field Address pixels
  field Address hwfuncs
  field Address hwdata
  field uInt32 hw_overlay

 #Public enumeration for setting the OpenGL window attributes. 
  gvar uInt SDL_GL_RED_SIZE := 0
  gvar uInt SDL_GL_GREEN_SIZE := 2
  gvar uInt SDL_GL_BLUE_SIZE := 3
  gvar uInt SDL_GL_ALPHA_SIZE := 4
  gvar uInt SDL_GL_BUFFER_SIZE := 5
  gvar uInt SDL_GL_DOUBLEBUFFER := 6
  gvar uInt SDL_GL_DEPTH_SIZE := 7
  gvar uInt SDL_GL_STENCIL_SIZE := 8
  gvar uInt SDL_GL_ACCUM_RED_SIZE := 9
  gvar uInt SDL_GL_ACCUM_GREEN_SIZE := 10
  gvar uInt SDL_GL_ACCUM_BLUE_SIZE := 11
  gvar uInt SDL_GL_ACCUM_ALPHA_SIZE := 12
  gvar uInt SDL_GL_STEREO := 13

 gvar uInt SDL_LOGPAL := 01h
 gvar uInt SDL_PHSYPAL := 02h

 #line 241

 # Returns address of current display surface (SDL_Surface)
 function SDL_GetVideoSurface -> surface
  arg Address surface #(SDL_Surface)
  external sdl_library "SDL_GetVideoSurface"

 #Returns address of hardware info. (SDL_VideoInfo) 
 function SDL_GetVideoInfo -> info
  arg Address info #(SDL_VideoInfo)
  external sdl_library "SDL_GetVideoInfo"

 #Returns 0 if the specified video mode is not available.
 function SDL_VideoModeOK width height bpp flags
  arg Int width height bpp
  arg uInt flags
  external sdl_library "SDL_VideoModeOK"

 #Returns the an array of screen dimenstions for given pixel format and video flags
 #Returns null if no dimensions available, or (SDL_Rect **)-1 if any is okay.
 function SDL_ListModes format flags -> rect_array
  arg Address format # (SDL_PixelFormat)
  arg uInt flags
  arg Address rect_array #(Pointer:Pointer:SDL_Rect or SDL_Rect **) 
  external sdl_library "SDL_ListModes"

 # Set up a video mode with the specified width, height and bits-per-pixel.
 #
 # If 'bpp' is 0, it is treated as the current display bits per pixel.
 #
 # If SDL_ANYFORMAT is set in 'flags', the SDL library will try to set the
 # requested bits-per-pixel, but will return whatever video pixel format is
 # available.  The default is to emulate the requested pixel format if it
 # is not natively available.
 #
 # If SDL_HWSURFACE is set in 'flags', the video surface will be placed in
 # video memory, if possible, and you may have to call SDL_LockSurface()
 # in order to access the raw framebuffer.  Otherwise, the video surface
 # will be created in system memory.
 #
 # If SDL_ASYNCBLIT is set in 'flags', SDL will try to perform rectangle
 # updates asynchronously, but you must always lock before accessing pixels.
 # SDL will wait for updates to complete before returning from the lock.
 #
 # If SDL_HWPALETTE is set in 'flags', the SDL library will guarantee
 # that the colors set by SDL_SetColors() will be the colors you get.
 # Otherwise, in 8-bit mode, SDL_SetColors() may not be able to set all
 # of the colors exactly the way they are requested, and you should look
 # at the video surface structure to determine the actual palette.
 # If SDL cannot guarantee that the colors you request can be set, 
 # i.e. if the colormap is shared, then the video surface may be created
 # under emulation in system memory, overriding the SDL_HWSURFACE flag.
 #
 # If SDL_FULLSCREEN is set in 'flags', the SDL library will try to set
 # a fullscreen video mode.  The default is to create a windowed mode
 # if the current graphics system has a window manager.
 # If the SDL library is able to set a fullscreen video mode, this flag 
 # will be set in the surface that is returned.
 #
 # If SDL_DOUBLEBUF is set in 'flags', the SDL library will try to set up
 # two surfaces in video memory and swap between them when you call 
 # SDL_Flip().  This is usually slower than the normal single-buffering
 # scheme, but prevents "tearing" artifacts caused by modifying video 
 # memory while the monitor is refreshing.  It should only be used by 
 # applications that redraw the entire screen on every update.
 #
 # If SDL_RESIZABLE is set in 'flags', the SDL library will allow the
 # window manager, if any, to resize the window at runtime.  When this
 # occurs, SDL will send a SDL_VIDEORESIZE event to you application,
 # and you must respond to the event by re-calling SDL_SetVideoMode()
 # with the requested size (or another size that suits the application).
 #
 # If SDL_NOFRAME is set in 'flags', the SDL library will create a window
 # without any title bar or frame decoration.  Fullscreen video modes have
 # this flag set automatically.
 #
 # This function returns the video framebuffer surface, or NULL if it fails.
 #
 # If you rely on functionality provided by certain video flags, check the
 # flags of the returned surface to make sure that functionality is available.
 # SDL will fall back to reduced functionality if the exact flags you wanted
 # are not available.

 function SDL_SetVideoMode width height bpp flags -> surface
  arg Int width height bpp
  arg uInt flags
  arg Pointer:SDL_Surface surface #(SDL_Surface)
  external sdl_library "SDL_SetVideoMode"

 # Makes sure the given list of rectangles is updated on the given screen.
 # If 'x', 'y', 'w' and 'h' are all 0, SDL_UpdateRect will update the entire
 # screen.
 # These functions should not be called while 'screen' is locked.

 function SDL_UpdateRects screen numrects rects
  arg Pointer:SDL_Surface screen
  arg Int numrects
  arg Address rects
  external sdl_library "SDL_UpdateRects"

 function SDL_UpdateRect screen x y w h
  arg Pointer:SDL_Surface screen
  arg Int x y
  arg uInt w h
  external sdl_library "SDL_UpdateRect"


 # On hardware that supports double-buffering, this function sets up a flip
 # and returns.  The hardware will wait for vertical retrace, and then swap
 # video buffers before the next video surface blit or lock will return.
 # On hardware that doesn not support double-buffering, this is equivalent
 # to calling SDL_UpdateRect(screen, 0, 0, 0, 0);
 # The SDL_DOUBLEBUF flag must have been passed to SDL_SetVideoMode() when
 # setting the video mode for this function to perform hardware flipping.
 # This function returns 0 if successful, or -1 if there was an error.

 function SDL_Flip screen
  arg SDL_Surface screen
  external sdl_library "SDL_Flip"


 # Set the gamma correction for each of the color channels.
 # The gamma values range (approximately) between 0.1 and 10.0
 # 
 # If this function isn't supported directly by the hardware, it will
 # be emulated using gamma ramps, if available.  If successful, this
 # function returns 0, otherwise it returns -1.

 #Internal function, uses wrapper to cast literal Floats
 function SDL_SetGamma_ red green blue ->z
  arg Float32 red green blue
  arg Int z
  external sdl_library "SDL_SetGamma"

 #Forcing Pliant to pass Float arguments by value, not by address!
 ((the_function SDL_SetGamma_ Float32 Float32 Float32 -> Int) arg 0) access := ((the_function SDL_SetGamma_ Float32 Float32 Float32 -> Int) arg 0):access .or. access_byvalue
 ((the_function SDL_SetGamma_ Float32 Float32 Float32 -> Int) arg 1) access := ((the_function SDL_SetGamma_ Float32 Float32 Float32 -> Int) arg 0):access .or. access_byvalue
 ((the_function SDL_SetGamma_ Float32 Float32 Float32 -> Int) arg 2) access := ((the_function SDL_SetGamma_ Float32 Float32 Float32 -> Int) arg 0):access .or. access_byvalue


 #Wrapper to cast Float to 32-bit floats.
 function SDL_SetGamma red green blue -> z
  arg Float red green blue
  arg Int z
  z:= SDL_SetGamma_ (cast red Float32) (cast green Float32) (cast blue Float32)


 # Set the gamma translation table for the red, green, and blue channels
 # of the video hardware.  Each table is an array of 256 16-bit quantities,
 # representing a mapping between the input and output for that channel.
 # The input is the index into the array, and the output is the 16-bit
 # gamma value at that index, scaled to the output color precision.
 # 
 # You may pass NULL for any of the channels to leave it unchanged.
 # If the call succeeds, it will return 0.  If the display driver or
 # hardware does not support gamma translation, or otherwise fails,
 # this function will return -1.

 function SDL_SetGammaRamp red green blue -> z
  arg uInt16 red green blue
  arg Int z
  external sdl_library "SDL_SetGammaRamp"

 # Retrieve the current values of the gamma translation tables.
 # 
 # You must pass in valid pointers to arrays of 256 16-bit quantities.
 # Any of the pointers may be NULL to ignore that channel.
 # If the call succeeds, it will return 0.  If the display driver or
 # hardware does not support gamma translation, or otherwise fails,
 # this function will return -1.

 function SDL_GetGammaRamp red green blue -> z
  arg uInt16 red green blue
  arg Int z
  external sdl_library "SDL_GetGammaRamp"

 # Sets a portion of the colormap for the given 8-bit surface.  If 'surface'
 # is not a palettized surface, this function does nothing, returning 0.
 # If all of the colors were set as passed to SDL_SetColors(), it will
 # return 1.  If not all the color entries were set exactly as given,
 # it will return 0, and you should look at the surface palette to
 # determine the actual color palette.
 #
 # When 'surface' is the surface associated with the current display, the
 # display colormap will be updated with the requested colors.  If 
 # SDL_HWPALETTE was set in SDL_SetVideoMode() flags, SDL_SetColors()
 # will always return 1, and the palette is guaranteed to be set the way
 # you desire, even if the window colormap has to be warped or run under
 # emulation.

 function SDL_SetColors surface colors firstcolor ncolors -> z
  arg Pointer:SDL_Surface surface
  arg Address colors
  arg Int firstcolor ncolors z
  external sdl_library "SDL_SetColors"


 # Sets a portion of the colormap for a given 8-bit surface.
 # 'flags' is one or both of:
 # SDL_LOGPAL  -- set logical palette, which controls how blits are mapped
 #                to/from the surface,
 # SDL_PHYSPAL -- set physical palette, which controls how pixels look on
 #                the screen
 # Only screens have physical palettes. Separate change of physical/logical
 # palettes is only possible if the screen has SDL_HWPALETTE set.
 #
 # The return value is 1 if all colours could be set as requested, and 0
 # otherwise.
 #
 # SDL_SetColors() is equivalent to calling this function with
 #     flags = (SDL_LOGPAL|SDL_PHYSPAL).

 function SDL_SetPalette surface flags colors firstcolor ncolors -> z
  arg Pointer:SDL_Surface surface
  arg SDL_Color colors
  arg uInt flags
  arg Int firstcolor ncolors z
  external sdl_library "SDL_SetPalette"


 # Maps an RGB triple to an opaque pixel value for a given pixel format
 function SDL_MapRGB format r g b -> z
  arg SDL_PixelFormat format
  arg uInt r g b z
  external sdl_library "SDL_MapRGB"


 # Maps an RGBA quadruple to a pixel value for a given pixel format
 function SDL_MapRGBA format r g b a -> z
  arg SDL_PixelFormat format
  arg uInt r g b a z
  external sdl_library "SDL_MapRGBA"

 # Maps a pixel value into the RGB components for a given pixel format
 function SDL_GetRGB pixel fmt r g b
  arg uInt pixel
  arg SDL_PixelFormat fmt
  arg uInt8 r g b
  external sdl_library "SDL_GetRGB"

 # Maps a pixel value into the RGBA components for a given pixel format
 function SDL_GetRGB pixel fmt r g b a
  arg uInt pixel
  arg SDL_PixelFormat fmt
  arg uInt8 r g b a
  external sdl_library "SDL_GetRGBA"


 # Allocate and free an RGB surface (must be called after SDL_SetVideoMode)
 # If the depth is 4 or 8 bits, an empty palette is allocated for the surface.
 # If the depth is greater than 8 bits, the pixel format is set using the
 # flags '[RGB]mask'.
 # If the function runs out of memory, it will return NULL.
 #
 # The 'flags' tell what kind of surface to create.
 # SDL_SWSURFACE means that the surface should be created in system memory.
 # SDL_HWSURFACE means that the surface should be created in video memory,
 # with the same format as the display surface.  This is useful for surfaces
 # that will not change much, to take advantage of hardware acceleration
 # when being blitted to the display surface.
 # SDL_ASYNCBLIT means that SDL will try to perform asynchronous blits with
 # this surface, but you must always lock it before accessing the pixels.
 # SDL will wait for current blits to finish before returning from the lock.
 # SDL_SRCCOLORKEY indicates that the surface will be used for colorkey blits.
 # If the hardware supports acceleration of colorkey blits between
 # two surfaces in video memory, SDL will try to place the surface in
 # video memory. If this isn't possible or if there is no hardware
 # acceleration available, the surface will be placed in system memory.
 # SDL_SRCALPHA means that the surface will be used for alpha blits and 
 # if the hardware supports hardware acceleration of alpha blits between
 # two surfaces in video memory, to place the surface in video memory
 # if possible, otherwise it will be placed in system memory.
 # If the surface is created in video memory, blits will be _much_ faster,
 # but the surface format must be identical to the video surface format,
 # and the only way to access the pixels member of the surface is to use
 # the SDL_LockSurface() and SDL_UnlockSurface() calls.
 # If the requested surface actually resides in video memory, SDL_HWSURFACE
 # will be set in the flags member of the returned surface.  If for some
 # reason the surface could not be placed in video memory, it will not have
 # the SDL_HWSURFACE flag set, and will be created in system memory instead.

 function SDL_CreateRGBSurface flags width height depth Rmask Gmask Bmask Amask -> surface
  arg Int width height depth
  arg uInt flags Rmask Gmask Bmask Amask
  arg Pointer:SDL_Surface surface #(SDL_Surface)
  external sdl_library "SDL_CreateRGBSurface"

 function SDL_CreateRGBSurfaceFrom pixels width height depth pitch Rmask Gmask Bmask Amask -> surface
  arg Address pixels
  arg Int width height depth pitch
  arg uInt Rmask Gmask Bmask Amask
  arg Pointer:SDL_Surface surface #(SDL_Surface)
  external sdl_library "SDL_CreateRGBSurfaceFrom"

 function SDL_FreeSurface surface
  arg Pointer:SDL_Surface surface
  external sdl_library "SDL_FreeSurface"

 # SDL_LockSurface() sets up a surface for directly accessing the pixels.
 # Between calls to SDL_LockSurface()/SDL_UnlockSurface(), you can write
 # to and read from 'surface->pixels', using the pixel format stored in 
 # 'surface->format'.  Once you are done accessing the surface, you should 
 # use SDL_UnlockSurface() to release it.
 #
 # Not all surfaces require locking.  If SDL_MUSTLOCK(surface) evaluates
 # to 0, then you can read and write to the surface at any time, and the
 # pixel format of the surface will not change.  In particular, if the
 # SDL_HWSURFACE flag is not given when calling SDL_SetVideoMode(), you
 # will not need to lock the display surface before accessing it.
 # 
 # No operating system or library calls should be made between lock/unlock
 # pairs, as critical system locks may be held during this time.
 #
 # SDL_LockSurface() returns 0, or -1 if the surface couldn't be locked.

 function SDL_LockSurface surface -> z
  arg Pointer:SDL_Surface surface
  arg Int z
  external sdl_library "SDL_LockSurface"

 function SDL_UnlockSurface surface
  arg Pointer:SDL_Surface surface
  external sdl_library "SDL_UnlockSurface"

 # Load a surface from a seekable SDL data source (memory or file.)
 # If 'freesrc' is non-zero, the source will be closed after being read.
 # Returns the new surface, or NULL if there was an error.
 # The new surface should be freed with SDL_FreeSurface().

 function SDL_LoadBMP_RW src freesrc -> surface
  arg Address src #(SDL_RWops)
  arg Int freesrc
  arg Pointer:SDL_Surface surface
  external sdl_library "SDL_LoadBMP_RW"

 # Load a surface from a file 
 function SDL_LoadBMP file -> surface
  arg CStr file
  arg Pointer:SDL_Surface surface #(SDL_Surface)  
  surface :> SDL_LoadBMP_RW (SDL_RWFromFile file "rb") 1

 # Save a surface to a seekable SDL data source (memory or file.)
 # If 'freedst' is non-zero, the source will be closed after being written.
 # Returns 0 if successful or -1 if there was an error.

 function SDL_SaveBMP_RW surface dst freedst -> z
  arg Pointer:SDL_Surface surface
  arg Address dst #(SDL_RWops)
  arg Int freedst z
  external sdl_library "SDL_SaveBMP_RW"

 #Save a surface to a file
 function SDL_SaveBMP surface file -> z
  arg Pointer:SDL_Surface surface
  arg CStr file
  arg Int z
  z := SDL_SaveBMP_RW surface (SDL_RWFromFile file "wb") 1

 # Sets the color key (transparent pixel) in a blittable surface.
 # If 'flag' is SDL_SRCCOLORKEY (optionally OR'd with SDL_RLEACCEL), 
 # 'key' will be the transparent pixel in the source image of a blit.
 # SDL_RLEACCEL requests RLE acceleration for the surface if present,
 # and removes RLE acceleration if absent.
 # If 'flag' is 0, this function clears any current color key.
 # This function returns 0, or -1 if there was an error.

 function SDL_SetColorKey surface flag key -> z
  arg Pointer:SDL_Surface surface
  arg uInt flag key
  arg Int z
  external sdl_library "SDL_SetColorKey"

 # This function sets the alpha value for the entire surface, as opposed to
 # using the alpha component of each pixel. This value measures the range
 # of transparency of the surface, 0 being completely transparent to 255
 # being completely opaque. An 'alpha' value of 255 causes blits to be
 # opaque, the source pixels copied to the destination (the default). Note
 # that per-surface alpha can be combined with colorkey transparency.
 #
 # If 'flag' is 0, alpha blending is disabled for the surface.
 # If 'flag' is SDL_SRCALPHA, alpha blending is enabled for the surface.
 # OR:ing the flag with SDL_RLEACCEL requests RLE acceleration for the
 # surface; if SDL_RLEACCEL is not specified, the RLE accel will be removed.
 #
 # The 'alpha' parameter is ignored for surfaces that have an alpha channel.

 function SDL_SetAlpha surface flag alpha -> z
  arg Pointer:SDL_Surface surface
  arg uInt flag alpha
  arg Int z
  external sdl_library "SDL_SetAlpha"

 # Sets the clipping rectangle for the destination surface in a blit.
 #
 # If the clip rectangle is NULL, clipping will be disabled.
 # If the clip rectangle doesn't intersect the surface, the function will
 # return SDL_FALSE and blits will be completely clipped.  Otherwise the
 # function returns SDL_TRUE and blits to the surface will be clipped to
 # the intersection of the surface area and the clipping rectangle.
 #
 # Note that blits are automatically clipped to the edges of the source
 # and destination surfaces.

 function SDL_SetClipRect surface rect -> b
  arg Pointer:SDL_Surface surface
  arg SDL_Rect rect
  arg Int b #(enum SDL_bool)
  external sdl_library "SDL_SetClipRect"

 # Gets the clipping rectangle for the destination surface in a blit.
 # 'rect' must be a pointer to a valid rectangle which will be filled
 # with the correct values.

 function SDL_GetClipRect surface rect
  arg Pointer:SDL_Surface surface
  arg Pointer:SDL_Rect rect
  external sdl_library "SDL_GetClipRect"

 # Creates a new surface of the specified format, and then copies and maps 
 # the given surface to it so the blit of the converted surface will be as 
 # fast as possible.  If this function fails, it returns NULL.
 #
 # The 'flags' parameter is passed to SDL_CreateRGBSurface() and has those 
 # semantics.  You can also pass SDL_RLEACCEL in the flags parameter and
 # SDL will try to RLE accelerate colorkey and alpha blits in the resulting
 # surface.
 #
 # This function is used internally by SDL_DisplayFormat().

 function SDL_ConvertSurface src fmt flags -> surface
  arg Pointer:SDL_Surface src surface
  arg SDL_PixelFormat fmt
  arg uInt flags
  external sdl_library "SDL_ConvertSurface"


 # This performs a fast blit from the source surface to the destination
 # surface.  It assumes that the source and destination rectangles are
 # the same size.  If either 'srcrect' or 'dstrect' are NULL, the entire
 # surface (src or dst) is copied.  The final blit rectangles are saved
 # in 'srcrect' and 'dstrect' after all clipping is performed.
 # If the blit is successful, it returns 0, otherwise it returns -1.
 #
 # The blit function should not be called on a locked surface.
 #
 # The blit semantics for surfaces with and without alpha and colorkey
 # are defined as follows:
 #
 # RGBA->RGB:
 #     SDL_SRCALPHA set:
 #      alpha-blend (using alpha-channel).
 #      SDL_SRCCOLORKEY ignored.
 #     SDL_SRCALPHA not set:
 #      copy RGB.
 #      if SDL_SRCCOLORKEY set, only copy the pixels matching the
 #      RGB values of the source colour key, ignoring alpha in the
 #      comparison.
 # 
 # RGB->RGBA:
 #     SDL_SRCALPHA set:
 #      alpha-blend (using the source per-surface alpha value);
 #      set destination alpha to opaque.
 #     SDL_SRCALPHA not set:
 #      copy RGB, set destination alpha to source per-surface alpha value.
 #     both:
 #      if SDL_SRCCOLORKEY set, only copy the pixels matching the
 #      source colour key.
 # 
 # RGBA->RGBA:
 #     SDL_SRCALPHA set:
 #      alpha-blend (using the source alpha channel) the RGB values;
 #      leave destination alpha untouched. [Note: is this correct?]
 #      SDL_SRCCOLORKEY ignored.
 #     SDL_SRCALPHA not set:
 #      copy all of RGBA to the destination.
 #      if SDL_SRCCOLORKEY set, only copy the pixels matching the
 #      RGB values of the source colour key, ignoring alpha in the
 #      comparison.
 # 
 # RGB->RGB: 
 #     SDL_SRCALPHA set:
 #      alpha-blend (using the source per-surface alpha value).
 #     SDL_SRCALPHA not set:
 #      copy RGB.
 #     both:
 #      if SDL_SRCCOLORKEY set, only copy the pixels matching the
 #      source colour key.
 #
 # If either of the surfaces were in video memory, and the blit returns -2,
 # the video memory was lost, so it should be reloaded with artwork and 
 # re-blitted:
 #      while ( SDL_BlitSurface(image, imgrect, screen, dstrect) == -2 ) {
 #              while ( SDL_LockSurface(image) < 0 )
 #                      Sleep(10);
 #              -- Write image pixels to image->pixels --
 #              SDL_UnlockSurface(image);
 #      }
 # This happens under DirectX 5.0 when the system switches away from your
 # fullscreen application.  The lock will also fail until you have access
 # to the video memory again.

 # You should call SDL_BlitSurface() unless you know exactly how SDL
 # blitting works internally and how to use the other blit functions.

 function SDL_BlitSurface src srcrect dst dstrect -> z
  arg Pointer:SDL_Surface src dst
  arg Address srcrect dstrect
  arg Int z
  external sdl_library "SDL_UpperBlit"

 # This is the public blit function, SDL_BlitSurface(), and it performs
 # rectangle validation and clipping before passing it to SDL_LowerBlit()

 function SDL_UpperBlit src srcrect dst dstrect -> z
  arg Pointer:SDL_Surface src dst
  arg SDL_Rect srcrect dstrect
  arg Int z
  external sdl_library "SDL_UpperBlit"

 # This is a semi-private blit function and it performs low-level surface
 # blitting only.

 function SDL_LowerBlit src srcrect dst dstrect -> z
  arg Pointer:SDL_Surface src dst
  arg SDL_Rect srcrect dstrect
  arg Int z
  external sdl_library "SDL_LowerBlit"

 # This function performs a fast fill of the given rectangle with 'color'
 # The given rectangle is clipped to the destination surface clip area
 # and the final fill rectangle is saved in the passed in pointer.
 # If 'dstrect' is NULL, the whole surface will be filled with 'color'
 # The color should be a pixel of the format used by the surface, and 
 # can be generated by the SDL_MapRGB() function.
 # This function returns 0 on success, or -1 on error.

 function SDL_FillRect dst dstrect color
  arg Pointer:SDL_Surface dst
  arg Address dstrect #SDL_Rect
  arg uInt color
  external sdl_library "SDL_FillRect"



 # This function takes a surface and copies it to a new surface of the
 # pixel format and colors of the video framebuffer, suitable for fast
 # blitting onto the display surface.  It calls SDL_ConvertSurface()
 #
 # If you want to take advantage of hardware colorkey or alpha blit
 # acceleration, you should set the colorkey and alpha value before
 # calling this function.
 #
 # If the conversion fails or runs out of memory, it returns NULL

 function SDL_DisplayFormat surface1 -> surface2
  arg Pointer:SDL_Surface surface1 surface2
  external sdl_library "SDL_DisplayFormat"


 # This function takes a surface and copies it to a new surface of the
 # pixel format and colors of the video framebuffer (if possible),
 # suitable for fast alpha blitting onto the display surface.
 # The new surface will always have an alpha channel.
 #
 # If you want to take advantage of hardware colorkey or alpha blit
 # acceleration, you should set the colorkey and alpha value before
 # calling this function.
 #
 # If the conversion fails or runs out of memory, it returns NULL

 function SDL_DisplayFormatAlpha surface1 -> surface2
  arg Pointer:SDL_Surface surface1 surface2
  external sdl_library "SDL_DisplayFormatAlpha"

 ##########################################################################
 # YUV video surface overlay functions                                      
 ##########################################################################
 # 
 # This function creates a video output overlay
 # Calling the returned surface an overlay is something of a misnomer because
 # the contents of the display surface underneath the area where the overlay
 # is shown is undefined - it may be overwritten with the converted YUV data.

 function SDL_CreateYUVOverlay width height format display -> overlay
  arg Int width height
  arg uInt format
  arg Pointer:SDL_Surface display
  arg Pointer:SDL_Overlay overlay
  external sdl_library "SDL_CreateYUVOverlay"

 # Lock an overlay for direct access, and unlock it when you are done 
 function SDL_LockYUVOverlay overlay -> z
  arg Pointer:SDL_Overlay overlay
  arg Int z
  external sdl_library "SDL_LockYUVOverlay"

 function SDL_UnlockYUVOverlay overlay
  arg Pointer:SDL_Overlay overlay
  external sdl_library "SDL_UnlockYUVOverlay"

 # Blit a video overlay to the display surface.
 # The contents of the video surface underneath the blit destination are
 # not defined.  
 # The width and height of the destination rectangle may be different from
 # that of the overlay, but currently only 2x scaling is supported.

 function SDL_DisplayYUVOverlay overlay dstrect
  arg Pointer:SDL_Overlay overlay
  arg Address dstrect #(SDL_Rect)
  external sdl_library "SDL_DisplayYUVOverlay"

 # Free a video overlay 

 function SDL_FreeYUVOverlay overlay
  arg Pointer:SDL_Overlay overlay
  external sdl_library "SDL_FreeYUVOverlay"

 #############################################################################
 # OpenGL support functions.                                                 #
 #############################################################################
 #
 # Dynamically load a GL driver, if SDL is built with dynamic GL.
 #
 # SDL links normally with the OpenGL library on your system by default,
 # but you can compile it to dynamically load the GL driver at runtime.
 # If you do this, you need to retrieve all of the GL functions used in
 # your program from the dynamic library using SDL_GL_GetProcAddress().
 #
 # This is disabled in default builds of SDL.
 #

 function SDL_GL_LoadLibrary path -> z
  arg CStr path; arg Int z
  external sdl_library "SDL_GL_LoadLibrary"

 # Get the address of a GL function (for extension functions)
 function SDL_GL_GetProcAddress proc
  arg CStr proc
  external sdl_library "SDL_GL_GetProcAddress"


 # Get an attribute of the OpenGL subsystem from the windowing
 # interface, such as glX. This is of course different from getting
 # the values from SDL's internal OpenGL subsystem, which only
 # stores the values you request before initialization.
 #
 # Developers should track the values they pass into SDL_GL_SetAttribute
 # themselves if they want to retrieve these values.

 function SDL_GL_GetAttribute attr value -> z
  arg uInt attr; arg Int z;
  arg Address value #(Int *)

 # Swap the OpenGL buffers, if double-buffering is supported.

 function SDL_GL_SwapBuffers
  external sdl_library "SDL_GL_SwapBuffers"

 # Internal functions that should not be called unless you have read
 # and understood the source code for these functions.

 function SDL_GL_UpdateRects numrects rects
  arg Int numrects;
  arg Address rects #(SDL_rect*)
  external sdl_library "SDL_GL_UpdateRects"

 function SDL_GL_Lock
  external sdl_library "SDL_GL_Lock"

 function SDL_GL_Unlock
  external sdl_library "SDL_GL_Unlock"


 ##########################################################################
 # These functions allow interaction with the window manager, if any.     
 ##########################################################################

 # Sets/Gets the title and icon text of the display window

 function SDL_WM_SetCaption title icon
  arg CStr title icon
  external sdl_library "SDL_WM_SetCaption"

 function SDL_WM_GetCaption title icon
  arg Pointer:CStr title icon
  external sdl_library "SDL_WM_GetCaption"


 # Sets the icon for the display window.
 # This function must be called before the first call to SDL_SetVideoMode().
 # It takes an icon surface, and a mask in MSB format.
 # If 'mask' is NULL, the entire icon surface will be used as the icon.

 function SDL_WM_SetIcon icon mask
  arg Pointer:SDL_Surface icon
  arg uInt8 mask
  external sdl_library "SDL_WM_SetIcon"


 # This function iconifies the window, and returns 1 if it succeeded.
 # If the function succeeds, it generates an SDL_APPACTIVE loss event.
 # This function is a noop and returns 0 in non-windowed environments.

 function SDL_WM_IconifyWindow -> z
  arg Int z
  external sdl_library "SDL_WM_IconifyWindow"


 # Toggle fullscreen mode without changing the contents of the screen.
 # If the display surface does not require locking before accessing
 # the pixel information, then the memory pointers will not change.
 #
 # If this function was able to toggle fullscreen mode (change from 
 # running in a window to fullscreen, or vice-versa), it will return 1.
 # If it is not implemented, or fails, it returns 0.
 #
 # The next call to SDL_SetVideoMode() will set the mode fullscreen
 # attribute based on the flags parameter - if SDL_FULLSCREEN is not
 # set, then the display will be windowed by default where supported.
 #
 # This is currently only implemented in the X11 video driver.

 function SDL_WM_ToggleFullScreen surface -> z
  arg Pointer:SDL_Surface surface; arg Int z
  external sdl_library "SDL_WM_ToggleFullScreen"

 # This function allows you to set and query the input grab state of
 # the application.  It returns the new input grab state.

 #typedef enum ... SDL_GrabMode
 gvar Int SDL_GRAB_QUERY := -1
 gvar Int SDL_GRAB_OFF := 0
 gvar Int SDL_GRAB_ON := 1
 gvar Int SDL_GRAB_FULLSCREEN := 2

 # Grabbing means that the mouse is confined to the application window,
 # and nearly all keyboard input is passed directly to the application,
 # and not interpreted by a window manager, if any.

 function SDL_WM_GrabInput mode1 -> mode2
  arg Int mode1 mode2 #(SDL_GrabMode)
  external sdl_library "SDL_WM_GrabInput"