Art and sound
Boxes and circles were enough to make the game work. Now it gets pictures and
sounds: the rabbit that has been in resources/ since the start, and a coin, a bat and
three sounds copied from one of the framework's own example games.
The files#
Run these from the game's folder. They copy from the framework you cloned into your home
folder when you installed rmp:
cp ~/raylib_multiplatform/examples/games/07_platformer/resources/coin.aseprite resources/cp ~/raylib_multiplatform/examples/games/07_platformer/resources/bat.aseprite resources/cp ~/raylib_multiplatform/examples/games/07_platformer/resources/coin.ogg resources/cp ~/raylib_multiplatform/examples/games/07_platformer/resources/hurt.ogg resources/cp ~/raylib_multiplatform/examples/games/07_platformer/resources/music.ogg resources/They are Kenney's work (kenney.nl), from his Pixel Platformer, Impact Sounds, Interface Sounds
and Music Loops packs, and so is the rabbit, from his Animal Pack. All of it is CC0: public domain,
free for any use, with nothing asked in return, not even a credit. The framework's copy keeps the
details in examples/games/07_platformer/resources/License-kenney.txt, and the
rabbit's licence is the License.txt already in your resources/.
The two .aseprite files are sprite sheets as Aseprite saves them, several frames
of a little animation in each. The framework reads them as they are: there is no export step
between drawing a frame and seeing it in the game.
A picture for the rabbit#
The player gets its picture in place of the white box, in its _ready(). Replace
the player's code:
#include "player.h"
#include "layers.h"
#include <rmp/assets.h>
#include <rmp/behavior.h>
#include <rmp/input.h>
#include <rmp/math.h>
namespace {
constexpr float SPEED = 260; // design units per second
} // namespace
void Player::_ready() {
sprite.texture = rmp::assets::load_texture("rabbit.png");
sprite.size = { 44, 64 };
// The body, and not the ears: a bat that only brushes an ear misses.
collider = rmp::rect({ 34, 40 });
collider.offset = { 0, 10 };
collision_layer = layer::PLAYER;
collision_mask = layer::COIN | layer::BAT;
// Eight directions, from the keyboard, a d-pad or a stick.
add<rmp::behavior::TopDown>({ .speed = SPEED });
}
void Player::_update(float /*delta*/) {
// The mouse held down, or a finger on the screen: run towards it. Not when
// the pointer is on a button, which is the interface's and not the game's.
if (rmp::input::pointer_down() && !rmp::input::consumed_pointer()) {
const Vector2 to = rmp::input::pointer() - position;
if (Vector2Length(to) > 8) velocity = Vector2Normalize(to) * SPEED;
}
}rmp::assets::load_texture("rabbit.png")
takes a name, not a path: the file is looked for in resources/, or in the pack that
a release can carry instead, as the end of this page shows. What it returns is an
rmp::Texture, which frees itself when the last copy of
it goes, so there is no unload to remember; asking twice for the same name gives the same texture
back, not a second copy.
An object with a sprite draws that instead
of its shape. The picture is 284 by 414 pixels, far too big for the arena, and
sprite.size draws it at 44 by 64 units instead.
Without a shape, an object collides as its picture, and a rabbit's picture is mostly ears. The
collider says what it collides as instead: a
box over the body, moved down by its offset, so a bat that only brushes an ear
misses.
Coins that spin, bats that flap#
The coins and the bats lose their circles and get their sheets, and two sounds go in. Replace the game scene's code; its header does not change:
#include "game.h"
#include "../objects/layers.h"
#include "../objects/player.h"
#include "game_over.h"
#include "pause.h"
#include <rmp/assets.h>
#include <rmp/audio.h>
#include <rmp/behavior.h>
#include <rmp/input.h>
#include <rmp/random.h>
#include <rmp/ui.h>
#include <string>
namespace {
// The arena is the design resolution: [window] width and height in the .toml.
constexpr Rectangle ARENA{ 0, 0, RMP_WINDOW_WIDTH, RMP_WINDOW_HEIGHT };
constexpr float SECONDS_PER_BAT = 4;
} // namespace
void GameScene::_ready() {
background = Color{ 52, 92, 60, 255 }; // grass
// P, Escape, or the gamepad's Start button.
rmp::input::action("pause", KEY_P, KEY_ESCAPE, GAMEPAD_BUTTON_MIDDLE_RIGHT);
auto &player = spawn<Player>({
.position = { ARENA.width / 2, ARENA.height / 2 },
.edges = rmp::Edge::CLAMP,
.bounds = ARENA,
});
player.add<rmp::behavior::Health>({
.hp = 3,
.destroy_on_death = false, // it stays on screen, under the game over
.on_death = [this](rmp::Object &) { caught(); },
.on_damage = [](rmp::Object &, int) { rmp::audio::play("hurt"); },
.hurt_by = layer::BAT,
});
_player = player.handle();
spawn_coin();
}
void GameScene::_update(float delta) {
if (rmp::input::just_pressed("pause")) rmp::Scene::push<PauseScene>();
_next_bat -= delta;
if (_next_bat <= 0) {
spawn_bat();
_next_bat = SECONDS_PER_BAT;
}
}
void GameScene::_draw() {
const int lives = _player->get<rmp::behavior::Health>()->hp;
rmp::ui::begin({ .placement = rmp::ui::Align::TOP_LEFT });
rmp::ui::row({ .gap = 24 }, [&] {
rmp::ui::text("Coins " + std::to_string(_coins));
rmp::ui::text("Lives " + std::to_string(lives));
#if defined(PLATFORM_ANDROID) || defined(PLATFORM_IOS)
// A phone has no key to pause with.
if (rmp::ui::button("Pause", { .style = rmp::ui::Variant::GHOST })) {
rmp::Scene::push<PauseScene>();
}
#endif
});
rmp::ui::end();
}
void GameScene::spawn_coin() {
// Anywhere in the arena, clear of its edges and of the score at the top.
const Vector2 at{ rmp::random::range(40.0f, ARENA.width - 40),
rmp::random::range(80.0f, ARENA.height - 40) };
auto &coin = spawn({ .position = at, .scale = { 2, 2 } });
coin.sprite.sheet = rmp::assets::load_sheet("coin.aseprite");
coin.sprite.play("spin");
coin.collision_layer = layer::COIN;
coin.collision_mask = layer::PLAYER;
coin.on_collision([this](rmp::Object &self, rmp::Object &) {
self.destroy();
++_coins;
rmp::audio::play("coin");
spawn_coin();
});
}
void GameScene::spawn_bat() {
// From just outside the arena, above it or below it.
const Vector2 at{ rmp::random::range(0.0f, ARENA.width),
rmp::random::chance(0.5f) ? -40.0f : ARENA.height + 40 };
auto &bat = spawn({ .position = at, .scale = { 2, 2 } });
bat.sprite.sheet = rmp::assets::load_sheet("bat.aseprite");
bat.sprite.play("fly");
bat.collision_layer = layer::BAT;
bat.collision_mask = layer::PLAYER;
// Each one a little faster or slower, so that they do not fly as one.
bat.add<rmp::behavior::Follow>({
.target = _player,
.speed = rmp::random::range(70.0f, 120.0f),
});
}
void GameScene::caught() { rmp::Scene::push<GameOverScene>(_coins); }rmp::assets::load_sheet() reads an
.aseprite file, with its frames, how long each one shows, and its tags: the named
animations in it. sprite.play("spin") names one
of those tags, and the framework moves through its frames from then on. The names are the ones in
the file, and nothing else: spin and fly are what these two sheets call
their animations. A tag the sheet does not have plays nothing and says so once, in the log.
The art is small, 18 pixels for a coin and 24 for a bat, and .scale = { 2, 2 }
draws it twice the size. scale applies to what
the object collides as as well, so the bigger bat bites from where it is drawn.
Sound#
rmp::audio::play("coin") plays a sound by name,
without its extension: coin finds coin.wav, coin.ogg,
coin.mp3 or coin.qoa, the first of them that exists. The sound is loaded
the first time it is played and kept. Playing it again while it still sounds starts another
voice instead of cutting the first one off, up to four at once; a fifth, while all four still
sound, cuts off the one that has played longest. The bite's sound goes in Health's
.on_damage, which runs every time a life is lost, the last one too.
The sound device opens with the first sound, so a game that plays none never opens it. A machine with no sound device at all is not an error: every call simply does nothing.
Music#
The music starts with the menu, so the menu gets a _ready(). Replace both of its
files:
#pragma once
// The title screen: the game's name, Play and Quit.
#include <rmp/scene.h>
class MainMenuScene : public rmp::Scene {
public:
void _ready() override;
void _draw() override;
};#include "main_menu.h"
#include "game.h"
#include <rmp/app.h>
#include <rmp/audio.h>
#include <rmp/ui.h>
void MainMenuScene::_ready() {
// Already playing when the menu comes back after a game: then it carries on.
rmp::audio::music("music");
}
void MainMenuScene::_draw() {
rmp::ui::begin();
rmp::ui::text(RMP_WINDOW_TITLE, { .size = rmp::ui::Size::LARGE });
if (rmp::ui::button("Play", { .style = rmp::ui::Variant::PRIMARY })) {
rmp::Scene::change<GameScene>();
}
#if !defined(PLATFORM_IOS)
if (rmp::ui::button("Quit")) rmp::app::quit();
#endif
rmp::ui::end();
}rmp::audio::music("music") streams
music.ogg, looping, and the framework keeps it fed every frame, under a pause menu
too. Asking for the track that is already playing does not start it again, so the song carries on
when a game ends and the menu comes back, instead of jumping to its beginning.
In a browser, nothing is heard until the player has clicked, touched or pressed a key: browsers keep a page silent until then, and no game can get round it.
resources/ in a release#
While you work, the game reads the files in resources/ as they are. A release can
carry them as one file instead, resources.rres, encrypted, and the game reads that
with the same calls and the same names: nothing in the code knows which it is. You can try it
here:
rmp pack
rmp run
rmp unpackrmp pack writes the pack into resources/, and the game reads it for
as long as it is there; rmp unpack deletes it, and the loose files are read again.
Most of the desktop builds CI makes for a release ship the pack in place of the loose files.
The pack takes every file below resources/, its folders too, each under its path
there, so a game names "sprites/hero.png" the same in development and in a release.
A release for
the desktop reads the resources/ that sits next to its executable, so it starts the
same from a file manager, a shortcut or a terminal open in some other folder: when there is one
there, the game makes the executable's folder its working directory before it loads anything.
When there is none, it stays in the folder it was started from and reads the
resources/ there. On NetBSD and OpenBSD raylib cannot tell where the
executable is, so a release there reads the resources/ of the folder it was started
from, and its log says so: ASSETS: this system does not say where the executable is…
The encryption keeps casual hands off your files and nothing more. Its password is
[resources] rres_password in the .toml, which rmp new made
up for this game, and it ends up inside the game's binary.
Run it#
rmp runMusic on the menu, a rabbit in the arena, and spinning coins and flapping bats, each with its sound. The Sprites, Assets and Audio chapters of the manual go further into each.
