HACKER Q&A
📣 doesntmeananyth

How to create a simple 2D game in C?


I'm interested in learning more about graphics programming and getting into some lower level coding than I'm usually doing, so I want to start by making some simple 2D games in C.

I'm particularly curious about how someone experienced in the field would approach this task using simpler tools, and that's why I'm looking at a fairly simple language like C (compared to something like C++) and doing only 2D for now.

Can you recommend some good learning resources that can help get me started?


  👤 kevindamm Accepted Answer ✓
Sometimes the C solution is made more complex because of its limitations w.r.t. what is included in the standard library and semantics such as RAII. And you can write C++ without using more than 10-15% of the language's features, so the language's potential complexity doesn't force program complexity.

That said, either choice is fine, you will likely benefit from choosing C or C++. In both cases, I would recommend SDL as a library for accessing basic graphics rendering and file loading functionality, as well as other game features like controllers and other input devices. It is platform dependent and has been around for quite some time.

An alternative useful library/framework (mostly for how widely it has been used in existing projects), is Tcl/Tk. It's not the simplest in design but it is quite capable and doesn't try to do too much more than provide 2D rendering, UI toolkit and basic input.


👤 Leftium
https://handmadehero.org/

It's very low level C (almost no third party libraries and using only emacs.)

Casey Muratori is quite experienced and codes everything live. So you can see his thinking process, including debugging bugs. (All recordings freely available)

Source code (plus the game) available for only $15

It started out writing to raw 2D graphics buffers, but somewhere it evolved into a pseudo-3D project.


👤 Dutchie987
Take a look at SFML. Very easy to get started, and quite capable.

https://www.sfml-dev.org/


👤 ekimehtor
It may be a good idea to leverage GPT these days. I asked Microsoft copilot to create a Python sketch of a simple Tetris Game and it spit it right out. I'm not suggesting you just leave it to GPT to write your code but there's no harm in having it build an example that you can then use as a framework.

👤 t312227
imho. (!) ...

don't get me wrong: i'm an old C fan, but why not C++!?

i think especially games work well with the object-oriented paradigm.

and i would use at least some standard graphics-library, for example something "basic" like openGL - yes, you mentioned, you just want to do 2D ... but getting to know openGL wouldn't hurt and its also pretty useful for 2D ;))

just my 0.02€