Tuesday 8 October 2013

This is why I want to be a TA for C++ Game Programming

Re-learn all things I had learnt before. This is exactly the reason why I really want to be the TA for C++ Game Programming class. The by-product of it is to re-post all my blog posts that I accidentally deleted. =P Also while helping the students finishing their assignment, I also learnt more about the topics covered in class.

So, in the past first half of the semester, we learnt about inline, const correctness, passing value by reference, float number, unit test, and assert. These good code practice are matter in game programming. It will improve the code efficiency and accuracy (float number comparison).

First a good code style guide! There is no such a perfect coding style. It's a style - means depends on personal preference. However, there is a good style that one can consider. This is an example: http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml. Well, at least I personally this is a nice guide. It covers some of the topic we discussed in C++ class. Well, for the rest of the topics covered in the first half of the semester, I will just put the main point.
  • inline: inline is a suggestion to a compiler to inline the function. Since inline doesn't generate the code (it just "copy-paste" the code into where it's called), it is highly suggested not to inline function that is complicated, iterative and longer than 10 lines. Also, although recent days compiler is smart enough, it is still better to put the inline function definition in the same file as the header file. Otherwise, the compiler might fire you an "unresolved external" error from linker. This site has a really nice FAQ about inline: http://www.parashift.com/c++-faq/inline-functions.html
  • const correctness: See this post =)
  • passing by reference: in general coding, we want to pass a parameter by reference as much as we can especially for class type object. Consider:
class Vector2
{
    float _x, _y;
public:
    Vector2( void ){ }
}
void Test1( const Vector2 &i_input ){ }
void Test2( const Vector2 i_input ){ }
Vector2 a;
Test1( a ); ====> when compiler compile this, Test1 function will be compiled directly
Test2( a ); ====> when compiler compile this, Vector2 constructor will be called to create const Vector2 i_input (or assignment operator if you have one), then compile the Test2
  • float number: float number cannot be represented precisely like integer in computer. By IEEE 754-1985, float number is represented with sign (1 bit) - exponent (8 bit) and fraction (23 bit) in 32-bit float and sign (1 bit) - exponent (11 bit) - fraction (52 bit) in 64-bit double. Again, this is a precision!
float a = 1.1f;
float b = 1.1f;
a = a * 0.2f;
b = b / 5.0f;
if( a == b )
    printf( "%f == %f\n", a, b );
else
    printf( "%f != %f???\n", a, b );
Which one do you think the compiler will print? The first one? Wrong! It is the second one (.022 != 0.22???)
  • unit test: the idea behind the unit test is to test the functionality of the functions we created. Are they working properly as they supposed to? Also this unit test must only be available in Debug build. What is this for in Release build?

No comments:

Post a Comment