KumiScript/renderer/Sprite.cs

59 lines
1.5 KiB
C#
Raw Normal View History

2024-01-26 05:37:51 +00:00
using KumiScript.Loader;
using SDL2;
namespace KumiScript.Renderer
{
public class Sprite : SceneElement, IDisposable
2024-01-26 05:37:51 +00:00
{
SDLTexture _texture;
SDLRenderer _renderer;
2024-01-26 05:37:51 +00:00
readonly int _height;
readonly int _width;
int _anim;
int _frame;
2024-01-26 05:37:51 +00:00
public Sprite(string path, SDLRenderer renderer)
{
_texture = new SDLTexture(path, renderer);
2024-01-26 05:37:51 +00:00
_renderer = renderer;
KSMetaParser parser = new KSMetaParser(string.Concat(path, ".ksmeta"));
_width = parser.GetAttribute("width");
_height = parser.GetAttribute("height");
_anim = 0;
_frame = 0;
2024-01-26 05:37:51 +00:00
}
public void Dispose()
2024-01-26 05:37:51 +00:00
{
_texture.Dispose();
return;
2024-01-26 05:37:51 +00:00
}
public override void Draw(int x, int y)
2024-01-26 05:37:51 +00:00
{
SDL.SDL_Rect destRect = new SDL.SDL_Rect();
destRect.x = x;
destRect.y = y;
destRect.w = _width;
destRect.h = _height;
SDL.SDL_Rect sourceRect = new SDL.SDL_Rect();
sourceRect.x = _anim * _width;
sourceRect.y = _frame * _height;
sourceRect.w = _width;
sourceRect.h = _height;
2024-01-26 05:37:51 +00:00
_texture.Draw(_renderer, sourceRect, destRect);
2024-01-26 05:37:51 +00:00
}
public override int GetBitmapHeight()
2024-01-26 05:37:51 +00:00
{
return _height;
2024-01-26 05:37:51 +00:00
}
public override int GetBitmapWidth()
2024-01-26 05:37:51 +00:00
{
return _width;
2024-01-26 05:37:51 +00:00
}
}
}