32blit

the embedded game development SDK

Loading images for 32blit - C++

For more information on the 32blit asset manager, see the 32blit asset pipeline documentation.

Fetching the necessary files

First, you will need to download the two images which we need for “Ninja Thief”:

You can then save these files inside a new folder called assets within the project directory.

Using assets.yml

The 32blit SDK uses the 32blit asset manager in order to parse any assets you need to supply to your game. You use the assets.yml file to tell the asset manager which files you require, along with any necessary additional information.

The default assets.yml file may look similar to this:

assets.cpp:
  assets/no-image.png:
    name: asset_no_image

In this example, line 1 tells the asset handler to place the following assets into a new file called assets.cpp (a corresponding assets.hpp will also be created). Line 2 provides the asset handler with the path to the asset you need it to convert (in this case, assets/no-image.png), and line 3 provides the name of the variable which this image will be stored in (asset_no_image).

In our case, we have two images, so we can specify one after the other, keeping a careful eye on the indentation:

assets.cpp:
  assets/background.png:
    name: asset_background

  assets/spritesheet.png:
    name: asset_spritesheet

CMake should automatically pick up any changes we have made to the assets.yml file, and during compilation it will tell the 32blit asset manager to process this file.

If the changes aren’t detected, try re-running CMake.

The assets.cpp file produced will contain two arrays of bytes, each representing the pixel data of one of the images we listed to assets.yml.

Accessing the images in our code

In our code, we can access the assets generated by the 32blit asset manager by including the assets.hpp header file (add this line in ninja_thief.hpp):

#include "assets.hpp"

We will now be able to refer to the asset_background and asset_spritesheet byte arrays in our code.

Surfaces

The 32blit SDK uses the Surface structure to handle images, so we need to create a Surface object containing the image data, for each image.

We can achieve this using the static member method load:

Surface* background_image = Surface::load(asset_background);

We will put this setup code into the init function, but since we need to access the surface in the render function, we need to declare it first, as a global variable:

// Outside of our functions, just after the includes
Surface* background = nullptr;

// Perform setup here
void init() {
    // Load the background from assets.cpp
    background = Surface::load(asset_background);
}