Sunday, 13 January 2013

The Monster Chase game

This is the first game I wrote by myself since I entered this EAE:MGS program. This is the game I build from scratch using C++ programming language. I had already started programming this small game last semester for the Introduction to C++ for Game Programming class. However I didn't post anything about it last year because this game wasn't interesting at all. It was just a simple text based program. No display - just text input and output. This semester I decided to start posting about it because it start getting interesting. ^__^

Oh, before I forget, this is the new Monster Chase screen shot:

New Monster Chase interface

Yes, it is still in preliminary phase. What can you expect? We are still learning writing our own game engine. ^__^

Last semester we mainly learnt the basic C++ (more than beginner):
  • const correctness: using const for function function input parameter that we are not going to modify. Also use const to get a value of class private member. IMPORTANT: const object of a class can't be called unless the member function also declared const. If the function member of a class doesn't change the value of the private or public member of the class, it's better to declare the function as const.
  • passing variable to function by reference: passing function parameter by reference is much cheaper that by value. It won't create a new copy of the variable in memory. However, there are times we want to pass by value. For example if we want to modify the value but not to change the original value.
  • "using namespace" correctness: using the keyword at function scope
  • warning free: warning should be treated as an error because it is a possible error
  • forward declaration: remove #include if it is possible. #include is expensive because it copies the whole file into the file. Forward declaration can be used only if current file (or class) doesn't need to know the size of the included class size or using the function of the included class. That is if we declare the included class as a pointer.
  • float point comparison: float point is a tricky issue in programming. Because computer cannot precisely calculate the value of a float, comparing two float number can be tricky. Therefore we need to be careful when comparing two float number. This is a nice article about float number comparison http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm
  • inline: inline member function of a class if it is beneficial and appropriate. When inline is used, the whole function will be copied to the caller. This way execution time is reduced (function calls involve execution time overhead). However, inline should be used only with small and frequency used functions. Too much inline will burn out compiler.
  • continue virtualizing destructors in inheritance chains
  • optimize structure/class size and aligned size by arrange the member of structure/class from largest to smallest
  • memory leak: all variables created by new, malloc, or aligned_malloc MUST be deleted manually!
  • global variable is created before main class. Therefore if the global variable uses memory which hasn't been created, system will crashed.
  • static variable of a function is treated by compiler as static global except it has function scope.
  • new creates a big chunk of memory therefore avoid using new as much as possible.

No comments:

Post a Comment