Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Create a sprite Image

I am trying to create a very basic sprite image.

First off i have an existing image (Width=100px, Height=100px).

I will be looping through this image between 10 and 100 times, each time placing it on the sprite next to the previous one.

The sprite is limited to 3000px wide.

Placing the images next to each other is fine, cause i can just combine them with a simple method, however, i need to limit the width of the combined images to 3000px, then start on a new line.

like image 820
stoic Avatar asked May 04 '12 15:05

stoic


People also ask

How do I turn a picture into a sprite?

This tool does it all for you, just drag and drop all your images onto the canvas, that's all you need to do, after you've finished adding all your images, click the "Download Sprite Sheet" button. Drag and drop as many images as you need. Sprite sheets are used to combine multiple images into a single image.

What is a sprite image?

An image sprite is a collection of images put into a single image. A web page with many images can take a long time to load and generates multiple server requests. Using image sprites will reduce the number of server requests and save bandwidth.


1 Answers

There is a lot of information about 2D-sprites in the following MSDN article: Rendering 2D sprites

Those examples are based on Microsoft's XNA, which is a platform that can be used within Visual Studio to develop games for Windows, Windows Phone and XBOX 360.

For example, to draw a sprite, you can use the following C# code (example taken from the MSDN article, XBOX 360 specific code removed):

private Texture2D SpriteTexture;
private Rectangle TitleSafe;

    protected override void LoadContent()
    {
        // Create a new SpriteBatch, which can be used to draw textures.
        spriteBatch = new SpriteBatch(GraphicsDevice);
        SpriteTexture = Content.Load<Texture2D>("ship");
        TitleSafe = GetTitleSafeArea(.8f);
    }

    protected Rectangle GetTitleSafeArea(float percent)
    {
        Rectangle retval = new Rectangle(
            graphics.GraphicsDevice.Viewport.X,
            graphics.GraphicsDevice.Viewport.Y,
            graphics.GraphicsDevice.Viewport.Width,
            graphics.GraphicsDevice.Viewport.Height);
        return retval;
    }

    protected override void Draw(GameTime gameTime)
    {
        graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
        spriteBatch.Begin();
        Vector2 pos = new Vector2(TitleSafe.Left, TitleSafe.Top);
        spriteBatch.Draw(SpriteTexture, pos, Color.White);
        spriteBatch.End();
        base.Draw(gameTime);
    }

You need to call LoadContent() to initialize it, then you need to call GetTitleSafeArea(100) to get the safe draw area (in this case wich 100 percent), finally you can use the Draw method. It accepts a parameter containing an instance of the GameTime class, which is a Snapshot of the game timing state expressed in values that can be used by variable-step (real time) or fixed-step (game time) games.

Please let me know if that helps you.

like image 62
Matt Avatar answered Sep 23 '22 21:09

Matt