Skip to content

rileylev/sdl2raii

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

41 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Example!

Here is an example emscripten/webassembly app made with this library. The corresponding source file can be found at this repo.

Thin?

minimal design changes

Design rules:

  • SDL_ => sdl::
  • IMG_ => sdl::img::
  • Owning types
    • owning types are provided for Surface, Texture, Window, and Renderer in the sdl::unique namespace.
    • these are unique pointers with the correct deleters supplied
    • Functions that create an SDL object and return a pointer (SDL_CreateWindow) return unique:: types.

    SDL_CreateWindow returns a sdl::unique::Window

  • no out parameters these are returned in a tuple or struct in the same order
  • when in parameters are pointers to allow a null value, provide an additional overload taking std::optional by value

Constants

  • these are either constants or old style enums (for or-ing together)
  • they are put in namespaces corresponding to the underscored prefixes in the SDL constant’s name
  • if they were named all uppercase, we downcase everything and keep the underscores otherwise casing is the same
  • SDL_WINDOW_FULLSCREEN => sdl::window::fullscreen

Edge cases

  • SDL_WINDOW_ constants live in namespace window_opts because there is already a Window type. Differing only by case is too tricky.
  • SDL_PollEvent is split into two functions:
    • HasNextEvent() which checks if the event queue has more events
    • NextEvent() which returns an optional containing the next event or nullopt if there are no more
  • SDL_WINDOWPOS_UNDEFINED => sdl::window::pos_undefined

Carrying out design rules

macros

  • documented in the horrible macros file
  • systematic changes are carried out through macros
  • macros are prefixed with SDLRAII_
  • private/implementation macros are postfixed with a trailing underscore
  • macros are undefined at the end of each file

Specific macros

SDLRAII_THE_PREFIX

  • this tells the preprocessor what prefix to append to get SDL’s name for something
  • examples
    • #define SDLRAII_THE_PREFIX SDL_ for things that start with SDL_
    • #define SDLRAII_THE_PREFIX IMG_ for things that start with IMG_

SDLRAII_PUT_PREFIX(X)

  • appends the aformentioned prefix to X

SDLRAII_DEFUNIQUE(name,destructor)

  • subclasses std::unique_ptr (publicly)
  • names it name
  • sets the deleter to destructor
  • sets the type to <prefix>_name

others

the rest provide a straightforward wrapping (either through using or a variadic template that forwards its arguments)

Dependencies

  • boost preprocessor
  • SDL2

About

A thin RAII and C++ aware wrapper for SDL2

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published