PC Games
• Orb Tutorials
• 2D shoot 'em up Latest Updates
SDL2 Versus game tutorial
Download keys for SDL2 tutorials on itch.io
The Legend of Edgar 1.37
SDL2 Santa game tutorial 🎅
SDL2 Shooter 3 tutorial
Tags • android (3) • battle-for-the-solar-system (10) • blob-wars (10) • brexit (1) • code (6) • edgar (9) • games (43) • lasagne-monsters (1) • making-of (5) • match3 (1) • numberblocksonline (1) • orb (2) • site (1) • tanx (4) • three-guys (3) • three-guys-apocalypse (3) • tutorials (17) • water-closet (4) Books The Attribute of the Strong (Battle for the Solar System, #3) The Pandoran War is nearing its end... and the Senate's Mistake have all but won. Leaving a galaxy in ruin behind them, they set their sights on Sol and prepare to finish their twelve year Mission. All seems lost. But in the final forty-eight hours, while hunting for the elusive Zackaria, the White Knights make a discovery in the former Mitikas Empire that could herald one last chance at victory. |
— Creating a basic widget system — Note: this tutorial assumes knowledge of C, as well as prior tutorials.
Introduction 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 cmake CMakeLists.txt, followed by 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. Purchase The source code for all parts of this tutorial (including assets) is available for purchase: From itch.io It is also available as part of the SDL2 tutorial bundle: |