Nullable fixes, use Texture2d resource for rendering directly.
This commit is contained in:
@@ -117,35 +117,14 @@ namespace DaggerFramework.Rendering
|
|||||||
_rotation = rotation;
|
_rotation = rotation;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override int LoadTexture(Texture2d texture)
|
public override void DrawTexture(Texture2d texture, Color tint)
|
||||||
{
|
{
|
||||||
Image image = new Image();
|
if (texture.Handle == -1)
|
||||||
|
|
||||||
unsafe
|
|
||||||
{
|
{
|
||||||
fixed (void* dataPtr = texture.Buffer)
|
LoadTexture(texture);
|
||||||
{
|
|
||||||
image.data = dataPtr;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
image.width = texture.Width;
|
Raylib.DrawTextureV(_texturePool[texture.Handle], _position, DaggerColorToRaylibColor(tint));
|
||||||
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));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void DrawRectangle(Vector2 size, Color color)
|
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)
|
public override void DrawText(Font font, string text, Color color)
|
||||||
{
|
{
|
||||||
if (font.FontHandle == -1)
|
if (font.Handle == -1)
|
||||||
{
|
{
|
||||||
LoadFont(font);
|
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));
|
Raylib.DrawTextPro(rayFont, text, _position, Vector2.Zero, _rotation, font.Size, 0.0f, DaggerColorToRaylibColor(color));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -255,7 +234,33 @@ namespace DaggerFramework.Rendering
|
|||||||
|
|
||||||
_fontPool.Add(fontRay);
|
_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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -79,13 +79,6 @@ namespace DaggerFramework.Rendering
|
|||||||
/// <param name="color">Background color.</param>
|
/// <param name="color">Background color.</param>
|
||||||
public abstract void ClearBackground(Color color);
|
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>
|
/// <summary>
|
||||||
/// Sets transforms for the next draw operation.
|
/// Sets transforms for the next draw operation.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -131,9 +124,9 @@ namespace DaggerFramework.Rendering
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Draws the texture.
|
/// Draws the texture.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="id">Texture handle.</param>
|
/// <param name="id">Texture to draw.</param>
|
||||||
/// <param name="tint">Texture tint.</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
|
public enum Msaa
|
||||||
|
|||||||
@@ -83,7 +83,7 @@ namespace DaggerFramework.Rendering
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override void DrawTexture(int id, Color tint)
|
public override void DrawTexture(Texture2d texture, Color tint)
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
throw new NotImplementedException();
|
||||||
}
|
}
|
||||||
@@ -94,12 +94,6 @@ namespace DaggerFramework.Rendering
|
|||||||
return 0.0;
|
return 0.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public override int LoadTexture(Texture2d texture)
|
|
||||||
{
|
|
||||||
throw new NotImplementedException();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
protected override void SetTargetFps(int fps)
|
protected override void SetTargetFps(int fps)
|
||||||
{
|
{
|
||||||
@@ -196,7 +190,6 @@ namespace DaggerFramework.Rendering
|
|||||||
throw new NotImplementedException();
|
throw new NotImplementedException();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private GL _gl;
|
private GL _gl;
|
||||||
private Glfw _glfw;
|
private Glfw _glfw;
|
||||||
private unsafe WindowHandle* _windowHandle;
|
private unsafe WindowHandle* _windowHandle;
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ public class Font : Resource
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Internal handle for the font. If it got successfully loaded into the GPU, the value will be other than -1.
|
/// Internal handle for the font. If it got successfully loaded into the GPU, the value will be other than -1.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
internal int FontHandle { get; set; } = -1;
|
internal int Handle { get; set; } = -1;
|
||||||
public int Size { get; set; } = 16;
|
public int Size { get; set; } = 16;
|
||||||
public Font(string path, byte[] buffer) : base(path, buffer)
|
public Font(string path, byte[] buffer) : base(path, buffer)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -2,10 +2,43 @@ namespace DaggerFramework
|
|||||||
{
|
{
|
||||||
public class Texture2d : Resource
|
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 Width { get; set; }
|
||||||
public int Height { 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 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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -5,10 +5,10 @@ namespace DaggerFramework.SceneGraph
|
|||||||
{
|
{
|
||||||
public class Entity
|
public class Entity
|
||||||
{
|
{
|
||||||
public EntityLayer Layer { get; set; }
|
public EntityLayer? Layer { get; set; }
|
||||||
public InputHandler Input => Layer.Scene.Input;
|
public InputHandler Input => Layer!.Scene.Input;
|
||||||
public AudioBackend Audio => Layer.Scene.Audio;
|
public AudioBackend Audio => Layer!.Scene.Audio;
|
||||||
public Renderer Renderer => Layer.Scene.Renderer;
|
public Renderer Renderer => Layer!.Scene.Renderer;
|
||||||
public int Id { get; set; }
|
public int Id { get; set; }
|
||||||
|
|
||||||
public void Start() => OnStart();
|
public void Start() => OnStart();
|
||||||
|
|||||||
@@ -8,19 +8,28 @@ namespace DaggerFramework.SceneGraph
|
|||||||
public class Particles2d : Drawable2d
|
public class Particles2d : Drawable2d
|
||||||
{
|
{
|
||||||
public int MaxParticles => _maxParticles;
|
public int MaxParticles => _maxParticles;
|
||||||
|
public ParticleSettings Settings => _settings;
|
||||||
|
|
||||||
public void BeginEmit(ParticleSettings settings)
|
public Particles2d(ParticleSettings settings)
|
||||||
{
|
{
|
||||||
_settings = settings;
|
_settings = settings;
|
||||||
_maxParticles = _settings.MaxParticles;
|
_maxParticles = _settings.MaxParticles;
|
||||||
_particleIndex = _maxParticles - 1;
|
_particleIndex = _maxParticles - 1;
|
||||||
InitializeParticles();
|
|
||||||
|
_particles = new Particle[_maxParticles];
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Restart()
|
public void Restart()
|
||||||
{
|
{
|
||||||
CleanupParticles();
|
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)
|
public override void OnDraw(Renderer renderer)
|
||||||
{
|
{
|
||||||
foreach (var particle in _particles)
|
foreach (var particle in _particles)
|
||||||
@@ -72,11 +81,6 @@ namespace DaggerFramework.SceneGraph
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void InitializeParticles()
|
|
||||||
{
|
|
||||||
_particles = new Particle[_maxParticles];
|
|
||||||
}
|
|
||||||
|
|
||||||
private void Emit()
|
private void Emit()
|
||||||
{
|
{
|
||||||
Particle particle = _particles[_particleIndex];
|
Particle particle = _particles[_particleIndex];
|
||||||
|
|||||||
@@ -6,20 +6,18 @@ namespace DaggerFramework.SceneGraph
|
|||||||
{
|
{
|
||||||
public class Sprite2d : Drawable2d
|
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()
|
protected override void OnStart()
|
||||||
{
|
{
|
||||||
var renderer = Layer.Scene.Renderer;
|
var renderer = Layer.Scene.Renderer;
|
||||||
_texId = renderer.LoadTexture(_texture);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void OnDraw(Renderer renderer)
|
public override void OnDraw(Renderer renderer)
|
||||||
{
|
{
|
||||||
renderer.DrawTexture(_texId, Color.White);
|
renderer.DrawTexture(_texture!, Color.White);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Texture2d _texture;
|
private Texture2d? _texture;
|
||||||
private int _texId;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -5,9 +5,9 @@ namespace DaggerFramework.SceneGraph
|
|||||||
{
|
{
|
||||||
public abstract class Layer : IDrawable
|
public abstract class Layer : IDrawable
|
||||||
{
|
{
|
||||||
public Scene Scene { get; set; }
|
public Scene? Scene { get; set; }
|
||||||
public InputHandler Input { get; set; }
|
public InputHandler? Input { get; set; }
|
||||||
public ResourceManager ResourceManager => Scene.ResourceManager;
|
public ResourceManager ResourceManager => Scene!.ResourceManager;
|
||||||
|
|
||||||
public void BeginDraw(Renderer renderer) => OnBeginDraw(renderer);
|
public void BeginDraw(Renderer renderer) => OnBeginDraw(renderer);
|
||||||
public void Draw(Renderer renderer) => OnDraw(renderer);
|
public void Draw(Renderer renderer) => OnDraw(renderer);
|
||||||
|
|||||||
@@ -9,8 +9,8 @@ namespace DaggerFramework.SceneGraph
|
|||||||
public class Scene : IMainLoop
|
public class Scene : IMainLoop
|
||||||
{
|
{
|
||||||
public Renderer Renderer { get => _renderer; set => _renderer = value; }
|
public Renderer Renderer { get => _renderer; set => _renderer = value; }
|
||||||
public InputHandler Input { get => _input; set => _input = value; }
|
public InputHandler? Input { get => _input; set => _input = value; }
|
||||||
public AudioBackend Audio => _audioBackend;
|
public AudioBackend? Audio => _audioBackend;
|
||||||
public ResourceManager ResourceManager => _resourceManager;
|
public ResourceManager ResourceManager => _resourceManager;
|
||||||
|
|
||||||
public double DeltaTime => _renderer.FrameTime;
|
public double DeltaTime => _renderer.FrameTime;
|
||||||
@@ -26,17 +26,22 @@ namespace DaggerFramework.SceneGraph
|
|||||||
_layers = new Dictionary<string, Layer>();
|
_layers = new Dictionary<string, Layer>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Scene(Renderer renderer)
|
public Scene(Renderer renderer, ResourceManager resourceManager)
|
||||||
{
|
{
|
||||||
_renderer = renderer;
|
_renderer = renderer;
|
||||||
|
_resourceManager = resourceManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Init() => SetupRenderer();
|
public void Init() => SetupRenderer();
|
||||||
public void Start()
|
public void Start()
|
||||||
{
|
{
|
||||||
foreach (var layer in _layers.Values)
|
foreach (var layer in _layers.Values)
|
||||||
|
{
|
||||||
|
if (_input is not null)
|
||||||
{
|
{
|
||||||
layer.Input = _input;
|
layer.Input = _input;
|
||||||
|
}
|
||||||
|
|
||||||
layer.Start();
|
layer.Start();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -48,7 +53,7 @@ namespace DaggerFramework.SceneGraph
|
|||||||
layer.Value.Update(DeltaTime);
|
layer.Value.Update(DeltaTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
Audio.Update();
|
Audio?.Update();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AddLayer(string name, Layer layer)
|
public void AddLayer(string name, Layer layer)
|
||||||
@@ -84,10 +89,10 @@ namespace DaggerFramework.SceneGraph
|
|||||||
Renderer.Initialize(new RendererSettings { Msaa = Msaa.Msaa4x, UseVSync = true });
|
Renderer.Initialize(new RendererSettings { Msaa = Msaa.Msaa4x, UseVSync = true });
|
||||||
}
|
}
|
||||||
|
|
||||||
private Dictionary<string, Layer> _layers;
|
private Dictionary<string, Layer> _layers = new();
|
||||||
private Renderer _renderer;
|
private Renderer _renderer;
|
||||||
private AudioBackend _audioBackend;
|
private AudioBackend? _audioBackend;
|
||||||
private InputHandler _input;
|
private InputHandler? _input;
|
||||||
private ResourceManager _resourceManager;
|
private ResourceManager _resourceManager;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user