Learning Graphics Programming in C
Tags: cprogramming, sdl, raycasting
Published: 2024-04-09 09:19:19

## Introduction

The TL;DR version of this post is that I worked on a sole project in `C` using the `SDL` library for graphics development to create a _**DOOM**_ style-ish `raycast` game...or more preciously, something tending towards that!

## Why This Project

Knowing how to program is one thing, but applying that programming knowledge is another altogether. This project was an attempted foray into the world of graphics programming to find out what it takes to be able to display graphics on screen. Being a sole project, I got to keep all the fun to myself; and by fun, I mean the sheer self-inflicted pain and torture I got from picking the wrong poison.
Be that as it may, it was not all bad news, as I can most certainly say I came out of the experience with a bit of a better grasp on some `C` concepts, such as passing pointers and working with structs. So then, this project was mostly for the benefit of my own learning.

## Accomplishments

![Project Flow Illustration](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kyr44sp6zhqpmtsdwxd6.png)

At the end of the project, I had a single level game environment that my character (i.e. the camera) could move around in, using input from both the keyboard and mouse.
Being a project with predefined constraints, most all of the technology choices and implementation strategies were already chosen for me. The `SDL` library, a multimedia API library, was used to do the windowing and for handling input from the user with more ease than if I had to write the lower level communication implementations with the hardware myself (a task I would probably have given-up on). Also being that the library is written in `C`, it presented an obvious pairing with the `C` programming language, where only a few header file includes and compilation flags were needed to make use of its functionality.
For the `raycasting` step, a line drawing algorithm by the name _Digital Differential Analysis (DDA)_ was implemented. This would take the simple 2D map and render it as a 3D version of the same, to create the impression of a space with depth (z-axis) added to it. For more on this step, here is a helpful [tutorial](https://lodev.org/cgtutor/raycasting.html).

## The Hard Parts

Being new to graphics programming, and programming in general, everything felt hard. Let's start with `C`.
I had only done a few practice projects in the programming language leading up to this point, and so my grasp on some of the concepts was still quite lose. Therefore having to do the entire project in the programming language felt like an uphill task, one that I powered through with just mounds and mounds of googling and tutorials.
This was actually the theme throughout the project, as I was also new to graphics programming, and was only hearing about `SDL` and `raycasting` for the first time coming into this. I had a tonne of questions, like..."What's a SDL library and why do I need it?", "Why's the DDA algorithm not also a library I can import and now instead I have to do maths?", "Why did I opt for this project in the first place knowing it's probably booby-trapped?", "Why's my hairline suddenly receding?", "Please help!"
Thankfully though, it eventually started to click, and the result was a semi-finalized game level, as can be seen below. Not much, but its something.

![Game Level Movement - GIF](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k0syxnnq1eq2vofue3kc.gif)

## Conclusion

While having a great idea one would like to see come to life is awesome, I have come to see the importance of properly scoping a project within realistic constraints to avoid biting off more than one can chew, or even getting disappointed altogether and giving up as the final desired outcome is not forthcoming as quickly as initially hoped for. Breaking up the grand idea into manageable chunks to deal with it piece by piece could probably help with morale as one can see the "progress bar" gradually inching forward, as compared to waiting to get to the end result to feel like progress has been made.
I have also gained a bit more confidence in programming in C, and I hope I can really push this to proficient levels. Graphics programming however, I might put a pause on for now until I can get to an appreciable level in my programming journey. That said, I am encouraged by the fact that my skills are improving, and the anxiety felt prior that I would probably never learn these things has, to my relief, greatly subsided.

**About Author**

Hey! Thanks for getting this far. A little about me; I am fairly new to the programming scene, and I look forward to experiencing as much of it as is possible through engaging in gainful projects and participating in various dev community activities. You can reach me via:

[LinkedIn](https://www.linkedin.com/in/leroy-mzughula/), [X/ Twitter](https://twitter.com/Mzughula-LeRoy)

**Here's links to the project:**

[Project Landing Page](https://leroy-m.github.io/maze-project/), [Project on GitHub](https://github.com/LeRoy-M/maze-project)