WOWCube Docs logo
WOWCube Docs
Mission Control
Section Shortcuts
APIExamplesSourceWOWConnectChangelog
Filters
SDK and language defaults persist in cookies.
SDK version
Navigation Tree
Collapsed by default, focused on the active path.
Made byMcKay Seamons
GitHub
  1. Home
  2. Docs
  3. Examples
  4. Simple Shapes - Bitmaps
Mission NodeSDK 6.1C++renderingProject Included

Simple Shapes - Bitmaps

Rendering Bitmaps This example demonstrates the use of WOWCube API for rendering bitmaps or sprites . A bitmap is a type of memory organization used to store...

Examples / SDK 6.1 / C++ / rendering

Rendering - Bitmaps

This example demonstrates the use of WOWCube API for rendering bitmaps or sprites.

A bitmap is a type of memory organization used to store digital images. The term bitmap comes from the computer graphics therminology, meaning just a map of bits, a spacially mapped array of bits. It refers to a spacially mapped array of pixels.

A sprite is a two-dimensional bitmap that is integrated into a larger scene, most often is a 2D video game.

Generally, the drawing of a bitmap requires two separate steps:

  • First, bitmap data must be loaded from image file into device memory.
  • Second, the coordinate of a pivot point of a bitmap should be passed to WOWCube API bitmap drawing funciton to render the bitmap on screen

A pivot point is a central point of any rotational system.

In WOWCube API, bitmap pivot point is always at the geometrical center of an image. For example, if bitmap width is 240 and height is 240, its pivot point is at (120, 120)

As it was mentioned above, a bitmap is an array of pixels. Each pixel has a color encoded in RGB value.

The RGB color model is an additive color model in which the red, green and blue primary colors of light are added together in various ways to reproduce a broad spectrum of colors. The name of the model comes from the first letters of the three primary colors: Red, Green and Blue

Actual values of each primary color range from 0 to 255, the range that a single 8-bit byte can offer. Since higher value means brighter color, the (0,0,0) represents no color, or so-called transparent color, (1,1,1) gives solid black, while (255,255,255) gives white.

The example

In order to demonstrate the rendering of bitmaps, we will create a cubeapp that visualizes a system of a star and planets orbiting the star.

Loading the bitmap data

First off, we must load image files to WOWCube device memory. Once loaded, the content of each image file creates a resource with its own unique identifier called a resouce handle, or just a handle.

Our example uses three static images, so let's declare three new variables for saving handles of these images

Simple Shapes - Bitmaps
CPP
1//Background image
2uint32_t bkg_image;
3
4//Foreground image
5uint32_t fg_image;
6
7//Star and planets
8uint32_t sun_image;
9
Wrapped for easier reading. Turn wrap off to inspect exact line lengths.

and load them when cubeapp initializes:

Simple Shapes - Bitmaps
CPP
1void Bitmaps::InitializeResources()
2{
3 ...
4
5 //Load images
6 bkg_image = Cubios::GFX_getAssetId("bkg.png");
7 fg_image = Cubios::GFX_getAssetId("fg.png");
8 sun_image = Cubios::GFX_getAssetId("star.png");
9
10 ...
11}
12
Wrapped for easier reading. Turn wrap off to inspect exact line lengths.

To load image file data into a graphics asset, WOWCube API GFX_getAssetId function is used:

Simple Shapes - Bitmaps
CPP
1Cubios::GFX_getAssetId(imageFile)
2
3 Parameters:
4 imageFile - name of an image alias. The alias can either be an actual name of a file from <project>/resources/images/ folder or custom name assigned to an image in cubeapp project file.
5
Wrapped for easier reading. Turn wrap off to inspect exact line lengths.

At this point, we have background and foreground images plus an image of a star. But what about planets?

For planets, we will implement a very simple particle system of just three particles, each drawn as a sprite.

As with any particly system, first we declare an array of particle objects that represent planets:

