Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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?

Cheers

share|improve this question
8  
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? –  Anko Mar 18 '12 at 11:45
1  
C++ is just as fast, at least when you don't do anything stupid. –  Psykocyber Mar 18 '12 at 11:48
2  
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. –  Koarl Mar 18 '12 at 22:48
2  
Out of simple curiosity: do you really must/have to develop using only C? Why? –  Coyote Mar 18 '12 at 23:04
2  
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. –  linitbuff Mar 20 '12 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.

share|improve this answer

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.

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.