Kicking Dependencies

I’ve been relatively happy with LuaJit, and using GLFW as a basic UI framework piece has made things relativelly easy.  But still, it’s just one more piece of code that I have to compile and include in a release.  I don’t own that piece of code, so I have to make sure the licensing is right, and the code is kept up to date, etc.

The great benefit is that it’s cross platform.  But, the downside is a 46K glfw.dll file that I have to carry around.  Well, what is GLFW in the first place?  It gives me a nice easy way to get a window, with a OpenGL device context already made and ready to go.  It also handles mouse and keyboard actions in a coherent way.  Besides that, it does a little bit of text handling, and can load Targa images.  By design, that’s about all it does.

For the most part, it’s wrapping functions that are already in the underlying OS.  So, I thought, why not just write those wrappers in Lua itself?  I’m already doing wrappers for GDI calls, so User32 is not any harder.  It was kicking my behind though, and I was having problems getting CreateWindowEx to work correctly.  Here’s one of those places where the “magic of Windows” kind of screws with you.  On Windows, when you make a call like ‘CreateWindowEx’, you don’t actually call a function by that name.  The wisdom of Windows will determine whether you need to be calling the ASCII version (CreateWindowA) or the Unicode version (CreateWindowW).  As long as you’re using Microsoft environments, this magic is figured out automatically.  When you’re on the frontier, doing your own interop, you have to be explicit about what you’re calling.  Well, I finally got all that straightened out, and I can finally put a window up on the screen:

Now that I can actually display a window, it’s a small step to be able to create my own GL context within that Window as well, all from Lua.  The amount of wrapper code can be counted in the hundreds of lines of Lua code.  The FFI of LuaJit makes this a relatively painless process.  Just grab sections out of a header file, put them into a ffi.cdef[[]] block, and start calling your functions as usual.

Having used this LuaJit FFI stuff, I really can’t see using any other mechanism.  The previous forms of interop to C from Lua were very challenging, and I’m very relieved that FFI reduces the challenge to simple copy/paste.

At any rate, that’s a savings of 46K for the release.  Now I can go spend that on some other new features.

 



Leave a comment