Skip to content

Commit

Permalink
Add Raylib graphics backend and use it in main
Browse files Browse the repository at this point in the history
Finally bring the newer abstract graphics systems into the main part of
the engine. Create backends for raylib and use them in the
LeonaTestState so we can finally render Leona again. Text rendering and
adjustments to Renderers in GracilisGame are still pending.
  • Loading branch information
meisekimiu committed Jun 4, 2024
1 parent 126e673 commit c5c2458
Show file tree
Hide file tree
Showing 21 changed files with 151 additions and 37 deletions.
7 changes: 3 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,14 @@ FetchContent_Declare(
)
FetchContent_MakeAvailable(raylib)

add_executable(adagio ${SOURCES})
target_link_libraries(adagio raylib)


add_subdirectory(third_party/entt)
add_subdirectory(third_party/Catch2)
add_executable(adagio_test ${TESTS})
target_link_libraries(adagio_test PRIVATE Catch2::Catch2 raylib EnTT::EnTT)

add_executable(adagio ${SOURCES})
target_link_libraries(adagio raylib EnTT::EnTT)

list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/third_party/Catch2/contrib)
include(CTest)
include(Catch)
Expand Down
36 changes: 36 additions & 0 deletions src/backends/raylib/RaylibGraphicsDevice.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include "RaylibGraphicsDevice.h"

void RaylibGraphicsDevice::begin() {
ClearBackground({clearColor.r, clearColor.g, clearColor.b, clearColor.a});
BeginDrawing();
}

void RaylibGraphicsDevice::end() {
EndDrawing();
}

void RaylibGraphicsDevice::setClearColor(const Adagio::Color &color) {
clearColor = color;
}

static Rectangle AdagioRectToRaylibRect(const Adagio::RectF &rect) {
return {rect.x(), rect.y(), rect.width(), rect.height()};
}

void
RaylibGraphicsDevice::drawTexture(Adagio::Texture2D &texture, const Adagio::RectF &source, const Adagio::RectF &dest,
const Adagio::Vector2d &origin, float rotation, const Adagio::Color &tint) {
RaylibTexture raylibTex = textureManager.useTexture(texture);
DrawTexturePro(raylibTex, AdagioRectToRaylibRect(source), AdagioRectToRaylibRect(dest),
{static_cast<float>(origin.x), static_cast<float>(origin.y)}, rotation,
{tint.r, tint.g, tint.b, tint.a});
}

Adagio::AbstractTextureManager *RaylibGraphicsDevice::getTextureManager() {
return &textureManager;
}

void RaylibGraphicsDevice::drawText(Font &font, const char *text, const Adagio::Vector2d &position, float fontSize,
float spacing, const Adagio::Color &tint) {

}
33 changes: 33 additions & 0 deletions src/backends/raylib/RaylibGraphicsDevice.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#ifndef GL_ADAGIO_RAYLIBGRAPHICSDEVICE_H
#define GL_ADAGIO_RAYLIBGRAPHICSDEVICE_H

#include "raylib.h"
#include "RaylibTextureLoader.h"
#include "../../graphics/GraphicsDevice.h"

typedef Texture2D RaylibTexture;

class RaylibGraphicsDevice : public Adagio::GraphicsDevice {
private:
RaylibTextureLoader loader;
Adagio::TextureManager<RaylibTexture> textureManager{&loader};
Adagio::Color clearColor{0, 0, 0, 255};
public:
void begin() override;

void end() override;

void setClearColor(const Adagio::Color &color) override;

Adagio::AbstractTextureManager *getTextureManager() override;

void drawTexture(Adagio::Texture2D &texture, const Adagio::RectF &source, const Adagio::RectF &dest,
const Adagio::Vector2d &origin, float rotation, const Adagio::Color &tint) override;

void drawText(Font &font, const char *text, const Adagio::Vector2d &position, float fontSize, float spacing,
const Adagio::Color &tint) override;

};


#endif //GL_ADAGIO_RAYLIBGRAPHICSDEVICE_H
8 changes: 8 additions & 0 deletions src/backends/raylib/RaylibTexture.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef GL_ADAGIO_RAYLIBTEXTURE_H
#define GL_ADAGIO_RAYLIBTEXTURE_H

#include "raylib.h"

typedef Texture2D RaylibTexture;

#endif //GL_ADAGIO_RAYLIBTEXTURE_H
12 changes: 12 additions & 0 deletions src/backends/raylib/RaylibTextureLoader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include "RaylibTextureLoader.h"
#include <utility>

std::pair<RaylibTexture, Adagio::TextureDimensions> RaylibTextureLoader::load(const char *resource) {
RaylibTexture tex = LoadTexture(resource);
Adagio::TextureDimensions dims{static_cast<unsigned int>(tex.width), static_cast<unsigned int>(tex.height)};
return std::make_pair(tex, dims);
}

void RaylibTextureLoader::unload(RaylibTexture texture) {
UnloadTexture(texture);
}
15 changes: 15 additions & 0 deletions src/backends/raylib/RaylibTextureLoader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef GL_ADAGIO_RAYLIBTEXTURELOADER_H
#define GL_ADAGIO_RAYLIBTEXTURELOADER_H

