Skip to content

Commit

Permalink
Add simplified interface for publishing msgs (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
jthomperoo committed Jan 2, 2023
1 parent fa7c40e commit c2d0ceb
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 74 deletions.
12 changes: 6 additions & 6 deletions CONTRIBUTING.md
Expand Up @@ -29,17 +29,17 @@ duplicated effort that would be great.

The project has the following dependencies:

- `cmake` == `3.22.X`
- `clang` == `11.1.X`
- `clang-tidy` == `11.1.X`
- `clang-format` == `11.1.X`
- `emscripten` == `3.1.X`
- `cmake` == `3.22.1`
- `clang` == `11.0.0`
- `clang-tidy` == `11.0.0`
- `clang-format` == `11.0.0`
- `emscripten` == `3.1.1`

Later minor and patch versions of these dependencies should still work, the CI build just uses the versions above.

To view the docs locally, the project requires:

- `python` == `3.8.X`
- `python` == `3.8.11`

To install the required pip dependencies to view the docs locally, run:

Expand Down
16 changes: 14 additions & 2 deletions docs/Architecture/Messages/message_bus.md
Expand Up @@ -13,15 +13,27 @@ Messages added to the message bus should be added as a unique pointer
that a message is sent with should be held by the message bus, helping to prevent memory leaks and memory management
confusion. The message bus will take ownership of the pointer and will clean it up after it has been sent out.

An example of adding a message to the message bus looks like this (taken from the Game class):
An example of adding a message to the message bus looks like this:

```c++
this->m_messageBus->Publish(std::make_unique<JamJar::MessagePayload<float>>(
this->m_messageBus->Publish(new JamJar::MessagePayload<float>(
JamJar::System::MESSAGE_UPDATE, float(TIME_STEP) / MICROSECOND_TO_SECOND_CONVERSION));
```
This dispatches a new update message, with the delta time as a payload.
The message is given as a pointer, but once it's published its memory will be managed by the message bus, so you should
not re-use messages, each time you publish create a new message. The message will be disposed of once it is no longer
needed.
Behind the scenes the message is wrapped in a `unique_ptr` - you can instead directly pass in a `unique_ptr` message
to the `Publish` method if you prefer:
```c++
this->m_messageBus->Publish(std::make_unique<JamJar::MessagePayload<float>>(
JamJar::System::MESSAGE_UPDATE, float(TIME_STEP) / MICROSECOND_TO_SECOND_CONVERSION));
```

## Dispatching Messages

Every message in the message bus is *dispatched* - sent out to any subscribing part of the engine - when the `Dispatch`
Expand Down
3 changes: 1 addition & 2 deletions docs/Documentation/image_loading.md
Expand Up @@ -28,8 +28,7 @@ An image can be loaded by sending a `MESSAGE_REQUEST_FILE_TEXTURE_LOAD` message
payload for the message, for example:

```c++
messageBus->Publish(
std::make_unique<JamJar::MessagePayload<std::unique_ptr<JamJar::Standard::FileTextureRequest>>>(
messageBus->Publish(new JamJar::MessagePayload<std::unique_ptr<JamJar::Standard::FileTextureRequest>>(
JamJar::Standard::FileTextureSystem::MESSAGE_REQUEST_FILE_TEXTURE_LOAD,
std::unique_ptr<JamJar::Standard::FileTextureRequest>(
new JamJar::Standard::FileTextureRequest({JamJar::hash("smiley"), "/assets/texture.png"}))));
Expand Down
17 changes: 5 additions & 12 deletions docs/Documentation/window_management_and_fullscreen.md
Expand Up @@ -90,9 +90,7 @@ The aspect ratio can be set at runtime with a message.

...

auto msg = std::make_unique<JamJar::MessagePayload<double>>(
JamJar::Standard::WindowSystem::MESSAGE_SET_ASPECT_RATIO, 4/3);
messageBus->Publish(std::move(msg));
messageBus->Publish(new JamJar::MessagePayload<double>(JamJar::Standard::WindowSystem::MESSAGE_SET_ASPECT_RATIO, 4/3););
```
This sets the aspect ratio to `4/3`.
Expand Down Expand Up @@ -120,9 +118,8 @@ The maximum resolution can be set at runtime with a message.
...
auto msg = std::make_unique<JamJar::MessagePayload<std::pair<int, int>>>(
JamJar::Standard::WindowSystem::MESSAGE_SET_MAX_RESOLUTION, std::pair<int, int>(400, 300));
messageBus->Publish(std::move(msg));
messageBus->Publish(new JamJar::MessagePayload<std::pair<int, int>>(
JamJar::Standard::WindowSystem::MESSAGE_SET_MAX_RESOLUTION, std::pair<int, int>(400, 300)));
```

This sets the maximum resolution to `400x300`.
Expand All @@ -146,9 +143,7 @@ You can enter fullscreen by sending a message:

...

auto msg =
std::make_unique<JamJar::Message>(JamJar::Standard::WindowSystem::MESSAGE_REQUEST_ENTER_FULLSCREEN);
this->messageBus->Publish(std::move(msg));
this->messageBus->Publish(new JamJar::Message(JamJar::Standard::WindowSystem::MESSAGE_REQUEST_ENTER_FULLSCREEN));
```
You can check if the request was successful by listening for these two events:
Expand All @@ -168,9 +163,7 @@ You can exit fullscreen by sending a message:
...
auto msg =
std::make_unique<JamJar::Message>(JamJar::Standard::WindowSystem::MESSAGE_REQUEST_EXIT_FULLSCREEN);
this->messageBus->Publish(std::move(msg));
this->messageBus->Publish(new JamJar::Message(JamJar::Standard::WindowSystem::MESSAGE_REQUEST_EXIT_FULLSCREEN));
```

You can check if the request was successful by listening for these two events:
Expand Down
42 changes: 27 additions & 15 deletions examples/Fullscreen/src/fullscreen.cpp
Expand Up @@ -13,24 +13,36 @@
#include "standard/file_texture/file_texture_system.hpp"
#include <memory>

using JamJar::Color;
using JamJar::Entity;
using JamJar::Material;
using JamJar::MessagePayload;
using JamJar::Texture;
using JamJar::TextureFilter;
using JamJar::TextureProperties;
using JamJar::Vector2D;
using JamJar::Standard::FileTextureRequest;
using JamJar::Standard::FileTextureSystem;
using JamJar::Standard::_2D::Camera;
using JamJar::Standard::_2D::Sprite;
using JamJar::Standard::_2D::Transform;

Fullscreen::Fullscreen(JamJar::MessageBus *messageBus) : Game(messageBus) {}

void Fullscreen::OnStart() {
this->messageBus->Publish(
std::make_unique<JamJar::MessagePayload<std::unique_ptr<JamJar::Standard::FileTextureRequest>>>(
JamJar::Standard::FileTextureSystem::MESSAGE_REQUEST_FILE_TEXTURE_LOAD,
std::unique_ptr<JamJar::Standard::FileTextureRequest>(new JamJar::Standard::FileTextureRequest(
{.key = JamJar::hash("smiley"),
.path = "/assets/texture.png",
.properties = JamJar::TextureProperties(
{.minFilter = JamJar::TextureFilter::NEAREST, .magFilter = JamJar::TextureFilter::NEAREST})}))));
this->messageBus->Publish(new MessagePayload<std::unique_ptr<FileTextureRequest>>(
FileTextureSystem::MESSAGE_REQUEST_FILE_TEXTURE_LOAD,
std::unique_ptr<FileTextureRequest>(new FileTextureRequest(
{.key = JamJar::hash("smiley"),
.path = "/assets/texture.png",
.properties =
TextureProperties({.minFilter = TextureFilter::NEAREST, .magFilter = TextureFilter::NEAREST})}))));

auto a = new JamJar::Entity(messageBus);
a->Add(new JamJar::Standard::_2D::Transform(JamJar::Vector2D(0, 0), JamJar::Vector2D(160, 90)));
a->Add(new JamJar::Standard::_2D::Sprite(
JamJar::Material(JamJar::Color(0, 1, 1, 1), JamJar::Texture(JamJar::hash("smiley")))));
auto a = new Entity(messageBus);
a->Add(new Transform(Vector2D(0, 0), Vector2D(160, 90)));
a->Add(new Sprite(Material(Color(0, 1, 1, 1), Texture(JamJar::hash("smiley")))));

auto camera = new JamJar::Entity(messageBus);
camera->Add(new JamJar::Standard::_2D::Transform());
camera->Add(new JamJar::Standard::_2D::Camera(JamJar::Color(0, 0, 0)));
auto camera = new Entity(messageBus);
camera->Add(new Transform());
camera->Add(new Camera(JamJar::Color(0, 0, 0)));
}
10 changes: 4 additions & 6 deletions examples/Fullscreen/src/input_listener.cpp
Expand Up @@ -15,15 +15,13 @@ void InputListener::OnMessage(JamJar::Message *message) {
auto *eventMessage = static_cast<JamJar::MessagePayload<JamJar::Standard::SDL2KeyEvent> *>(message);
auto event = eventMessage->payload;
if (event.key == "F") {
auto msg =
std::make_unique<JamJar::Message>(JamJar::Standard::WindowSystem::MESSAGE_REQUEST_ENTER_FULLSCREEN);
this->messageBus->Publish(std::move(msg));
this->messageBus->Publish(
new JamJar::Message(JamJar::Standard::WindowSystem::MESSAGE_REQUEST_ENTER_FULLSCREEN));
break;
}
if (event.key == "E") {
auto msg =
std::make_unique<JamJar::Message>(JamJar::Standard::WindowSystem::MESSAGE_REQUEST_EXIT_FULLSCREEN);
this->messageBus->Publish(std::move(msg));
this->messageBus->Publish(
new JamJar::Message(JamJar::Standard::WindowSystem::MESSAGE_REQUEST_EXIT_FULLSCREEN));
break;
}
break;
Expand Down
35 changes: 23 additions & 12 deletions examples/MouseInput/src/mouse_input.cpp
Expand Up @@ -9,20 +9,31 @@
#include "standard/file_texture/file_texture_system.hpp"
#include <memory>

using JamJar::Color;
using JamJar::Entity;
using JamJar::Material;
using JamJar::MessagePayload;
using JamJar::Texture;
using JamJar::TextureFilter;
using JamJar::TextureProperties;
using JamJar::Vector2D;
using JamJar::Standard::FileTextureRequest;
using JamJar::Standard::FileTextureSystem;
using JamJar::Standard::_2D::Camera;
using JamJar::Standard::_2D::Transform;

MouseInput::MouseInput(JamJar::MessageBus *messageBus) : Game(messageBus) {}

void MouseInput::OnStart() {
this->messageBus->Publish(new MessagePayload<std::unique_ptr<FileTextureRequest>>(
FileTextureSystem::MESSAGE_REQUEST_FILE_TEXTURE_LOAD,
std::unique_ptr<FileTextureRequest>(new FileTextureRequest(
{.key = JamJar::hash("smiley"),
.path = "/assets/texture.png",
.properties =
TextureProperties({.minFilter = TextureFilter::NEAREST, .magFilter = TextureFilter::NEAREST})}))));

this->messageBus->Publish(
std::make_unique<JamJar::MessagePayload<std::unique_ptr<JamJar::Standard::FileTextureRequest>>>(
JamJar::Standard::FileTextureSystem::MESSAGE_REQUEST_FILE_TEXTURE_LOAD,
std::unique_ptr<JamJar::Standard::FileTextureRequest>(new JamJar::Standard::FileTextureRequest(
{.key = JamJar::hash("smiley"),
.path = "/assets/texture.png",
.properties = JamJar::TextureProperties(
{.minFilter = JamJar::TextureFilter::NEAREST, .magFilter = JamJar::TextureFilter::NEAREST})}))));

auto camera = new JamJar::Entity(messageBus);
camera->Add(new JamJar::Standard::_2D::Transform());
camera->Add(new JamJar::Standard::_2D::Camera(JamJar::Color(0, 0, 0)));
auto camera = new Entity(messageBus);
camera->Add(new Transform());
camera->Add(new Camera(Color(0, 0, 0)));
}
49 changes: 30 additions & 19 deletions examples/Sprites/src/simple_game.cpp
Expand Up @@ -13,30 +13,41 @@
#include "standard/file_texture/file_texture_system.hpp"
#include <memory>

using JamJar::Color;
using JamJar::Entity;
using JamJar::Material;
using JamJar::MessagePayload;
using JamJar::Texture;
using JamJar::TextureFilter;
using JamJar::TextureProperties;
using JamJar::Vector2D;
using JamJar::Standard::FileTextureRequest;
using JamJar::Standard::FileTextureSystem;
using JamJar::Standard::_2D::Camera;
using JamJar::Standard::_2D::Sprite;
using JamJar::Standard::_2D::Transform;

SimpleGame::SimpleGame(JamJar::MessageBus *messageBus) : Game(messageBus) {}

void SimpleGame::OnStart() {

this->messageBus->Publish(
std::make_unique<JamJar::MessagePayload<std::unique_ptr<JamJar::Standard::FileTextureRequest>>>(
JamJar::Standard::FileTextureSystem::MESSAGE_REQUEST_FILE_TEXTURE_LOAD,
std::unique_ptr<JamJar::Standard::FileTextureRequest>(new JamJar::Standard::FileTextureRequest(
{.key = JamJar::hash("smiley"),
.path = "/assets/texture.png",
.properties = JamJar::TextureProperties(
{.minFilter = JamJar::TextureFilter::NEAREST, .magFilter = JamJar::TextureFilter::NEAREST})}))));
this->messageBus->Publish(new MessagePayload<std::unique_ptr<FileTextureRequest>>(
FileTextureSystem::MESSAGE_REQUEST_FILE_TEXTURE_LOAD,
std::unique_ptr<FileTextureRequest>(new FileTextureRequest(
{.key = JamJar::hash("smiley"),
.path = "/assets/texture.png",
.properties =
TextureProperties({.minFilter = TextureFilter::NEAREST, .magFilter = TextureFilter::NEAREST})}))));

auto a = new JamJar::Entity(messageBus);
a->Add(new JamJar::Standard::_2D::Transform(JamJar::Vector2D(-30, 0), JamJar::Vector2D(30, 30)));
a->Add(new JamJar::Standard::_2D::Sprite(
JamJar::Material(JamJar::Color(0, 1, 0, 1), JamJar::Texture(JamJar::hash("smiley")))));
auto a = new Entity(messageBus);
a->Add(new Transform(Vector2D(-30, 0), Vector2D(30, 30)));
a->Add(new Sprite(JamJar::Material(JamJar::Color(0, 1, 0, 1), Texture(JamJar::hash("smiley")))));

auto b = new JamJar::Entity(messageBus);
b->Add(new JamJar::Standard::_2D::Transform(JamJar::Vector2D(30, 0), JamJar::Vector2D(30, 30)));
b->Add(new JamJar::Standard::_2D::Sprite(
JamJar::Material(JamJar::Color(1, 0, 1, 1), JamJar::Texture(JamJar::hash("smiley")))));
auto b = new Entity(messageBus);
b->Add(new Transform(Vector2D(30, 0), Vector2D(30, 30)));
b->Add(new Sprite(JamJar::Material(JamJar::Color(1, 0, 1, 1), Texture(JamJar::hash("smiley")))));

auto camera = new JamJar::Entity(messageBus);
camera->Add(new JamJar::Standard::_2D::Transform());
camera->Add(new JamJar::Standard::_2D::Camera(JamJar::Color(0, 0, 0)));
auto camera = new Entity(messageBus);
camera->Add(new Transform());
camera->Add(new Camera(JamJar::Color(0, 0, 0)));
}
1 change: 1 addition & 0 deletions include/message/message_bus.hpp
Expand Up @@ -17,6 +17,7 @@ class MessageBus {
MessageBus();
void Dispatch();
void Publish(std::unique_ptr<Message> message);
void Publish(Message *message);
void Subscribe(Listener *subscriber, uint32_t type);
};
}; // namespace JamJar
Expand Down
2 changes: 2 additions & 0 deletions src/message/message_bus.cpp
Expand Up @@ -27,6 +27,8 @@ void JamJar::MessageBus::Dispatch() {

void JamJar::MessageBus::Publish(std::unique_ptr<Message> message) { this->m_messages.push(std::move(message)); }

void JamJar::MessageBus::Publish(Message *message) { this->m_messages.push(std::unique_ptr<Message>(message)); }

void JamJar::MessageBus::Subscribe(Listener *subscriber, uint32_t type) {
if (this->m_subscribers.count(type) == 0) {
this->m_subscribers[type] = std::vector<Listener *>();
Expand Down

0 comments on commit c2d0ceb

Please sign in to comment.