Simple Shapes - Bitmaps
CPP
1typedef struct
2{
3 uint32_t sprite;
4 float x;
5 float y;
6 int z;
7 float angle;
8 float radius;
9 float speed;
10 float orbIncl;
11} Planet_t;
12
13Planet_t planets[NUM_PLANETS];
14
Wrapped for easier reading. Turn wrap off to inspect exact line lengths.

Each planet object has the following properties:

  • sprite - a handle to the bitmap resource used for rendering of the particle
  • (x, y) - screen coordinate of the plant;
  • z - "depth" coordinate of the planet;
  • angle - current angular position of the planet;
  • radius - a radius of planet's orbit
  • speed - plant angular speed
  • orbIncl - an inclanation of planet's orbit

Next step is to complete an emission stage, or, in other words, to put our planets at initial positions:

Simple Shapes - Bitmaps
CPP
1void Bitmaps::InitializeResources()
2{
3 ...
4
5 //Load images
6 bkg_image = Cubios::GFX_getAssetId("bkg.png");
7 fg_image = Cubios::GFX_getAssetId("fg.png");
8 sun_image = Cubios::GFX_getAssetId("star.png");
9
10 //Initialize sprites
11 planets[0].sprite = Cubios::GFX_getAssetId("planet1.png");
12 planets[0].x = 120.0;
13 planets[0].y = 120.0;
14 planets[0].angle = 0;
15 planets[0].radius = 100;
16 planets[0].speed = 0.05;
17 planets[0].orbIncl = 0.2;
18
19 planets[1].sprite = Cubios::GFX_getAssetId("planet2.png");
20 planets[1].x = 120.0;
21 planets[1].y = 120.0;
22 planets[1].angle = 30;
23 planets[1].radius = 60;
24 planets[1].speed = 0.07;
25 planets[1].orbIncl = 0.22;
26
27 planets[2].sprite = Cubios::GFX_getAssetId("planet3.png");
28 planets[2].x = 120.0;
29 planets[2].y = 120.0;
30 planets[2].angle = 200;
31 planets[2].radius = 160;
32 planets[2].speed = 0.01;
33 planets[2].orbIncl = 0.6;
34
35 ...
36}
37
Wrapped for easier reading. Turn wrap off to inspect exact line lengths.

Drawing sprites

We have all bitmap resources loaded, so let's draw them!

The simulation stage of our particle system is where we animate sprites of our planets to make them orbit the star. It is done in ON_PhysicsTick() callback:

Simple Shapes - Bitmaps
CPP
1void Bitmaps::on_PhysicsTick(const std::array<Cubios::Screen, 3>& screens)
2{
3 //Calculate positions of plantes
4 for(int i=0;i<NUM_PLANETS;i++)
5 {
6 float fSin = sin(planets[i].angle);
7 float fCos = cos(planets[i].angle);
8
9 planets[i].angle += planets[i].speed;
10 if(planets[i].angle>PI_2) planets[i].angle = planets[i].angle-PI_2;
11
12 planets[i].x = 120.0 + planets[i].radius*fSin;
13 planets[i].y = 120.0 + planets[i].radius*fCos*planets[i].orbIncl*incl;
14
15 //Is the planet ahead of behind the sun?
16 if(planets[i].angle>PI/2 && planets[i].angle<PI_2-PI/2)
17 {
18 planets[i].z = 1;
19 }
20 else
21 {
22 planets[i].z = -1;
23 }
24 }
25
26 //Animate global inclanation
27 if(incl<1.0)
28 incl+=0.001;
29 else
30 if(incl>-1.0)
31 {
32 incl-=0.001;
33 }
34}
35
Wrapped for easier reading. Turn wrap off to inspect exact line lengths.

The actual algorithm is quite simple: coordinates of each planet are getting changed according to the circle equation. The "depth" parameter is evaluated from the value of current angular position of a planet. It is needed to be able to tell whether the planet is behind or ahead of the star.

And finally, the rendering stage is as usual performed in on_Render() callback:

