Calling SDL2 Functions in Julia

I am trying to call some SDL2 functions from the SDL2.dll in Julia, but I keep getting unable to load error. I’m not sure what I’m doing wrong. I followed the help files in the calling C and Fortran help files.

#flags
const SDL_INIT_TIMER = 1
const SDL_INIT_AUDIO = 16
const SDL_INIT_VIDEO = 32
const SDL_INIT_JOYSTICK = 512
const SDL_INIT_HAPTIC = 4096
const SDL_INIT_GAMECONTROLLER = 8192
const SDL_INIT_EVENTS = 16384
const SDL_INIT_NOPARACHUTE = 1048576

sdl2 = dlopen("SDL2.dll")

SDL_Init = ccall(:SDL_Init,"sdl2"), UInt32,(UInt32), flags)

What example did you follow? ccall doesn’t have a signature like this, and I can’t even recognize what you are trying to do. What do you think each argument to ccall means in your code?

1 Like

I followed the example on the CCall example from the help pages. I changed it, but I still get the same error. SDL_Init is an unsigned int var that has another unsigned int for its flags.

Well, there are still at least 3 syntactic error here that don’t depend on ccall:

  • parentheses are not balanced
  • (UInt32) is not a tuple, (UInt32,) is
  • variable flags is never defined

There may also be 2 logical errors:

  • for ccall((:func, "lib"), ...) to work “lib” should be on your loading path; if it’s not, you should use ccall((:func, "/full/path/to/lib"), ...) instead, otherwise ccall just won’t know where to look for your function
  • according to SDL2 docs, SDL_Init returns int in C and thus Cint in Julia, which is an alias to Int32, not UInt32.

I have a semi-functional wrap of SDL2 that I made using Clang.jl (that automatically write all the function signatures from the C headers), I can make a repository if you are interested, it’s easier than trying to wrap things by hand.

1 Like

Did you have a look at GLFW?! At least when i was deciding for a windowing library, i decided for GLFW for it’s active development and nicely wrapped Julia library. Its Julia wrapper lives in GLFW.jl!
Ignore this, when you actually rather need the wrappers for sound etc, which isn’t part of GLFW.

@dfdx
I see. I think I am getting a better understanding of how to use the ccall function.

@jonathanBieler
That would be nice. I tried searching to see if there was already a SDL2 wrapped for Julia, but I didn’t see any.

GLFW is a great library, but the SDL2 has many more functions and is a great multimedia library.