Sprites are the cornerstone of displaying graphics to the screen. A sprite determines where the graphic is drawn, how big it will be, what the point of origin is, the color and tone of the sprite, the opacity, the visbility, whether or not it's mirrored, and much more. In short, sprites are what make odl graphics work.
Another integral part of displaying graphics are Bitmaps. These will be explained in the next section; for now, all you need to know is that new Bitmap("icon.png") loads the file located at icon.png.
A sprite must be disposed with Dispose() when it is no longer referenced! This will ensure the Bitmap is properly disposed too - if you don't pay very close attention to what memories are and aren't disposed, you're at risk of introducing memory leaks!
Let's start with the most basic way you could possibly show an image to the screen.
We're creating a Spriteobject here and attaching an icon.png graphic to it. The graphic is loaded, and the sprite is shown at (x, y) = (0, 0) - the top left of the screen.
If we wanted to change the position of the sprite, we would make use of the X and Y properties. For another manipulation, let's pretend that we want to display the image at 50% of its original size. We would set the ZoomX and ZoomY properties to 0.5, where 1 means the original width/height.
As you can see here, the sprite is shown at half the original size (and maintained aspect ratio, as we've set ZoomX and ZoomY to the same value!), and displayed that at (x, y) = (50, 100) from the top left.
This is the basic idea behind sprites. They contain many more properties that you can manipulate to alter how the image is shown on screen, and everything below here will simply demonstrate some of those properties.
The example below illustrates rotation, mirroring and transparency. Angle is an integer between 0 and 360, the degrees of rotation (clockwise), and MirrorX and MirrorY are booleans that will flip the sprite over the X and Y axes respectively. Opacity is a byte, where 0 means fully transparent (invisible), and 255 means fully opaque (visible).
Evidently, this sprite was mirrored both vertically and horizontally and rotated 45°, but it's not exactly shown at (x, y) = (200, 100). That's because rotation and zooming are performed on the origin point of the sprite. If not set, that is (0, 0), the top left of the sprite. These are the OX and OY properties. This will be illustrated in the more advanced examples section.
A sprite can also be given a Color value. This is the color that will be overlayed on top of the image. The different ways to create or use a Colorobject will be explained in the next section, but the usage is shown in the example below:
using System;
using odl;
namespace TestNamespace
{
class Program
{
public static void Main(string[] args)
{
Graphics.Start();
Window w = new Window();
w.Initialize();
Sprite s = new Sprite();
s.X = 50;
s.Y = 100;
s.ZoomX = 0.5;
s.ZoomY = 0.5;
s.Bitmap = new Bitmap("icon.png");
w.Show();
while (Graphics.CanUpdate())
{
Graphics.Update();
}
s.Dispose();
Graphics.Stop();
}
}
}
using System;
using odl;
namespace TestNamespace
{
class Program
{
public static void Main(string[] args)
{
Graphics.Start();
Window w = new Window();
w.Initialize();
Sprite s = new Sprite();
s.X = 200;
s.Y = 100;
s.Angle = 45;
s.MirrorX = true;
s.MirrorY = true;
s.Opacity = 128;
s.Bitmap = new Bitmap("icon.png");
w.Show();
while (Graphics.CanUpdate())
{
Graphics.Update();
}
s.Dispose();
Graphics.Stop();
}
}
}
using System;
using odl;
namespace TestNamespace
{
class Program
{
public static void Main(string[] args)
{
Graphics.Start();
Window w = new Window();
w.Initialize();
Sprite s = new Sprite();
s.Color = Color.RED;
s.Bitmap = new Bitmap("icon.png");
w.Show();
while (Graphics.CanUpdate())
{
Graphics.Update();
}
s.Dispose();
Graphics.Stop();
}
}
}