Skip to content

Tutorial: Create and Use SetIcon

eXpl0it3r edited this page Oct 18, 2012 · 1 revision

Create and use SetIcon

Intro

A custom icon for your program looks just that little more professional, especially if the icon file is 'hidden' in the exe. In this tutorial we will convert your icon in bytes, put it in the exe and display it. For the conversion from image to bytes we will use the free and great program GIMP. Here is an example of the result:

Example icon

What to know about icons?

Most icons used on your pc are or 16x16 or 32x32. These numbers are the amount of pixels (width x height). The smaller one is used in the titlebar of your program, the 32x32 is used in the taskbar (Windows anyway). For a icon to look good it almost always needs a transparent background around the object the icon must present. For this tutorial we will use the SFML icon (a green loop). Here is the basic png. Basic logo As you might have guessed by now, we have a small problem. The titlebar only needs to be 16x16, but the taskbar needs 32x32. If we only load the 16x16 the taskbar icon will look stretched and ugly. To fix this, we are gonna load only the 32x32 and it will be resized by SFML to 16x16 for the titlebar. This resizing might not always be pretty - so that you know.

Convert image to bytes

Open your icon/image (or the green loop above) in Gimp. Gimp (for windows) has the nice feature that the programs own 16x16 (titlebar) changes to the image you got loading. This way you can see if your icon will look good (when resized) in your programs titlebar. Now you only have to go to the file menu of GIMP and choose 'save as'. In the save dialogue go to select file type and pick "C sourcecode". A new window will pop up, be sure to turn off "use Glib" and turn on alfa channel. The 'name prefix' is up to you, for this example we use "sfml-icon". The generated code will look something like this:

