Example 3: Source Rectangle

Last updated

Last updated
using System;
using odl;
namespace TestNamespace
{
class Program
{
public static void Main(string[] args)
{
Graphics.Start();
Window win = new Window();
win.Initialize(true, true);
Viewport vp = new Viewport(0, 0, win.Width, win.Height);
Sprite s = new Sprite(vp);
s.Bitmap = new Bitmap("boy");
s.SrcRect.Width = s.Bitmap.Width / 4;
s.SrcRect.Height = s.Bitmap.Height / 4;
int dir = 0;
int frame = 0;
win.Show();
while (Graphics.CanUpdate())
{
if (Input.Press(SDL2.SDL.SDL_Keycode.SDLK_DOWN))
{
dir = 0;
s.Y += 3;
frame++;
}
else if (Input.Press(SDL2.SDL.SDL_Keycode.SDLK_LEFT))
{
dir = 1;
s.X -= 3;
frame++;
}
else if (Input.Press(SDL2.SDL.SDL_Keycode.SDLK_RIGHT))
{
dir = 2;
s.X += 3;
frame++;
}
else if (Input.Press(SDL2.SDL.SDL_Keycode.SDLK_UP))
{
dir = 3;
s.Y -= 3;
frame++;
}
else
{
frame = 0;
}
dir %= 4;
s.SrcRect.X = ((frame / 10) % 4) * s.SrcRect.Width;
s.SrcRect.Y = dir * s.SrcRect.Height;
Graphics.Update();
}
if (!vp.Disposed) vp.Dispose();
Graphics.Stop();
}
}
}