Nullable fixes, use Texture2d resource for rendering directly.

This commit is contained in:
2024-01-21 17:34:49 +01:00
parent cfbf46860d
commit 5f4e32e2e0
10 changed files with 105 additions and 74 deletions

View File

@@ -117,35 +117,14 @@ namespace DaggerFramework.Rendering
_rotation = rotation;
}
public override int LoadTexture(Texture2d texture)
public override void DrawTexture(Texture2d texture, Color tint)
{
Image image = new Image();
unsafe
if (texture.Handle == -1)
{
fixed (void* dataPtr = texture.Buffer)
{
image.data = dataPtr;
}
LoadTexture(texture);
}
image.width = texture.Width;
image.height = texture.Height;
image.mipmaps = 1;
image.format = PixelFormat.PIXELFORMAT_UNCOMPRESSED_R8G8B8A8;
Texture2D rayTexture;
rayTexture = Raylib.LoadTextureFromImage(image);
_texturePool.Add(rayTexture);
return _texturePool.Count - 1;
}
public override void DrawTexture(int id, Color tint)
{
Raylib.DrawTextureV(_texturePool[id], _position, DaggerColorToRaylibColor(tint));
Raylib.DrawTextureV(_texturePool[texture.Handle], _position, DaggerColorToRaylibColor(tint));
}
public override void DrawRectangle(Vector2 size, Color color)
@@ -206,12 +185,12 @@ namespace DaggerFramework.Rendering
public override void DrawText(Font font, string text, Color color)
{
if (font.FontHandle == -1)
if (font.Handle == -1)
{
LoadFont(font);
}
var rayFont = _fontPool[font.FontHandle];
var rayFont = _fontPool[font.Handle];
Raylib.DrawTextPro(rayFont, text, _position, Vector2.Zero, _rotation, font.Size, 0.0f, DaggerColorToRaylibColor(color));
}
@@ -255,7 +234,33 @@ namespace DaggerFramework.Rendering
_fontPool.Add(fontRay);
font.FontHandle = _fontPool.Count - 1;
font.Handle = _fontPool.Count - 1;
}
private void LoadTexture(Texture2d texture)
{
Image image = new();
unsafe
{
fixed (void* dataPtr = texture.Buffer)
{
image.data = dataPtr;
}
}
image.width = texture.Width;
image.height = texture.Height;
image.mipmaps = texture.Mipmaps;
image.format = (PixelFormat)texture.Format;
Texture2D rayTexture;
rayTexture = Raylib.LoadTextureFromImage(image);
_texturePool.Add(rayTexture);
texture.Handle = _texturePool.Count - 1;
}

View File

@@ -79,13 +79,6 @@ namespace DaggerFramework.Rendering
/// <param name="color">Background color.</param>
public abstract void ClearBackground(Color color);
/// <summary>
/// Loads the texture onto the GPU for later use in DrawTexture or other Texture related methods.
/// </summary>
/// <param name="texture">Texture to load.</param>
/// <returns>A texture handler on the GPU.</returns>
public abstract int LoadTexture(Texture2d texture);
/// <summary>
/// Sets transforms for the next draw operation.
/// </summary>
@@ -131,9 +124,9 @@ namespace DaggerFramework.Rendering
/// <summary>
/// Draws the texture.
/// </summary>
/// <param name="id">Texture handle.</param>
/// <param name="id">Texture to draw.</param>
/// <param name="tint">Texture tint.</param>
public abstract void DrawTexture(int id, Color tint);
public abstract void DrawTexture(Texture2d texture, Color tint);
}
public enum Msaa

View File

@@ -83,7 +83,7 @@ namespace DaggerFramework.Rendering
}
/// <inheritdoc />
public override void DrawTexture(int id, Color tint)
public override void DrawTexture(Texture2d texture, Color tint)
{
throw new NotImplementedException();
}
@@ -94,12 +94,6 @@ namespace DaggerFramework.Rendering
return 0.0;
}
/// <inheritdoc />
public override int LoadTexture(Texture2d texture)
{
throw new NotImplementedException();
}
/// <inheritdoc />
protected override void SetTargetFps(int fps)
{
@@ -196,7 +190,6 @@ namespace DaggerFramework.Rendering
throw new NotImplementedException();
}
private GL _gl;
private Glfw _glfw;
private unsafe WindowHandle* _windowHandle;

View File

