I propose here a cross-platform C++ version of the well known Breakout game (or Arkanoid if you prefer). The interesting thing here is that it is coded in less than 64 lines of C++ code. While the code is quite compact, it stays easily readable. The secret ? I use the CImg Library to perform all necessary operations on images. The
CImg Library is an easy-to-use C++ template image processing library that has the particularity to entirely fit in one single header file. It means that with a single include, you are able to deal with images and perform many operations on it (display, filtering, drawing, ...).
This work follows my previous submission on "Tetris in less than 128 lines of code", already proposed on CodeProject.
The code is divided into two distinct parts. The first one creates games graphics (colored bricks, ball, racket and background image) from scratch, using the primitive drawing functions of the
The second part consists of the main event loop, catching the user's events and computing the ball/racket motion and the possible collisions. In fact, there's nothing specially difficult here, but all is coded in quite a compact way.
This is a good way to see how simple it is to code small animations using the CImg Library. It is also worth noticing that the source is cross-platform and you may compile it on Unix or Mac OS X without modifying a single line. I hope this may give some idea for people that are discouraged because of the relative code complexity needed when one wants to do basic image operations inside C++ source codes.
The proposed archive contains the small source code of the game (file 'breakout.cpp'), the
CImg Library header file 'CImg.h' needed for the compilation, as well as the compiled executable (Win32) and the Visual Express 2005 solution used to compile. So, the archive should be self-contained.
Hope you'll enjoy it.
- 23rd February, 2007: Initial post