Biicode-logo
  • Explore
  • Plans
  • Learn
    • Get Started
    • Downloads
    • Docs
    • Blog
    • Forum
  • Log in
C/C++ Arduino Raspberry Pi (cross compiling) Node.js
  • Biicode
    • Installation
      • Install Biicode
      • Install C/C++ tools
      • Debian based distributions
      • Arch based distributions
      • Run biicode from source
      • Install C/C++ tools manually
        • Verify your installation
      • Connect through a proxy server
    • Getting started
      • Basics
      • Create your first project
        • [optional] Keeping #includes short
      • Using an IDE
      • Build and run
      • Publishing
    • Dependencies
      • Dependencies
      • Modifying the version you depend on
        • Depending on a block track
    • Publishing
      • Tag a version
      • Private blocks
    • Custom build configuration
      • CMake basics
      • Where is biicode´s “magic”?
      • Define and prepare targets
        • Which source code files are part of the block’s library
        • Choose STATIC or SHARED library
        • Modify which executable targets are made
        • Which source code files are part of each executable
        • Modify which test targets are made
      • Configure targets
        • Configure library target
        • Configure executable target
      • Select build type: Debug or Release
      • Complete variable reference
    • Adapt your library
      • Concepts to understand
        • Key facts
      • Without a previous CMakeLists.txt
        • 1. Look for unresolved dependencies with bii deps
        • 2. Execute bii build
        • 3. Test the libary’s reusability
      • With a previous CMakeLists.txt
        • Option 1: Let biicode do its job in an isolated file
        • Option 2: Build your own target library and link them to BII_LIB_TARGET
        • Option 3: Adapt your CMakeLists.txt filtering files
    • Advanced Usage
      • Custom Layouts
        • Simple Layout
        • TMP Layout
        • Classic Layout
        • CLion Layout
      • Tests
      • Open multiple blocks
        • Working with your own blocks
        • Opening your block
        • Publishing updated code
        • Closing edited block
        • Working with any published block
        • Open a block
        • Publish the changes
        • Close the block
        • Depend on the block you’ve just published
      • Toolchains
      • Override a dependency
        • Override a dependency with block tracks
      • Advanced build configuration
        • Publish, share and reuse CMake scripts
        • Overriding dependencies build options and configuration
      • Publish a block track
        • Publish a new block Track
      • Private blocks
    • Examples
      • Basic Compression Library
        • Simple Huffman Compression - Uncompression
        • Open and build
      • Boost Libraries
        • Boost.Lambda
        • Boost.Coroutine
        • Alternative setup call
        • Extra configuration variables
        • Contribute to the setup scripts
      • Box2D
        • Bounces of a circle falling
        • Open and build
      • C++ challenge
        • Create a new project
        • Copy the code
        • Find and retrieve dependencies
        • Build and run
      • CImg
        • Tron game
        • Open and build
      • Crypto++
        • Encrypt a message
        • Open and build
      • CSparse
        • Read a matrix and solve a linear system
        • Create a new project
        • Open and build
      • cURL
        • HTML page gatherer
        • Open and build
      • Eigen
        • Middle rows from a matrix
        • Open and build
      • Expression Parser
        • Simple form of mathematical expression parsing
        • Open and build
      • fit
        • Tests
      • Flatbuffers
        • Charge a *.fbs file and generate a C++ header
      • Freeglut
        • 1. Create a new project
        • 2. Creating reusable code
        • 3. Find dependencies
        • 4. Build and run
      • GLFW
        • Running the examples
      • GLUI User Interface Library
        • GLUI Window Template
      • Google Mock (GMock)
        • GMock Examples
        • Mocking a simple function
      • Google Test (GTest)
        • Testing a factorial function: Simple test
        • Testing a factorial function: Test suites
        • Open and build
      • HTTP Server
        • How does it work?
        • How can I use it?
        • The code
        • Supported Operating Systems
        • More information
        • List of dependencies
      • json11
        • Simple convert data to json and vice versa
      • json++
        • Simple parser and converter from JSON to XML
      • Miniutf
      • Multivariate Splines
      • libuv
        • Http client/server application
        • Change libuv’s version
      • Little CMS
        • ICC Profile Examples
        • Open and build
      • Log4z
        • Fast stream log strings test
      • lwan Web Server
        • Hello World Example
      • MiLi
      • MuParser
      • OpenCV
        • Showing an image and detecting faces
        • Open and build
      • OpenSSL
        • Encrypting with MD5 and SHA1
        • Develop your project
        • Open and build
      • POCO
        • PDF example
        • Using NetSSL_OpenSSL or NetSSL_Win library
      • PTypes
      • SDL
        • Graphical window interface
        • Open and build
      • SQLite
        • Shopping list database
        • SQLite++ Wrapper
        • Open and build
      • TinyThread++
        • Open and build the examples
        • Simple Hello World with a thread
        • Draw a fractal
      • Zlib
        • Usage example (difficulty: medium)
      • ZMQ
        • Simple client-server with C++ binding
      • ZMQ with Google Protocol Buffers Serialization
    • Integrations
      • Generators and IDEs
        • Eclipse CDT
        • Visual Studio
        • CLion
      • IDEs and VCS
      • Git (GitHub, Bitbucket, etc.)
        • With a new repository
        • Create a block from a git repository
        • Publish from git commit
      • Continuous Integration
        • AppVeyor
        • Travis CI
      • Koding
      • Doxygen
        • Create a Doxyfile template
        • Edit your Doxyfile
        • Generate the Documentation
    • Reference
      • biicode.conf: configure your biicode projects
        • [requirements]
        • [parent]
        • [paths]
        • [dependencies]
        • [mains]
        • [tests]
        • [hooks]
        • [includes]
        • [data]
      • Commands
        • bii build: build your project
        • bii buzz: init, find and build
        • bii clean: delete meta-information
        • bii close: finish editing published blocks
        • bii configure: configure your project
        • bii deps: show block dependencies
        • bii diff: compare block versions
        • bii find: find your external dependencies
        • bii init: creates a new project
        • bii new: creates new blocks
        • bii open: edit published blocks
        • bii publish: publish your blocks
        • bii setup: install necessary tools
        • bii test: test your code
        • bii update: update a block
        • bii user: specify your username
      • Configuration Files
        • layout.bii: define your project layout
        • policies.bii: defining the policies for the code you want to reuse
        • ignore.bii: filtering your files
        • settings.bii: defining your tools and preferences
        • types.bii: configuring non-standard file extensions
    • Release notes
    • FAQs
      • Is biicode free?
      • Is biicode an editor in the cloud?
      • Is biicode a VCS?
      • Can I use biicode with my favourite VCS?
      • Which languages are supported?
      • How does biicode relate to Maven, NPM, PyPI...?
    • Troubleshooting
      • Eclipse projects: “Launch failed. Binary not found” (OS X)
      • g++ doesn’t compile simple code, using thread header
      • Default Build Configuration with bii build not working
  • Arduino
    • Installation
      • Install Biicode
      • Install Arduino tools
      • Install Arduino tools manually
        • Install Arduino SDK manually
    • Getting started
      • Installing biicode and Arduino tools
      • Create your project
      • Define your board
      • Build and upload your program
      • Depending on Fenix Blink
      • Build and upload
    • Arduino commands
      • bii configure -t arduino: configure your project
      • bii build: build your project
      • bii arduino:upload: send your code into the Arduino
      • bii arduino:settings: configure your Arduino settings
      • bii arduino:monitor: start a serial monitor
      • bii configure –toolchain=arduino: enable, disable or change the Arduino cross compilation
    • How to
      • Eclipse IDE configuration
        • How to import your project
        • How to fix “Unresolved inclusion: Arduino.h”
      • Configure your SDK, port and board
        • I changed my Arduino’s port, what happens now?
        • How can I change my Arduino project properties?
        • bii arduino:settings options
      • How to adapt your code
        • 1. Projects with one single .ino file
        • 2. Projects with multiple .ino files
      • How to use the Arduino Yun
        • Download Arduino 1.5
        • Configure your settings
    • Examples
      • Arduino Serial Monitor
        • C++ code
        • Turn ON/OFF one LED
      • Servo and LCD 2x16
        • What do we need?
        • Scheme
        • The code: Display the servo angle into a LCD
        • Build and upload the code
      • Arduino Serial Interface
        • How does it work?
        • How do I use it?
        • C++ code
        • Arduino code
        • Build and run!
    • Troubleshooting
      • Launching Arduino IDE, I get an error ./arduino: 22: ./arduino: java: not found in Ubuntu
  • Raspberry Pi Cross Compilation
    • Installation
      • Install Biicode
      • Install RPI tools
      • Install RPI cross-compiling tools manually
        • C++ tools installation
        • Raspberry Pi tools installation
    • Getting started
      • 1. Installing biicode and C/C++ cross-building tools
      • 2. Create your project
      • 3. Build and run your program (cross-compiling)
      • 4. Send your executable to your Raspberry Pi
      • 5. Depending on WiringPi
    • RPi commands
      • bii rpi:send: send a bin folder
      • bii rpi:settings: configure your Raspberry Pi settings
      • bii rpi:ssh: connect by ssh with the Raspberry Pi
      • bii configure –toolchain=rpi: enable, disable or change the Raspberry Pi cross compilation.
    • How to
      • Installing the biicode package from downloads page is too slow
      • Output selection and volume control
      • Raspberry Pi GPIO Pin Layout
    • Examples
      • WiringPi: C GPIO library
        • How to make a LED blink with Raspberry Pi
        • How to use the RPi PWM output to control a motor
        • How to use softServo to control a Servo
      • HTTP Server: how to control a led by web
        • How can i use it?
      • A funny moving doll with Raspberry PI and biicode
        • You just need paper, scissors, a servo, a Raspberry Pi and biicode!
        • Choose the paper doll you like most
        • Putting it all together!
        • Stick the head to the servo and put the servo in the body
        • Connect the servo to the 5v, GPIO17 and 0v pins
    • Troubleshooting
      • Is it possible to change the version of gcc used for cross-compiling to the Raspberry Pi?
  • Node.js
    • Getting started
      • 1. Installing biicode and node.js
      • 2. Create your project
      • 3. Run your program
      • 4. Depending on redis
    • How to
      • Run your node programs
  • biicode docs
  • Biicode
  • Examples
  • CImg
  • Edit on GitHub
C++ challenge
Crypto++

CImg¶

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.

Tron game¶

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!

Create a new project¶

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;
}

Manage your dependencies¶

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()

Build the project¶

Now, build and run your Tron game!

$ bii build
$ cd bin
$ # Run the executable

Open and build¶

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
Note that this example block already includes all required modifications above in both biicode.conf and CMakeLists.txt.

Any doubts? Do not hesitate to contact us visit our forum and feel free to ask any questions.

C++ challenge
Crypto++

© Copyright 2014, biicode.