SDL Setup
So let’s start with a quick explanation. I’ve done some research, and I’ve decided I need to commit to some larger project. I picked out my language, C. The reasons for this are simple: I like low-level, and I was hoping for more control over what I’m doing. Then I decided on a library. I needed something cross-platform; that narrowed down my decisions. I also wanted to develop a lot of the logic myself. You know, as an exercise. After a short trip to Google, SDL2 turned out to be the best choice (mobile support too!).
Initializing
First things first: make sure to include the library so that we have all of the functions defined!
So this was a quick thing to figure out, and I ended up making it it’s own function. The process for initializing an SDL2 window is simple, really.
According to the documentation, this returns 0
when successful, or some other
value when failed. running that check we get
And now we have SDL2 ready for us to create a Window!
Window
Now we insert this into out do stuff
above to create the Window that we can
display into. Reading the documentation
we see that we pass a name, position of the Window, size and some flags that
determine the properties of the Window. For simplicity, we can start with the
following:
Events
Alright. So we have a window, we have it initialized, now we just have to add an event loop and we’ll be able to see our Window! Events turn out to be necessary to show the Window, because by processing the events SDL knows to display the Window in the first place.
Now, looking up the documentation we find
This populates the event we pass a reference to, and returns a 1 if there was
an event. If there was no event, we get a 0. Conveniently works for a while
loop! We also have to keep in mind that we want to continue processing events
until we get an SDL_QUIT Event, which we handle as we see below:
Weaving Together
Now we have all the pieces necessary for a window to actually show up on our screen. Tying everything above together, you get something like:
To Sum Up
So after getting this all together, we have a window that we can draw too. Up next I’ll go over my adventures in drawing to the screen, some keyboard handling, and whatever else I compile from my more recent commits to the project I’m working on.