User Guide: I Modules - Suraii/Azurite GitHub Wiki

I - Modules

What's a module ?

Let's start with the basics.

Modules are part of the game that allows it to communicate with its environment (keyboard, screen, speakers, ...).

Placeholders

Modules can be anything, but the Game specify 4 main types of modules called placeholders :

  • The Display Module, used to draw sprites
  • The Input Module, used to fetch inputs
  • The Audio Module, used to play sounds
  • The Asset Module, used to load files

These placeholders aren't mandatory if you don't need all these features

Built-in Modules

At this moment there is one built-in module named SfmlModule, it can be used as any of the 4 placeholders.

Example

#include <Azurite/Game.hpp> // Azurite motherclass
#include <Azurite/modules/SfmlModule.hpp> // The built-in Sfml module

int main() {
    Azurite::Game game; // Creating a game object, the first step of any Azurite project
    
    std::unique_ptr<Azurite::AModule> sfml_mod(new Azurite::SfmlModule(game)); // Creating an instance of the Sfml module

    game.addModule("sfml", std::move(sfml_mod)) // Adding the module to our game
    .useAsInputModule() // Specify that we want to use this module as the default Input module
    .useAsAudioModule() // ... Audio module
    .useAsAssetModule() // ... Asset module
    .useAsDisplayModule(); // ... Display module
}

Creating your own modules

If the built-in Sfml module doesn't fulfill all your needs, don't worry you can add your own modules.

New types of module

If you need modules for different things than, input, audio, asset or display, you can create your own modules types. You just have to create a class that inherit the module abstract class, named AModule.

Example

Let's say you want to create a multiplayer game, and to do so, you want to implement a Network module.

#include <Azurite/Game.hpp>
#include <Azurite/Modules.hpp> // The modules abstract classes header

#include <iostream>

class NetworkModule : public Azurite::AModule { // Creating our module class
public:
    NetworkModule(Azurite::Game &owner) : AModule(owner) {} // We need a reference to the game motherclass in the ctor
    ~NetworkModule() {}
    void onStart() override {
        // Things to do when the game starts
    }
    void onTick() override {
        // Things to do on each game tick (100 times / second)
    }
    void onStop() override {
        // Things to do when the game stops
    }
    void do_network_things() { // Dummy method
        std::cout << "Doing network things" << std::endl;
    }
};

int main()
{
    Azurite::Game game;

    std::unique_ptr<Azurite::AModule> network_mod(new NetworkModule(game)); // Creating an instance of our module

    game.addModule("network", std::move(network_module)); // Adding it to the game

    NetworkModule &module = dynamic_cast<NetworkModule &>(game.getModule("network")); // Fetching our module from the game class

    module.do_network_things(); // Calling our dummy method 
}

Creating placeholder modules

Let's say you want to use something else than the Sfml module for displaying sprite. Well you can create your own class, it just have to inherit the Display module abstract class, named ADisplayModule.

Example

#include <Azurite/Game.hpp>
#include <Azurite/Modules.hpp>

class MyDisplayModule : public Azurite::ADisplayModule { // Creating your module class
    // Placeholders abstracts like ADisplayModule inherits AModule, so you have to implement its methods and call its ctor
    MyDisplayModule(Azurite::Game &owner) : ADisplayModule(owner), AModule(owner) {}
    ~MyDisplayModule() {}
    // Same behavior than on the above custom module
    void onStart() override {
    }
    void onTick() override {
    }
    void onStop() override {
    }
    // These methods will be called on drawable entities (you'll see these on the entity chapter)
    // Implement them how you want
    void drawSprite(const std::string spriteSheet, unsigned tileId, Azurite::Transform2D transform) override {

    }
    void drawRectangle(Azurite::Transform2D shape, Azurite::Color color) override {

    }
};

int main()
{
    Azurite::Game game;

    std::unique_ptr<Azurite::AModule> my_display_mod(new MyDisplayModule(game));

    game.addModule("my", std::move(my_display_mod))
    .useAsDisplayModule();
}

That's all for modules !

TLDR

  • Modules are used for input / output
  • Placeholders are special types of modules, like display module or asset module
  • There's a built-in Sfml module that can be used as any of the 4 placeholders
  • You can create your own modules if needed

Don't hesitate to contact me if you need anything !

⚠️ **GitHub.com Fallback** ⚠️