← ALL PROJECTS

GRAPHICS · C++

Space Invaders

My first C++ project: a small game engine, and Space Invaders running on it.

C++, OpenGL 3.3, GLFW, CMake

stress mode

$ ./build/spaceinvaders --stress
stress entities=640 batches=2 draws=2 frame_ms=4.34 hash_cells=56
The stress-mode log from the README, measured with Mesa llvmpipe, a software renderer.

Problem

Drawing every sprite on its own costs one draw call each, and checking every pair of objects for collisions grows with the square of their count. Both get slow fast.

What I built

An engine with an OpenGL 3.3 renderer, components kept in packed arrays, a spatial hash for collisions and a sprite batcher. The game is the player, the invader swarm, bullets and the score. A separate stress scene spawns 640 moving sprites.

How

  1. Window and inputGLFW, OpenGL 3.3 core
  2. Packed component arraysAn entity id is a slot index
  3. SystemsWalk the arrays each frame
    Spatial hashTest only nearby cells
  4. Sprite batcherOne atlas, up to 512 sprites a batch
  5. RendererOne shader, one draw call per batch

Each AABB goes into every grid cell it touches, and the narrow phase only tests that cell and its neighbours. The original pixel masks are packed into one texture atlas.

A batch closes when the texture changes or it reaches 512 sprites. The renderer uses one GLSL 330 shader, one dynamic vertex buffer and one glDrawElements per batch.

Results

  • With batch capacity 1, the stress scene logged 658 draw calls. With capacity 512 it logged 2 for 640 entities.
  • A frame took 4.34 ms in stress mode, on a software renderer.
  • Headless tests cover the component arrays, the batcher and the spatial hash. The spatial test moves 640 boxes, checks a sample against brute force and requires fewer pair tests than checking every pair.
NEXT: Whodunit →