-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Basic Sprite component + RenderSprite renderer
This Component + System deals with basic sprite rendering functionality. The plan is to add additional optional components that can coexist with the `Sprite` component to add additional rendering options. Then a sprite animation component/system can manipulate these various values to create reusable and extendable sprite animation.
- Loading branch information
1 parent
e66e17b
commit f54001f
Showing
16 changed files
with
325 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#ifndef GL_ADAGIO_SPRITE_H | ||
#define GL_ADAGIO_SPRITE_H | ||
|
||
#include "../../graphics/Texture2D.h" | ||
#include "../../graphics/AbstractTextureManager.h" | ||
#include "../../math/Vector2.h" | ||
#include "../../math/Rect.h" | ||
|
||
struct Sprite { | ||
Adagio::Texture2D texture; | ||
Adagio::Vector2d position; | ||
int zIndex{0}; | ||
}; | ||
|
||
#endif //GL_ADAGIO_SPRITE_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#include "LoadSprite.h" | ||
|
||
Sprite loadSprite(const std::string &resource, Adagio::AbstractTextureManager &textureManager) { | ||
Sprite s; | ||
s.texture = textureManager.load(resource); | ||
s.zIndex = 0; | ||
s.position = {0, 0}; | ||
return s; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#ifndef GL_ADAGIO_LOADSPRITE_H | ||
#define GL_ADAGIO_LOADSPRITE_H | ||
|
||
#include "../components/Sprite.h" | ||
|
||
Sprite loadSprite(const std::string &resource, Adagio::AbstractTextureManager &textureManager); | ||
|
||
#endif //GL_ADAGIO_LOADSPRITE_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#include "RenderSprite.h" | ||
#include "../components/Sprite.h" | ||
#include "../components/Position.h" | ||
|
||
void | ||
RenderSprite(entt::registry ®istry, Adagio::SpriteBatch &spriteBatch, Adagio::RenderingServices &services) { | ||
auto view = registry.view<Sprite>(); | ||
for (auto [entity, sprite]: view.each()) { | ||
Adagio::Vector2d position = sprite.position; | ||
Position *posComponent = registry.try_get<Position>(entity); | ||
if (posComponent) { | ||
position += posComponent->position; | ||
} | ||
spriteBatch.draw(sprite.texture, position, static_cast<short>(sprite.zIndex)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#ifndef GL_ADAGIO_RENDERSPRITE_H | ||
#define GL_ADAGIO_RENDERSPRITE_H | ||
|
||
#include "entt/entt.hpp" | ||
#include "../../graphics/SpriteBatch.h" | ||
#include "../../state/GameState.h" | ||
|
||
void | ||
RenderSprite(entt::registry ®istry, Adagio::SpriteBatch &spriteBatch, Adagio::RenderingServices &services); | ||
|
||
|
||
#endif //GL_ADAGIO_RENDERSPRITE_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#include "EcsTestingHarness.h" | ||
|
||
EcsTestingHarness::EcsTestingHarness() { | ||
renderingServices = {&spriteBatch, graphicsDevice.getTextureManager(), &stats}; | ||
} | ||
|
||
void EcsTestingHarness::reset() { | ||
spriteBatch.begin(); | ||
spriteBatch.end(); | ||
registry.clear(); | ||
stats.reset(); | ||
} | ||
|
||
void EcsTestingHarness::testRendererFrame(Adagio::RendererFn renderer) { | ||
spriteBatch.begin(); | ||
renderer(registry, spriteBatch, renderingServices); | ||
spriteBatch.end(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#ifndef GL_ADAGIO_ECSTESTINGHARNESS_H | ||
#define GL_ADAGIO_ECSTESTINGHARNESS_H | ||
|
||
#include "../../src/graphics/SpriteBatch.h" | ||
#include "../../src/state/RenderingServices.h" | ||
#include "harness/EcsMockGraphicsDevice.h" | ||
#include "harness/MockGameStats.h" | ||
#include "entt/entt.hpp" | ||
#include "../../src/state/EntityGameState.h" | ||
|
||
class EcsTestingHarness { | ||
public: | ||
EcsMockGraphicsDevice graphicsDevice; | ||
Adagio::SpriteBatch spriteBatch{&graphicsDevice}; | ||
MockGameStats stats; | ||
entt::registry registry; | ||
Adagio::RenderingServices renderingServices{}; | ||
|
||
EcsTestingHarness(); | ||
|
||
void reset(); | ||
|
||
void testRendererFrame(Adagio::RendererFn renderer); | ||
}; | ||
|
||
|
||
#endif //GL_ADAGIO_ECSTESTINGHARNESS_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#include <catch2/catch.hpp> | ||
#include "../../../src/game/factories/LoadSprite.h" | ||
#include "../harness/MockTextureLibrary.h" | ||
|
||
|
||
TEST_CASE("It can make a basic Sprite object", "[factory][LoadSprite]") { | ||
auto textureMan = MockTextureLibrary(); | ||
auto sprite = loadSprite("test.jpg", textureMan); | ||
|
||
REQUIRE(sprite.texture.handle == 0xcafe); | ||
REQUIRE(sprite.texture.getSecretId() == 0xcafe); | ||
REQUIRE(sprite.position.x == 0); | ||
REQUIRE(sprite.position.y == 0); | ||
REQUIRE(sprite.zIndex == 0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#include "EcsMockGraphicsDevice.h" | ||
|
||
void EcsMockGraphicsDevice::begin() { | ||
spritesRenderedOnFrame.clear(); | ||
} | ||
|
||
void EcsMockGraphicsDevice::end() { | ||
// do nothing | ||
} | ||
|
||
void EcsMockGraphicsDevice::setClearColor(const Adagio::Color &color) { | ||
clearColor = color; | ||
} | ||
|
||
void | ||
EcsMockGraphicsDevice::drawTexture(Adagio::Texture2D &texture, const Adagio::RectF &source, const Adagio::RectF &dest, | ||
const Adagio::Vector2d &origin, float rotation, const Adagio::Color &tint) { | ||
Adagio::SpriteState sprite; | ||
sprite.texture = &texture; | ||
sprite.rotation = rotation; | ||
sprite.tint = tint; | ||
sprite.origin = origin; | ||
sprite.destination = dest; | ||
sprite.source = source; | ||
spritesRenderedOnFrame.push_back(sprite); | ||
} | ||
|
||
Adagio::AbstractTextureManager *EcsMockGraphicsDevice::getTextureManager() { | ||
return &textureLibrary; | ||
} | ||
|
||
Adagio::Color EcsMockGraphicsDevice::getClearColor() const { | ||
return clearColor; | ||
} | ||
|
||
const std::vector<Adagio::SpriteState> *EcsMockGraphicsDevice::getSprites() const { | ||
return &spritesRenderedOnFrame; | ||
} | ||
|
||
void EcsMockGraphicsDevice::drawText(Font &font, const char *text, const Adagio::Vector2d &position, float fontSize, | ||
float spacing, const Adagio::Color &tint) { | ||
// do nothing for now | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#ifndef GL_ADAGIO_ECSMOCKGRAPHICSDEVICE_H | ||
#define GL_ADAGIO_ECSMOCKGRAPHICSDEVICE_H | ||
|
||
#include "../../../src/graphics/GraphicsDevice.h" | ||
#include "../../../src/graphics/SpriteState.h" | ||
#include "MockTextureLibrary.h" | ||
|
||
class EcsMockGraphicsDevice : public Adagio::GraphicsDevice { | ||
public: | ||
void begin() override; | ||
|
||
void end() override; | ||
|
||
void setClearColor(const Adagio::Color &color) 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; | ||
|
||
Adagio::AbstractTextureManager *getTextureManager() override; | ||
|
||
[[nodiscard]] Adagio::Color getClearColor() const; | ||
|
||
[[nodiscard]] const std::vector<Adagio::SpriteState> *getSprites() const; | ||
|
||
protected: | ||
Adagio::Color clearColor{0, 0, 0, 0}; | ||
std::vector<Adagio::SpriteState> spritesRenderedOnFrame; | ||
MockTextureLibrary textureLibrary; | ||
}; | ||
|
||
|
||
#endif //GL_ADAGIO_ECSMOCKGRAPHICSDEVICE_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#include "MockGameStats.h" | ||
|
||
bool MockGameStats::isRunning() const { | ||
return running; | ||
} | ||
|
||
void MockGameStats::closeGame() { | ||
running = false; | ||
} | ||
|
||
float MockGameStats::getFrameDelta() const { | ||
return static_cast<float>(gameTime - lastTickTime); | ||
} | ||
|
||
double MockGameStats::getGameTime() const { | ||
return gameTime; | ||
} | ||
|
||
void MockGameStats::advanceTime(double delta) { | ||
lastTickTime = gameTime; | ||
gameTime += delta; | ||
} | ||
|
||
void MockGameStats::reset() { | ||
lastTickTime = 0; | ||
gameTime = 0; | ||
running = true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#ifndef GL_ADAGIO_MOCKGAMESTATS_H | ||
#define GL_ADAGIO_MOCKGAMESTATS_H | ||
|
||
#include "../../../src/state/GameStats.h" | ||
|
||
class MockGameStats : public Adagio::GameStats { | ||
public: | ||
[[nodiscard]] bool isRunning() const override; | ||
|
||
void closeGame() override; | ||
|
||
[[nodiscard]] float getFrameDelta() const override; | ||
|
||
[[nodiscard]] double getGameTime() const override; | ||
|
||
void advanceTime(double delta); | ||
|
||
void reset(); | ||
|
||
protected: | ||
bool running{true}; | ||
double gameTime{0.0}; | ||
double lastTickTime{0.0}; | ||
}; | ||
|
||
|
||
#endif //GL_ADAGIO_MOCKGAMESTATS_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#include "MockTextureLibrary.h" | ||
|
||
Adagio::Texture2D MockTextureLibrary::load(std::string resource) { | ||
return Adagio::Texture2D(0xcafe, 0xcafe, 256, 128); | ||
} | ||
|
||
void MockTextureLibrary::unload(Adagio::Texture2D texture) { | ||
// do nothing | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#ifndef GL_ADAGIO_MOCKTEXTURELIBRARY_H | ||
#define GL_ADAGIO_MOCKTEXTURELIBRARY_H | ||
|
||
#include "../../../src/graphics/AbstractTextureManager.h" | ||
|
||
class MockTextureLibrary : public Adagio::AbstractTextureManager { | ||
public: | ||
Adagio::Texture2D load(std::string resource) override; | ||
|
||
void unload(Adagio::Texture2D texture) override; | ||
}; | ||
|
||
#endif //GL_ADAGIO_MOCKTEXTURELIBRARY_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#include <catch2/catch.hpp> | ||
#include "../../../src/game/components/Sprite.h" | ||
#include "../../../src/game/components/Position.h" | ||
#include "../../../src/game/systems/RenderSprite.h" | ||
#include "../EcsTestingHarness.h" | ||
|
||
static EcsTestingHarness harness; | ||
|
||
TEST_CASE("No components defined", "[renderer][RenderSprite]") { | ||
harness.reset(); | ||
REQUIRE_NOTHROW(RenderSprite(harness.registry, harness.spriteBatch, harness.renderingServices)); | ||
} | ||
|
||
TEST_CASE("It renders a basic sprite", "[renderer][RenderSprite]") { | ||
harness.reset(); | ||
auto sprite = harness.registry.create(); | ||
auto &spriteComponent = harness.registry.emplace<Sprite>(sprite); | ||
spriteComponent.texture = Adagio::Texture2D{1, 1, 128, 128}; | ||
spriteComponent.position = Adagio::Vector2d{1, 2}; | ||
|
||
harness.testRendererFrame(RenderSprite); | ||
|
||
auto renderedSprites = *(harness.graphicsDevice.getSprites()); | ||
REQUIRE(renderedSprites.size() == 1); | ||
REQUIRE(renderedSprites[0].texture->handle == 1); | ||
REQUIRE(renderedSprites[0].texture->getSecretId() == 1); | ||
REQUIRE(renderedSprites[0].texture->getWidth() == 128); | ||
REQUIRE(renderedSprites[0].texture->getHeight() == 128); | ||
REQUIRE(renderedSprites[0].destination.position.x == 1); | ||
REQUIRE(renderedSprites[0].destination.position.y == 2); | ||
REQUIRE(renderedSprites[0].destination.size.x == 128); | ||
REQUIRE(renderedSprites[0].destination.size.y == 128); | ||
} | ||
|
||
TEST_CASE("A sprite's position property is offset off of the `Position` component if it exists") { | ||
harness.reset(); | ||
auto sprite = harness.registry.create(); | ||
auto &spriteComponent = harness.registry.emplace<Sprite>(sprite); | ||
auto &position = harness.registry.emplace<Position>(sprite); | ||
spriteComponent.texture = Adagio::Texture2D{1, 1, 128, 128}; | ||
spriteComponent.position = Adagio::Vector2d{1, 2}; | ||
position.position = Adagio::Vector2d{300, 300}; | ||
|
||
harness.testRendererFrame(RenderSprite); | ||
|
||
auto renderedSprite = (*(harness.graphicsDevice.getSprites()))[0]; | ||
REQUIRE(renderedSprite.destination.position.x == 301); | ||
REQUIRE(renderedSprite.destination.position.y == 302); | ||
} |