I'm having trouble with how to structure a game using c99. I've seen a few tutorials on making a game loop, but they are all done with c++ and classes. My main problem seems to be moving data around between the functions without creating a mess, and what stuff to put in what header files etc. Do I just do something similar to the c++ loops, and create a class-like header with a structure containing all items needed by more than one of the functions, along with the prototypes of said functions, and include the header in each function's header file? Then, in the main function, instantiate the structure and pass a pointer to it to every function in the loop?

Is this ok, or is there a better way to do it, and are there any good 'c' specific tutorials available?


  • 9
    \$\begingroup\$ Writing C is like riding a rocket to work. It'll be really fast, you'll learn all about mechanical engineering and you'll totally rake in the geek cred. On the other hand, it takes forever to build a rocket, when it breaks down it's really hard to figure out where the hell that segfault came from and a hastily built rocket may unexpectedly explode and shower the earth with little bits of you. Are you sure you should do it in C? \$\endgroup\$
    – Anko
    Mar 18, 2012 at 11:45
  • 1
    \$\begingroup\$ C++ is just as fast, at least when you don't do anything stupid. \$\endgroup\$ Mar 18, 2012 at 11:48
  • 2
    \$\begingroup\$ If you like to think in terms of object oriented programming, then having a struct for whatever would be a class in the tutorials you read and implementing functions that take a pointer to an instance of said struct as their first argument for whatever member functions you'd need would allow you to pretty much follow your tutorials verbatim. \$\endgroup\$
    – Koarl
    Mar 18, 2012 at 22:48
  • 2
    \$\begingroup\$ Out of simple curiosity: do you really must/have to develop using only C? Why? \$\endgroup\$
    – Coyote
    Mar 18, 2012 at 23:04
  • 4
    \$\begingroup\$ The reason I want to do it in C is because I like to know how to do things without using high level abstractions. When I have a decent grasp of this I may think about using c++. I think it helps to learn the hard way. \$\endgroup\$
    – linitbuff
    Mar 20, 2012 at 1:46

3 Answers 3


Fabian Sanglard has some excellent code reviews of id's C engines, such as his Quake 2 code review. It's a good reference for how a reasonably "modern" game was written in C.


Aside from the above answer, the common technique is to have a group of functions like draw and update that you can put in their own files and then call them from a loop in the main file. And to go deeper, you can make files for each state of your game like the main menu or the start screen, etc. and call those from the draw/update.


Depending on the type of game you are building you will have multiple ways to create you game loop.

A general approach would be to first create arrays of components (structs or arrays) your game will need. Then for each update calculate the delta time since the last update and run the appropriate functions for the right components.

Once you ran your input management, your game logics, world updates, animations and so on, you can either draw the scene or ignore drawing if your game runs faster than the FPS. Usually it's a good idea to draw on regular intervals and run the simulation independently of the graphics refresh rate.

You should study component based programming. It's not just a fad. And many people forget it's possible to do component based programming with standard C.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .