• 2D shoot 'em up
SDL2 Rogue tutorial
SDL2 Gunner tutorial
SDL2 Shooter 2 tutorial
SDL2 Widget tutorial
SDL2 Adventure tutorial
— Creating a basic widget system —
In the first part, we looked at how to create a bunch of widgets for a menu. In this part, we'll look at actually attaching actions to the menu items, so that they can be invoked.
Extract the archive, run make, and then use ./widgets02 to run the code. You will see a window open like the one above. Use the Up and Down arrows on you keyboard to change the highlighted menu option. Press Space or Return on the highlighted menu option to action it. When you're done, either select Exit or close the window.
Inspecting the code
Making our widget actionable is an easy task than you might think. To achieve it, we're going to make use of function pointers. Starting with structs.h, where we've updated our Widget struct:
We've added in a function pointer called "action". If we now look at widgets.c, we can see it in use. We've updated the doWidgets function to handle pressing Space or Return.
Now, when we press Space or Return, the activeWidget's action will be called (so long as it's not null). What the action does will depend on the function we've assigned to it. We do all this in demo.c, where we setup our widgets. Turning to initDemo:
For each widget we create, we're also assigning an action function (w->action = ...). For the start widget, we're assigning a function called start; for load, an action called load, etc. Note that when we come to the exit widget, our function is called quit, as exit is already defined and we don't want to override it.
All of our functions are defined in demo.c. Each is very short. Starting with start:
The start function sets the value of message (a char array pointer). As does load:
The same with options:
And with credits:
Quit, on the other hand, does something different, but exactly what you might expect:
When quit is invoked, the application terminates, with a call to exit. This is partly why our "exit" widget's function is called quit, so we can exit.
Finally, we want to update our draw function, so that our message is displayed:
We need only add one line, a called to drawText, so that our message is presented.
That's it for invoking our widgets. This might not look like much on the surface, but what we've actually done is allowed our widgets to perform anything we desire in our code. Afterall, it's just calling a function, which we can define to do whatever we want. In the tutorials to follow, we'll see plenty of examples of how we can leverage this.
The source code for all parts of this tutorial (including assets) is available here:
It is also available as part of the SDL2 tutorial bundle (with on-going updates):
If you do not wish to create an itch.io account, you can also purchase the tutorial bundle using PayPal. This method will be slower, however, as it will require manual verification of the transaction.
Share your comments and thoughts below. All comments are anonymous and cannot be edited.