Harrison Deng d8eda24809 Playground now has more control; Implemented framework changes.
Live framerate and update rate changing commands added.
2020-08-22 22:41:04 -05:00

116 lines
4.0 KiB
C#

using System.Drawing;
using System.Numerics;
using SDL2;
using SlatedGameToolkit.Framework.Loaders;
using SlatedGameToolkit.Framework.Graphics.Render;
using SlatedGameToolkit.Framework.Graphics.Text;
using SlatedGameToolkit.Framework.Graphics.Textures;
using SlatedGameToolkit.Framework.Graphics.Window;
using SlatedGameToolkit.Framework.Input.Devices;
using SlatedGameToolkit.Framework.StateSystem;
using SlatedGameToolkit.Framework.StateSystem.States;
using SlatedGameToolkit.Framework.Graphics;
namespace SlatedGameToolkit.Tools.Utilities.Playground
{
public class MainState : IState
{
private WindowContext window;
private Camera2D camera;
private MeshBatchRenderer renderer;
private BitmapFont font;
private Texture logoTexture, fillerTexture;
private RectangleMesh logo, textureTester, untextured;
private string lastPressed;
public WindowContext CurrentWindow { get { return window;}}
public bool Activate()
{
return true;
}
public bool Deactivate()
{
return true;
}
public void Deinitialize()
{
logoTexture.Dispose();
fillerTexture.Dispose();
font.Dispose();
renderer.Dispose();
window.Dispose();
}
public string getName()
{
return "main state";
}
public unsafe void Initialize(StateManager manager)
{
window = new WindowContext("SlatedGameToolkit Playground");
Vector2 drawableDimensions = window.GetDrawableDimensions();
float ratio = drawableDimensions.Y / drawableDimensions.X;
camera = new Camera2D(2f, ratio * 2f);
renderer = new MeshBatchRenderer(camera, BatchVertexSize: 2048);
logoTexture = TextureLoader.Load2DTexture("Resources/Playground/yhdnbgnc.png");
logo = new RectangleMesh(logoTexture, Color.White);
logo.Width = 0.5f;
logo.Height = 0.5f;
logo.X = -0.25f;
logo.Y = -0.25f;
fillerTexture = TextureLoader.Load2DTexture("Resources/Playground/filler.png");
textureTester = new RectangleMesh(fillerTexture, Color.White);
textureTester.Width = 0.15f;
textureTester.Height = 0.15f;
textureTester.X = 0.25f;
untextured = new RectangleMesh(null, Color.Red);
untextured.Width = 0.15f;
untextured.Height = 0.15f;
untextured.X = 0.25f;
untextured.Y = - 0.15f;
font = new BitmapFont("Resources/Playground/earwig_factory_rg.ttf");
font.PixelHeight = 128;
font.PrepareCharacterGroup("ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890".ToCharArray());
}
public void Render(double delta)
{
camera.InterpolatePosition(delta);
renderer.Begin(Matrix4x4.Identity);
renderer.Draw(logo);
renderer.Draw(textureTester);
renderer.Draw(untextured);
font.WriteLine(renderer, 0.25f, -0.35f, "ABCDEFGHIJKLMNOPQRSTUVWXYZ\n1234567890", Color.White);
renderer.End();
}
public void Update(double timeStep)
{
if (Keyboard.IsKeyPressed(SDL.SDL_Keycode.SDLK_DOWN)) {
camera.MoveTo += new Vector2(0, (-0.25f)) * (float) timeStep;
}
if (Keyboard.IsKeyPressed(SDL.SDL_Keycode.SDLK_UP)) {
camera.MoveTo += new Vector2(0, (0.25f)) * (float) timeStep;
}
if (Keyboard.IsKeyPressed(SDL.SDL_Keycode.SDLK_LEFT)) {
camera.MoveTo += new Vector2((-0.25f) * (float) timeStep, 0);
}
if (Keyboard.IsKeyPressed(SDL.SDL_Keycode.SDLK_RIGHT)) {
camera.MoveTo += new Vector2((0.25f) * (float) timeStep, 0);
}
}
public void KeyPressed(SDL.SDL_Keycode keycode, bool pressed) {
if (pressed) lastPressed = SDL.SDL_GetKeyName(keycode);
}
}
}