diff --git a/DaggerFramework/Source/Rendering/RaylibRenderer.cs b/DaggerFramework/Source/Rendering/RaylibRenderer.cs
index 2516fff..c368475 100644
--- a/DaggerFramework/Source/Rendering/RaylibRenderer.cs
+++ b/DaggerFramework/Source/Rendering/RaylibRenderer.cs
@@ -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;
}
diff --git a/DaggerFramework/Source/Rendering/Renderer.cs b/DaggerFramework/Source/Rendering/Renderer.cs
index efa283c..b28f82d 100644
--- a/DaggerFramework/Source/Rendering/Renderer.cs
+++ b/DaggerFramework/Source/Rendering/Renderer.cs
@@ -79,13 +79,6 @@ namespace DaggerFramework.Rendering
/// Background color.
public abstract void ClearBackground(Color color);
- ///
- /// Loads the texture onto the GPU for later use in DrawTexture or other Texture related methods.
- ///
- /// Texture to load.
- /// A texture handler on the GPU.
- public abstract int LoadTexture(Texture2d texture);
-
///
/// Sets transforms for the next draw operation.
///
@@ -131,9 +124,9 @@ namespace DaggerFramework.Rendering
///
/// Draws the texture.
///
- /// Texture handle.
+ /// Texture to draw.
/// Texture tint.
- public abstract void DrawTexture(int id, Color tint);
+ public abstract void DrawTexture(Texture2d texture, Color tint);
}
public enum Msaa
diff --git a/DaggerFramework/Source/Rendering/StandardRenderer.cs b/DaggerFramework/Source/Rendering/StandardRenderer.cs
index bd6640b..e8defed 100644
--- a/DaggerFramework/Source/Rendering/StandardRenderer.cs
+++ b/DaggerFramework/Source/Rendering/StandardRenderer.cs
@@ -83,7 +83,7 @@ namespace DaggerFramework.Rendering
}
///
- 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;
}
- ///
- public override int LoadTexture(Texture2d texture)
- {
- throw new NotImplementedException();
- }
-
///
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;
diff --git a/DaggerFramework/Source/Resources/Font.cs b/DaggerFramework/Source/Resources/Font.cs
index c945183..f4ad91b 100644
--- a/DaggerFramework/Source/Resources/Font.cs
+++ b/DaggerFramework/Source/Resources/Font.cs
@@ -5,7 +5,7 @@ public class Font : Resource
///
/// Internal handle for the font. If it got successfully loaded into the GPU, the value will be other than -1.
///
- 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)
{
diff --git a/DaggerFramework/Source/Resources/Texture2d.cs b/DaggerFramework/Source/Resources/Texture2d.cs
index 3af1127..ee67bed 100644
--- a/DaggerFramework/Source/Resources/Texture2d.cs
+++ b/DaggerFramework/Source/Resources/Texture2d.cs
@@ -2,10 +2,43 @@ namespace DaggerFramework
{
public class Texture2d : Resource
{
+ ///
+ /// Internal handle for the texture. If it got successfully loaded into the GPU, the value will be other than -1.
+ ///
+ 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
}
}
\ No newline at end of file
diff --git a/DaggerFramework/Source/SceneGraph/Entities/Entity.cs b/DaggerFramework/Source/SceneGraph/Entities/Entity.cs
index 33fe0a7..18b50dc 100644
--- a/DaggerFramework/Source/SceneGraph/Entities/Entity.cs
+++ b/DaggerFramework/Source/SceneGraph/Entities/Entity.cs
@@ -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();
diff --git a/DaggerFramework/Source/SceneGraph/Entities/Particles2d.cs b/DaggerFramework/Source/SceneGraph/Entities/Particles2d.cs
index 2df7250..a42a726 100644
--- a/DaggerFramework/Source/SceneGraph/Entities/Particles2d.cs
+++ b/DaggerFramework/Source/SceneGraph/Entities/Particles2d.cs
@@ -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];
diff --git a/DaggerFramework/Source/SceneGraph/Entities/Sprite2d.cs b/DaggerFramework/Source/SceneGraph/Entities/Sprite2d.cs
index 6d4a308..ab62013 100644
--- a/DaggerFramework/Source/SceneGraph/Entities/Sprite2d.cs
+++ b/DaggerFramework/Source/SceneGraph/Entities/Sprite2d.cs
@@ -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;
}
}
\ No newline at end of file
diff --git a/DaggerFramework/Source/SceneGraph/Layer.cs b/DaggerFramework/Source/SceneGraph/Layer.cs
index 977e378..1982762 100644
--- a/DaggerFramework/Source/SceneGraph/Layer.cs
+++ b/DaggerFramework/Source/SceneGraph/Layer.cs
@@ -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);
diff --git a/DaggerFramework/Source/SceneGraph/Scene.cs b/DaggerFramework/Source/SceneGraph/Scene.cs
index e77632e..297e3f5 100644
--- a/DaggerFramework/Source/SceneGraph/Scene.cs
+++ b/DaggerFramework/Source/SceneGraph/Scene.cs
@@ -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();
}
- 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 _layers;
+ private Dictionary _layers = new();
private Renderer _renderer;
- private AudioBackend _audioBackend;
- private InputHandler _input;
+ private AudioBackend? _audioBackend;
+ private InputHandler? _input;
private ResourceManager _resourceManager;
}
}
\ No newline at end of file