Visual problem solving with SDL2

I am assembling some projects in my Challenge Topics repository on GitHub (https://github.com/Rachels-Courses/Challenge-Topics). These are projects that I’ve written to show in class, asking the students to come up with a better algorithm to solve some problem.

Here are some examples:

Screenshot-Searching

With this one, a linear and a random search are implemented to search a sorted list. The challenge is to ask students how a better searching algorithm can be done for our sorted list, where we do searching more intelligently than just going from the beginning to the end.

You can also expand this to talk about creating a data structure that auto-sorts when items are inserted, and whether to implement this by sorting immediately after an insert, or do a smart-insert and look for the appropriate position for the new object.

 

Screenshot-Travelling Salesperson

For this one, a list of cities are generated at random points on a map. The random paths generator essentially adds paths between cities in the order that the cities were generated. The challenge here is to ask students how to build a more intelligent route, visiting all cities once, and returning home afterward.

So for example, in the above screenshot, the salesperson goes from Grandview to Leawood to Raymore, which is a bit nonsensical.

You can also demonstrate Dijkstra’s Algorithm as well.

 

Screenshot-Collision Detection

Another problem we can look at is detecting how two objects are colliding, such as in a game. There are multiple ways to handle it – for example, you could do bounding-box collision detection and check the perimeter of the images themselves, each edge. Pros and cons? Could be empty space colliding, such as the top-right of the bunny and bottom-left of the dog.

Another option is to use the distance formula and figure out what a good distance is before considering the two images colliding. This works well for circular objects, but might not work as well for an oblong object.

Or, you could do a combination of both, maybe each object has several bounding boxes within it (like the dog’s head, and dog’s torso).


 

The idea behind these exercises isn’t to necessarily get the “most efficient” algorithm to any given problem, but to have students brainstorm and actually think about the problem visually, instead of having everything be intangible. Having pure console-based applications using just cin and cout can be boring, and even when implementing something modelled on a real-life system, it can be hard to really get into it. Even many command-line programs use something like pdcurses to give more organization to the presentation of the program.

So by using the SDL library, we can still write C++ code and demonstrate some problems in a more graphical way. Even if SDL is not available on the students’ school computers, if the teacher has a laptop they can install SDL2 on their own and display it to the entire class for a more social lesson.

Learn more about SDL at: https://www.libsdl.org/

And my challenge repository is at: https://github.com/Rachels-Courses/Challenge-Topics

The Magic of SDL

For a while, it felt like C++ was falling behind in usefulness, when it came to indie game development. With my C++ games, I’d have to compile for Windows, Linux, and OSX (or rely on someone else to build for OSX for me), but games that could be played in a browser, or on a phone, had some advantage – easier to play and easily portable for Web, more ubiquitous marketplace for mobile.

Over the past two or three years I’ve experimented with game-dev using something other than C++. I wrote a few games with Java and LibGDX, I made some browser-based games with JavaScript and the HTML5 canvas, and I’ve published a few small applications using Lua and the Gideros framework.

Since then, SDL2 has come out. And, while Gideros has actually been a joy to use, Gideros does not currently offer support to export for Desktop, and I would like to be able to offer some of my apps for more than just Android.

Looking at SDL2, it looks like you can have it all now – Cross-platform PC, iOS, Android, and if you’re using something emscripten, you can even build your C++/SDL game into a JavaScript/Canvas project.

So, finally, it seems as if I could possibly have my cake and eat it, too.  When I’m programming for myself, I want it to be in a language I enjoy, and I want to be writing software and games that I care about – otherwise, why would I do it in my free time?

I am currently working on writing a framework on top of SDL for use with my PC/Mobile games – I will be reworking Pickin’ Sticks LXXIV, Dolphin & Kitten, and Fantazio de Esperanto to all be using this new framework, and they will be extended and, hopefully during the year, completed. I’m excited!