My experience with SDL2 and game development, part 1
In my last post I mentioned that I have been working around with SDL2 and a game for PC and Android. Here I will talk about my experiences with this library and show you what the game looks like.
Simple DirectMedia Layer, or SDL for short, is a low-level audio, keyboard, mouse, touchscreen and graphics library aimed at creating low-level (mostly 2D) games on various platforms.
When we do something related to homebrews, be it games, menus or other types, on the PSP, we usually first choose a graphics library, or for those writing it in LUA, we choose a LUA interpreter with our preferred library.
The most common PSP graphics library out there is simply called graphics.c (or graphics.h). It was one of the first and oldest libraries out there and still used by some homebrews and menus (like the famous 138Menu by my friend GBOT). Another famous homebrew, Wagic, and famous menu, yMenu, use the JGE++ library, who was coded by, I don’t know, and is available on the PSP, PC and smartphones (iOS, Android, others?). The last homebrew I coded for the PSP was arkMenu, which uses a new graphics library created by xerpi called ya2dlib. The reason I used ya2d was because it was fast, powerful and I am a friend of xerpi (we are both Spaniards) so I was in constant contact with him asking him to improve it or writing new features to it to fit arkMenu.
All this made me get interested in writing a completely new game from scratch for PC and Android, and just like I would do on the PSP, I first had to choose the development environment and graphics library.
When it comes to gaming today most people will tell you to write your game using Unity or other fancy IDE’s like Visual Studio. But there’s a problem: these IDE’s make small games consume a shitload of resources, as well as requiring modern graphics cards and requiring a powerful PC to work with, which I do not have at hand (I do all my coding on an intel atom netbook from 2010 with very basic intel integrated graphics card that barely does 3D). All this said: I needed to do all the coding by hand, like a damn pro, and use a low-level library, like I would do on the PSP. After researching for 5 minutes (most of which was spent on loading firefox itself) I came across SDL2 and a bunch of tutorials about it.
SDL2 provides a BIG improvement from the first SDL: it now has hardware acceleration for 2D rendering, amongst other things, but this is the most important. SDL2 has everything that I needed to make a simple game: graphics, TrueType Fonts, sound, controller (keyboard, mouse, touchscreen, etc) so it was the perfect choice. That said, I started by doing what I always do when I migrate to a new graphics library: write wrappers for the screen, images, sound and other library-specific code so I don’t have to worry about remembering a new API. This is pretty straightforward and requires very little effort and has very little impact on performance.
Once that was done, I proceeded to writing a sample. I created a small background with a static character moving around depending on which keyboard arrow you pressed, and it teleported to the coordinates of the mouse if you clicked on screen. Believe it or not, this small and useless “code” gets you familiar with the library and in my case, it served as inspiration for the game I ended up writing.
After this, I worked on this “test” by adding a few more things like character animation, a local greedy best first algortithm for path finding (this is actually a bad choice, a global best A* algorithm would be ideal but I’m too lazy to change it now).
Alas I knew the “test” was starting to look more like a game and less like a test, but what kind of game? That’s where inspiration took over: I had the idea to write a game where you have to cross over a minefield (where the mines are not visible of course) using a wide variety of weapons and items that you can randomly find. The areas of the game are randomly generated, with the amount of hidden mines increasing depending on the difficulty level. This is actually the easiest way to code how a game difficulty works: using probability. In other words, the highest the difficulty, the more probability of there being mines and less probability of there being items or “safe spots”. In statistics, probability goes from 0 (which means “impossible occurance”, it can never happen) to 1 (which means “needed occurance”, it will always happen), but floating point data takes up between 32 and 64 bits, if we interpolate it to percentage (between 0 and 100) we can achieve the same thing and only require an unsigned char (which takes up just 8 bits). In game programming, we wanna be as efficient as possible. Once you have a percentage, you can just use the rand() function from the standard C library, like this:
srand(time(NULL)); // initialize the random number generator
int r = rand()%100; //we obtain a random number from 0 to 99
if (r<probability)
// add something to the map, or whatever
Other things like the game’s map are represented by a two-dimensional array (aka a matrix) of cells that each hold a background floor (which can have an invible mine, a visible mine, a rock/lake/blockade, a safe spot, etc) and a doubly linked list that contains references to Objects, these Objects can be characters the player controls, items the player can pick up, or a cow, which works like both of them. Due to the game being written in an OOP manner (with only a few aspects of it using simple namespaces) all items and weapons inherit from the character class and share many of the same functionality, using inheritance and all that OOP *** that I will not talk much about in here. Did I mention I do all of this using a preprocessor I myself wrote that converts classes to plain C structs? Yeah I think I did, but whatever I just like to brag about some of the stuff I make when I’m inspired.
The game generates maps randomly depending on the difficulty, so now all I had to do was code a simple menu to change difficulties, nothing really big, and of course a high-level scoring system. Having high scores means also having savedatas, and having savedatas also means encrypting them, at least a bit, to just prevent casual script kiddies from modifying the saves and giving themselves a big *** score. The simplest and easiest form of encryption anyone can think of is by xoring the data with a key.
When we xor value A with value B we get a resulting value C, if we xor this value C with value B we get value A, when xoring value C with value A we get value B. In a more “mathematical” way:
A XOR B = C
C XOR B = A
C XOR A = B
We can use this to create a key (value B), xor the savedata (value A) with it and obtain an encrypted savedata (value C). Then to load we just xor the encrypted savedata (value C) with the key (value B) and we obtain the original savedata (value A).
This is of course the oldest and simplest form of encryption and is EXTREMELY easy to bypass, but we are not talking about a critical application here, we’re talking about yet-another mobile game, and giving yourself a superhigh score is only lying to yourself, so I really don’t give a ***. BTW the key I use for my game is an easter egg.
Well that’s enough for now, on part 2 I will talk about my experience porting the game to Android and I will (finally) show some screenshots of the game.
You guys ROKK !!! 😉
Nice work mate.. Any chance of getting it on IOS tho?
An iOS developer license costs about 4 times more than Android. Even if I want to approach this game the same way I approach homebrews, even if I want it to be free, I will be forced to place ads on the Android version and if an iOS version ever gets made, I will most likely be forced to make it a buck. Interestingly enough a Windows Phone developer license is free, but they have pretty weird and stupid rules about design choices (they basically force Metro down your throat, if your app is not Metro-friendly they deny it).
Cydia? Do all Cydia apps use this developer license?
Yes, you need to pay for a license. Might aw sell just write a webApp (A website in a webviewer Activity). Also, it is not possible to compile an iOS app on any machine other than an Apple PC. At work, we pay 5000$ per developer per year to use WinDev Mobile to make Android/iPhone apps, but we still need a standalone Mac to compile the solution (Android works out-of-the-box).
It is 100% free to play around on Android, you can have fun creating solutions, installing them on your phone, share them with your friends, etc. (You only pay to publish, not develop) On iOS, you cannot even compile a tutorial if you don’t have your license installed, so you need to pay to test it out…
I would love to test either PC or android versions when available
Credits:
——–
The JGE++ Team is:
– James Hui (a.k.a. Dr.Watson)
– Sijiu Duan (a.k.a. Chi80)
Special thanks to:
——————
– Aeolusc (Character encoding conversion code)
– Cheese (WAV playback code)
– Cooleyes (MP3 hardware decoder)
– Fan990099
– Firenonsuch
– Newcreat
– Subelf
– Youmentang
– Jasmine
This is from the readme on the JGE++ repository, so it is certainly out of date and probably incomplete for current versions.
The Wagic team basically was maintaining JGE++ after its original author abandoned it.
Among other things, we added support for Linux, iOS and Android to JGE++. Initially it only supported Windows and the PSP
Is there a repository where I can find an updated version? Using Google, I only found the original XDL on code.google.com.
XDL –> SDK
Interesting read, thanks Acid_snake! Looking forward to part 2
wow dud you are great
Perhaps the best game to try and make is 8 bit style game with a 3d world filled with fascinating possibility. Most developers i know are stuck in the engine that provides too much polygon count and is strictly longer to make. I like how playmore let snk create a game like the king of fighters because it has the style i am referring to but it really is stage 1 of what i mean. As we get more updating technology it will be harder to stick with one engine development system. Especially after you work really hard on it. Best to just see what console architecture offers to get an idea of gaming development before anything even if android or ios may not have its greatness.
I wish you luck AC.
This is awesome
@Acid_Snake
Nice article, I wrote my first game in pure C (not C++) using the graphics API Allegro 5, it’s really easy to use once you are comfortable with C and it’s neatly documented, it also has keyboard, screen, mouse, and I think it also has android support but don’t quote me on that.
I have a question, though, you said you wrote wrappers so you don’t have to remember the APIs function calls, doesn’t that affect performance? I mean, I heard of something called function overhead in C, and that basically means that you can consume all the stack memory if you call a lot of functions within functions?
That’s the reason when I used allegro I only wrote a wrapper to check for errors and store them in a log file. Any way, your game sounds fun 😉
The overhead that wrappers produce is not that big, worst things are done in other places.
Great article.
Is there any chances to port this lib based on SDL 1.2 to PSP ? It would be great.
https://github.com/Ousret/EasySDL
SDL, at least SDL2, has already been ported to the PSP.
http://ng.org.ua/index.php/mediafiles/video/kontsertnye-programmy/item/69-nash-bog-vsekh-bolshe/69-nash-bog-vsekh-bolshe?pc=conduit&