@@ -5,7 +5,7 @@ public class Font : Resource
/// <summary>
/// Internal handle for the font. If it got successfully loaded into the GPU, the value will be other than -1.
/// </summary>
internal int FontHandle { get; set; } = -1;
internal int Handle { get; set; } = -1;
public int Size { get; set; } = 16;
public Font(string path, byte[] buffer) : base(path, buffer)
{

View File

@@ -2,10 +2,43 @@ namespace DaggerFramework
{
public class Texture2d : Resource
{
/// <summary>
/// Internal handle for the texture. If it got successfully loaded into the GPU, the value will be other than -1.
/// </summary>
internal int Handle { get; set; } = -1;
public int Width { get; set; }
public int Height { get; set; }
public int Mipmaps { get; set; } = 1;
public TextureFormat Format { get; set; } = TextureFormat.UncompressedR8G8B8A8;
public Texture2d(string path, byte[] buffer) : base(path, buffer)
{
}
public static Texture2d Empty => new Texture2d(string.Empty, new byte[] { });
}
public enum TextureFormat
{
UncompressedGrayscale = 1,
UncompressedGrayAlpha,
UncompressedR5G6B5,
UncompressedR8G8B8,
UncompressedR5G5B5A1,
UncompressedR4G4B4A4,
UncompressedR8G8B8A8,
UncompressedR32,
UncompressedR32G32B32,
UncompressedR32G32B32A32,
CompressedDXT1Rgb,
CompressedDXT1Rgba,
CompressedDXT3Rgba,
CompressedDXT5Rgba,
CompressedETC1Rgb,
CompressedETC2Rgb,
CompressedETC2EACRgba,
CompressedPVRTRgb,
CompressedPVRTRgba,
CompressedASTC4x4Rgba,
CompressedASTC8x8Rgba
}
}

View File

@@ -5,10 +5,10 @@ namespace DaggerFramework.SceneGraph
{
public class Entity
{
public EntityLayer Layer { get; set; }
public InputHandler Input => Layer.Scene.Input;
public AudioBackend Audio => Layer.Scene.Audio;
public Renderer Renderer => Layer.Scene.Renderer;
public EntityLayer? Layer { get; set; }
public InputHandler Input => Layer!.Scene.Input;
public AudioBackend Audio => Layer!.Scene.Audio;
public Renderer Renderer => Layer!.Scene.Renderer;
public int Id { get; set; }
public void Start() => OnStart();

View File

@@ -8,19 +8,28 @@ namespace DaggerFramework.SceneGraph
public class Particles2d : Drawable2d
{
public int MaxParticles => _maxParticles;
public ParticleSettings Settings => _settings;
public void BeginEmit(ParticleSettings settings)
public Particles2d(ParticleSettings settings)
{
_settings = settings;
_maxParticles = _settings.MaxParticles;
_particleIndex = _maxParticles - 1;
InitializeParticles();
_particles = new Particle[_maxParticles];
}
public void Restart()
{
CleanupParticles();
InitializeParticles();
// Allocate a new particle array if max particles property got changed.
if (_maxParticles != _settings.MaxParticles)
{
_particles = new Particle[_maxParticles];
}
}
public override void OnDraw(Renderer renderer)
{
foreach (var particle in _particles)
@@ -72,11 +81,6 @@ namespace DaggerFramework.SceneGraph
}
}
private void InitializeParticles()
{
_particles = new Particle[_maxParticles];
}
private void Emit()
{
Particle particle = _particles[_particleIndex];

View File

@@ -6,20 +6,18 @@ namespace DaggerFramework.SceneGraph
{
public class Sprite2d : Drawable2d
{
public Texture2d Texture { get => _texture; set => _texture = value; }
public Texture2d Texture { get => _texture ?? Texture2d.Empty; set => _texture = value; }
protected override void OnStart()
{
var renderer = Layer.Scene.Renderer;
_texId = renderer.LoadTexture(_texture);
}
public override void OnDraw(Renderer renderer)
{
renderer.DrawTexture(_texId, Color.White);
renderer.DrawTexture(_texture!, Color.White);
}
private Texture2d _texture;
private int _texId;
private Texture2d? _texture;
}
}

View File

@@ -5,9 +5,9 @@ namespace DaggerFramework.SceneGraph
{
public abstract class Layer : IDrawable
{
public Scene Scene { get; set; }
public InputHandler Input { get; set; }
public ResourceManager ResourceManager => Scene.ResourceManager;
public Scene? Scene { get; set; }
public InputHandler? Input { get; set; }
public ResourceManager ResourceManager => Scene!.ResourceManager;
public void BeginDraw(Renderer renderer) => OnBeginDraw(renderer);
public void Draw(Renderer renderer) => OnDraw(renderer);

View File

@@ -9,8 +9,8 @@ namespace DaggerFramework.SceneGraph
public class Scene : IMainLoop
{
public Renderer Renderer { get => _renderer; set => _renderer = value; }
public InputHandler Input { get => _input; set => _input = value; }
public AudioBackend Audio => _audioBackend;
public InputHandler? Input { get => _input; set => _input = value; }
public AudioBackend? Audio => _audioBackend;
public ResourceManager ResourceManager => _resourceManager;
public double DeltaTime => _renderer.FrameTime;
@@ -26,9 +26,10 @@ namespace DaggerFramework.SceneGraph
_layers = new Dictionary<string, Layer>();
}
public Scene(Renderer renderer)
public Scene(Renderer renderer, ResourceManager resourceManager)
{
_renderer = renderer;
_resourceManager = resourceManager;
}
public void Init() => SetupRenderer();
@@ -36,7 +37,11 @@ namespace DaggerFramework.SceneGraph
{
foreach (var layer in _layers.Values)
{
layer.Input = _input;
if (_input is not null)
{
layer.Input = _input;
}
layer.Start();
}
}
@@ -48,7 +53,7 @@ namespace DaggerFramework.SceneGraph
layer.Value.Update(DeltaTime);
}
Audio.Update();
Audio?.Update();
}
public void AddLayer(string name, Layer layer)
@@ -84,10 +89,10 @@ namespace DaggerFramework.SceneGraph
Renderer.Initialize(new RendererSettings { Msaa = Msaa.Msaa4x, UseVSync = true });
}
private Dictionary<string, Layer> _layers;
private Dictionary<string, Layer> _layers = new();
private Renderer _renderer;
private AudioBackend _audioBackend;
private InputHandler _input;
private AudioBackend? _audioBackend;
private InputHandler? _input;
private ResourceManager _resourceManager;
}
}