/* GIMP RGBA C-Source image dump (sfml.c) */
static const struct {
  unsigned int 	 width;
  unsigned int 	 height;
  unsigned int 	 bytes_per_pixel; /* 3:RGB, 4:RGBA */ 
  unsigned char	 pixel_data[32 * 32 * 4 + 1];
} sfml_icon = {
  32, 32, 4,
  "\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377"
  "\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377"
  "\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377"
  "\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0"
  "\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377"
  "\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377"
  "\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377"
  "\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0"
  "\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377"
  "\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377"
  "\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377"
  "\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0"
  "\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377"
  "\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377"
  "\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377"
  "\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0"
  "\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377"
  "\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377"
  "\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377"
  "\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0"
  "\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377"
  "\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377"
  "\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377"
  "\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0"
  "\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377"
  "\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377"
  "\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377"
  "\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0"
  "\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377"
  "\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377"
  "\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377"
  "\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0"
  "\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377"
  "\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377"
  "\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377"
  "\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0"
  "\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377"
  "\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377"
  "\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377"
  "\0\377\377\377\0\377\377\377\0\377\377\377\0\24\261'\15\15\255';\14\260)"
  "j\15\257(\231\15\257(\240\14\257(\223\15\260(\207\14\256'{\16\256'n\16\260"
  "(G\0\252\0\3\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377"
  "\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377"
  "\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377"
  "\0\377\377\377\0\377\377\377\0\14\256#\26\15\260(t\15\256(\256\15\257(\335"
  "\15\257(\376\15\257(\377\15\257(\377\15\257(\377\15\257(\377\15\257(\377"
  "\15\257(\377\15\257(\377\15\257(\377\15\257(\377\15\256(\344\15\257(\206"
  "\15\256(&\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377"
  "\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377"
  "\0\377\377\377\0\377\377\377\0\377\377\377\0\14\257(@\16\257'\250\15\257"
  "(\371\15\257(\377\15\257(\377\15\257(\377\15\257(\371\16\257(\316\15\256"
  "(\236\16\256'n\16\260)^\16\257(l\15\257(y\15\257(\206\16\257)\226\15\257"
  ")\334\15\257(\377\15\257(\377\15\257(\377\16\260)\252\0\266$\7\377\377\377"
  "\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0"
  "\377\377\377\0\377\377\377\0\21\252\"\17\16\257)p\15\260(\327\15\257(\377"
  "\15\257(\377\15\257(\372\16\260)\252\16\257)]\13\261'.\0\2313\5\377\377\377"
  "\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0"
  "\377\377\377\0\377\377\377\0\0\377\0\1\14\261)>\15\257(\237\14\257(\365\15"
  "\257(\377\16\257'\274\0\252\0\3\377\377\377\0\377\377\377\0\377\377\377\0"
  "\377\377\377\0\377\377\377\0\377\377\377\0\13\260)D\15\256(\344\15\257(\377"
  "\15\257(\377\16\257'\342\14\257'|\13\252+\30\377\377\377\0\377\377\377\0"
  "\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377"
  "\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377"
  "\377\0\377\377\377\0\377\377\377\0\13\257+0\15\257(\351\15\257(\377\15\256"
  "(_\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\0\237\40\10\14"
  "\257'\217\15\257(\376\15\257(\377\15\260(\307\15\261'N\0\252\0\3\377\377"
  "\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377"
  "\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0"
  "\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377"
  "\377\377\0\377\377\377\0\15\256(_\15\257(\377\14\257(\315\377\377\377\0\377"
  "\377\377\0\377\377\377\0\13\256&/\15\256(\324\15\257(\377\15\257(\372\15"
  "\260(z\0\252\0\3\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0"
  "\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377"
  "\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377"
  "\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377"
  "\0\14\252$\25\15\257(\377\15\257(\352\377\377\377\0\377\377\377\0\16\254"
  ")%\15\257(\362\15\257(\377\15\260(\327\17\257(3\377\377\377\0\377\377\377"
  "\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0"
  "\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377"
  "\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377"
  "\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\17\257$#\15"
  "\257(\377\15\257(\354\377\377\377\0\0\266$\7\16\257'\317\15\257(\377\15\257"
  "(\262\0\252\34\11\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0"
  "\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377"
  "\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377"
  "\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377"
  "\0\377\377\377\0\377\377\377\0\377\377\377\0\15\260(z\15\257(\377\15\256"
  "(\253\377\377\377\0\16\257)\226\15\257(\377\15\257(\335\21\252\"\17\377\377"
  "\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377"
  "\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0"
  "\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377"
  "\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377"
  "\377\0\377\377\377\0\17\255)2\15\257(\370\15\257(\377\15\256'O\15\260(M\15"
  "\257(\377\15\257(\370\17\255)2\377\377\377\0\377\377\377\0\377\377\377\0"
  "\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377"
  "\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377"
  "\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377"
  "\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\21\252\"\17\15"
  "\257(\335\15\257(\377\15\260(\232\377\377\377\0\15\256(\253\15\257(\377\15"
  "\260(z\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377"
  "\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0"
  "\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377"
  "\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377"
  "\377\0\377\377\377\0\377\377\377\0\0\252\34\11\15\257(\262\15\257(\377\15"
  "\257(\320\0\237\40\10\377\377\377\0\15\257(\354\15\257(\377\17\257$#\377"
  "\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377"
  "\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377"
  "\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0"
  "\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377"
  "\377\377\0\17\257(3\15\260(\327\15\257(\377\15\257(\362\16\254)%\377\377"
  "\377\0\377\377\377\0\15\257(\352\15\257(\377\14\252$\25\377\377\377\0\377"
  "\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377"
  "\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377"
  "\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0"
  "\377\377\377\0\377\377\377\0\377\377\377\0\0\252\0\3\15\260(z\15\257(\372"
  "\15\257(\377\15\256(\324\13\256&/\377\377\377\0\377\377\377\0\377\377\377"
  "\0\14\257(\315\15\257(\377\15\256(_\377\377\377\0\377\377\377\0\377\377\377"
  "\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0"
  "\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377"
  "\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\0\252"
  "\0\3\15\261'N\15\260(\307\15\257(\377\15\257(\376\14\257'\217\0\237\40\10"
  "\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\15\256(_\15\257"
  "(\377\15\257(\351\13\257+0\377\377\377\0\377\377\377\0\377\377\377\0\377"
  "\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377"
  "\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377"
  "\0\377\377\377\0\13\252+\30\14\257'|\16\257'\342\15\257(\377\15\257(\377"
  "\15\256(\344\13\260)D\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377"
  "\0\377\377\377\0\377\377\377\0\0\252\0\3\16\257'\274\15\257(\377\14\257("
  "\365\15\257(\237\14\261)>\0\377\0\1\377\377\377\0\377\377\377\0\377\377\377"
  "\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0"
  "\0\2313\5\13\261'.\16\257)]\16\260)\252\15\257(\372\15\257(\377\15\257(\377"
  "\15\260(\327\16\257)p\21\252\"\17\377\377\377\0\377\377\377\0\377\377\377"
  "\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0"
  "\0\266$\7\16\260)\252\15\257(\377\15\257(\377\15\257(\377\15\257)\334\16"
  "\257)\226\15\257(\206\15\257(y\16\257(l\16\260)^\16\256'n\15\256(\236\16"
  "\257(\316\15\257(\371\15\257(\377\15\257(\377\15\257(\377\15\257(\371\16"
  "\257'\250\14\257(@\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377"
  "\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0"
  "\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\15\256(&\15\257"
  "(\206\15\256(\344\15\257(\377\15\257(\377\15\257(\377\15\257(\377\15\257"
  "(\377\15\257(\377\15\257(\377\15\257(\377\15\257(\377\15\257(\376\15\257"
  "(\335\15\256(\256\15\260(t\14\256#\26\377\377\377\0\377\377\377\0\377\377"
  "\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377"
  "\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0"
  "\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\0\252\0\3\16\260"
  "(G\16\256'n\14\256'{\15\260(\207\14\257(\223\15\257(\240\15\257(\231\14\260"
  ")j\15\255';\24\261'\15\377\377\377\0\377\377\377\0\377\377\377\0\377\377"
  "\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377"
  "\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0"
  "\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377"
  "\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377"
  "\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377"
  "\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0"
  "\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377"
  "\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377"
  "\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377"
  "\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0"
  "\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377"
  "\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377"
  "\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377"
  "\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0"
  "\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377"
  "\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377"
  "\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377"
  "\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0"
  "\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377"
  "\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377"
  "\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377"
  "\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0"
  "\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377"
  "\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377"
  "\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377"
  "\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0"
  "\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377"
  "\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377"
  "\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377"
  "\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0"
  "\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377"
  "\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377"
  "\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377"
  "\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0"
  "\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377"
  "\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377"
  "\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377"
  "\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0"
  "\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0",
};

