Tiled map loading through TiledLib

This commit is contained in:
Simon Holmberg
2012-09-27 16:43:48 +02:00
parent b6218e4600
commit f83d7d5257
39 changed files with 10213 additions and 45 deletions
@@ -0,0 +1,70 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
<PropertyGroup>
<ProjectGuid>{D054E3A6-7E05-4255-984F-69FE40D9137F}</ProjectGuid>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>DepthsBelowContentPipeline</RootNamespace>
<AssemblyName>DepthsBelowContentPipeline</AssemblyName>
<XnaFrameworkVersion>v4.0</XnaFrameworkVersion>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\x86\Debug</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<PlatformTarget>x86</PlatformTarget>
<XnaPlatform>Windows</XnaPlatform>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.Xna.Framework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=842cf8be1de50553, processorArchitecture=x86">
<Private>False</Private>
<SpecificVersion>True</SpecificVersion>
</Reference>
<Reference Include="Microsoft.Xna.Framework.Graphics, Version=4.0.0.0, Culture=neutral, PublicKeyToken=842cf8be1de50553, processorArchitecture=x86">
<Private>False</Private>
<SpecificVersion>True</SpecificVersion>
</Reference>
<Reference Include="Microsoft.Xna.Framework.Content.Pipeline, Version=4.0.0.0, Culture=neutral, PublicKeyToken=842cf8be1de50553, processorArchitecture=x86">
<Private>False</Private>
<SpecificVersion>true</SpecificVersion>
</Reference>
<Reference Include="System">
<Private>False</Private>
</Reference>
<Reference Include="System.Xml">
<Private>False</Private>
</Reference>
<Reference Include="System.Core">
<RequiredTargetFramework>4.0</RequiredTargetFramework>
</Reference>
<Reference Include="System.Xml.Linq">
<RequiredTargetFramework>4.0</RequiredTargetFramework>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="MapProcessor.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\TiledLib\TiledLib\TiledLib.csproj">
<Project>{EC3F6988-459C-4783-89E9-F34C0CC731C7}</Project>
<Name>TiledLib</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="$(MSBuildExtensionsPath)\Microsoft\XNA Game Studio\Microsoft.Xna.GameStudio.ContentPipelineExtensions.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
+130
View File
@@ -0,0 +1,130 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Content.Pipeline;
using Microsoft.Xna.Framework.Content.Pipeline.Graphics;
using Microsoft.Xna.Framework.Graphics;
using TiledLib;
namespace DepthsBelowContentPipeline
{
// Each tile has a texture, source rect, and sprite effects.
[ContentSerializerRuntimeType("DepthsBelow.Tile, DepthsBelow")]
public class MapTileContent
{
public ExternalReference<Texture2DContent> Texture;
public Rectangle SourceRectangle;
public SpriteEffects SpriteEffects;
}
// For each layer, we store the size of the layer and the tiles.
[ContentSerializerRuntimeType("DepthsBelow.Layer, DepthsBelow")]
public class MapLayerContent
{
public int Width;
public int Height;
public MapTileContent[] Tiles;
}
// For the map itself, we just store the size, tile size, and a list of layers.
[ContentSerializerRuntimeType("DepthsBelow.Map, DepthsBelow")]
public class MapContent
{
public int TileWidth;
public int TileHeight;
public List<MapLayerContent> Layers = new List<MapLayerContent>();
}
[ContentProcessor(DisplayName = "TMX Processor - DepthsBelow")]
public class MapProcessor : ContentProcessor<TiledLib.MapContent, MapContent>
{
public override MapContent Process(TiledLib.MapContent input, ContentProcessorContext context)
{
//System.Diagnostics.Debugger.Launch();
// build the textures
TiledHelpers.BuildTileSetTextures(input, context, "maps");
// generate source rectangles
TiledHelpers.GenerateTileSourceRectangles(input, "maps");
// now build our output, first by just copying over some data
MapContent output = new MapContent
{
TileWidth = input.TileWidth,
TileHeight = input.TileHeight
};
// iterate all the layers of the input
foreach (LayerContent layer in input.Layers)
{
// we only care about tile layers in our demo
TileLayerContent tlc = layer as TileLayerContent;
if (tlc != null)
{
// create the new layer
MapLayerContent outLayer = new MapLayerContent
{
Width = tlc.Width,
Height = tlc.Height,
};
// we need to build up our tile list now
outLayer.Tiles = new MapTileContent[tlc.Data.Length];
for (int i = 0; i < tlc.Data.Length; i++)
{
// get the ID of the tile
uint tileID = tlc.Data[i];
// if the tile is empty, let the tile be null
if (tileID == 0)
continue;
// use that to get the actual index as well as the SpriteEffects
int tileIndex;
SpriteEffects spriteEffects;
TiledHelpers.DecodeTileID(tileID, out tileIndex, out spriteEffects);
// figure out which tile set has this tile index in it and grab
// the texture reference and source rectangle.
ExternalReference<Texture2DContent> textureContent = null;
Rectangle sourceRect = new Rectangle();
// iterate all the tile sets
foreach (var tileSet in input.TileSets)
{
// if our tile index is in this set
if (tileIndex - tileSet.FirstId < tileSet.Tiles.Count)
{
// store the texture content and source rectangle
textureContent = tileSet.Texture;
sourceRect = tileSet.Tiles[(int)(tileIndex - tileSet.FirstId)].Source;
// and break out of the foreach loop
break;
}
}
// now insert the tile into our output
outLayer.Tiles[i] = new MapTileContent
{
Texture = textureContent,
SourceRectangle = sourceRect,
SpriteEffects = spriteEffects
};
}
// add the layer to our output
output.Layers.Add(outLayer);
}
}
// return the output object. because we have ContentSerializerRuntimeType attributes on our
// objects, we don't need a ContentTypeWriter and can just use the automatic serialization.
return output;
}
}
}
@@ -0,0 +1,33 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("DepthsBelowContentPipeline")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Microsoft")]
[assembly: AssemblyProduct("DepthsBelowContentPipeline")]
[assembly: AssemblyCopyright("Copyright © Microsoft 2012")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("7a91de46-472e-481e-84cf-b6fbdce7eb9e")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]