About | Rules and Guide | Sign In/Create Account | Write a Post
Construction1990’s Internet Montage? No!
Please excuse the site weirdness. Mike is fixing and making things.
Ludum Dare 31 — Coming December 5th-8th 2014!

  • ??? Begins: in 10 days, 15 hours, 46 minutes, 6 seconds
  • October Ends: in 11 days, 15 hours, 58 minutes, 6 seconds
  • Ludum Dare 31 begins: in 46 days, 16 hours, 46 minutes, 6 seconds
  • (FYI: Clock might be off) | Ludum Dare 31: Real World Gatherings (Now Open!)

    [ October Challenge 2014 | Resources | Submit/Edit | View All ]


    Presenting VDZ’s Basic XNA Sprite Engine

    Posted by (twitter: @VDZ)
    April 15th, 2012 3:06 pm

    Since I didn’t want to rewrite a whole bunch of code next weekend, I decided to tidy up and release this bunch of code.
    It’s some helpful code that allows for easy sprite handling in XNA 4.0.

    Features
    -Easy sprite creation (just CreateSprite(name, position) and it’ll automatically render it from that point on)
    -Changing position, scaling, rotation is also easy
    -Depth system where deeper sprites are always rendered behind less deep sprites
    -Automatically loads the Content/Graphics folder for you so you can just refer to the images by name
    -Basic layered sprite support (multiple sprites combining to make one sprite, for example a base body with armor and a weapon overlaid on it)
    -Basic animation support
    -Licensed under Creative Commons Attribution 3.0 Unported, so you’re free to modify it or use it commercially

    Code includes a simple example project.

    DLL download link (MediaFire, 7.43 KB)
    Source download link with example (MediaFire, 92.74 KB) (Microsoft Visual Studio 2010 solution)

    Basic documentation

    How it works
    -Create a folder ‘Graphics’ with at least 1 image in it in your Content folder. The folder or any of its subfolders must not contain any non-graphic files.
    -Put a SpriteFont in your Content folder with the name ‘Default’. It will be used as font for GTexts and the log.
    -Call SpriteEngine.Initialize in the constructor of your XNA game class (note that it MUST be in the constructor, NOT in Game.Initialize()), after setting Content.RootDirectory.
    -Call SpriteEngine.LoadContent in the LoadContent method of your XNA game class.
    -That’s all the initialization it needs. Call SpriteEngine.Draw in your Draw method to actually have it do something. Note that it does not clear the screen for you; keep GraphicsDevice.Clear in there.
    -If you want to use animations or ‘camera scrolling’, call SpriteEngine.Update in your Update method.
    -From now on you can use SpriteEngine.CreateSprite to create sprites in your game. It returns a sprite object, which you can use to change its position, rotation, image, etc. Use SpriteEngine.EraseGObject to erase a sprite, or SpriteEngine.Nuke to erase everything.

    Classes
    SpriteEngine – Main class. Most of the functions you need will be in here.
    GObject – Base class for displayable objects (Sprites and GTexts). Contains basic information like position, scale and depth.
    GText – A displayed string that can be moved around and has a depth, much like a sprite.
    Sprite – Class representing a sprite, containing basic information such as its position, images, rotation, scale, etc.
    LayeredSprite – A sprite composed of multiple sprites. Uses a simple layer system where when the sprite is drawn, all of its sub-sprites are drawn at its location instead.

    Details on what each property and method does can be found in comments in the code, I’m too lazy to type them all out.

    License
    This work is licensed under a Creative Commons Attribution 3.0 Unported License.
    You are free:
    * to Share — to copy, distribute and transmit the work
    * to Remix — to adapt the work
    * to make commercial use of the work
    Under the following conditions:
    * Attribution — You must attribute the work in the manner specified by the author or licensor (but not in any way that suggests that they endorse you or your use of the work).

    Have fun, and good luck with your games.

    Tags: , , , , , , ,

    Leave a Reply

    You must be logged in to post a comment.


    All posts, images, and comments are owned by their creators.

    [cache: storing page]