XNA Code Generator for Tile Studio 2.55

Posted in: C#,Game Development |

Playing with XNA yesterday made me wonder how easy it would be to build a Tile based game, such as a 2D 'walk around' adventure game (ie; Zelda, or the likes). I downloaded a copy of Tile Studio 2.55 not too long ago and have been staring at it's icon on my desktop for quite some time now.

It came 'out of the box' with a handful of nice code generators, and it even included a .NET code generator ... but it only supported the BooGame library, and generated Boo code.

So, I thought to myself, why not look for an XNA Exporter ... so I loaded up the Tile Studio website, and found nothing there. I googled briefly and found nothing as well. So, I started on a little code adventure and decided to build my own. Now, keep in mind, I'm by no means an XNA Expert so my method for creating the tile map in C# may or may not be 'the best' or even 'proper'. However, the map works ... and I'm happy. I started on the project around 10:20am and am now writing this post at 11:50am ... so, it wasn't "too hard" and didn't take too much time. The time spent also included learning the Tile Studio Definition language and how it worked as well.

So, onto some 'proof', here's a few screenshots:

Tile Studio Map

This is Tile Studio, with my little map loaded up
--the Map is 100x100, and the Tiles are 20x20.

Visual Studio Project Files

Here is my Visual Studio project, after performing a code generation
in Tile Studio and refreshing the project directory and including the files.

XNA Game with Tile Studio Generated Map

And, here is my Map rendering to an XNA Game Window.

Now, in the project files shot you might notice that there are three files highlighted. The first is the exported Tile Map file from Tile Studio, the second is the actual Map definition (Tile2.cs) and the third is a helper class I wrote that is automatically exported for you that contains the MapDefinition and Tile classes.

And, here's the best part, your not gonna believe it ... or maybe you will. To load the map up, you simply do:

C#:
  1. //In Class scope
  2. WatrRoad map;

C#:
  1. // in LoadContent
  2. map = new WatrRoad(Content.Load<Texture2D>("WatrRoad"));

C#:
  1. // in Draw
  2. map.Draw(spriteBatch);

And that's it ... the MapDefinition provides a 'Draw' method to loop through all the tiles and render them to the screen. I've not yet optimized this to store a cached copy, but I also haven't tested it with any real 'logic' yet to try building a game. You can download a copy of the XNA C# Tile Studio Definition file from here, expect updates in the future to support more of Tile Studio's feature set.

Download the Definition File Here

Download the Full Example Project Here

Currently Supported Features:

  • Export Tile Sets to BMP and place in 'Content' sub-directory
  • Export CS File containing all Maps
  • Export TileStudioMap.cs containing MapDefinition and Tile helper classes
  • Easy Map instantiation, just pass a reference to the Map's Texture2D containing the Tiles
  • Easy Map Rendering, just call the Map's "Draw" method and pass the SpriteBatch

JsonViewEngine for ASP.NET MVC Framework

Posted in: C# |

I am currently tasked with a project at work that, after building out the design documents, was going to rely on ASP.NET MVC and at the same time take advantage of the URL Rewriting that comes with it 'out of the box' for 'data source urls' for AJAX requests. We decided to use the jQuery library, and the $.getJSON() function within it to request for JSON data.

Well, if you've played with ASP.NET MVC, you've probably noticed that it doesn't have any 'quick and simple' "json serialization in a view" ... or, does it?

I recently saw Scott Guthrie at a local .NET User Group event, and he mentioned that the ASP.NET MVC Framework was completely "pluggable", and you could easily replace the built-in View Engine. So, I immediately searched for examples, found one ... tossed it out and whipped together a really quick and simple 'JsonViewEngine' class.

Here's the code:


C#:
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Diagnostics.CodeAnalysis;
  5. using System.IO;
  6. using System.Security.Permissions;
  7. using System.Web;
  8. using System.Web.Script.Serialization;
  9. using System.Web.UI;
  10. using System.Web.Mvc;
  11.  
  12.  
  13. namespace Sim.Microsite.Framework.Web.Mvc.JsonViewEngine
  14. {
  15.     [AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  16.     [AspNetHostingPermission(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  17.     public class JsonViewEngine: IViewEngine
  18.     {
  19.         #region IViewEngine Members
  20.         public void RenderView(ViewContext viewContext)
  21.         {
  22.             if (viewContext == null)
  23.             {
  24.                 throw new ArgumentNullException("viewContext");
  25.             }
  26.            
  27.             viewContext.HttpContext.Response.Clear();
  28.             viewContext.HttpContext.Response.ContentType = "application/json";
  29.  
  30.             System.Text.StringBuilder jsonText = new System.Text.StringBuilder();
  31.             JsonFx.Json.JsonWriter jsonWriter = new JsonFx.Json.JsonWriter(jsonText);
  32.             jsonWriter.Write(viewContext.ViewData);
  33.  
  34.             viewContext.HttpContext.Response.Write( jsonText.ToString() );
  35.         }
  36.         #endregion
  37.     }
  38. }

As you may or may not be able to tell, I am using the JsonFx serializer for this project, but you could easily replace that with the built-in System.Web.Script.Serialization.JavaScriptSerializer or any other library of your choice.

After building this, I also decided to take it and create an XmlViewEngine that simply returns XML data, as we have some use for that as well with this project ... I'll leave you to implement that on your own, it's basically the same code ... just uses XmlSerializer instead -- however, I wrote some custom code for serializing the IDictionary that the default ViewPage uses for ViewData (even though ViewData is defined as an object in the viewContext ... ViewPage defines it as generic dictionary).

So ... have fun with that.

TGB Resource Maker

Posted in: C#,Game Development |

During the design and general work flow of the Urban Platform Kit, I've decided that it would be within my best interests to create a "Resource Maker" application for the Torque Game Builder.

What does this mean? Well, it means my task list just grew quite a bit, and I added a new project onto my "pending" list.

I am not sure what the overall outcome of this project will be -- but I do know that i will be using it to create the resourceDatabase.cs for the Urban Platform Kit -- LOTS of graphics, and hand editting that file is not the funnest thing in the world -- some added items I'll be adding to the project for simplicity is the ability to take a number of PNG's and tile them into a single PNG (including transparencies) so I don't have to keep creating the 'sheets' when steve hands me new or updated art work (what a pain that is ... Wow!)

LinkShare  Referral  Prg - default banner