robvdl/MapEditor — Issue #11: Create Tile class
Author: robvdl
Created: 2012-02-02 08:24:35+00:00
State: open

Abstract
-----------

Create a class to store a single tile called **Tile**, which is handled by a **Tileset**. To begin with we will keep things simple and not store multiple frames for animation just yet. The tile should always be in **32 bit RGBA** mode with alpha channel only. Other formats could maybe later be converted by the editor when loaded, but for now we are only supporting 32 bit images for storing our tiles. We will also be storing some properties on a Tile object, for example size (int), and name (string). Do not use Qt objects here, just STL, as we do not want to lock the engine to Qt. **SFML** can be used for importing the PNG's, we can maybe support JPEG's too and other formats SFML supports, but we may have to convert them to 32 bit when loading.

Properties (stored privately):
-------------------------------------

* **string name** - name of the tile, optional, if the user wants to set a name for each tile, otherwise just blank by default
* **int size** - (16, 32, 64, 128, 256, ...) we store tile size and don't store width or height separately, so the tiles are always square
* **binary bitmap data** - the raw bitmap data is stored in the Tile object (probably best on the heap)

Methods we will need:
------------------------------

* **draw(SfmlSurface, x, y)** - draw the tile on an SFML surface at x, y for previewing a tile
* **setName()/getName()** - obvious
* **getSize()** - return the size of the tile (16, 32, 64, etc), we don't support setSize after a Tile is already constructed, as it makes no sense. Tiles are handled by a Tileset, which has the same size for every Tile anyway. The size can therefore only be set when a Tile object is constructed, and they are normally constructed by the Tileset anyway.
* **getUuid()** - get unique id (GUID string) for storing of unique keys for every game object in a non-relational leveldb database, there will be no way to set the unique id after creation, only with the alternative constructor which is only used when loading a tile from the db, is when a uuid can be manually set, in this case the uuid always comes from the db.

Constructors (with defaults shown)
-----------------------------------------------

* **Tile(size=64, name="")** - default constructor, give it a size and name (optional). This auto generated a uuid.
* **Tile(size, uuid, encoded TileData)** - alternative constructor, loads a tile from the db, when we have a known uuid and tile data encoded in JSON. This will decode the JSON tile data coming from the db.

Saving and exporting
-----------------------------

* **toJson()** - generate a JSON string of the tile data and properties, so it can be saved by the Tileset object to the db later. Should always be returned in a {} block as the root element, so the string can be embedded by the Tileset that also has it's own .toJson() method.
* **import("file.png")** import an image file into the tile, should overwrite the current tile data, larger images should be cropped for now. When we support multiple frames later, we might want to support importing a large file into a multi frame tile through slicing.
* **export("file.png")** - export to png (maybe support other formats later)

Storing frames
--------------------

When we start storing multiple frames later, we should probably not create a separate class for each frame, but rather store each frame in a basic array or struct, or other basic data type. We should also store a private numFrames int, as well as getNumFrames, setNumFrames, addFrame, removeFrame methods that change the number of frames.