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.1PawnrenderingProject 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 / Pawn / 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
PAWN
1new bkg_image;
2new fg_image;
3new sun_image;
4
Wrapped for easier reading. Turn wrap off to inspect exact line lengths.

and load them when cubeapp initializes:

Simple Shapes - Bitmaps
PAWN
1ON_Init()
2{
3 ...
4
5 //Load images
6 bkg_image = GFX_getAssetId("bkg.png");
7 fg_image = GFX_getAssetId("fg.png");
8 sun_image = 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
PAWN
1GFX_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
PAWN
1new planets[NUM_PLANETS][.sprite, Fixed:.x, Fixed:.y,.z,.angle,Fixed:.radius,.speed,Fixed:.orbIncl];
2
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
PAWN
1public ON_Init(id, size, const pkt[])
2{
3 ...
4
5 //Load images
6 bkg_image = GFX_getAssetId("bkg.png");
7 fg_image = GFX_getAssetId("fg.png");
8 sun_image = GFX_getAssetId("star.png");
9
10 //Initialize sprites
11 planets[0].sprite = 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 = 5;
17 planets[0].orbIncl = 0.2;
18
19 planets[1].sprite = 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 = 7;
25 planets[1].orbIncl = 0.22;
26
27 planets[2].sprite = 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 = 1;
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
PAWN
1public ON_PhysicsTick()
2{
3 //Calculate positions of plantes
4 for(new i=0;i<NUM_PLANETS;i++)
5 {
6 new fs = FixedSin(planets[i].angle);
7
8 new Fixed:fSin = fixed(FixedSin(planets[i].angle))/256.0;
9 new Fixed:fCos = fixed(FixedCos(planets[i].angle))/256.0;
10
11 planets[i].angle += planets[i].speed;
12 if(planets[i].angle>360) planets[i].angle = planets[i].angle-360;
13
14 planets[i].x = 128.0 + planets[i].radius*fSin;
15 planets[i].y = 128.0 + planets[i].radius*fCos*planets[i].orbIncl*incl;
16
17 //Is the planet ahead of behind the sun?
18 if(planets[i].angle>90 && planets[i].angle<270)
19 {
20 planets[i].z = 1;
21 }
22 else
23 {
24 planets[i].z = -1;
25 }
26 }
27
28 //Animate global inclanation
29 if(incl<1.0)
30 incl+=0.001;
31 else
32 if(incl>-1.0)
33 {
34 incl-=0.001;
35 }
36}
37
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
PAWN
1public ON_Render()
2{
3 for(new screenNumber = 0; screenNumber<3; screenNumber++)
4 {
5 GFX_setRenderTarget(screenNumber);
6
7 //Render background
8 GFX_drawImage([128,128], 0xFF, 0, 100, 100, 0, MIRROR_BLANK, bkg_image);
9
10 //Render planets in proper z-order
11 for(new i=0;i<NUM_PLANETS;i++)
12 {
13 if(planets[i].z<0)
14 {
15 //Render planets behind the sun
16 GFX_drawImageXY(fint(planets[i].x),fint(planets[i].y), 0xFF, 0, 100, 100, 0, MIRROR_BLANK, planets[i].sprite);
17 }
18 }
19
20 //Render sun
21 GFX_drawImage([128,128], 0xFF, 0, 100, 100, 0, MIRROR_BLANK, sun_image);
22
23 for(new i=0;i<NUM_PLANETS;i++)
24 {
25 if(planets[i].z>0)
26 {
27 //Render planets in from of the sun
28 GFX_drawImageXY(fint(planets[i].x),fint(planets[i].y), 0xFF, 0, 100, 100, 0, MIRROR_BLANK, planets[i].sprite);
29 }
30 }
31
32 //Render foreground glow
33 GFX_drawImage([128,128], 0xFF, 0, 100, 100, 0, MIRROR_BLANK, fg_image);
34
35
36 //Commit to all three screens
37 GFX_render();
38 }
39}
40
Wrapped for easier reading. Turn wrap off to inspect exact line lengths.

To render bitmaps, GFX_drawImageXY function is used:

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

Project files

main.pwn
project/src/main.pwn
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 / Pawn / rendering / Simple Shapes - Bitmaps
main.pwn
Examples / SDK 6.1 / Pawn / rendering / Simple Shapes - Bitmaps / project / src
wowcubeapp-build.json
Examples / SDK 6.1 / Pawn / rendering / Simple Shapes - Bitmaps / project
Simple Shapes - Points
Examples / SDK 6.1 / Pawn / rendering
Previous Node
wowcubeapp-build.json
Examples / SDK 6.1 / Pawn / rendering / Simple Shapes - Circles / project
Next Node
info.json
Examples / SDK 6.1 / Pawn / rendering / Simple Shapes - Bitmaps