Start

Theory

Reference

Examples

← All examples
Rendering · Getting Started

Background Changer

About the smallest ASGE program

About as small as an ASGE program gets: one component holding an RGBA color, cleared to the screen every frame, randomized on a keypress.

A good first file to read before anything involving ECS or the asset pipeline.

Controls

  • SPACE Randomize the background color
RenderingGetting Started
Background Changer screenshot

Run it yourself

build just this example
cmake --build --preset windows-debug --target background_changer
# binary lands under bin/ (e.g. bin/Debug/background_changer.exe on Windows)

See Installation for the full build setup.

Source

examples/background_changer/Game.hpp
#pragma once

#include <ASGE/ASGE.hpp>
#include "Component.hpp"

class BackgroundChangingState final : public asge::game::state::IGameState<int>
{
    BackgroundComponent m_BackgroundComponent{ BackgroundComponent::GetRandomColor() };

public:
    [[nodiscard]] std::optional<asge::game::state::Transition<int>>
    Update(float inDeltaTime, asge::input::InputState const& inInput) override;
    void Render(asge::video::IRenderer& inRenderer) override;
    void OnSystemEvent(asge::event::SystemEvent const& inSysEvent) override;
};

class BackgroundChangingGame final : public asge::game::Game<int>
{
public:
    explicit BackgroundChangingGame(asge::video::IRenderer& inRenderer, asge::audio::AudioDevice& inAudioDev);

protected:
    [[nodiscard]] std::unique_ptr<StateType> CreateState(int inId) override;
};
examples/background_changer/Game.cpp
#include "Game.hpp"

std::optional<asge::game::state::Transition<int>> BackgroundChangingState::Update(
    [[maybe_unused]] float inDeltaTime, [[maybe_unused]] asge::input::InputState const& inInput)
{
    return std::nullopt;
}

void BackgroundChangingState::Render(asge::video::IRenderer &inRenderer)
{
    inRenderer.Clear(m_BackgroundComponent.GetColor());
}

void BackgroundChangingState::OnSystemEvent(asge::event::SystemEvent const &inSysEvent)
{
    if ( auto const* keyEvent = inSysEvent.TryGet<asge::event::KeyboardEvent>() )
    {
        if ( keyEvent->s_Down && keyEvent->s_Keycode == asge::input::Keycode::SPACE )
        {
            m_BackgroundComponent.SetColor( BackgroundComponent::GetRandomColor() );
        }
    }
}

BackgroundChangingGame::BackgroundChangingGame(asge::video::IRenderer& inRenderer, asge::audio::AudioDevice& inAudioDev)
: Game(inRenderer, inAudioDev)
{
    SetInitialState(0);
}

std::unique_ptr<BackgroundChangingGame::StateType>
BackgroundChangingGame::CreateState([[maybe_unused]] int inId)
{
    return std::make_unique<BackgroundChangingState>();
}
examples/background_changer/Component.hpp
#pragma once

#include <random>
#include <ASGE/ASGE.hpp>

namespace asge_g = asge::graphics;

class BackgroundComponent
{
private:
    using color_type = asge_g::RGBA_Color;
    color_type m_Color;
public:
    BackgroundComponent( color_type const& initColor )
        : m_Color( initColor )
    {}
    
    color_type const& GetColor() const noexcept;
    static color_type GetRandomColor() noexcept;
    void SetColor( color_type const& inColor );
};

Browse the full example on GitHub →