; -------------------------------------------------------- ; Tile Studio Definition for use with Microsoft XNA 2.0 C# ; ; Version 1.0 - May 5, 2008 ; David Higgins (higginsd@zoulcreations.com) ; -------------------------------------------------------- ; create the graphics files #tileset #tilebitmap Content\\.BMP #end tilebitmap #end tileset #file .cs // Load this Map like so in your LoadContent() // map = new MyMap(Content.Load\("\")); using System; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Graphics; namespace com.zoulcreations.TileStudio { #tileset #map public class : MapDefinition { /// \ /// Load the Map by passing a reference to a Texture2D for /// \ public (Texture2D texture) : base(, , , , texture) { #mapdata this.Map[( * this.MapWidth) + ] = this.Tiles[-1];\n #end mapdata } } #end map #end tileset } #end file #file TileStudioMap.cs ##if !TILE_STUDIO_MAP // prevent duplicate definitions, should only include this file once ##define TILE_STUDIO_MAP using System; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Graphics; namespace com.zoulcreations.TileStudio { /// \ /// This is the main type for your game /// \ public class MapDefinition { private int _width; private int _height; private int _tileWidth; private int _tileHeight; /// \ /// The width of the Map, in Tiles /// \ public int MapWidth { get { return _width; } set { _width = value; } } /// \ /// The Map Height, in Tiles /// \ public int MapHeight { get { return _height; } set { _height = value; } } /// \ /// The width of the Tiles /// \ public int TileWidth { get { return _tileWidth; } set { _tileWidth = value; } } /// \ /// The height of the tiles /// \ public int TileHeight { get { return _tileHeight; } set { _tileHeight = value; } } private Tile[] _tiles; /// \ /// A map of where the tiles are located in the Texture /// \ public Tile[] Tiles { get { return _tiles; } set { _tiles = value; } } private Texture2D _texture; /// \ /// The Texture storing the Tiles /// \ public Texture2D Texture { get { return _texture; } set { _texture = value; } } private Tile[] _map; public Tile[] Map { get { return _map; } set { _map = value; } } public MapDefinition(int width, int height, int tileWidth, int tileHeight, Texture2D texture) { _width = width; _height = height; _tileWidth = tileWidth; _tileHeight = tileHeight; _texture = texture; int w = texture.Width / _tileWidth; int h = texture.Height / _tileHeight; _tiles = new Tile[w * h]; int t = 0; for (int x = 0; x \< w; x++) { for (int y = 0; y \< h; y++) { _tiles[t] = new Tile(x * _tileHeight, y * _tileWidth); t++; } } // pre-allocate map space _map = new Tile[_width * _height]; } public void Draw(SpriteBatch spriteBatch) { for (int x = 0; x \< _height; x++) { for (int y = 0; y \< _width; y++) { Tile tile = Map[(x * _width) + y]; if (tile == null) continue; // skip the missing tile Vector2 position = new Vector2(x * _tileWidth, y * _tileHeight); Rectangle source = new Rectangle(tile.OffsetX, tile.OffsetY, _tileWidth, _tileHeight); Color color = Color.White; spriteBatch.Draw(_texture, position, source, color); } } } } public class Tile { private int _x; private int _y; public int OffsetX { get { return _x; } set { _x = value; } } public int OffsetY { get { return _y; } set { _y = value; } } public Tile(int x, int y) { _x = x; _y = y; } } } ##endif #end file