Yes, I'm down here. The beauty of GIMP is that this code is directly ready for use. I prefer to copy the code and put it in a separate header file. See c++ tutorials on how to do that.

Setting the icon

To set the icon we will use the SetIcon function. More info here: SetIcon docu There is not much to tell about the function, besides it takes the generated file by GIMP perfectly. The function requires 3 variables. The width, height and pointer to the image array data. The width and height can be retrieved with sfml_icon.width or .height thanks to the structure set up by GIMP. This line:

window.SetIcon(sfml_icon.width,  sfml_icon.height,  sfml_icon.pixel_data); 

Here below is the end result code. I removed the icon data, you can copy the one above if you want it to work.

////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Window.hpp>
#include <SFML/OpenGL.hpp>


//////////////////////////////////////////////////////////// 
// Put here the exported GIMP image array!
////////////////////////////////////////////////////////////

int main()
{
	// Create the main window
	sf::Window window(sf::VideoMode(640, 480, 32), "SFML Window", sf::Style::Default, sf::ContextSettings(32));
	window.SetFramerateLimit(60); 

	////////////////////////////////////////////////////////////
	/// This line is added! Make sure the name of your image is the same
	///
	window.SetIcon( sfml_icon.width,  sfml_icon.height,  sfml_icon.pixel_data ); 
	///
	////////////////////////////////////////////////////////////

	// Create a clock for measuring the time elapsed
	sf::Clock clock;

	// Set the color and depth clear values
	glClearDepth(1.f);
	glClearColor(0.f, 0.f, 0.f, 0.f);

	// Enable Z-buffer read and write
	glEnable(GL_DEPTH_TEST);
	glDepthMask(GL_TRUE);

	// Setup a perspective projection
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(90.f, 1.f, 1.f, 500.f);

	// Start the game loop
	while (window.IsOpened())
	{
		// Process events
		sf::Event event;
		while (window.PollEvent(event))
		{
			// Close window : exit
			if (event.Type == sf::Event::Closed)
				window.Close();

			// Escape key : exit
			if ((event.Type == sf::Event::KeyPressed) && (event.Key.Code == sf::Key::Escape))
				window.Close();

			// Resize event : adjust viewport
			if (event.Type == sf::Event::Resized)
				glViewport(0, 0, event.Size.Width, event.Size.Height);
	   }

		// Activate the window before using OpenGL commands.
		// This is useless here because we have only one window which is
		// always the active one, but don't forget it if you use multiple windows
		window.SetActive();

		// Clear color and depth buffer
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

		// Apply some transformations
		glMatrixMode(GL_MODELVIEW);
		glLoadIdentity();
		glTranslatef(0.f, 0.f, -200.f);
		glRotatef(clock.GetElapsedTime() * 0.05f, 1.f, 0.f, 0.f);
		glRotatef(clock.GetElapsedTime() * 0.03f, 0.f, 1.f, 0.f);
		glRotatef(clock.GetElapsedTime() * 0.09f, 0.f, 0.f, 1.f);

		// Draw a cube
		glBegin(GL_QUADS);

			glColor3f(1.f, 0.f, 0.f);
			glVertex3f(-50.f, -50.f, -50.f);
			glVertex3f(-50.f,  50.f, -50.f);
			glVertex3f( 50.f,  50.f, -50.f);
			glVertex3f( 50.f, -50.f, -50.f);

			glColor3f(1.f, 0.f, 0.f);
			glVertex3f(-50.f, -50.f, 50.f);
			glVertex3f(-50.f,  50.f, 50.f);
			glVertex3f( 50.f,  50.f, 50.f);
			glVertex3f( 50.f, -50.f, 50.f);

			glColor3f(0.f, 1.f, 0.f);
			glVertex3f(-50.f, -50.f, -50.f);
			glVertex3f(-50.f,  50.f, -50.f);
			glVertex3f(-50.f,  50.f,  50.f);
			glVertex3f(-50.f, -50.f,  50.f);

			glColor3f(0.f, 1.f, 0.f);
			glVertex3f(50.f, -50.f, -50.f);
			glVertex3f(50.f,  50.f, -50.f);
			glVertex3f(50.f,  50.f,  50.f);
			glVertex3f(50.f, -50.f,  50.f);

			glColor3f(0.f, 0.f, 1.f);
			glVertex3f(-50.f, -50.f,  50.f);
			glVertex3f(-50.f, -50.f, -50.f);
			glVertex3f( 50.f, -50.f, -50.f);
			glVertex3f( 50.f, -50.f,  50.f);

			glColor3f(0.f, 0.f, 1.f);
			glVertex3f(-50.f, 50.f,  50.f);
			glVertex3f(-50.f, 50.f, -50.f);
			glVertex3f( 50.f, 50.f, -50.f);
			glVertex3f( 50.f, 50.f,  50.f);

		glEnd();

		// Finally, display the rendered frame on screen
		window.Display();
	}

	return EXIT_SUCCESS;
}

That's all folks!

Clone this wiki locally