Sprites
This commit is contained in:
@@ -24,6 +24,8 @@ public:
|
||||
void MoveLeft(float speed);
|
||||
void MoveRight(float speed);
|
||||
|
||||
void Move(float angle_rad, float speed);
|
||||
|
||||
void Rotate(float speed);
|
||||
void SetRotation(float new_rotation);
|
||||
|
||||
|
@@ -9,7 +9,8 @@ namespace Engine {
|
||||
|
||||
class Engine::Renderable : public Entity {
|
||||
public:
|
||||
virtual void Render() {}
|
||||
// *must* be overridden.
|
||||
virtual void Render() = 0;
|
||||
public:
|
||||
explicit Renderable(const Vector2& position, float rotation = 0.0f) : Entity(position, rotation) {}
|
||||
};
|
30
include/Engine/Entity/Sprite.h
Normal file
30
include/Engine/Entity/Sprite.h
Normal file
@@ -0,0 +1,30 @@
|
||||
#pragma once
|
||||
|
||||
#include <Engine/Entity/Renderable.h>
|
||||
#include <JGL/types/Texture.h>
|
||||
#include <J3ML/Geometry/AABB2D.hpp>
|
||||
|
||||
namespace Engine {
|
||||
class Sprite;
|
||||
}
|
||||
|
||||
class Engine::Sprite : public Renderable {
|
||||
protected:
|
||||
Texture* texture = nullptr;
|
||||
// Positive alpha mask.
|
||||
Texture* alpha_mask = nullptr;
|
||||
Vector2 scale = {1, 1};
|
||||
// Local space, Where the face_angle rotation should be preformed about.
|
||||
Vector2 origin = {0, 0};
|
||||
Color4 base_color = Colors::White;
|
||||
public:
|
||||
[[nodiscard]] Texture* GetTexture();
|
||||
// World space.
|
||||
[[nodiscard]] virtual AABB2D GetBounds();
|
||||
public:
|
||||
void Render() override;
|
||||
public:
|
||||
~Sprite() override;
|
||||
explicit Sprite(const Vector2& pos, const float face_angle = 0.0f, const Color4& base_color = Colors::White, Texture* texture = nullptr,
|
||||
Texture* alpha_mask = nullptr) : Renderable(pos, face_angle), texture(texture), alpha_mask(alpha_mask), base_color(base_color) {}
|
||||
};
|
@@ -4,6 +4,7 @@
|
||||
#include <Engine/Entity/Renderable.h>
|
||||
#include <Engine/Entity/Hud.h>
|
||||
#include <Engine/Level/Fixed.h>
|
||||
#include <JGL/types/RenderTarget.h>
|
||||
|
||||
namespace Engine {
|
||||
class Scene;
|
||||
@@ -36,7 +37,7 @@ public:
|
||||
virtual void Unload();
|
||||
virtual void Init() {}
|
||||
virtual void Update();
|
||||
virtual void Render();
|
||||
virtual void Render(RenderTarget* render_target = nullptr);
|
||||
public:
|
||||
explicit Scene(const std::string& name) : name(name) {}
|
||||
virtual ~Scene();
|
||||
|
@@ -11,6 +11,6 @@ public:
|
||||
|
||||
void Init() final;
|
||||
void Update() final;
|
||||
void Render() final;
|
||||
void Render(RenderTarget* render_target = nullptr) final;
|
||||
~LoadingScreen() final;
|
||||
};
|
||||
|
@@ -11,9 +11,13 @@ void Engine::Entity::MoveY(float speed) {
|
||||
position.y = position.y + (speed * Globals::DeltaTime());
|
||||
}
|
||||
|
||||
void Engine::Entity::Move(float angle_rad, float speed) {
|
||||
position.x = position.x + (speed * Globals::DeltaTime()) * Math::Cos(angle_rad);
|
||||
position.y = position.y + (speed * Globals::DeltaTime()) * Math::Sin(angle_rad);
|
||||
}
|
||||
|
||||
void Engine::Entity::MoveForward(float speed) {
|
||||
position.x = position.x + (speed * Globals::DeltaTime()) * Math::Cos(face_angle);
|
||||
position.y = position.y + (speed * Globals::DeltaTime()) * Math::Sin(face_angle);
|
||||
Move(face_angle, speed);
|
||||
}
|
||||
|
||||
void Engine::Entity::MoveBackward(float speed) {
|
||||
@@ -63,7 +67,6 @@ bool Engine::Entity::AppendChild(Entity* entity) {
|
||||
Engine::Entity::~Entity() {
|
||||
for (auto* e : children)
|
||||
delete e;
|
||||
|
||||
children = {};
|
||||
}
|
||||
|
||||
|
@@ -1 +0,0 @@
|
||||
|
21
src/Engine/Entity/Sprite.cpp
Normal file
21
src/Engine/Entity/Sprite.cpp
Normal file
@@ -0,0 +1,21 @@
|
||||
#include <Engine/Entity/Sprite.h>
|
||||
|
||||
Engine::Sprite::~Sprite() {
|
||||
delete texture;
|
||||
delete alpha_mask;
|
||||
}
|
||||
|
||||
void Engine::Sprite::Render() {
|
||||
if (texture && !alpha_mask)
|
||||
J2D::DrawSprite(texture, position, face_angle, origin, scale, base_color);
|
||||
if (texture && alpha_mask)
|
||||
J2D::DrawSprite(texture, alpha_mask, position, face_angle, origin, scale, base_color);
|
||||
}
|
||||
|
||||
AABB2D Engine::Sprite::GetBounds() {
|
||||
return { position, texture->GetDimensions() + position };
|
||||
}
|
||||
|
||||
Texture* Engine::Sprite::GetTexture() {
|
||||
return texture;
|
||||
}
|
@@ -27,18 +27,21 @@ void Engine::Scene::Update() {
|
||||
e->Update();
|
||||
}
|
||||
|
||||
void Engine::Scene::Render() {
|
||||
void Engine::Scene::Render(RenderTarget* render_target) {
|
||||
for (auto& f : FixedList)
|
||||
if (f->Enabled())
|
||||
f->Render();
|
||||
|
||||
// TODO Render order. In this system it's not possible for child entities to be rendered before the parent.
|
||||
|
||||
J2D::Begin(render_target, true);
|
||||
for (auto& e : EntityList)
|
||||
if (auto* r = dynamic_cast<Renderable*>(e))
|
||||
r->Render();
|
||||
|
||||
if (HeadsUpDisplay)
|
||||
HeadsUpDisplay->Render();
|
||||
J2D::End();
|
||||
}
|
||||
|
||||
Engine::Scene::~Scene() {
|
||||
|
@@ -1,9 +1,7 @@
|
||||
#include <Game/Entity/Box.h>
|
||||
|
||||
void Game::Box::Render() {
|
||||
J2D::Begin(nullptr, true);
|
||||
J2D::FillRect(Colors::Red, Vector2(position), {20, 20});
|
||||
J2D::End();
|
||||
J2D::FillRect(Colors::Red, Vector2(position), {20, 20});
|
||||
}
|
||||
|
||||
void Game::Box::Update() {
|
||||
|
@@ -2,8 +2,5 @@
|
||||
#include <Engine/Globals.h>
|
||||
|
||||
void Game::DemoGameHud::Render() {
|
||||
float framerate = Globals::Window->GetRefreshRate();
|
||||
J2D::Begin(nullptr, true);
|
||||
J2D::DrawString(Colors::Whites::Ivory, "Framerate: " + std::to_string((int) framerate), 0, 0, 1, 16);
|
||||
J2D::End();
|
||||
J2D::DrawString(Colors::Whites::Ivory, "Framerate: " + std::to_string((int) Globals::Window->GetRefreshRate()), 0, 0, 1, 16);
|
||||
}
|
||||
|
@@ -14,14 +14,14 @@ void LoadingScreen::Update() {
|
||||
Globals::ChangeScene("Scene0");
|
||||
}
|
||||
|
||||
void LoadingScreen::Render() {
|
||||
void LoadingScreen::Render(RenderTarget* render_target) {
|
||||
auto text_length = JGL::Fonts::Jupiteroid.MeasureString("Loading ....", 24);
|
||||
std::string text = "Loading";
|
||||
int dots = static_cast<int>(floor(elapsed / 0.35)) % 4;
|
||||
for (int i = 0; i < dots; ++i)
|
||||
text += ".";
|
||||
|
||||
J2D::Begin(nullptr, true);
|
||||
J2D::Begin(render_target, true);
|
||||
J2D::DrawSprite(RedactedSoftware, {Globals::Window->GetSize().x - RedactedSoftware->GetDimensions().x, Globals::Window->GetSize().y - RedactedSoftware->GetDimensions().y}, angle, {0.5, 0.5});
|
||||
J2D::DrawString(Colors::Whites::Ivory, text, (Globals::Window->GetSize().x - text_length.x) - RedactedSoftware->GetDimensions().x , Globals::Window->GetSize().y - text_length.y * 1.125, 1, 24);
|
||||
J2D::End();
|
||||
|
Reference in New Issue
Block a user