Skip to content

Commit

Permalink
Post jam changes (#28)
Browse files Browse the repository at this point in the history
* Added hash function for vector 2 and some other stuff...

* Fixing bug with not adding rotation to sprites.

* Added loops property to AnimatedSpriteComponent.

* Fixed bug with animations sometimes not starting on the first frame.
  • Loading branch information
Chukobyte authored Jan 16, 2022
1 parent 52592f3 commit e33b4fc
Show file tree
Hide file tree
Showing 14 changed files with 212 additions and 140 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ RELEASE_FLAGS = -DHAVE_SNPRINTF=1
BUILD_OBJECT := $(PROJECT_NAME).exe
TEST_BUILD_OBJECT := test_$(PROJECT_NAME).exe

SRC = $(wildcard src/main.cpp src/core/*.cpp src/core/math/*.cpp src/core/utils/*.cpp src/core/rendering/*.cpp src/core/rendering/shader/*.cpp src/core/input/*.cpp src/core/audio/*.cpp src/core/timer/*.cpp src/core/scripting/python/*.cpp src/core/ecs/*.cpp src/core/networking/*.cpp include/stb_image/*.cpp)
SRC = $(wildcard src/main.cpp src/core/*.cpp src/core/math/*.cpp src/core/utils/*.cpp src/core/rendering/*.cpp src/core/rendering/shader/*.cpp src/core/input/*.cpp src/core/audio/*.cpp src/core/timer/*.cpp src/core/scripting/python/*.cpp src/core/ecs/*.cpp src/core/ecs/system/systems/*.cpp src/core/networking/*.cpp include/stb_image/*.cpp)
SRC_C = $(wildcard lib/glad.c include/kuba_zip/zip.c)

OBJ = $(SRC:.cpp=.o)
OBJ_C = $(SRC_C:.c=.o)

TEST_SRC = $(wildcard src/test/*.cpp src/test/unit/*.cpp src/core/*.cpp src/core/math/*.cpp src/core/utils/*.cpp src/core/rendering/*.cpp src/core/rendering/shader/*.cpp src/core/input/*.cpp src/core/timer/*.cpp src/core/scripting/python/*.cpp src/core/ecs/*.cpp src/core/networking/*.cpp include/stb_image/*.cpp)
TEST_SRC = $(wildcard src/test/*.cpp src/test/unit/*.cpp src/core/*.cpp src/core/math/*.cpp src/core/utils/*.cpp src/core/rendering/*.cpp src/core/rendering/shader/*.cpp src/core/input/*.cpp src/core/timer/*.cpp src/core/scripting/python/*.cpp src/core/audio/*.cpp src/core/ecs/*.cpp src/core/ecs/system/systems/*.cpp src/core/networking/*.cpp include/stb_image/*.cpp)
TEST_OBJ = $(TEST_SRC:.cpp=.o)

EXPORT_PACKAGE_DIR := export_package
Expand Down
6 changes: 4 additions & 2 deletions src/core/ecs/component/components/animated_sprite_component.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
struct AnimatedSpriteComponent {
std::map<std::string, Animation> animations;
Animation currentAnimation; // Preselects first added animation
unsigned int currentFrameIndex;
bool isPlaying;
unsigned int currentFrameIndex = 0;
bool isPlaying = false;
bool loops = true;
bool flipX = false;
bool flipY = false;
Color modulate = Color(1.0f, 1.0f, 1.0f, 1.0f);
uint32_t startAnimationTickTime; // Used to keep track when the first frame began for synchronization
};

#endif //ANIMATED_SPRITE_COMPONENT_H
1 change: 1 addition & 0 deletions src/core/ecs/entity/entity_tag_cache.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <vector>
#include <set>
#include <unordered_map>

Expand Down
1 change: 0 additions & 1 deletion src/core/ecs/system/entity_system.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <set>

#include "../entity/entity_tag_cache.h"
#include "../../global_dependencies.h"

const unsigned int MAX_SYSTEMS = 32;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include "animated_sprite_rendering_entity_system.h"

AnimatedSpriteRenderingEntitySystem::AnimatedSpriteRenderingEntitySystem() {
renderer = GD::GetContainer()->renderer2D;
componentManager = GD::GetContainer()->componentManager;
cameraManager = GD::GetContainer()->cameraManager;
sceneManager = GD::GetContainer()->sceneManager;
enabled = true;
}

void AnimatedSpriteRenderingEntitySystem::Render() {
if (IsEnabled()) {
for (Entity entity : entities) {
Transform2DComponent transform2DComponent = componentManager->GetComponent<Transform2DComponent>(entity);
AnimatedSpriteComponent animatedSpriteComponent = componentManager->GetComponent<AnimatedSpriteComponent>(entity);
Animation currentAnimation = animatedSpriteComponent.currentAnimation;
AnimationFrame currentFrame = currentAnimation.animationFrames[animatedSpriteComponent.currentFrameIndex];
if (animatedSpriteComponent.isPlaying) {
unsigned int newIndex = static_cast<unsigned int>(((SDL_GetTicks() - animatedSpriteComponent.startAnimationTickTime) / currentAnimation.speed) % currentAnimation.frames);
if (newIndex != animatedSpriteComponent.currentFrameIndex) {
// Index changed
currentFrame = currentAnimation.animationFrames[newIndex];
if (newIndex + 1 == currentAnimation.frames) {
// Animation Finished
if (!animatedSpriteComponent.loops) {
animatedSpriteComponent.isPlaying = false;
}
}
animatedSpriteComponent.currentFrameIndex = newIndex;
componentManager->UpdateComponent(entity, animatedSpriteComponent);
}
}
Camera2D camera = cameraManager->GetCurrentCamera2D();
Transform2DComponent parentTransform = SceneNodeHelper::GetCombinedParentsTransforms(sceneManager, componentManager, entity);
Vector2 drawDestinationPosition = SpaceHandler::WorldToScreen(transform2DComponent.position + parentTransform.position, transform2DComponent.ignoreCamera);
Vector2 drawScale = !transform2DComponent.ignoreCamera ? transform2DComponent.scale * parentTransform.scale * camera.zoom : transform2DComponent.scale * parentTransform.scale;
Vector2 drawDestinationSize = Vector2(currentFrame.drawSource.w * drawScale.x, currentFrame.drawSource.h * drawScale.y);
Rect2 drawDestination = Rect2(drawDestinationPosition, drawDestinationSize);
assert(currentFrame.texture != nullptr && "Current frame texture is null");
renderer->BatchDrawSprite(
currentFrame.texture,
currentFrame.drawSource,
drawDestination,
transform2DComponent.zIndex,
transform2DComponent.rotation,
animatedSpriteComponent.modulate,
animatedSpriteComponent.flipX,
animatedSpriteComponent.flipY
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,54 +14,8 @@ class AnimatedSpriteRenderingEntitySystem : public EntitySystem {
SceneManager *sceneManager = nullptr;

public:

AnimatedSpriteRenderingEntitySystem() {
renderer = GD::GetContainer()->renderer2D;
componentManager = GD::GetContainer()->componentManager;
cameraManager = GD::GetContainer()->cameraManager;
sceneManager = GD::GetContainer()->sceneManager;
enabled = true;
}

void Render() override {
if (IsEnabled()) {
for (Entity entity : entities) {
Transform2DComponent transform2DComponent = componentManager->GetComponent<Transform2DComponent>(entity);
AnimatedSpriteComponent animatedSpriteComponent = componentManager->GetComponent<AnimatedSpriteComponent>(entity);
Animation currentAnimation = animatedSpriteComponent.currentAnimation;
AnimationFrame currentFrame = currentAnimation.animationFrames[animatedSpriteComponent.currentFrameIndex];
if (animatedSpriteComponent.isPlaying) {
unsigned int newIndex = static_cast<unsigned int>((SDL_GetTicks() / currentAnimation.speed) % currentAnimation.frames);
if (newIndex != animatedSpriteComponent.currentFrameIndex) {
// Index changed
currentFrame = currentAnimation.animationFrames[newIndex];
if (newIndex + 1 == currentAnimation.frames) {
// Animation Finished
}
animatedSpriteComponent.currentFrameIndex = newIndex;
componentManager->UpdateComponent(entity, animatedSpriteComponent);
}
}
Camera2D camera = cameraManager->GetCurrentCamera2D();
Transform2DComponent parentTransform = SceneNodeHelper::GetCombinedParentsTransforms(sceneManager, componentManager, entity);
Vector2 drawDestinationPosition = SpaceHandler::WorldToScreen(transform2DComponent.position + parentTransform.position, transform2DComponent.ignoreCamera);
Vector2 drawScale = !transform2DComponent.ignoreCamera ? transform2DComponent.scale * parentTransform.scale * camera.zoom : transform2DComponent.scale * parentTransform.scale;
Vector2 drawDestinationSize = Vector2(currentFrame.drawSource.w * drawScale.x, currentFrame.drawSource.h * drawScale.y);
Rect2 drawDestination = Rect2(drawDestinationPosition, drawDestinationSize);
assert(currentFrame.texture != nullptr && "Current frame texture is null");
renderer->BatchDrawSprite(
currentFrame.texture,
currentFrame.drawSource,
drawDestination,
transform2DComponent.zIndex,
transform2DComponent.rotation,
animatedSpriteComponent.modulate,
animatedSpriteComponent.flipX,
animatedSpriteComponent.flipY
);
}
}
}
AnimatedSpriteRenderingEntitySystem();
void Render() override;
};

#endif //ANIMATED_SPRITE_RENDERING_ENTITY_SYSTEM_H
34 changes: 34 additions & 0 deletions src/core/ecs/system/systems/sprite_rendering_entity_system.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include "sprite_rendering_entity_system.h"

SpriteRenderingEntitySystem::SpriteRenderingEntitySystem() {
renderer = GD::GetContainer()->renderer2D;
componentManager = GD::GetContainer()->componentManager;
cameraManager = GD::GetContainer()->cameraManager;
sceneManager = GD::GetContainer()->sceneManager;
enabled = true;
}

void SpriteRenderingEntitySystem::Render() {
if (IsEnabled()) {
for (Entity entity : entities) {
Transform2DComponent transform2DComponent = componentManager->GetComponent<Transform2DComponent>(entity);
SpriteComponent spriteComponent = componentManager->GetComponent<SpriteComponent>(entity);
Camera2D camera = cameraManager->GetCurrentCamera2D();
Transform2DComponent parentTransform = SceneNodeHelper::GetCombinedParentsTransforms(sceneManager, componentManager, entity);
Vector2 drawDestinationPosition = SpaceHandler::WorldToScreen(transform2DComponent.position + parentTransform.position, transform2DComponent.ignoreCamera);
Vector2 drawScale = !transform2DComponent.ignoreCamera ? transform2DComponent.scale * parentTransform.scale * camera.zoom : transform2DComponent.scale * parentTransform.scale;
Vector2 drawDestinationSize = Vector2(spriteComponent.drawSource.w * drawScale.x, spriteComponent.drawSource.h * drawScale.y);
spriteComponent.drawDestination = Rect2(drawDestinationPosition, drawDestinationSize);
renderer->BatchDrawSprite(
spriteComponent.texture,
spriteComponent.drawSource,
spriteComponent.drawDestination,
transform2DComponent.zIndex,
transform2DComponent.rotation,
spriteComponent.modulate,
spriteComponent.flipX,
spriteComponent.flipY
);
}
}
}
34 changes: 2 additions & 32 deletions src/core/ecs/system/systems/sprite_rendering_entity_system.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,38 +14,8 @@ class SpriteRenderingEntitySystem : public EntitySystem {
SceneManager *sceneManager = nullptr;

public:
SpriteRenderingEntitySystem() {
renderer = GD::GetContainer()->renderer2D;
componentManager = GD::GetContainer()->componentManager;
cameraManager = GD::GetContainer()->cameraManager;
sceneManager = GD::GetContainer()->sceneManager;
enabled = true;
}

void Render() override {
if (IsEnabled()) {
for (Entity entity : entities) {
Transform2DComponent transform2DComponent = componentManager->GetComponent<Transform2DComponent>(entity);
SpriteComponent spriteComponent = componentManager->GetComponent<SpriteComponent>(entity);
Camera2D camera = cameraManager->GetCurrentCamera2D();
Transform2DComponent parentTransform = SceneNodeHelper::GetCombinedParentsTransforms(sceneManager, componentManager, entity);
Vector2 drawDestinationPosition = SpaceHandler::WorldToScreen(transform2DComponent.position + parentTransform.position, transform2DComponent.ignoreCamera);
Vector2 drawScale = !transform2DComponent.ignoreCamera ? transform2DComponent.scale * parentTransform.scale * camera.zoom : transform2DComponent.scale * parentTransform.scale;
Vector2 drawDestinationSize = Vector2(spriteComponent.drawSource.w * drawScale.x, spriteComponent.drawSource.h * drawScale.y);
spriteComponent.drawDestination = Rect2(drawDestinationPosition, drawDestinationSize);
renderer->BatchDrawSprite(
spriteComponent.texture,
spriteComponent.drawSource,
spriteComponent.drawDestination,
transform2DComponent.zIndex,
0.0f,
spriteComponent.modulate,
spriteComponent.flipX,
spriteComponent.flipY
);
}
}
}
SpriteRenderingEntitySystem();
void Render() override;
};

#endif //SPRITE_RENDERING_ENTITY_SYSTEM_H
53 changes: 53 additions & 0 deletions src/core/ecs/system/systems/text_rendering_entity_system.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include "text_rendering_entity_system.h"

TextRenderingEntitySystem::TextRenderingEntitySystem() {
renderer = GD::GetContainer()->renderer2D;
sceneManager = GD::GetContainer()->sceneManager;
componentManager = GD::GetContainer()->componentManager;
cameraManager = GD::GetContainer()->cameraManager;
enabled = true;
}

void TextRenderingEntitySystem::OnRegisterEntity(Entity entity) {
EntitySystem::OnRegisterEntity(entity);
UpdateEntityText(entity, componentManager->GetComponent<TextLabelComponent>(entity));
}

void TextRenderingEntitySystem::OnEntityDestroyed(Entity entity) {
EntitySystem::OnEntityDestroyed(entity);
textLines.erase(entity);
}

void TextRenderingEntitySystem::UpdateEntityText(Entity entity, const TextLabelComponent& textLabelComponent) {
textLines.erase(entity);
TextLines entityTextLines = StringUtil::ConvertIntoWordWrappedLines(
textLabelComponent.text,
textLabelComponent.wordWrap,
textLabelComponent.maxCharactersPerLine);
textLines.emplace(entity, entityTextLines);
}

void TextRenderingEntitySystem::Render() {
if (IsEnabled()) {
for (Entity entity : entities) {
Transform2DComponent transform2DComponent = componentManager->GetComponent<Transform2DComponent>(entity);
Camera2D camera = cameraManager->GetCurrentCamera2D();
Transform2DComponent parentTransform = SceneNodeHelper::GetCombinedParentsTransforms(sceneManager, componentManager, entity);
Vector2 textLabelPosition = SpaceHandler::WorldToScreen(transform2DComponent.position + parentTransform.position, transform2DComponent.ignoreCamera);
TextLabelComponent textLabelComponent = componentManager->GetComponent<TextLabelComponent>(entity);
Vector2 textLabelScale = !transform2DComponent.ignoreCamera ? transform2DComponent.scale * parentTransform.scale * camera.zoom : transform2DComponent.scale * parentTransform.scale;
// TODO: Replace with more robust word wrapping
for (unsigned int i = 0; i < textLines[entity].lines.size(); i++) {
renderer->BatchDrawFont(
textLabelComponent.font,
textLines[entity].lines[i],
textLabelPosition.x,
textLabelPosition.y + (i * textLabelComponent.font->GetSize()) + (i * textLabelComponent.newLinePadding),
transform2DComponent.zIndex,
textLabelScale.x,
textLabelComponent.color
);
}
}
}
}
55 changes: 5 additions & 50 deletions src/core/ecs/system/systems/text_rendering_entity_system.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,56 +16,11 @@ class TextRenderingEntitySystem : public EntitySystem {
std::unordered_map<Entity, TextLines> textLines = {};

public:
TextRenderingEntitySystem() {
renderer = GD::GetContainer()->renderer2D;
sceneManager = GD::GetContainer()->sceneManager;
componentManager = GD::GetContainer()->componentManager;
cameraManager = GD::GetContainer()->cameraManager;
enabled = true;
}

void OnRegisterEntity(Entity entity) override {
EntitySystem::OnRegisterEntity(entity);
UpdateEntityText(entity, componentManager->GetComponent<TextLabelComponent>(entity));
}

void OnEntityDestroyed(Entity entity) override {
EntitySystem::OnEntityDestroyed(entity);
textLines.erase(entity);
}

void UpdateEntityText(Entity entity, const TextLabelComponent& textLabelComponent) {
textLines.erase(entity);
TextLines entityTextLines = StringUtil::ConvertIntoWordWrappedLines(textLabelComponent.text,
textLabelComponent.wordWrap,
textLabelComponent.maxCharactersPerLine);
textLines.emplace(entity, entityTextLines);
}

void Render() override {
if (IsEnabled()) {
for (Entity entity : entities) {
Transform2DComponent transform2DComponent = componentManager->GetComponent<Transform2DComponent>(entity);
Camera2D camera = cameraManager->GetCurrentCamera2D();
Transform2DComponent parentTransform = SceneNodeHelper::GetCombinedParentsTransforms(sceneManager, componentManager, entity);
Vector2 textLabelPosition = SpaceHandler::WorldToScreen(transform2DComponent.position + parentTransform.position, transform2DComponent.ignoreCamera);
TextLabelComponent textLabelComponent = componentManager->GetComponent<TextLabelComponent>(entity);
Vector2 textLabelScale = !transform2DComponent.ignoreCamera ? transform2DComponent.scale * parentTransform.scale * camera.zoom : transform2DComponent.scale * parentTransform.scale;
// TODO: Replace with more robust word wrapping
for (unsigned int i = 0; i < textLines[entity].lines.size(); i++) {
renderer->BatchDrawFont(
textLabelComponent.font,
textLines[entity].lines[i],
textLabelPosition.x,
textLabelPosition.y + (i * textLabelComponent.font->GetSize()) + (i * textLabelComponent.newLinePadding),
transform2DComponent.zIndex,
textLabelScale.x,
textLabelComponent.color
);
}
}
}
}
TextRenderingEntitySystem();
void OnRegisterEntity(Entity entity) override;
void OnEntityDestroyed(Entity entity) override;
void UpdateEntityText(Entity entity, const TextLabelComponent& textLabelComponent);
void Render() override;
};

#endif //TEXT_RENDERING_ENTITY_SYSTEM_H
Loading

0 comments on commit e33b4fc

Please sign in to comment.