Id software programming Specification

Source: Internet
Author: User

I read the beauty of the article code a few days ago-appreciation of doom3 source code, the idtech4 engine coding standard mentioned in it, I think it is very good, but unfortunately it is all in English, here for convenience, translate it into Chinese. The translation is as follows:

General Specification

Use the tab key equivalent to four spaces to replace the Space key.

Use {} In (if, else, function, struct, typedefs, class definition, etc {}:

if ( x ) {}

The starting line of the else statement is the same as that of the IF statement:

if ( x ) {} else {}

Expressions with parentheses separated by spaces:

Replace: If (x) {} with: x = (y * 0.5f); Replace: x = (y * 0.5f );

For floating point values, unless the display description is used for double, values are assigned in exact floating point mode:

Use: Float F = 0.5f; Replace: Float F = 0.5; Use: Float F = 1.0f; Replace: Float F = 1.f;

Function names start with uppercase letters:

void Function( void );

A function name composed of multiple words. The first letter of each word is capitalized:

void ThisFunctionDoesSomething( void );

Standard Header of Function Description:

/*====================FunctionName  Description====================*/

Variable names start with lowercase letters:

float x;

A variable composed of multiple words. The first word starts with a lower-case letter, and the subsequent variables start with an upper-case letter:

float maxDistanceFromPlane;

When typedef is a custom type, use the same naming rules as variables, but add "_ t" as the Suffix:

typedef int fileHandle_t;

Struct type definitions use the same naming rules as variable definitions, but add "_ t" as the Suffix:

struct renderEntity_t;

The enum type definition uses the same naming rule as the variable definition, but it must be suffixed with "_ t. All constants in the Enum structure are in uppercase. If multiple words are contained, separate them with underscores:

enum contact_t {CONTACT_NONE,CONTACT_EDGE,CONTACT_MODELVERTEX,CONTACT_TRMVERTEX};

Add the suffix "-R" to the recursive function name:

void WalkBSP_r( int node );

Define defines that all uppercase letters are used. When multiple words are included, they are separated by underscores:

#define SIDE_FRONT0

Use the const keyword as much as possible:

Use: const int * P; // pointer to const intint * const P; // const pointer to intconst int * const P; // do not use const pointer to const INT: int const * P;

Class Definition Specification

Standard Header of class description:

/*===============================================================================Description===============================================================================*/

The class names start with "ID", and all subsequent words start with an uppercase letter:

class idVec3;

The class variable naming rules are the same as those in general rules:

class idVec3 {    floatx;    floaty;    floatz;}

The class method name follows the same rule as the function name in general rules:

class idVec3 {    floatLength( void ) const;}

Class variables and Methods Adopt a more elegant approach. The variable type and function return value type are in the first column, and the variable name and method name are in the second column:

class idVec3 {    floatx;    floaty;    floatz;    floatLength( void ) const;    const float *ToFloatPtr( void ) const;}

Place the * Number of the pointer in the first column, which facilitates the readability of the program.

The writing sequence of class variables and methods is as follows:

1.list of friend classes2.public variables3.public methods4.protected variables5.protected methods6.private variables7.private methods

In this way, the public interface is easily discovered at the beginning.

When the class method does not change the class member variable, you must make it a "const" method.

Avoid using "const_cast ".

The "const" object is returned unless the object needs to be modified.

In most cases, avoid using function overloading, such:

Use: const idanim * getanimbyindex (INT index) const; const idanim * getanimbyname (const char * Name) const; const idanim * getrandomanim (float randomdiversity) const; replace: const idanim * getanim (INT index) const; const idanim * getanim (const char * Name) const; const idanim * getanim (float randomdiversity) const;


Id studio naming rules

id<name>Dlg      // dialog classid<name>Ctrl     // dialog control classid<name>Frm      // frame windowid<name>View     // view windowid<name>         // any other class

File naming rules
Each class should be located in an independent source file, unless it is more meaningful after being divided into several small classes.

The file life should be the same as the class name removing the "ID" Prefix:

class idWinding;files:Winding.cppWinding.h

When a class is divided into multiple files, the names of these files start with the class name removing the "ID" prefix, followed by underscores, followed by the subset Name:

class idRenderWorld;files:RenderWorld_load.cppRenderWorld_demo.cppRenderWorld_portals.cpp

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.