Here is Space invaders in C++/OpenGL. The main objective is of course to have a working game but more important the goal of this project is to develops somes features use in modern video game
Smart Pointers
A lot of object like aliens or player rockets are dynamically allocated , so on the heap. The difficulity of variable allocate on the heap is to manage the lifetime on the variable : When to allocate or when to delete, a small forgot of deallocation can mean a memory leak that will become bigger with time
With C++ 11, the language got smart pointers. Smart pointer are wrapper of a pointer variables which automatically manager lifetime of a pointer.
This project use unique_ptr (uniquer ownership) and shared_ptr( multiple ownership for an entity)
Event System
Entity objects have to communicate between them . A player rocket touch an alien entity or alien collides with player by example. To avoid that player class use enemy data, an event manager is useful to simulate interaction between objects.
Like an observer design pattern, player and alien subscribe as a listener on an event manager. When an "event" (rocket collision) is trigger, an event is create and add to the event manager which can after contact listener (in this case alien and rocket classes) to manager the event.
Pool Memory
Allocate object on the heap during runtime it's a bad thing due time to get memory adressy ,this can have negative performance of the game.
One solution is to allocated a finite number of rockets and aliens but this will limit the numbers of each entity
The pool memory is one of the solution. We can allocate a big chunk of memory and then divide this big chunk with defined block of memory.
When a rocket entity is need, we pull block memory from the memory chunk, and when the rocket is destroy we give back this block of memory to the memory pool.
Because each blocks of memory of the memory pool have the same size and we use 2 pointer to know where are "free" or "use" memory , their is no "hole" of memory during
allocation and deallocation of memory.
With a pool memory, we can use dynamic memory but without the overhead of calling the memory manager. We allocate only one time the memory : at the beginning of the game
Dynamic Game Loop
Gameloop is the core of the game. Each Entity (input,player,physics,enemy) is call in a defined order. But sometime we need an dynamic ordering instead of a static
order, by example we don't need to call some entity or we wish to change the order of the gameloop
On this project, I create a linkedlist of entity that way we can add or remove entities need for the gameloop
Images
Aucun commentaire:
Enregistrer un commentaire