MonoGame

Sep 8, 2012 at 9:57 AM

Hi,

I'm new to this and I'm trying to get this working with a MonoGame project.  The tutorial works up to the point I have to instantiate an XnaDisplayDevice.  The ContentManager and GraphicsDevice ctor params are Microsoft Xna classes, so trying to pass in the MonoGame equivalents fails.

I guess I need a version of this library built against MonoGame rather than Microsoft.

First of all, am i right, secondly, has anyone done this yet and thirdly, if not I'll have a go.

Cheers.

Coordinator
Sep 12, 2012 at 1:07 PM

I would suggest implementing something like a MonoDisplayDevice by implementing the IDisplayDevice interface. Have a look at the source code for XnaDisplayDevice. If you can actually load the xTile assembly but use your mono-based device implementation, it should work in theory.

Oct 8, 2012 at 5:40 AM

Hi, it works fine in monogame, but i have to modify this method in class "ContentTypeReaderManager", near at line that contains if(l_readerType !=null):

 

	internal ContentTypeReader[] LoadAssetReaders()
        {
#pragma warning disable 0219, 0649
            // Trick to prevent the linker removing the code, but not actually execute the code
            if (falseflag)
            {
                // Dummy variables required for it to work on iDevices ** DO NOT DELETE ** 
                // This forces the classes not to be optimized out when deploying to iDevices
                var hByteReader = new ByteReader();
                var hSByteReader = new SByteReader();
                var hDateTimeReader = new DateTimeReader();
                var hDecimalReader = new DecimalReader();
                var hBoundingSphereReader = new BoundingSphereReader();
                var hBoundingFrustumReader = new BoundingFrustumReader();
                var hRayReader = new RayReader();
                var hCharListReader = new ListReader<Char>();
                var hRectangleListReader = new ListReader<Rectangle>();
                var hVector3ListReader = new ListReader<Vector3>();
                var hStringListReader = new ListReader<StringReader>();
                var hSpriteFontReader = new SpriteFontReader();
                var hTexture2DReader = new Texture2DReader();
                var hCharReader = new CharReader();
                var hRectangleReader = new RectangleReader();
                var hStringReader = new StringReader();
                var hVector2Reader = new Vector2Reader();
                var hVector3Reader = new Vector3Reader();
                var hVector4Reader = new Vector4Reader();
                var hCurveReader = new CurveReader();
                var hIndexBufferReader = new IndexBufferReader();
                var hBoundingBoxReader = new BoundingBoxReader();
                var hMatrixReader = new MatrixReader();
                var hBasicEffectReader = new BasicEffectReader();
                var hVertexBufferReader = new VertexBufferReader();
                var hAlphaTestEffectReader = new AlphaTestEffectReader();
            }
#pragma warning restore 0219, 0649

            int numberOfReaders;
			
            // The first content byte i read tells me the number of content readers in this XNB file
            numberOfReaders = _reader.Read7BitEncodedInt();
            contentReaders = new ContentTypeReader[numberOfReaders];
		
            // For each reader in the file, we read out the length of the string which contains the type of the reader,
            // then we read out the string. Finally we instantiate an instance of that reader using reflection
            for (int i = 0; i < numberOfReaders; i++)
            {
                // This string tells us what reader we need to decode the following data
                // string readerTypeString = reader.ReadString();
				string originalReaderTypeString = _reader.ReadString();
 
				// Need to resolve namespace differences
				string readerTypeString = originalReaderTypeString;
								
				readerTypeString = PrepareType(readerTypeString);

				Type l_readerType = Type.GetType(readerTypeString);

            	if(l_readerType !=null)
					contentReaders[i] = (ContentTypeReader)Activator.CreateInstance(l_readerType,true);
            	else
				{
					l_readerType = PrepareTypeExecutingAssemby(readerTypeString);
					//l_readerType = Type.GetType(readerTypeString);


					if (l_readerType == null)
						throw new ContentLoadException("Could not find matching content reader of type (" + readerTypeString + ") ");

					contentReaders[i] = (ContentTypeReader)Activator.CreateInstance(l_readerType,true);
				}
				// I think the next 4 bytes refer to the "Version" of the type reader,
                // although it always seems to be zero
                int typeReaderVersion = _reader.ReadInt32();
            }

            return contentReaders;
        }
		

and add this method:

	public static Type PrepareTypeExecutingAssemby(string typeStr)
		{
			int inicio =  typeStr.IndexOf( "."  , typeStr.IndexOf("[[")) + 1 ;
			string className = typeStr.Substring(inicio  , typeStr.LastIndexOf(",") - inicio);


			string assemblyName = Assembly.GetEntryAssembly().GetName().Name;
			string res = typeStr.Substring(0, typeStr.IndexOf("[[")) + "[["+assemblyName+ "." + className + ","+ assemblyName +"]]";
			Type objectType = Type.GetType(res);

			return objectType;
		}


and add this class taked from git "\MonoGame-2.5.0.0\MonoGame.Framework\Content\ContentReaders\ExternalReferenceReader.cs":
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Microsoft.Xna.Framework.Content
{
	/// <summary>
	/// External reference reader, provided for compatibility with XNA Framework built content
	/// </summary>
	internal class ExternalReferenceReader : ContentTypeReader
	{
		public ExternalReferenceReader()
			: base(null)
		{
			
		}
		
		protected internal override object Read(ContentReader input, object existingInstance)
		{
			return input.ReadExternalReference<object>();
		}
	}
}


hope it helps.

May 10, 2013 at 1:54 AM
Using pre-compiled content, I had no problem getting the xTile demo to run on PSMobile via MonoGame.

Image

I put the project together if anyone wants to use xTile on a Vita game... https://dl.dropboxusercontent.com/u/23693330/xTile_PSMobile.zip
Coordinator
May 10, 2013 at 8:37 AM
Brilliant! Thanks for sharing this! :)
May 31, 2014 at 10:40 PM
Hi i'm very new in this, sorry for my ignorance. How can i modify the "ContentTypeReaderManager"?? i'm going insane!

Cheers!
Dec 31, 2014 at 2:53 AM
F8_Sean wrote:
Using pre-compiled content, I had no problem getting the xTile demo to run on PSMobile via MonoGame.

Image

I put the project together if anyone wants to use xTile on a Vita game... https://dl.dropboxusercontent.com/u/23693330/xTile_PSMobile.zip
link is broken