#include "RaylibTexture.h"
#include "../../graphics/TextureLoader.h"

class RaylibTextureLoader : public Adagio::TextureLoader<RaylibTexture> {
public:
std::pair<RaylibTexture, Adagio::TextureDimensions> load(const char *resource) override;

void unload(RaylibTexture texture) override;
};


#endif //GL_ADAGIO_RAYLIBTEXTURELOADER_H
2 changes: 2 additions & 0 deletions src/game/SandboxGame.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "SandboxGame.h"

SandboxGame::SandboxGame(Adagio::SpriteBatch &sb) : Adagio::Game(sb) {}

void SandboxGame::init() {
InitWindow(640, 480, "Raylib Test");
SetTargetFPS(60);
Expand Down
2 changes: 2 additions & 0 deletions src/game/SandboxGame.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

class SandboxGame : public Adagio::Game {
public:
explicit SandboxGame(Adagio::SpriteBatch &sb);

void init() override;

void loadContent() override;
Expand Down
3 changes: 2 additions & 1 deletion src/game/states/GracilisGame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ void GracilisGame::init() {
}

void GracilisGame::loadContent(Adagio::SpriteBatch &spriteBatch) {
spriteBatch.clearColor = BLACK;
spriteBatch.setClearColor({0, 0, 0, 255});
// spriteBatch.clearColor = BLACK;
shipTex = LoadTexture("assets/ship.png");
wallopTex = LoadTexture("assets/wallop.png");
const auto ship = registry.create();
Expand Down
15 changes: 8 additions & 7 deletions src/game/states/LeonaTestState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,23 @@ void LeonaTestState::init() {
}

void LeonaTestState::loadContent(Adagio::SpriteBatch &spriteBatch) {
spriteBatch.clearColor = BLUE;
leonaTex = LoadTexture("assets/leona_smile.png");
spriteBatch.setClearColor({0, 0, 255, 255});
leonaTex = spriteBatch.getGraphicsDevice()->getTextureManager()->load("assets/leona_smile.png");
}

void LeonaTestState::update(Adagio::GameStats &stats, Adagio::StateMachine *gameStates) {
leonaPos = GetMousePosition();
auto pos = GetMousePosition();
leonaPos = {pos.x, pos.y};
}

void LeonaTestState::draw(Adagio::SpriteBatch &spriteBatch, const Adagio::GameStats &stats) {
void LeonaTestState::draw(Adagio::SpriteBatch &spriteBatch, Adagio::RenderingServices &services) {
auto txt = spriteBatch.drawText("Lookit Leona", {0, 0}, 10);
txt->fontSize = 36;
auto leonaSprite = spriteBatch.draw(leonaTex, leonaPos, 0);
leonaSprite->origin.x = leonaTex.width / 2;
leonaSprite->origin.y = leonaTex.height / 2;
leonaSprite->origin.x = leonaTex.getWidth() / 2;
leonaSprite->origin.y = leonaTex.getHeight() / 2;
}

void LeonaTestState::unloadContent() {
UnloadTexture(leonaTex);
// TODO: we need to pass texture manager to unloadContent so we can actually unload stuff
}
7 changes: 4 additions & 3 deletions src/game/states/LeonaTestState.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <raylib.h>
#include "../../state/StateMachine.h"
#include "../../state/GameState.h"
#include "../../math/Vector2.h"

class LeonaTestState : public Adagio::GameState { // NOLINT(cppcoreguidelines-pro-type-member-init)
public:
Expand All @@ -13,13 +14,13 @@ class LeonaTestState : public Adagio::GameState { // NOLINT(cppcoreguidelines-pr

void update(Adagio::GameStats &stats, Adagio::StateMachine *gameStates) override;

void draw(Adagio::SpriteBatch &spriteBatch, const Adagio::GameStats &stats) override;
void draw(Adagio::SpriteBatch &spriteBatch, Adagio::RenderingServices &services) override;

void unloadContent() override;

protected:
Vector2 leonaPos;
Texture2D leonaTex;
Adagio::Vector2d leonaPos;
Adagio::Texture2D leonaTex{0, 0, 0, 0};
};


Expand Down
12 changes: 6 additions & 6 deletions src/game/systems/ShipRendererSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ void ShipRendererSystem(entt::registry &registry, Adagio::SpriteBatch &spriteBat
for (auto [entity, shipSprite, ship, pos]: view.each()) {
Rectangle clippingRect{0, 0, FRAME_WIDTH, FRAME_HEIGHT};
clippingRect.x = FRAME_WIDTH * shipSprite.frame;
auto sprite = spriteBatch.draw(*(shipSprite.texture), pos.position);
sprite->source = clippingRect;
sprite->destination.x = floorf(pos.position.x);
sprite->destination.y = floorf(pos.position.y);
sprite->destination.width = FRAME_WIDTH;
sprite->destination.height = FRAME_HEIGHT;
// auto sprite = spriteBatch.draw(*(shipSprite.texture), pos.position);
// sprite->source = clippingRect;
// sprite->destination.x = floorf(pos.position.x);
// sprite->destination.y = floorf(pos.position.y);
// sprite->destination.width = FRAME_WIDTH;
// sprite->destination.height = FRAME_HEIGHT;
const float frameDelta = stats.getGameTime() - shipSprite.lastFrame;
if (frameDelta > secondsUntilNextFrame) {
shipSprite.lastFrame = stats.getGameTime();
Expand Down
12 changes: 6 additions & 6 deletions src/game/systems/WallopRendererSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ void WallopRendererSystem(entt::registry &registry, Adagio::SpriteBatch &spriteB
for (auto [entity, wallopSprite, pos]: view.each()) {
Rectangle clippingRect{0, 0, FRAME_WIDTH, FRAME_HEIGHT};
clippingRect.x = FRAME_WIDTH * wallopSprite.frame;
auto sprite = spriteBatch.draw(*(wallopSprite.texture), pos.position);
sprite->source = clippingRect;
sprite->destination.x = floorf(pos.position.x);
sprite->destination.y = floorf(pos.position.y);
sprite->destination.width = FRAME_WIDTH * 0.5;
sprite->destination.height = FRAME_HEIGHT * 0.5;
// auto sprite = spriteBatch.draw(*(wallopSprite.texture), pos.position);
// sprite->source = clippingRect;
// sprite->destination.x = floorf(pos.position.x);
// sprite->destination.y = floorf(pos.position.y);
// sprite->destination.width = FRAME_WIDTH * 0.5;
// sprite->destination.height = FRAME_HEIGHT * 0.5;
const float frameDelta = stats.getGameTime() - wallopSprite.lastFrame;
if (frameDelta > secondsUntilNextFrame) {
wallopSprite.lastFrame = stats.getGameTime();
Expand Down
2 changes: 1 addition & 1 deletion src/graphics/GraphicsDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace Adagio {

virtual void setClearColor(const Adagio::Color &color) = 0;

[[nodiscard]] virtual AbstractTextureManager *getTextureManager() const = 0;
[[nodiscard]] virtual AbstractTextureManager *getTextureManager() = 0;

virtual void drawTexture(Texture2D &texture, const RectF &source, const RectF &dest, const Vector2d &origin,
float rotation,
Expand Down
2 changes: 1 addition & 1 deletion src/graphics/SpriteBatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ namespace Adagio {
renderingQueue.insert(it, r);
}

const GraphicsDevice *SpriteBatch::getGraphicsDevice() {
GraphicsDevice *SpriteBatch::getGraphicsDevice() {
return graphicsDevice;
}
} // Adagio
2 changes: 1 addition & 1 deletion src/graphics/SpriteBatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace Adagio {

void end();

const GraphicsDevice *getGraphicsDevice();
GraphicsDevice *getGraphicsDevice();

static int queueReservation;

Expand Down
10 changes: 7 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@
#include "game/states/GracilisGame.h"
#include "state/Runner.h"

#include "backends/raylib/RaylibGraphicsDevice.h"

int main() {
SandboxGame game;
// LeonaTestState state;
RaylibGraphicsDevice graphicsDevice;
Adagio::SpriteBatch spriteBatch(&graphicsDevice);
SandboxGame game(spriteBatch);
LeonaTestState leona{};
GracilisGame state;
Adagio::Runner runner(&game, &state);
Adagio::Runner runner(&game, dynamic_cast<Adagio::GameState *>(&leona));
runner.run();
return EXIT_SUCCESS;
}
2 changes: 1 addition & 1 deletion src/state/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <utility>

namespace Adagio {
Game::Game(SpriteBatch spriteBatch) : spriteBatch(std::move(spriteBatch)) {
Game::Game(SpriteBatch &spriteBatch) : spriteBatch(std::move(spriteBatch)) {
}

void Game::update() {
Expand Down
2 changes: 1 addition & 1 deletion src/state/Game.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Adagio {
SpriteBatch spriteBatch;
StateMachine stateMachine = StateMachine(&spriteBatch);

explicit Game(SpriteBatch sb);
explicit Game(SpriteBatch &sb);

void update();

Expand Down
2 changes: 1 addition & 1 deletion test/graphics/MockGraphicsDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,6 @@ MockGraphicsDevice::drawText(Font &font, const char *text, const Adagio::Vector2
drawnObjects.push_back(txt);
}

Adagio::AbstractTextureManager *MockGraphicsDevice::getTextureManager() const {
Adagio::AbstractTextureManager *MockGraphicsDevice::getTextureManager() {
return nullptr;
}
2 changes: 1 addition & 1 deletion test/graphics/MockGraphicsDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class MockGraphicsDevice : public Adagio::GraphicsDevice {
void drawText(Font &font, const char *text, const Adagio::Vector2d &position, float fontSize, float spacing,
const Adagio::Color &tint) override;

Adagio::AbstractTextureManager *getTextureManager() const override;
Adagio::AbstractTextureManager *getTextureManager() override;

std::vector<Adagio::RenderState *> *getDrawnObjects();

Expand Down

0 comments on commit c5c2458

Please sign in to comment.