Skip to content

Commit

Permalink
Improved the Sprite Renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
Kraaven committed Jun 23, 2024
1 parent a480947 commit 38470b4
Show file tree
Hide file tree
Showing 12 changed files with 96 additions and 26 deletions.
59 changes: 35 additions & 24 deletions RayGame/Renderers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,14 @@ public class SpriteRenderer : IRenderer
/// </summary>
public GameObject Container { get; set; }

private Texture2D sprite;

private Rectangle source;

// private Texture2D sprite;
private List<(Rectangle,Sprite)> SpriteList;
private int SpriteIndex;
private Rectangle destination;

private Vector2 Origin;

public Transform transform;
private (Rectangle ,Sprite) SelectedSprite;

// private Texture2D spritesheet;
/// <summary>
/// Initializes the renderer. This method is called when the renderer is first added to a <see cref="GameObject"/>.
Expand All @@ -93,39 +92,51 @@ public void Start()
{
transform = new Transform();
destination = new Rectangle();
SpriteList = new();
SpriteIndex = 0;
}

public Sprite GetSprite(int SIndex)
{
return SpriteList[SIndex].Item2;
}

/// <summary>
/// Retrieves the current sprite texture.
/// </summary>
/// <returns>The current <see cref="Texture2D"/> sprite.</returns>
public Texture2D GetSprite()
public void SetIndex(int i)
{
return sprite;
if (i < SpriteList.Count)
{
SpriteIndex = i;
}
}

/// <summary>
/// Sets the sprite texture.
/// </summary>
/// <param name="inputSprite">The new <see cref="Texture2D"/> sprite to set.</param>
public void SetSprite(Texture2D ImputSprite)

public void ChangeIndex(int i)
{
sprite = ImputSprite;
source = new Rectangle(0, 0, sprite.Width, sprite.Height);

if (SpriteIndex + i < SpriteList.Count)
{
SpriteIndex += i;
}
}

public void AddSprite(Sprite InputSprite)
{
Rectangle src = new Rectangle(0,0,InputSprite.Image.Width,InputSprite.Image.Height);
SpriteList.Add(new (src,InputSprite));
}

/// <summary>
/// Updates the renderer. This method is called once per frame.
/// </summary>
public void Update()
{
SelectedSprite = SpriteList[SpriteIndex];


destination.Position = Container.Transform.Position + transform.Position;
destination.Width = sprite.Width * (Container.Transform.Scale * transform.Scale);
destination.Height = sprite.Height * (Container.Transform.Scale* transform.Scale);
destination.Width = SelectedSprite.Item2.Image.Width * (Container.Transform.Scale * transform.Scale);
destination.Height = SelectedSprite.Item2.Image.Height * (Container.Transform.Scale* transform.Scale);
Origin = new Vector2(destination.Width / 2, destination.Height / 2);

Raylib.DrawTexturePro(sprite,source,destination,Origin, Container.Transform.GetRotation() + transform.GetRotation(),Color.White);
Raylib.DrawTexturePro(SelectedSprite.Item2.Image,SelectedSprite.Item1,destination,Origin, Container.Transform.GetRotation() + transform.GetRotation(),Color.White);
Console.WriteLine(destination);
}
}
30 changes: 30 additions & 0 deletions RayGame/Resources.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Raylib_cs;

namespace RayGame;

public class Resources
{

}

public class Sprite
{
public Texture2D Image;

public Sprite(string TextureName)
{
try
{
var I = Raylib.LoadImage($"/Resources/{TextureName}");
Image = Raylib.LoadTextureFromImage(I);
Raylib.UnloadImage(I);
}
catch (Exception e)
{
Console.WriteLine("Image does not Exist");
throw;
}
}


}
15 changes: 15 additions & 0 deletions RayGame/bin/Debug/RayGame.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified RayGame/bin/Debug/net7.0/RayGame.dll
Binary file not shown.
Binary file modified RayGame/bin/Debug/net7.0/RayGame.pdb
Binary file not shown.
15 changes: 15 additions & 0 deletions RayGame/bin/Debug/net7.0/RayGame.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1 +1 @@
b3f94a3005f13e86a2b5d6ee29bad87c0808b77f
ee154ba413beb2ea85b29ee1976f6fa3aaeae069
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,4 @@ C:\Users\dhruv\RiderProjects\RayGame\RayGame\bin\Debug\net7.0\runtimes\win-x64\n
C:\Users\dhruv\RiderProjects\RayGame\RayGame\bin\Debug\net7.0\runtimes\win-x86\native\raylib.dll
C:\Users\dhruv\RiderProjects\RayGame\RayGame\obj\Debug\net7.0\RayGame.csproj.AssemblyReference.cache
C:\Users\dhruv\RiderProjects\RayGame\RayGame\obj\Debug\net7.0\RayGame.csproj.CopyComplete
C:\Users\dhruv\RiderProjects\RayGame\RayGame\bin\Debug\net7.0\Demo\test.png
C:\Users\dhruv\RiderProjects\RayGame\RayGame\bin\Debug\net7.0\RayGame.xml
Binary file modified RayGame/obj/Debug/net7.0/RayGame.dll
Binary file not shown.
Binary file modified RayGame/obj/Debug/net7.0/RayGame.pdb
Binary file not shown.
Binary file modified RayGame/obj/Debug/net7.0/ref/RayGame.dll
Binary file not shown.
Binary file modified RayGame/obj/Debug/net7.0/refint/RayGame.dll
Binary file not shown.

0 comments on commit 38470b4

Please sign in to comment.