Click or drag to resize

SpriteDatabase Class

Store Sprite information in a database. You can preload your database with sprite definitions, and then create the sprites as needed. This can drastically reduce the initial load time of a game or something. Though, what it really does is spread out the load time. It still takes the same amount of time to load all the sprites, it just loads them on-demand. Using a SpriteDatabase often hides any load time issues.
Inheritance Hierarchy
SystemObject
  SpriteLibrarySpriteDatabase

Namespace:  SpriteLibrary
Assembly:  SpriteLibrary (in SpriteLibrary.dll) Version: 1.0.0.6 (1.0.0.6)
Syntax
C#
public class SpriteDatabase

The SpriteDatabase type exposes the following members.

Constructors
  NameDescription
Public methodCode exampleSpriteDatabase
The sprite database instantiation function. The filename can either be a file on the computer or it can be the string name of a resource (the filename without the extension. If your file is accessed by Properties.Resources.MySprites, the "filename" would be "MySprites")
Top
Methods
  NameDescription
Public methodStatic memberCloneByXMLSerializingT
This is an inefficient, but simple function to clone a class. It works by serializing an item to a string, and then deserializing it into a class. The end result is that any value which is publically visible is duplicated, but it is a completely separate class from the original.
Public methodEquals (Inherited from Object.)
Protected methodFinalize (Inherited from Object.)
Public methodGetHashCode (Inherited from Object.)
Public methodGetImageFromName
This function returns an image from the Properties.Resources. If we tell it to UseSmartImages, then it caches the image in memory. This makes it a little faster to return. If you have a lot of sprites to load, using this system can speed up things a fair bit. But, try to remember not to change the image that this returns unless you duplicate it first. Otherwise you will end up changing the image for all the other times you reference it. This is usualy a bad thing.
Public methodGetImageNames
Return a list of the image names in the Properties.Resources
Public methodGetType (Inherited from Object.)
Public methodStatic memberCode exampleLoadObjectFromXmlFileT
Load in an XML serialized item from the specified ResourceManager. You will usually make an XML file by creating an object (as a variable) and using WriteToXmlFileT(String, T) to serialize it and save it to a file on your desktop. Then you can drag and drop that file into your project and then use this LoadObjectFromXmlFile function. You can google XML Serialization for more information.
Protected methodMemberwiseClone (Inherited from Object.)
Public methodCode exampleOpenEditWindow
Open a Sprite Edit Window. This window does not let you draw a sprite. What it does is to help you define your sprites and makes the process of using Sprites in your program a lot easier.
Public methodStatic memberCode exampleReadFromXmlFileT
Reads an object instance from an XML file.

Object type must have a parameterless constructor.

Public methodStatic memberReadFromXmlStringT
This is a generic function which the SpriteDatabase uses. It does XML Deserialization of most anything, and generates an XML String. XML Serialization will take any public value of a public class and make an XML entry for it. It is a very convienent way to save and retrieve data. You can "Serialize" the value with the WriteToXMLString function.
Public methodSave
Tell the database to save the sprite definitions. Use this while you are creating your game. When you are done, you will usually want to take your sprite definition file and add it to the resources of your game. The resources cannot be saved to, so you cannot continue to add new sprites once you are loading and saving them from a resources file. But, the resources file is included with the program when you build it.
Public methodSetIcon
Change the Icon for the SpriteEntryForm
Public methodSetSnapGridSize
The SnapGrid is the block-size that your sprite will be. For example, I will often have sprites with a snapgrid of 50,50. This means that the sprite can be 50x50, 100x50, or anything with a step-size specified in the snap-grid. It takes a "Size" specified by System.Drawing.Size.
Public methodSpriteFromName
Generate a new, named sprite from a sprite template stored in the database. Most of the time you do not want to use this yourself. SpriteControllers that are defined with a database will automatically look up sprite templates that they do not have sprites for. This function is just a wrapper for SmartDuplicateSprite.
Public methodSpriteNames
Return a list of the SpriteNames that this Database knows how to create.
Public methodToString (Inherited from Object.)
Public methodStatic memberCode exampleWriteToXmlFileT
Writes the given object instance to an XML file. Only Public properties and variables will be written to the file. These can be any type though, even other classes. If there are public properties/variables that you do not want written to the file, decorate them with the [XmlIgnore] attribute. Object type must have a parameterless constructor.
Public methodStatic memberWriteToXMLStringT
This is a generic function which the SpriteDatabase uses. It does XML Serialization of most anything, and generates an XML String. XML Serialization will take any public value of a public class and make an XML entry for it. It is a very convienent way to save data. You can "Deserialize" the value with the ReadFromXMLString function.
Top
Fields
  NameDescription
Public fieldSpriteInfoList
This is the list of SpriteInfo records that the database knows about. You can create your own list, modify this list, or whatever. The database has some reasonable functions for loading and saving a sprite database.
Top
Examples
This is an example of how to use a SpriteDatabase. When you begin developing your project, you want to start by creating a SpriteDatabase and pointing it to a file, and then opening up an EditorWindow.
C#
public partial class MyGameForm : Form
{
    SpriteController mySpriteController = null;
    SpriteDatabase mySpriteDatabase = null;

    public MyGameForm()
    {
        InitializeComponent();
        MainDrawingArea.BackgroundImage = Properties.Resources.Background;
        MainDrawingArea.BackgroundImageLayout = ImageLayout.Stretch;

        string Desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
        string MyFile = Path.Combine(Desktop, "myFile.xml");
        mySpriteDatabase = new SpriteDatabase(Properties.Resources.ResourceManager, MyFile);

        mySpriteController = new SpriteController(MainDrawingArea, mySpriteDatabase);

        mySpriteDatabase.OpenEditWindow();
        mySpriteDatabase.Save();
    }
}
The Editor Window will let you find the sprites that are contained in the various images you have as resources in your program, and it will save a file with those sprite templates. Any SpriteController that you have instantiated with a Sprite Database (see SpriteController(PictureBox, SpriteDatabase)) will now be able to create named sprites from the templates defined in the database. After the first use, the named sprites will be accessible from within that controller just like any other named sprites.

After you have created your SpriteDatabase file, you will want to add your file to your program resources. Then, you will change the SpriteDatabase to use the resource instead of a file. If we named the file "MySpriteDatabase.xml", and it got added to your resources with the name "MySpriteDatabase", you would pass "MySpriteDatabase" to the database instantiation.

C#
public partial class MyGameForm : Form
{
    SpriteController mySpriteController = null;
    SpriteDatabase mySpriteDatabase = null;

    public MyGameForm()
    {
        InitializeComponent();
        MainDrawingArea.BackgroundImage = Properties.Resources.Background;
        MainDrawingArea.BackgroundImageLayout = ImageLayout.Stretch;

        //string Desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
        //string MyFile = Path.Combine(Desktop, "myFile.xml");
        //mySpriteDatabase = new SpriteDatabase(Properties.Resources.ResourceManager, MyFile);
        mySpriteDatabase = new SpriteDatabase(Properties.Resources.ResourceManager, "MySpriteDatabase");

        mySpriteController = new SpriteController(MainDrawingArea, mySpriteDatabase);

        //mySpriteDatabase.OpenEditWindow();
        //mySpriteDatabase.Save();
    }
}
See Also