215 lines
8.1 KiB
C++
215 lines
8.1 KiB
C++
#include "3rdparty/entt/entt.hpp"
|
|
#include "CyrusEngine.h"
|
|
#include "common/Components.h"
|
|
#include "graphics/font/FontManager.h"
|
|
#include "graphics/shape/Components.h"
|
|
#include "graphics/shape/Shape.h"
|
|
#include "graphics/shape/ShapeRendererSystem.h"
|
|
#include "graphics/sprites/AnimationSystem.h"
|
|
#include "graphics/sprites/Sprite.h"
|
|
#include "graphics/sprites/SpriteRenderingSystem.h"
|
|
#include "graphics/text/Text.h"
|
|
#include "graphics/texture/TextureManager.h"
|
|
#include "physics/PhysicsSystem.h"
|
|
#include "physics/PhysicsWorld.h"
|
|
|
|
int main()
|
|
{
|
|
CE::EngineConfiguration config;
|
|
config.windowTitle = "CyrusEngine Playground";
|
|
config.backgroundColor = CE::Color::Blue;
|
|
config.autoResizeWorldToWindow = true;
|
|
|
|
CE::Initialize(config);
|
|
|
|
CE::ShapeLayer shapeLayer;
|
|
shapeLayer.setup();
|
|
shapeLayer.setProjectionMatrix(CE::GetProjectionMatrix());
|
|
|
|
CE::TextureManager textureManager;
|
|
|
|
textureManager.createAndAddTexture("mainSheet", "../assets/images/main.png");
|
|
auto mainSheetTexture = textureManager.get("mainSheet");
|
|
mainSheetTexture->loadToGPU();
|
|
|
|
// auto catFrameSet = FrameSet::createFrameSet(mainSheetTexture, 240, 700, 480, 900);
|
|
|
|
CE::FontManager fontManager;
|
|
fontManager.createAndAddFont("mainFont");
|
|
auto mainFont = fontManager.get("mainFont");
|
|
mainFont->load("../assets/fonts/Liberastika-Regular.ttf", 24);
|
|
// mainFont->loadFromCharset(
|
|
// "../assets/fonts/RobotoCondensed-Medium.ttf", 18,
|
|
// "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 .,!?-:;'\"()ÅÄÖåäö");
|
|
|
|
auto fpsText = CE::Text::createText(mainFont.get(), "FPS:", 15, 15, 50);
|
|
fpsText->setColor(CE::Color::Black);
|
|
|
|
entt::registry spriteRegistry;
|
|
|
|
CE::SpriteLayer spriteLayer;
|
|
spriteLayer.initialize();
|
|
spriteLayer.setProjectionMatrix(CE::GetProjectionMatrix());
|
|
|
|
// --- ECS Setup ---
|
|
entt::registry registry;
|
|
CE::ShapeRendererSystem shapeRenderSystem;
|
|
|
|
// --- Physics Setup ---
|
|
CE::PhysicsWorld physicsWorld(0.0f, 9.8f); // gravity pointing down
|
|
CE::PhysicsSystem physicsSystem;
|
|
|
|
// Create a dynamic circle that falls
|
|
{
|
|
auto entity = registry.create();
|
|
registry.emplace<CE::TransformComponent>(entity, CE::Math::Vec2{395.0f, 100.0f});
|
|
registry.emplace<CE::CircleComponent>(entity, 25.0f);
|
|
registry.emplace<CE::Color>(entity, 1.0f, 0.0f, 0.0f); // Red
|
|
registry.emplace<CE::RigidBodyComponent>(entity); // dynamic by default
|
|
}
|
|
|
|
// Create another dynamic circle
|
|
{
|
|
auto entity = registry.create();
|
|
registry.emplace<CE::TransformComponent>(entity, CE::Math::Vec2{420.0f, 50.0f});
|
|
registry.emplace<CE::CircleComponent>(entity, 20.0f);
|
|
registry.emplace<CE::Color>(entity, 0.0f, 1.0f, 0.0f); // Green
|
|
registry.emplace<CE::RigidBodyComponent>(entity);
|
|
}
|
|
|
|
// Create a dynamic rect
|
|
{
|
|
auto entity = registry.create();
|
|
CE::TransformComponent transform;
|
|
transform.position = CE::Math::Vec2{350.0f, 150.0f};
|
|
// transform.rotation = 1.0f;
|
|
registry.emplace<CE::TransformComponent>(entity, transform);
|
|
registry.emplace<CE::RectComponent>(entity, 60.0f, 40.0f);
|
|
registry.emplace<CE::Color>(entity, 1.0f, 1.0f, 0.0f); // Yellow
|
|
registry.emplace<CE::RigidBodyComponent>(entity);
|
|
}
|
|
|
|
// Create a static ground platform (rect)
|
|
{
|
|
auto entity = registry.create();
|
|
CE::TransformComponent transform;
|
|
transform.rotation = 0.18f; // Slight angle to test rotation syncing
|
|
transform.position = CE::Math::Vec2{400.0f, 550.0f};
|
|
registry.emplace<CE::TransformComponent>(entity, transform);
|
|
registry.emplace<CE::RectComponent>(entity, 700.0f, 20.0f);
|
|
registry.emplace<CE::Color>(entity, 0.5f, 0.5f, 0.5f); // Grey
|
|
CE::RigidBodyComponent rb;
|
|
rb.isStatic = true;
|
|
rb.friction = 0.0f; // More friction for the ground
|
|
registry.emplace<CE::RigidBodyComponent>(entity, rb);
|
|
}
|
|
|
|
// Initialize physics bodies from ECS components
|
|
physicsSystem.init(registry, physicsWorld);
|
|
|
|
// auto catFrameSet = CE::Sprite::CreateFrameSet(mainSheetTexture, 240, 700, 480, 900, 40, 40,
|
|
// 30);
|
|
|
|
// auto cat = CE::Sprite::CreateAnimation(spriteRegistry, catFrameSet, 0.0f, 200.f, 300.f, 60.f,
|
|
// 60.f, true, 0);
|
|
|
|
// CE::Sprite::CreateSpriteComponent(
|
|
// spriteRegistry, CE::TextureRegion::createTextureRegion(mainSheetTexture, 0, 0, 1000,
|
|
// 600), 1000, 600, 0, 0);
|
|
|
|
while (CE::Run())
|
|
{
|
|
CE::BeginRendering();
|
|
CE::ClearBuffer();
|
|
|
|
if (CE::IsWindowResized())
|
|
{
|
|
shapeLayer.setProjectionMatrix(CE::GetProjectionMatrix());
|
|
spriteLayer.setProjectionMatrix(CE::GetProjectionMatrix());
|
|
}
|
|
|
|
// CE::AnimationSystem::update(CE::GetElapsedTime(), spriteRegistry);
|
|
// CE::SpriteRenderingSystem::Draw(spriteRegistry, spriteLayer);
|
|
|
|
physicsSystem.update(registry, physicsWorld, CE::GetElapsedTime());
|
|
shapeRenderSystem.DrawInOrder(registry, shapeLayer);
|
|
|
|
spriteLayer.draw(fpsText.get());
|
|
|
|
spriteLayer.flush();
|
|
shapeLayer.flush();
|
|
CE::EndRendering();
|
|
}
|
|
|
|
// Cleanup physics bodies before registry is destroyed
|
|
// physicsSystem.cleanup(registry, physicsWorld);
|
|
|
|
fontManager.release();
|
|
textureManager.release();
|
|
CE::Shutdown();
|
|
return 0;
|
|
}
|
|
|
|
// === Instanced rendering examples ===
|
|
|
|
// // Multiple filled circles - each call batches an instance, all drawn at once in drawAll()
|
|
// shapeManager.drawCircle(100.0f, 400.0f, 30.0f, 1.0f, 1.0f, 0.0f); // Yellow
|
|
// shapeManager.drawCircle(180.0f, 400.0f, 25.0f, 1.0f, 0.5f, 0.0f); // Orange
|
|
// shapeManager.drawCircle(250.0f, 400.0f, 40.0f, 0.0f, 1.0f, 1.0f); // Cyan
|
|
|
|
// // Hollow circles (ring shapes) - also instanced, thickness controls ring width
|
|
// shapeManager.drawHollowCircle(500.0f, 150.0f, 60.0f, 10.0f, 1.0f, 1.0f, 1.0f); // White ring
|
|
// shapeManager.drawHollowCircle(650.0f, 150.0f, 45.0f, 5.0f, 1.0f, 0.0f,
|
|
// 1.0f); // Magenta ring
|
|
|
|
// // Multiple rectangles - instanced in a single draw call
|
|
// shapeManager.drawRect(50.0f, 50.0f, 80.0f, 40.0f, 0.0f, 0.8f, 0.2f); // Green rect
|
|
// shapeManager.drawRect(550.0f, 400.0f, 120.0f, 60.0f, 0.8f, 0.2f, 0.8f); // Purple rect
|
|
// shapeManager.drawRect(300.0f, 500.0f, 100.0f, 100.0f, 1.0f, 1.0f, 1.0f,
|
|
// 0.5f); // Semi-transparent white
|
|
|
|
// // Lines (non-instanced, batched as vertices)
|
|
// shapeManager.drawLine(600.0f, 300.0f, 750.0f, 450.0f, 1.0f, 1.0f, 0.0f); // Yellow line
|
|
// shapeManager.drawLine(600.0f, 450.0f, 750.0f, 300.0f, 1.0f, 1.0f, 0.0f); // X pattern
|
|
|
|
// -------------ecs render system draws all shape entities
|
|
// --- ECS Setup ---
|
|
// entt::registry registry;
|
|
// CE::ShapeRendererSystem shapeRenderer;
|
|
|
|
// // Create a red circle entity
|
|
// {
|
|
// auto entity = registry.create();
|
|
// registry.emplace<CE::TransformComponent>(entity, CE::Math::Vec2{200.0f, 300.0f});
|
|
// registry.emplace<CE::CircleComponent>(entity, 40.0f);
|
|
// registry.emplace<CE::Color>(entity, 1.0f, 0.0f, 0.0f);
|
|
// }
|
|
|
|
// // Create a green circle entity
|
|
// {
|
|
// auto entity = registry.create();
|
|
// registry.emplace<CE::TransformComponent>(entity, CE::Math::Vec2{350.0f, 300.0f});
|
|
// registry.emplace<CE::CircleComponent>(entity, 30.0f);
|
|
// registry.emplace<CE::Color>(entity, 0.0f, 1.0f, 0.0f);
|
|
// }
|
|
|
|
// // Create a yellow rectangle entity
|
|
// {
|
|
// auto entity = registry.create();
|
|
// registry.emplace<CE::TransformComponent>(entity, CE::Math::Vec2{500.0f, 250.0f});
|
|
// registry.emplace<CE::RectComponent>(entity, 120.0f, 80.0f);
|
|
// registry.emplace<CE::Color>(entity, 1.0f, 1.0f, 0.0f);
|
|
// }
|
|
|
|
// // Create a white line entity
|
|
// {
|
|
// auto entity = registry.create();
|
|
// registry.emplace<CE::TransformComponent>(entity, CE::Math::Vec2{100.0f, 100.0f});
|
|
// registry.emplace<CE::LineComponent>(entity, CE::Math::Vec2{0.0f, 0.0f},
|
|
// CE::Math::Vec2{150.0f, 100.0f});
|
|
// registry.emplace<CE::Color>(entity, 1.0f, 1.0f, 1.0f);
|
|
// }
|
|
|
|
// ECS render system draws all shape entities
|
|
// shapeRenderer.render(registry, shapeManager);
|