The CImg Library is an open-source C++ toolkit for image processing. It consists of a single header file “CImg.h” providing a minimal set of C++ classes and methods that can be used in your own sources, to load/save, process and display images. Very portable (Unix/X11,Windows, MacOS X, FreeBSD, .. ), efficient, easy to use, it’s a pleasant library for developping image processing algorithms in C++.
The main CImg block is at tschumperle/cimg and contains several examples. Its generated from this github repo.
This is a classic Tron game that shows how to load and manipulate images with Cimg library in a simple way.
Let’s try it out!
Init a new project and a new tron.cpp file inside and copy the code below:
$ bii init tron_example -L
$ cd tron_example
$ # Create tron.cpp and copy the code
tron.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | /*
#
# File : tron.cpp
# ( C++ source file )
#
# Description : A clone of the famous (and very simple) Tron game.
# This file is a part of the CImg Library project.
# ( http://cimg.sourceforge.net )
#
# Copyright : David Tschumperle
# ( http://tschumperle.users.greyc.fr/ )
#
# License : CeCILL v2.0
# ( http://www.cecill.info/licences/Licence_CeCILL_V2-en.html )
#
# This software is governed by the CeCILL license under French law and
# abiding by the rules of distribution of free software. You can use,
# modify and/ or redistribute the software under the terms of the CeCILL
# license as circulated by CEA, CNRS and INRIA at the following URL
# "http://www.cecill.info".
#
# As a counterpart to the access to the source code and rights to copy,
# modify and redistribute granted by the license, users are provided only
# with a limited warranty and the software's author, the holder of the
# economic rights, and the successive licensors have only limited
# liability.
#
# In this respect, the user's attention is drawn to the risks associated
# with loading, using, modifying and/or developing or reproducing the
# software by the user in light of its specific status of free software,
# that may mean that it is complicated to manipulate, and that also
# therefore means that it is reserved for developers and experienced
# professionals having in-depth computer knowledge. Users are therefore
# encouraged to load and test the software's suitability as regards their
# requirements in conditions enabling the security of their systems and/or
# data to be ensured and, more generally, to use and operate it in the
# same conditions as regards security.
#
# The fact that you are presently reading this means that you have had
# knowledge of the CeCILL license and that you accept its terms.
#
*/
#include "CImg.h"
using namespace cimg_library;
// Main procedure
//----------------
int main(int argc, char **argv) {
// Print usage, help and retrieve command line options
//-----------------------------------------------------
cimg_usage("A very simple Tron game, using the CImg Library");
cimg_help("--- Quick help ----------------------------\n"
" Player 1 (blue) :\n"
" Use keys 'W' (up), 'S' (down), 'A' (left)\n"
" and 'D' (right) to control your player.\n"
" Right 'CONTROL' key enables turbospeed\n"
" Player 2 (red) : \n"
" Use arrow keys to control your player.\n"
" 'TAB' key enables turbospeed.\n"
"-------------------------------------------");
const char *geom = cimg_option("-g","300x300","Size of the game board");
const int delay = cimg_option("-s",10,"Game speed (lower value means faster)");
const bool twoplayers = !cimg_option("-1",false,"One player only");
const int zoom = cimg_option("-z",1,"Zoom factor");
const bool full = cimg_option("-f",false,"Fullscreen mode");
unsigned int W = 400, H = 400;
std::sscanf(geom,"%u%*c%u",&W,&H);
// Define game colors and variables
//----------------------------------
const unsigned char blue[] = { 128,200,255}, red[] = { 255,0,0 }, white[] = { 255,255,255 };
int score1=0, score2=0, round_over=0, ix1=-1, iy1=-1, x1=0, y1=0, u1=0, v1=0, ix2=-1, iy2=-1, x2=0, y2=0, u2=0, v2=0;
bool start_round = true, turbo1 = false, turbo2 = false;
// Create background image
//--------------------------
CImg<unsigned char> background, img;
background.assign(64,64,1,3,0).noise(60).draw_plasma().resize(W,H).blur(2).normalize(0,128).draw_rectangle(0,0,W-1,H-1,white,1.0f,~0U);
// Open display window
//---------------------
CImgDisplay disp(background,"* CImg-Tron *");
if (zoom>1) disp.resize(-100*zoom,-100*zoom);
if (full) disp.toggle_fullscreen().display(background);
// Start main game loop
//----------------------
while (!disp.is_closed() && !disp.is_keyESC()) {
// Init new game round if necessary
//----------------------------------
if (start_round) {
// Init game variables
round_over = 0;
ix1=-1; iy1=-1; x1 = 10; y1 = 10; u1 = 1; v1 = 0; turbo1 = false;
ix2=-1; iy2=-1; x2 = W-11; y2 = H-11; u2 = -1; v2 = 0; turbo2 = false;
img = background;
start_round = false;
// Display a simple pre-round page
CImg<unsigned char> logo, pressakey;
logo.draw_text(0,0," CImg-Tron ",white,0,1,33).resize(-100,-100,1,3);
CImg<unsigned char> tmp = (+background).draw_image((W-logo.width())/2,(H-logo.height())/2-20,logo,logo.get_channel(0).dilate(6).normalize(0,1)).
draw_text(W/2-60,H/2+10,"Blue ( %u )",blue,0,1,13,score1).
draw_text(W/2+10,H/2+10,"Red ( %u )",red,0,1,13,score2);
pressakey.draw_text(0,0,"* Press a key to start round *",white);
for (float i = 0; i<1; i+=0.05f) ((+tmp)*=i).display(disp.wait(20));
disp.flush();
for (unsigned long t = 0; !disp.key() && !disp.is_closed(); ++t) {
if (!(t%10)) { if (t%20) disp.display(tmp); else disp.display((+tmp).draw_image(W/2-70,H/2+50,pressakey,pressakey,1,255)); }
if (disp.wait(20).is_resized()) disp.resize(disp);
}
if (disp.is_keyESC()) disp.flush();
}
// Test collision between players and borders
if (x1<0 || x1>=img.width() || y1<0 || y1>=img.height() ||
img(x1,y1,0)!=background(x1,y1,0) ||
img(x1,y1,1)!=background(x1,y1,1) ||
img(x1,y1,2)!=background(x1,y1,2) ||
((ix1>=0 || iy1>=0) && (img(ix1,iy1,0)!=background(ix1,iy1,0) || // Collision test for turbo mode
img(ix1,iy1,1)!=background(ix1,iy1,1) ||
img(ix1,iy1,2)!=background(ix1,iy1,2)))) { round_over=1; score2++; }
if (twoplayers) {
if (x2<0 || x2>=img.width() || y2<0 || y2>=img.height() ||
img(x2,y2,0)!=background(x2,y2,0) ||
img(x2,y2,1)!=background(x2,y2,1) ||
img(x2,y2,2)!=background(x2,y2,2) ||
((ix2>=0 || iy2>=0) && (img(ix2,iy2,0)!=background(ix2,iy2,0) || // Collision test for turbo mode
img(ix2,iy2,1)!=background(ix2,iy2,1) ||
img(ix2,iy2,2)!=background(ix2,iy2,2)))) { round_over=2; score1++; }
}
// Draw new players positions
img.draw_point(x1,y1,blue);
if (ix1>=0 && iy1>=0) img.draw_point(ix1,iy1,blue);
if (twoplayers) {
img.draw_point(x2,y2,red);
if (ix2>=0 && iy2>=0) img.draw_point(ix2,iy2,red);
}
if (disp.is_resized()) disp.resize(disp);
img.display(disp);
// Update players positions
x1+=u1; y1+=v1;
if (turbo1) { ix1 = x1; iy1 = y1; x1+=u1; y1+=v1; } else { ix1 = iy1 = -1; }
if (twoplayers) {
x2+=u2; y2+=v2;
if (turbo2) { ix2 = x2; iy2 = y2; x2+=u2; y2+=v2; } else { ix2 = iy2 = -1; }
}
// Test keyboard events
int nu1 = u1, nv1 = v1, nu2 = u2, nv2 = v2;
if (disp.is_keyARROWLEFT()) { nu1 = -1; nv1 = 0; }
if (disp.is_keyARROWRIGHT()) { nu1 = 1; nv1 = 0; }
if (disp.is_keyARROWUP()) { nu1 = 0; nv1 = -1; }
if (disp.is_keyARROWDOWN()) { nu1 = 0; nv1 = 1; }
turbo1 = disp.is_keyCTRLRIGHT();
if (twoplayers) {
if (disp.is_keyA()) { nu2 = -1; nv2 = 0; }
if (disp.is_keyD()) { nu2 = 1; nv2 = 0; }
if (disp.is_keyW()) { nu2 = 0; nv2 = -1; }
if (disp.is_keyS()) { nu2 = 0; nv2 = 1; }
turbo2 = disp.is_keyTAB();
}
if (nu1!=-u1 && nv1!=-v1) { u1 = nu1; v1 = nv1; }
if (nu2!=-u2 && nv2!=-v2) { u2 = nu2; v2 = nv2; }
// Check if round is over.
if (round_over) {
const int xc = round_over==1?x1:x2, yc = round_over==1?y1:y2;
for (int r=0; r<50; r+=3) img.draw_circle(xc,yc,r,round_over==1?blue:red,r/300.0f).display(disp.wait(20));
for (int rr=0; rr<50; rr+=3)
((+img)*=(50-rr)/50.0f).draw_circle(xc,yc,(50+rr),round_over==1?blue:red,1/6.0f).display(disp.wait(20));
start_round = true;
}
// Wait a small amount of time
disp.wait(delay);
}
return 0;
}
|
Check the dependencies of the project with bii deps:
$ bii deps
INFO: Processing changes...
your_user/cimg_example depends on:
unresolved:
CImg.h
Now, edit the biicode.conf file generated in the project folder. Add your [requirements]
depending on the version you want and map your [includes]
:
[requirements]
tschumperle/cimg: 4
[includes]
CImg.h: tschumperle/cimg
Now, checking again bii deps shows all resolved dependencies.
If you’re not using Windows OS, you might need the external X11 library dependency, check it!
Linux OS:
$ sudo apt-get install libx11-dev
Mac OS:
Go to XQuartz home page, download the package and install it.
You might need to modify the CMakeLists.txt of your block in order to include the external X11 dependency in the linking process:
include(${CMAKE_HOME_DIRECTORY}/biicode.cmake)
ADD_BII_TARGETS()
IF(APPLE)
FIND_PACKAGE(X11)
TARGET_LINK_LIBRARIES(${BII_BLOCK_TARGETS} PUBLIC ${X11_LIBRARIES})
INCLUDE_DIRECTORIES(/opt/X11/include)
ENDIF()
IF(UNIX)
IF(NOT APPLE)
TARGET_LINK_LIBRARIES(${BII_BLOCK_TARGETS} PUBLIC X11)
ENDIF()
ENDIF()
This example is already in biicode: examples/cimg. Give it a try, just open a new project and open the block.
$ bii init cimg_example
$ cd cimg_example
$ bii open examples/cimg
$ bii build
$ cd bin
$ # Run the generated executable
$ ./examples_cimg_tron # on linux
$ examples_cimg_tron.exe # on windows
Any doubts? Do not hesitate to contact us visit our forum and feel free to ask any questions.