Simple Shapes - Bitmaps
CPP
1void Bitmaps::on_Render(std::array<Cubios::Screen, 3>& screens)
2{
3 for(auto it = screens.begin(); it != screens.end(); ++it)
4 {
5 Cubios::GFX_setRenderTarget(it->ID());
6
7 //Render background
8 Cubios::GFX_drawImage(this->bkg_image, 128,128, 0xFF, Cubios::Gfx::Colors::black, 100,100, 0, Cubios::GFX_mirror_t::MIRROR_BLANK);
9
10
11 //Render planets in proper z-order
12 for(int i=0;i<NUM_PLANETS;i++)
13 {
14 if(planets[i].z<0)
15 {
16 //Render planets behind the sun
17 Cubios::GFX_drawImage(planets[i].sprite,(int)planets[i].x,(int)planets[i].y, 0xFF, Cubios::Gfx::Colors::black, 100,100, 0, Cubios::GFX_mirror_t::MIRROR_BLANK);
18 }
19 }
20
21 //Render sun
22 Cubios::GFX_drawImage( this->sun_image,128,128, 0xFF, Cubios::Gfx::Colors::black, 100,100, 0, Cubios::GFX_mirror_t::MIRROR_BLANK);
23
24
25 for(int i=0;i<NUM_PLANETS;i++)
26 {
27 if(planets[i].z>0)
28 {
29 //Render planets in from of the sun
30 Cubios::GFX_drawImage(planets[i].sprite,(int)planets[i].x,(int)planets[i].y, 0xFF, Cubios::Gfx::Colors::black, 100,100, 0, Cubios::GFX_mirror_t::MIRROR_BLANK);
31 }
32 }
33
34 //Render foreground glow
35 Cubios::GFX_drawImage(this->fg_image,128,128, 0xFF, Cubios::Gfx::Colors::black, 100,100, 0, Cubios::GFX_mirror_t::MIRROR_BLANK);
36
37 Cubios::GFX_render();
38 }
39}
40
Wrapped for easier reading. Turn wrap off to inspect exact line lengths.

To render bitmaps, GFX_drawImage function is used:

Simple Shapes - Bitmaps
CPP
1i32_t GFX_drawImage(spriteID_t id, i32_t x, i32_t y, u32_t opacity, u32_t colorKey, u32_t scale_x, u32_t scale_y, u32_t angle, u32_t mirror)
2
3 Parameters:
4 id - bitmap resource handle value obtained with GFX_getAssetId() function
5 x - X screen coordinate of bitmap pivot point, [0, 240]
6 y - Y screen coordinate of bitmap pivot point, [0, 240]
7 opacity - image global opacity value, [0,255]
8 colorKey - image color key value
9 scale_x - horizontal axis scaling coeffitent, [0,100]. Value of 100 means full image width
10 scale_y - vertical axis scaling coeffitent, [0,100]. Value of 100 means full image height
11 angle - bitmap rotation angle, [0, 359]
12 mirror - bitmap mirroring mode. See documentation.
13
14
Wrapped for easier reading. Turn wrap off to inspect exact line lengths.
Context Rail

Project files

Bitmaps.cpp
project/src/Bitmaps.cpp
Bitmaps.h
project/src/Bitmaps.h
wowcubeapp-build.json
project/wowcubeapp-build.json
Jump Grid

On This Page

Rendering BitmapsThe exampleLoading the bitmap dataDrawing sprites
Context Rail

Related nodes

info.json
Examples / SDK 6.1 / C++ / rendering / Simple Shapes - Bitmaps
Bitmaps.cpp
Examples / SDK 6.1 / C++ / rendering / Simple Shapes - Bitmaps / project / src
Bitmaps.h
Examples / SDK 6.1 / C++ / rendering / Simple Shapes - Bitmaps / project / src
wowcubeapp-build.json
Examples / SDK 6.1 / C++ / rendering / Simple Shapes - Bitmaps / project
Previous Node
wowcubeapp-build.json
Examples / SDK 6.1 / C++ / rendering / Simple Shapes - Circles / project
Next Node
info.json
Examples / SDK 6.1 / C++ / rendering / Simple Shapes - Bitmaps