Files
glitchy-engine-beef/Sandbox/src/SandboxApp2D.bf
T

323 lines
7.7 KiB
Beef

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;
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();
Texture2D _ge_logo ~ _?.ReleaseRef();
BlendState _alphaBlendState ~ _?.ReleaseRef();
BlendState _opaqueBlendState ~ _?.ReleaseRef();
EffectLibrary _effectLibrary ~ delete _;
EcsWorld _world = new EcsWorld() ~ delete _;
Renderer2D Renderer2D ~ 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, true);
_rasterizerState = new RasterizerState(_context, rsDesc);
_depthTarget = new DepthStencilTarget(_context, _context.SwapChain.Width, _context.SwapChain.Height);
_texture = new Texture2D(_context, "content/Textures/Checkerboard.dds");
_ge_logo = new Texture2D(_context, "content/Textures/GE_Logo.dds");
let sampler = SamplerStateManager.GetSampler(
SamplerStateDescription()
{
MagFilter = .Point
});
_texture.SamplerState = sampler;
_ge_logo.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();
}
VertexLayout layout ~ delete _;
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);
cameraTransform.Update();
}
var camera = _world.GetComponent<CameraComponent>(_cameraEntity);
camera.Aspect = Application.Get().Window.Context.SwapChain.BackbufferViewport.Width /
Application.Get().Window.Context.SwapChain.BackbufferViewport.Height;
RenderCommand.Clear(null, .(0.2f, 0.2f, 0.2f));
_depthTarget..Clear(1.0f, 0, .Depth).Bind();
// Draw test geometry
_context.SetRenderTarget(null);
_context.BindRenderTargets();
_context.SetRasterizerState(_rasterizerState);
_context.SetViewport(_context.SwapChain.BackbufferViewport);
Renderer2D.Begin(.SortByTexture, .(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)));
}
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)));
}
}
Renderer2D.Draw(_texture, 0, 0, 20, 10, .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();
}
ColorRGBA _squareColor0 = ColorRGBA.CornflowerBlue;
ColorRGBA _squareColor1;
public override void OnEvent(Event event)
{
EventDispatcher dispatcher = scope EventDispatcher(event);
dispatcher.Dispatch<ImGuiRenderEvent>(scope (e) => OnImGuiRender(e));
dispatcher.Dispatch<WindowResizeEvent>(scope (e) => OnWindowResize(e));
}
private bool OnImGuiRender(ImGuiRenderEvent e)
{
ImGui.Begin("Test");
ImGui.ColorEdit3("Square Color", ref _squareColor0);
_squareColor1 = ColorRGBA.White - _squareColor0;
ImGui.End();
return false;
}
private bool OnWindowResize(WindowResizeEvent e)
{
_depthTarget.ReleaseRef();
_depthTarget = new DepthStencilTarget(_context, _context.SwapChain.Width, _context.SwapChain.Height);
return false;
}
}
class SandboxApp2D : Application
{
public this()
{
PushLayer(new ExampleLayer2D());
}
#if SANDBOX_2D
[Export, LinkName("CreateApplication")]
#endif
public static Application CreateApplication()
{
return new SandboxApp2D();
}
}
}