3
\$\begingroup\$

I'm making a game where it came to adding a base tile class, but I already have a sprite one, which does the same at first glance. Rasterizers are the ones I should have started with but I missed quite a few points in 2D graphics.

Can you explain the difference with a couple of examples?

\$\endgroup\$
0

2 Answers 2

10
\$\begingroup\$

Basically, a sprite is an image. It can be used in variety of ways in a computer game. A tile, on the other hand, is a bundle of data that might refer to a sprite.

A tile probably also contains properties like blocking information (can you walk over this tile?) or path cost information that a sprite itself doesn't need or which don't make sense for it. Or a tile might have multiple sprites (for different tile states, such an intact and a destroyed wall).

Consequently, if you already have a sprite type in your code, a good way to represent your tile might be:

struct Tile {
  Sprite * image;

  // ...other data...
};

(Although I used a pointer for clarity, you can also use some other form of reference to a sprite, often this is done to keep Tile small.)


Historically, the difference was a little more drastic: a tile was generally part of the background and a sprite was generally part of the foreground. The background elements were mostly-fixed and mostly-immobile, while the foreground elements moved. Limited processing power necessitated the significant distinction; this is not really the case any more, so you can build your tile maps out of sprites if you want.

\$\endgroup\$
1
\$\begingroup\$

A sprite and a tile are really both bunches of data and doesn't have to be tied to a piece of graphic (in case they need to be invisible for example).

A sprite is a (graphical) object that can be moved around on the screen freely and individually.

A tile (also called a "character" on older computers and video game systems) on the other hand is part of a grid which it can never leave, so it can never move around individually (otherwise it would be a sprite and not a tile). This grid of tiles (usually called a background or pattern layer) can be moved around so that all tiles move around together, this is known as scrolling. Moving the background to the left is the same thing as scrolling it to the right (since the "camera" is moving to the right in that case). A background is made up of an array of tiles usually called a "tilemap" or a "name-table".

A sprite may have data for position (X and Y), size, flipping state (horizontal and vertical), graphic index (what refers to its graphic, this is also called a "name") and certain other things like an enable flag (for disabling it when not in use) and display priority used to determine if it is to be displayed above or below a tilemap. All sprite objects are usually stored in an array called a Sprite Attribute Table or SAT.

A tile on the other hand doesn't need position attributes, since its position is determined from its position in the tilemap. It just needs a graphic index (AKA "name" and that's why the tilemap is sometimes called a "name-table" as its often just a table of tile "names") and possibly flipping information if you want your tiles to be flippable. A single tile can't be disabled, only be made invisible (by giving it a fully transparent graphic texture). The whole background can be disabled though. Tile size is also a global thing for the whole background layer, so there is no need for size information for each tile either.

You can have multiple backgrounds drawn over each other, each with their own scroll offsets, making the backgrounds individually scrollable. Though the top "backgrounds" are technically foregrounds, we still tend to call them backgrounds for historical reasons (the classic TMS9918 video chip could not display sprites below the single background layer). There can also be several layers of sprites above, below and between the backgrounds based on each sprite's priority attribute.

Example of Sprite attributes:

struct Sprite {
  Graphic* spritesheet;                //pointer to the spritesheet texture
  int name;                            //sprite index in spritesheet
  int x;
  int y;                               //sprite position
  int priority;                        //sprite display priority
  int w;
  int h;                               //sprite dimensions
  bool flip_h;
  bool flip_v;                         //sprite flip state
  bool enable;                         //sprite enable flag
};

Example of tile attributes:

struct Tile {
  Graphic* tilesheet;                  //pointer to the tilesheet texture
  int name;                            //tile index in tilesheet
  bool flip_h;
  bool flip_v;                         //tile flip state
};

This is a simple example for strictly simulating hardware sprites and tiles as used in older computers and video game system video chips. A modern game might have collision information and more game-related information included with the tile and sprite data (solid information, damage floor etc). In classic video chips all tiles in a tilemap may have to share the same tilesheet, but here I gave each tile the possibility to use any tilesheet by giving a tilesheet pointer to each tile.

In more modern terms, "tiles" are still elements in a grid while "sprites" have come to be used more as a generic term for almost any type of texture, but especially a texture tied to an object such as a software sprite or software tile. But it's good to know that they are really two different hardware graphics systems invented for the sake of doing (limited) manipulation of screen information with very little memory usage.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .