Skip to main content
gamedev.net
🔒 Locked

How do i decrease the lines of code in the main.c *please help*

Started by MindWipe Oct 27, 2000 at 5:16 AM 1 replies 200+ views
Original Post
MindWipe
MindWipe
This is something that have trubled me a long time. In example in OpenGl, draing a cube takes lotsa rows of code. How can I put this in one file, so that I can only write: DrawCube(x,y,z); Or the AI, how do I do this. So that I can only write: CheckAI(unitnr); or something like that. I know many people have losta C-files. But until now I have only had one, and as you might understand, that file have been a really long one. Please help, MindWipe
"To some its a six-pack, to me it's a support group."
Quantum
Quantum
well, its easy
you just create more .c files, and put the function definitions in there
but make sure you have a corresponding .h file for each .c file, which contains the prototypes for the functions
then any .c file which needs to call the functions just has to #include the file.
e.g:

    

// in main.c :


//give access to cube functions:


#include "cube.h"
void main()
{
// blah blah blah

DrawCube(10,10,10);
}
//--------


// in cube.h :


//always put these at the top to stop multiple inclusion

#ifndef _CUBE_H_
#define _CUBE_H_

//prototype for DrawCube():
void DrawCube(float x, float y, float z);

//end the _CUBE_H_ if statement

#endif

//------------


// in cube.c:


//implement the functions:


#include "cube.h"

void DrawCube(float x, float y, float z)
{
//blah..

}




thats for functions

if you want to use global variables, put this in the source file:
int variable; //or whatever..

and:
extern int variable; //or whatever..
in the .h file
then when other files include the h file, it tells them that they''ll find the definition of this variable in another file..


anyway, hope this helps
MindWipe
MindWipe
Thanks!
"To some its a six-pack, to me it's a support group."

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.