mirror of
https://github.com/aharabada/glitchy-engine-beef.git
synced 2026-09-05 13:01:52 +00:00
Reimplemented Renderer2D
- Renderer2D now static - Renderer now calls Init and Deinit of Renderer2D - Renderer2D now calls Init and Deinit of FontRenderer - Fixed Deinit order of Layers and renderer (e.g. Layers now deinit first) - Sandbox2D now uses SandboxApp (only loads different layer)
This commit is contained in:
@@ -0,0 +1,390 @@
|
||||
using System;
|
||||
using GlitchyEngine;
|
||||
using GlitchyEngine.Events;
|
||||
using System.Diagnostics;
|
||||
using GlitchLog;
|
||||
using GlitchyEngine.ImGui;
|
||||
using ImGui;
|
||||
using GlitchyEngine.Renderer;
|
||||
using GlitchyEngine.Math;
|
||||
using GlitchyEngine.World;
|
||||
using GlitchyEngine.Renderer.Text;
|
||||
using System.IO;
|
||||
using msdfgen;
|
||||
|
||||
namespace Sandbox
|
||||
{
|
||||
class ExampleLayer2D : Layer
|
||||
{
|
||||
struct Vertexy : IVertexData
|
||||
{
|
||||
public Vector2 Position;
|
||||
|
||||
public this(Vector2 position)
|
||||
{
|
||||
Position = position;
|
||||
}
|
||||
|
||||
public this(float x, float y)
|
||||
{
|
||||
Position = .(x, y);
|
||||
}
|
||||
|
||||
public static VertexElement[] VertexElements ~ delete _;
|
||||
|
||||
public static VertexElement[] IVertexData.VertexElements => VertexElements;
|
||||
|
||||
static this()
|
||||
{
|
||||
VertexElements = new VertexElement[]
|
||||
(
|
||||
VertexElement(.R32G32_Float, "POSITION")
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
RasterizerState _rasterizerState ~ _?.ReleaseRef();
|
||||
|
||||
GraphicsContext _context ~ _?.ReleaseRef();
|
||||
DepthStencilTarget _depthTarget ~ _?.ReleaseRef();
|
||||
|
||||
Texture2D _texture ~ _?.ReleaseRef();
|
||||
|
||||
BlendState _alphaBlendState ~ _?.ReleaseRef();
|
||||
BlendState _opaqueBlendState ~ _?.ReleaseRef();
|
||||
|
||||
EffectLibrary _effectLibrary ~ delete _;
|
||||
|
||||
EcsWorld _world = new EcsWorld() ~ delete _;
|
||||
|
||||
//Renderer2D Renderer2D ~ delete _;
|
||||
|
||||
String testText ~ delete _;
|
||||
|
||||
TextureViewer _textureViewer ~ delete _;
|
||||
|
||||
[AllowAppend]
|
||||
public this() : base("Example")
|
||||
{
|
||||
Application.Get().Window.IsVSync = false;
|
||||
|
||||
_context = Application.Get().Window.Context..AddRef();
|
||||
|
||||
_effectLibrary = new EffectLibrary(_context);
|
||||
|
||||
_effectLibrary.LoadNoRefInc("content\\Shaders\\basicShader.hlsl");
|
||||
|
||||
// Create rasterizer state
|
||||
GlitchyEngine.Renderer.RasterizerStateDescription rsDesc = .(.Solid, .Back, false);
|
||||
_rasterizerState = new RasterizerState(_context, rsDesc);
|
||||
|
||||
_depthTarget = new DepthStencilTarget(_context, _context.SwapChain.Width, _context.SwapChain.Height);
|
||||
|
||||
_texture = new Texture2D(_context, "content/Textures/Checkerboard.dds");
|
||||
|
||||
let sampler = SamplerStateManager.GetSampler(
|
||||
SamplerStateDescription()
|
||||
{
|
||||
MagFilter = .Point,
|
||||
AddressModeU = .Wrap,
|
||||
AddressModeV = .Wrap,
|
||||
});
|
||||
|
||||
_texture.SamplerState = sampler;
|
||||
|
||||
sampler.ReleaseRef();
|
||||
|
||||
BlendStateDescription blendDesc = .();
|
||||
blendDesc.RenderTarget[0] = .(true, .SourceAlpha, .InvertedSourceAlpha, .Add, .SourceAlpha, .InvertedSourceAlpha, .Add, .All);
|
||||
_alphaBlendState = new BlendState(_context, blendDesc);
|
||||
_opaqueBlendState = new BlendState(_context, .Default);
|
||||
|
||||
InitEcs();
|
||||
|
||||
//Renderer2D = new Renderer2D(_context, _effectLibrary);
|
||||
//Init2D();
|
||||
|
||||
Texture2DDesc desc = .();
|
||||
desc.Format = .R8G8B8A8_UNorm;
|
||||
desc.Width = 2;
|
||||
desc.Height = 2;
|
||||
desc.ArraySize = 1;
|
||||
desc.CpuAccess = .None;
|
||||
desc.MipLevels = 1;
|
||||
desc.Usage = .Default;
|
||||
|
||||
fonty = new Font(_context, "C:\\Windows\\Fonts\\arial.ttf", 64, true, 'A', 16);
|
||||
var japanese = new Font(_context, "C:\\Windows\\Fonts\\YuGothM.ttc", 64, true, '\0', 1);
|
||||
var emojis = new Font(_context, "C:\\Windows\\Fonts\\seguiemj.ttf", 64, true, '😂' - 10, 1);
|
||||
var mathstuff = new Font(_context, "C:\\Windows\\Fonts\\cambria.ttc", 64, true, 'α', 1);
|
||||
fonty.Fallback = japanese..ReleaseRefNoDelete();
|
||||
japanese.Fallback = emojis..ReleaseRefNoDelete();
|
||||
emojis.Fallback = mathstuff..ReleaseRefNoDelete();
|
||||
|
||||
// Load test text
|
||||
File.ReadAllText("test.txt", testText = new String(), true);
|
||||
|
||||
_textureViewer = new TextureViewer();
|
||||
|
||||
controller = new OrthographicCameraController(16 / 9f);
|
||||
}
|
||||
|
||||
OrthographicCameraController controller;
|
||||
|
||||
Font fonty ~ _.ReleaseRef();
|
||||
|
||||
VertexLayout layout ~ _?.ReleaseRef();
|
||||
|
||||
GeometryBinding quadBinding ~ _?.ReleaseRef();
|
||||
|
||||
/*
|
||||
void Init2D()
|
||||
{
|
||||
var effect = _effectLibrary.Load("content\\Shaders\\render2dShader.hlsl", "Renderer2D");
|
||||
|
||||
layout = new VertexLayout(_context, Vertexy.VertexElements, effect.VertexShader);
|
||||
|
||||
effect.ReleaseRef();
|
||||
|
||||
VertexBuffer quadVertices = new VertexBuffer(_context, typeof(Vertexy), 4, .Immutable);
|
||||
|
||||
Vertexy[4] vertices = .(
|
||||
Vertexy(0, 0),
|
||||
Vertexy(0, 1),
|
||||
Vertexy(1, 1),
|
||||
Vertexy(1, 0)
|
||||
);
|
||||
|
||||
quadVertices.SetData(vertices);
|
||||
|
||||
IndexBuffer quadIndices = new IndexBuffer(_context, 6, .Immutable);
|
||||
|
||||
uint16[6] indices = .(
|
||||
0, 1, 2,
|
||||
2, 3, 0
|
||||
);
|
||||
|
||||
quadIndices.SetData(indices);
|
||||
|
||||
quadBinding = new GeometryBinding(_context);
|
||||
quadBinding.SetPrimitiveTopology(.TriangleList);
|
||||
quadBinding.SetVertexBufferSlot(quadVertices, 0);
|
||||
quadBinding.SetVertexLayout(layout);
|
||||
quadBinding.SetIndexBuffer(quadIndices);
|
||||
|
||||
quadVertices.ReleaseRef();
|
||||
quadIndices.ReleaseRef();
|
||||
}
|
||||
*/
|
||||
|
||||
Entity _cameraEntity;
|
||||
|
||||
void InitEcs()
|
||||
{
|
||||
_world.Register<TransformComponent>();
|
||||
_world.Register<MeshComponent>();
|
||||
_world.Register<CameraComponent>();
|
||||
|
||||
// Create camera entity
|
||||
_cameraEntity = _world.NewEntity();
|
||||
var cameraTransform = _world.AssignComponent<TransformComponent>(_cameraEntity);
|
||||
var camera = _world.AssignComponent<CameraComponent>(_cameraEntity);
|
||||
*cameraTransform = TransformComponent();
|
||||
camera.NearPlane = 0.1f;
|
||||
camera.FarPlane = 10.0f;
|
||||
camera.FovY = Math.PI_f / 4;
|
||||
cameraTransform.Position = .(0, -1, -5);
|
||||
/*
|
||||
for(int x < 20)
|
||||
for(int y < 20)
|
||||
{
|
||||
Entity entity = _world.NewEntity();
|
||||
|
||||
var transform = _world.AssignComponent<TransformComponent>(entity);
|
||||
transform.Transform = Matrix.Translation(x * 0.2f, y * 0.2f, 0) * Matrix.Scaling(0.1f);
|
||||
|
||||
var mesh = _world.AssignComponent<MeshComponent>(entity);
|
||||
mesh.Mesh = _quadGeometryBinding;
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
public override void Update(GameTime gameTime)
|
||||
{
|
||||
var cameraTransform = _world.GetComponent<TransformComponent>(_cameraEntity);
|
||||
|
||||
if(Application.Get().Window.IsActive)
|
||||
{
|
||||
Vector2 movement = .();
|
||||
|
||||
if(Input.IsKeyPressed(Key.W))
|
||||
{
|
||||
movement.Y += 1;
|
||||
}
|
||||
if(Input.IsKeyPressed(Key.S))
|
||||
{
|
||||
movement.Y -= 1;
|
||||
}
|
||||
|
||||
if(Input.IsKeyPressed(Key.A))
|
||||
{
|
||||
movement.X -= 1;
|
||||
}
|
||||
if(Input.IsKeyPressed(Key.D))
|
||||
{
|
||||
movement.X += 1;
|
||||
}
|
||||
|
||||
if(movement != .Zero)
|
||||
movement.Normalize();
|
||||
|
||||
movement *= (float)(gameTime.FrameTime.TotalSeconds);
|
||||
|
||||
cameraTransform.Position += .(movement, 0);
|
||||
//Runtime.NotImplemented();
|
||||
//cameraTransform.Update();
|
||||
}
|
||||
|
||||
var camera = _world.GetComponent<CameraComponent>(_cameraEntity);
|
||||
camera.Aspect = Application.Get().Window.Context.SwapChain.BackbufferViewport.Width /
|
||||
Application.Get().Window.Context.SwapChain.BackbufferViewport.Height;
|
||||
|
||||
controller.Update(gameTime);
|
||||
|
||||
TransformSystem.Update(_world);
|
||||
|
||||
RenderCommand.Clear(null, .(0.2f, 0.2f, 0.2f));
|
||||
RenderCommand.Clear(_depthTarget, 1.0f, 0, .Depth);
|
||||
|
||||
// Draw test geometry
|
||||
_context.SetRenderTarget(null);
|
||||
_depthTarget.Bind();
|
||||
_context.BindRenderTargets();
|
||||
|
||||
_context.SetRasterizerState(_rasterizerState);
|
||||
|
||||
RenderCommand.SetViewport(_context.SwapChain.BackbufferViewport);
|
||||
|
||||
Renderer2D.BeginScene(controller.Camera, .BackToFront);
|
||||
|
||||
_alphaBlendState.Bind();
|
||||
|
||||
for(int x < 10)
|
||||
for(int y < 10)
|
||||
{
|
||||
int i = (x + y) % 2;
|
||||
|
||||
Renderer2D.DrawQuad(Vector3(2 * x, 2 * y, 0.5f), Vector2(1.5f, 1), MathHelper.PiOverFour, (i == 0) ? _squareColor0 : _squareColor1);
|
||||
}
|
||||
|
||||
Renderer2D.DrawQuad(Vector3(0, 0, 1), Vector2(10), 0, _texture, .White, .(0, 0, 1, 1));
|
||||
|
||||
FontRenderer.DrawText(fonty, "Hallo! gjy", 0, 0, 64, .White, .White);
|
||||
|
||||
Renderer2D.EndScene();
|
||||
|
||||
/*
|
||||
Renderer2D.Begin(.FrontToBack, .(80, 80));
|
||||
|
||||
//_opaqueBlendState.Bind();
|
||||
_alphaBlendState.Bind();
|
||||
|
||||
//Renderer2D.DrawQuad(5, 5, 1, 1, .Red);
|
||||
//Renderer2D.DrawQuad(5, 5, 1, 1, .Blue);
|
||||
|
||||
Random r = scope Random(1337);
|
||||
|
||||
//_texture.Bind(0);
|
||||
//_ge_logo.Bind(1);
|
||||
|
||||
for(int x < 40)
|
||||
for(int y < 40)
|
||||
{
|
||||
int i = (x + y) % 2 + 1;//((x + (y * 40)) ^ (x * y)) % 3;
|
||||
|
||||
if(i == 1)
|
||||
{
|
||||
Renderer2D.Draw(_texture, 2 * x, 2 * y, 1, 1, .(r.Next(0, 256), r.Next(0, 256), r.Next(0, 256)), 10);
|
||||
}
|
||||
else if(i == 2)
|
||||
{
|
||||
Renderer2D.Draw(_ge_logo, 2 * x, 2 * y, 1, 1, .(r.Next(0, 256), r.Next(0, 256), r.Next(0, 256)), 10);
|
||||
}
|
||||
}
|
||||
|
||||
Renderer2D.Draw(_ge_logo, 0, 0, 180, 40, .White, 20);
|
||||
|
||||
/*
|
||||
Renderer2D.Draw(_texture, 10, 50, 100, 100, .White);
|
||||
|
||||
Renderer2D.Draw(_texture, 120, 50, 100, 100, .White);
|
||||
|
||||
Renderer2D.Draw(_texture, 10, 160, 100, 100, .White);
|
||||
|
||||
Renderer2D.Draw(_texture, 120, 160, 100, 100, .White);
|
||||
*/
|
||||
//_alphaBlendState.Bind();
|
||||
|
||||
//Renderer2D.Draw(_ge_logo, 80, 50, 100, 100, .White);
|
||||
|
||||
Renderer2D.End();
|
||||
*/
|
||||
/*
|
||||
Renderer2D.Begin(.FrontToBack, .(_context.SwapChain.Width, _context.SwapChain.Height));
|
||||
|
||||
Renderer2D.End();
|
||||
|
||||
_alphaBlendState.Bind();
|
||||
Renderer2D.Begin(.FrontToBack, .(_context.SwapChain.Width, _context.SwapChain.Height), 100);
|
||||
|
||||
FontRenderer.DrawText(Renderer2D, fonty, "Hallo! gjy", 0, 0, 64, .White, .White);
|
||||
|
||||
Renderer2D.End();
|
||||
*/
|
||||
}
|
||||
|
||||
int32 size = 500;
|
||||
|
||||
ColorRGBA _squareColor0 = ColorRGBA.CornflowerBlue;
|
||||
ColorRGBA _squareColor1;
|
||||
|
||||
public override void OnEvent(Event event)
|
||||
{
|
||||
EventDispatcher dispatcher = EventDispatcher(event);
|
||||
|
||||
dispatcher.Dispatch<ImGuiRenderEvent>(scope (e) => OnImGuiRender(e));
|
||||
|
||||
dispatcher.Dispatch<WindowResizeEvent>(scope (e) => OnWindowResize(e));
|
||||
|
||||
controller.OnEvent(event);
|
||||
}
|
||||
|
||||
private bool OnImGuiRender(ImGuiRenderEvent e)
|
||||
{
|
||||
ImGui.Begin("Test");
|
||||
|
||||
ImGui.ColorPicker4("Color", *(float[4]*)&_squareColor0);
|
||||
|
||||
_squareColor1 = ColorRGBA.White - _squareColor0;
|
||||
_squareColor1.A = _squareColor0.A;
|
||||
|
||||
ImGui.End();
|
||||
|
||||
_textureViewer.ViewTexture(fonty.[Friend]_atlas);
|
||||
|
||||
_context.SetRenderTarget(null);
|
||||
_depthTarget.Bind();
|
||||
_context.BindRenderTargets();
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private bool OnWindowResize(WindowResizeEvent e)
|
||||
{
|
||||
_depthTarget.ReleaseRef();
|
||||
_depthTarget = new DepthStencilTarget(_context, _context.SwapChain.Width, _context.SwapChain.Height);
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user