Skip to content

Angluca/hare-sdl2

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

64 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

hare-sdl2

These are bindings between Hare and SDL2, plus SDL satellite libraries SDL_image, SDL_mixer, and SDL_ttf. There are two modules:

  • sdl::v2c exposes the raw C library and types with few modifications.
  • sdl wraps sdl::v2c into idiomatic Hare.

Ditto for the satellites. The bindings are for the SDL2 branch of each project, not the 2.0.0 release.

See the sections below for more information on each module. If you would like additional libraries (SDL2_Pango?), let me know! They're mostly autogenerated, so it's not a big issue to add more.

Remember, plain SDL is almost never the right choice for making GUIs.

Usage

  • Install libsdl2. For the examples, you will need libsdl2-image, libsdl2-mixer, and libsdl2-ttf.
  • Run make, make info, and make demo.

C bindings

These are a nearly direct translation from C to Hare, with the following changes:

  • C types are converted to their natural Hare counterpart, Uint16 -> u16, etc. Of note, C's char* becomes *c::char from the types::c module.
  • Pointers that may be null have nullable added manually. I may have missed some, so feel free to fix my mistakes.
  • Names that are reserved words in Hare are suffixed with an underscore.
  • Opaque pointer types are replaced with opaque, no pointer. The pointer is then added at usage sites. This allows us to make them nullable.
  • Enum value prefixes are not stripped. In C, enum values are added to the global namespace. So from a user's perspective there is often little difference between a #define and an enum, and the SDL library often makes the "wrong" choice. This module directly copies SDL's choices. In Hare, as many other languages, enum values are not added to the parent namespace. So, to access SDL_QUIT in Hare, one must write SDL_EventType::SDL_QUIT.
  • Documentation and comments are not copied over. Refer to the SDL documentation.

Idiomatic bindings

These are wrappers over sdl::v2c (and satellites), with the following changes:

  • Function and type names are converted from SDL_PascalCase to snake_case.
  • enum values have their SDL_ENUMNAME_ previx removed.
  • char* strings are converted to Hare strings and vice versa.
  • Length + pointer arrays are converted to Hare slices and vice versa.
  • Sentinel-value errors are converted into Hare tagged unions.
  • Documentation is brought over from SDL and adapted for Hare.
  • SDL include files each get their own module. Because all of their symbols are unique, I recommend importing all into the sdl scope via use sdl;, use sdl = sdl::video;, and so on. Then, SDL_CreateWindow becomes sdl::create_window rather than video::create_window. Satellite libraries should be imported with their prefix, use img = sdl::image;.
  • Deprecated and "don't use this" functions are omitted.
  • Functions with Hare standard library equivalents are omitted.
  • The SDL_Event tagged union is now a Hare tagged union.
  • Functions that use SDL_RWops now use io::handle.

This wrapper is still a work-in-progress! If it is missing something that you need, please feel free to email me.