top of page
Search

[60] In a Bind

  • Writer: Conlan Walker
    Conlan Walker
  • Dec 9, 2022
  • 2 min read

This one may or may not be more terse than the last, as I spent more time fixing bugs and finding answers to some questions than I would usually spend actually writing this post.

My goal this week was to put in place everything I'd need to bind a C function to Lua, within a table of tables, before binding at least 1 actual function.


The basic idea is that for a global table called "SDL", there are different categories of functions, structs, constants, et cetera. For example, the first function I've bound, "SDL_CreateWindow", is bound as "SDL.Window.CreateWindow". "Window" is one of the aforementioned categories, with CreateWindow simply being a method within it.


For automatically binding things of a certain category/subcategory, I have 2 arrays and 1 int associated with it. One array contains the actual content of the category, another contains the names for each member of the category as viewed from Lua. The int simply says how many members there are. These 3 would be fed through a for loop that automatically binds them.

ree

Here are the contents of CreateWindow. I've taken a fair bit of code from a repository I found that provides Lua bindings for SDL, though it's not for Lua 5.4, and there's some structural decisions of the library I disagree with, at least in the context of this game.

test

Some of the functions being used within CreateWindow

here are not pure Lua C API, rather they too are utility functions that perform what should be simple tasks, like telling if the first element on the C stack is a certain type or not.






I haven't actually implemented that for loop system for automatically binding functions within a category, so for this example I'm doing it manually, and just for CreateWindow.

ree

Now on the Lua side of things, here's how CreateWindow is actually called in Lua.

Here, I'm setting the window's title to "Cool Window!", and setting the dimensions to 640x480.

I'm also centering the window's vertical position, while setting its horizontal position to be -50 pixels relative to the left side of the screen.

ree

Finally, here's the output of all of this.

The window is all of the things I set it to be, and the print() worked correctly too, which is what the green text at the top is.

ree

And that's it. I'm aiming to get most of the boilerplate binding sort of stuff solved before next year, but I've been bad at setting goals for a given time span accurately in the past.

 
 
 

Comments


bottom of page