Jump to content

c programming conventions


cassius
 Share

Recommended Posts

Hi

I am returning to c after a long absence and would like a few tips on how best to code.

 

How much of my actual gameplay code should go in main;

At the moment I have all functions in an include file and it works ok;

So am I on the right lines?

Thanks.

amd quad core 4 ghz / geforce 660 ti 2gb / win 10

Blender,gimp,silo2,ac3d,,audacity,Hexagon / using c++

Link to comment
Share on other sites

I think there is no strict rule here. Make as you comfortable to work with.

 

I would try to keep main() as simple as possible. Something like this (pseudo):

void main()

{

InitGraphics();

while(!AppTerminate())

{

UpdateWorld();

UpdatePhysics();

Render();

}

}

 

Considering files, you can split your code as you want.

 

It's better to split functions to different files if:

- you want to group similar functions in "class" manner;

- you want to reduce compile time changing little parts of code;

- you want to work with teammates on your code and/or use VCS (version control system) with file locking;

- you want to separate rarely modified code from often modified;

- and many other reasons.

 

Otherwise you can hold all your functions in one big file. I use this approach while project is tiny. But since it's grown I make refactoring and split functions to different files.

Link to comment
Share on other sites

It makes sense to keep the main file as small as possible, because everything you put in there, can not be directly included into other projects. Try to keep all reusable (somewhat standalone) code in their own .cpp and .h files, so you can choose which features you want in your other projects.

  • Upvote 3

Ryzen 9 RX 6800M ■ 16GB XF8 Windows 11 ■
Ultra ■ LE 2.53DWS 5.6  Reaper ■ C/C++ C# ■ Fortran 2008 ■ Story ■
■ Homepage: https://canardia.com ■

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...