Skip to content

Instantly share code, notes, and snippets.

@pta2002
Created April 29, 2017 20:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pta2002/5241f19d93e3c6a27efa9dbf642a991e to your computer and use it in GitHub Desktop.
Save pta2002/5241f19d93e3c6a27efa9dbf642a991e to your computer and use it in GitHub Desktop.
require "./sdl"
LibSDL.init(LibSDL::INIT_EVERYTHING)
window = LibSDL.create_window("It works!", LibSDL::WINDOWPOS_UNDEFINED, LibSDL::WINDOWPOS_UNDEFINED, 800, 600, 0)
surface = LibSDL.get_window_surface(window)
_format = surface.value.format
LibSDL.fill_rect(surface, nil, LibSDL.map_rgb(pointerof(_format), 0xFF, 0xFF, 0xFF))
LibSDL.update_window_surface(window)
LibSDL.delay(2000)
# Crystal bindings of SDL2
@[Link("sdl2")]
lib LibSDL
type Window = Void
type BlitMap = Void
struct Rect
x : Int32
y : Int32
w : Int32
h : Int32
end
struct Color
r, g, b, a : UInt8
end
struct Palette
ncolors : UInt32
colors : Color*
version : UInt32
refcount : UInt32
end
struct PixelFormat
palette : Palette*
bitsperpixel : UInt8
bytesperpixel : UInt8
rmask : UInt32
gmask : UInt32
bmask : UInt32
amask : UInt32
rloss : UInt8
gloss : UInt8
bloss : UInt8
aloss : UInt8
rshift : UInt8
gshift : UInt8
bshift : UInt8
ashift : UInt8
refcount : Int32
next_pf : PixelFormat*
end
struct Surface
flags : UInt32
format : PixelFormat
w, h : Int32
pitch : Int32
pixels : Void*
usedata : Void*
locked : Int32
locked_data : Void*
clip_rect : Rect
map : BlitMap*
refcount : Int32
end
INIT_TIMER = 0x01_u32
INIT_AUDIO = 0x10_u32
INIT_VIDEO = 0x20_u32
INIT_JOYSTICK = 0x200_u32
INIT_HAPTIC = 0x1000_u32
INIT_GAMECONTROLLER = 0x2000_u32
INIT_EVENTS = 0x4000_u32
INIT_NOPARACHUTE = 0x10000_u32
INIT_EVERYTHING = 0xFFFF_u32
WINDOWPOS_CENTERED = 0x2FFF0000
WINDOWPOS_UNDEFINED = 0x1FFF0000
fun init = SDL_Init(flags : UInt32) : Int32
fun create_window = SDL_CreateWindow(title : LibC::Char*, x : Int32, y : Int32, w : Int32, h : Int32, flags : UInt32) : Window*
fun get_window_surface = SDL_GetWindowSurface(window : Window*) : Surface*
fun fill_rect = SDL_FillRect(dst : Surface*, rect : Rect*, color : UInt32) : Int32
fun map_rgb = SDL_MapRGB(format : PixelFormat*, r : UInt8, g : UInt8, b : UInt8) : UInt32
fun update_window_surface = SDL_UpdateWindowSurface(window : Window*)
fun delay = SDL_Delay(ms : UInt32)
fun get_error = SDL_GetError() : LibC::Char*
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment