Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 06 Mar 2011
Offline Last Active Sep 08 2013 06:38 PM

#5079120 Porting from XNA to MonoGame

Posted by Tarika on 20 July 2013 - 06:02 AM

Hi BGH welcome to gamedev!


Many of the questions you asked have already been answered many many times before so I recommend you try the search features of the forum, however here is a brief summary of the answers you'll find:


  • XNA is no longer supported correct, however it works and will continue to work for years to come. So if you are new to coding games, stick with XNA until you are happy to move on.
  • MonoGame would be the next stage *after* building your game, it's very straight forward (not easy) to port to as long as the game is optimized and does not rely on windows only features. Also as they are still working on the content pipeline, it's very useful to still have XNA installed.
  • XNA doesn't actually have any plugin's, you can get 3rd party libraries that give you extra features, but no plugins that directly tamper with the API. Which 3rd party libraries you are planning on using? (note: most of the popular ones like physics engines etc... provide a DLL that works on MonoGame too now days).

Try not to dwell on trying to port before your game is built, it's a mistake I and others have made in the past over and over, this tends to lead to games never getting built, but that's just my two cents smile.png






#5079106 single pressed key to do whole sprite verse

Posted by Tarika on 20 July 2013 - 03:02 AM

Consider something, in order for your game to render fast it needs to call Draw() and Update() as often as possible (update is not required to but that's going off topic). This means if you have something that requires a certain amount of time to execute after it is triggered, the action will have to last for longer than 1 draw call. Here is an example of such a technique used in XNA 4.0:

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace AnimationExample
    public class Game1 : Game
        class Bullet
            public Vector2 Position, Velocity;
            public bool IsDead;
            public Bullet(Vector2 position, Vector2 velocity)
                this.Position = position;
                this.Velocity = velocity;

        List<Bullet> Bullets = new List<Bullet>();
        Random Random1 = new Random();
        GraphicsDeviceManager Graphics;
        SpriteBatch Batch1;
        KeyboardState OldState, NewState;
        Texture2D ProjectileTexture;
        float HalfTextureH, QuaterTextureH;

        public Game1()
            Graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

        protected override void LoadContent()
            Batch1 = new SpriteBatch(GraphicsDevice);
            ProjectileTexture = Content.Load<Texture2D>("tex1");
            HalfTextureH = ProjectileTexture.Height / 2.0f;
            QuaterTextureH = ProjectileTexture.Height / 4.0f;

        protected override void Update(GameTime gameTime)
            OldState = NewState;
            NewState = Keyboard.GetState();

            // detect key press.
            if (NewState.IsKeyDown(Keys.Space) && OldState.IsKeyUp(Keys.Space))

            // update any existing projectiles.

            // remove any projectiels that are dead.
            Bullets.RemoveAll(t => t.IsDead);

            Window.Title = Bullets.Count + " projectile(s) are alive.";

        void ShootBullet(float minSpeed = 2f, float maxSpeed = 10f)
            // get a random y starting coordinate.
            float yPos = ((GraphicsDevice.Viewport.Height - HalfTextureH)
                       * (float)Random1.NextDouble()) - QuaterTextureH;

            // calculate speed.
            float xVel = MathHelper.Clamp(maxSpeed * (float)Random1.NextDouble(), 
                         minSpeed, maxSpeed);

            // add the projectile.
            Bullets.Add(new Bullet(Vector2.UnitY * yPos, Vector2.UnitX * xVel));    

        void UpdateBullet(Bullet bullet)
            bullet.Position += bullet.Velocity;

            // check if the bullet has gone off the screen.
            if (bullet.Position.X > 800) bullet.IsDead = true;

        protected override void Draw(GameTime gameTime)



        void DrawBullet(Bullet bullet)
            Batch1.Draw(ProjectileTexture, bullet.Position, Color.White);

A screenshot for good measure :)




Here is the texture I used if you wish to try the example out:



Note, it is important to understand that if you wish something to happen over a duration of time longer than a single update/draw call, then you will always have to keep track of the state of that action, this is called state management. Each important state of something usually requires a logic update each time Update() is called, this is a core principle of game development.


Side Notes


The example will likely run slow if you were to run it on the Xbox 360 or Windows Phone because it is unoptimized and creates a lot of garbage. This could easily be rectified by using a fixed array as a resource pool where we only ever create a number of Bullet instances once, each bullet would need an extra bool to say if it was being used or not, however this is out of scope so if you come across that problem later on in development, feel free to PM me.



#5078078 Sprite tearing due imprecise pixel coordinates (both uv and pos), how to fix it?

Posted by Tarika on 16 July 2013 - 01:58 AM

Before my response Id like to express my concern towards the language you are using, swearing 3 times just because you are frustrated on a forum where young creative people regularly visit does not seem very becoming of someone who's been a member for almost 5 years. I am sure many other members of the forum would agree with me that if you truly wish to have help from us, that refraining from that sort of behaviour in the future would be appropriate. At this time I'll take it that you didn't realise your language would offend, I'll try to help this time.


OK to the question you asked, if you are allowing non-integer positioning and scaling of the geometry then the pixels you output may not necessarily ever map to the screen coordinates you are hoping for, to make matters worse even if you get it to work on your computer, it may not look right on others. There are some work around's like adding a half texel (not pixel) offset to your UV's, but it really depends on what interpolation method you are relying upon.


In the end there are only a few options when it comes to this sort of thing, either accept it the way it is, try using higher resolution textures so the sampler can give better results, play with the half texel hack and hope it works for other computers, and/or stick to integer positioning and scaling to help the correct positioning of pixels.



#5052158 2D Deferred lighting in XNA

Posted by Tarika on 11 April 2013 - 10:35 AM

I just had a poke around on that tutorial you are using, it looks like the author unfortunately doesn't understand what differed means. The technique he's using to me looks more like the way BasicEffect implements it's 3 lights with added normal mapping. There's nothing wrong with that, but I thought I'd point it out as you are learning as I am and it's I'm sure you'd prefer to understand it.


To further explain what deferred lighting means, check out this Wikipedia article; http://en.wikipedia.org/wiki/Deferred_lighting


There are many ways to create lighting in a 2D game, and the tutorial you are following is definitely one of those, if you were after shadowing you could even do something like Catalin Zima explains on his website: http://www.catalinzima.com/2010/07/my-technique-for-the-shader-based-dynamic-2d-shadows/ however it's important to note that these methods usually seldom go together very easily.


Although it does not offer shadows nor normal mapping out the box, I have written an example on my developer blog to demonstrate how to accomplish straight forward light mapping: http://xpod-games.com/2013/04/2d-light-mapping-in-xna-4-0/


I hope this helps.



#5049130 How easy will it be for me to make a pure 2d platformer/rpg in directx 11?

Posted by Tarika on 02 April 2013 - 05:15 AM

As you are looking to target DX11 but want some out of the box functionality for 2D stuff, why don't you take a look at the DirectXTK, it offers various things you'll likely be familiar with (from XNA): http://directxtk.codeplex.com/



#5039997 Article Inspiration

Posted by Tarika on 06 March 2013 - 09:16 AM

I often see a lot of people struggling with scalars when it comes to texture sampling etc... so maybe that would be a good candidate to add to the mathematical topics already suggested.


Also on a general note maybe a topic on how texture sampling works, and a run down on what various methods there.



#5025383 How to automatically load textures associated with a 3D model

Posted by Tarika on 25 January 2013 - 04:06 AM

As the saying goes, why re-invent the wheel. The default behaviour for the  ModelProcessor loads associated textures into into BasicEffect instances, so the least complicated way to load the textures and use your own effect is to load the Model then replace the effects with your own versions (copying the textures from the old BasicEffect instances). 


If you are writing your own ModelProcessor where you set a custom effect upon build, then I'd recommend looking at the skinned model example as that demonstrates replacing  effects in a content processor: http://xbox.create.msdn.com/en-US/education/catalog/sample/skinned_model


There is no need for you to do this manually, so get studying and good luck :)



#5016764 2D Camera Transformation

Posted by Tarika on 02 January 2013 - 11:39 AM

I still think you are getting a bit confused with view and projection matrices, so I've put a bit of an example together for you that demonstrates how I would normally perform camera rotation in a 2D game:


> http://aimee.xpod.be/CameraRotationExample2D.rar


Note that in this example I disregard the SpriteBatch class to demonstrate another way of drawing sprites (by using the centre of the sprite instead of the top left as origin) which I believe will help you understand things a bit more. Also I create an orthographic projection matrix which means you don't need to figure out where the centre of the screen is because it is placed always at (0,0,0).


Again hope this helps, and if you still get stuck, let me know.



#5013543 2D Camera Transformation

Posted by Tarika on 22 December 2012 - 07:47 PM

I think you are mixing up the view and projection matrix leading to a misunderstanding of why we use world/view/projection in the first place. World is the theoretical transform of the vertices in world space, view can be seen as the camera, it converts world coordinates into ones relative to a camera, now here's the important part; the view matrix doesn't have any knowledge of the size of the view port, that is the job of the projection matrix.


The projection matrix is responsible for converting the view position of a vertex onto screen coordinates, In xna the process only works if you follow this order of transformation.

#5012877 2D Camera Transformation

Posted by Tarika on 20 December 2012 - 12:43 PM

Have a think about it, CreateRotationZ actually doesn't know what you want to rotate around, take a look at how monoxna has implemented this function:

[source lang="csharp"]public static Matrix CreateRotationZ(float radians){ Matrix returnMatrix = Matrix.Identity; returnMatrix.M11 = (float)Math.Cos(radians); returnMatrix.M12 = (float)Math.Sin(radians); returnMatrix.M21 = -returnMatrix.M12; returnMatrix.M22 = returnMatrix.M11; return returnMatrix;}[/source]

taken from http://monoxna.googl...ework/Matrix.cs

In order to get the desired result out of multiplication with matrices, you need to make sure that you multiply matrices in the right order. So:

- multiplying a translation by a scale will enlarge or shrink that translation.
- multiplying a translation by a rotation will in effect rotate that translation around 0,0,0.
- multiplying a rotation by a translation will move that rotation to the desired position.
- multiplying a rotation by a scale alone will literally multiply that scale.

and so on, and so on, so you can see why the order in which you multiply matrices is important. Another thing to note is that I don't see you using the Matrix.CreateLookAt method, from your code it looks like you are trying to create a view matrix, you can certainly create a view matrix other ways but you are probably better using the CreateLookAt method as it's built to do the job.

Hope this helps.


#4952256 Getting Average Color of DXTCompressed Texture

Posted by Tarika on 24 June 2012 - 02:30 AM

Another excuse to use System.Linq Posted Image

public Color GetAvgColor(Texture2D tex, Rectangle rect, int mipLevel = 0)
	Color[] data = new Color[rect.Width * rect.Height];
	if (data.Length == 0) return Color.White;
	tex.GetData<Color>(mipLevel, rect, data, 0, data.Length);
	int r = data.Sum(t => t.R) / data.Length;
	int g = data.Sum(t => t.G) / data.Length;
	int b = data.Sum(t => t.B) / data.Length;
	int a = data.Sum(t => t.A) / data.Length;
	return new Color(r, g, b, a);

edit with a bit of explanation: the 3rd overload of the GetData allows you to specify a rectangle to retrieve colours from, so you simply have to make sure your data array is the same size as what your after returning.

#4949970 compatibility-problem with older .NET versions in Windows 8

Posted by Tarika on 17 June 2012 - 02:34 AM

Hi Syncan, welcome to gamedev! I see you are using managed DirectX, and that you have already had some answers over at MSDN (http://social.msdn.microsoft.com/Forums/nl/Vsexpressvb/thread/a35a60e5-738e-4a55-ba4c-e445ff072127).

To add to Nik02's reply, unfortunately Microsoft stopped supporting that API a long while ago because of the many notorious bugs that kept on creeping up. They decided to instead revise the whole thing with XNA, so you may find that although your app works in WindowsXP, running it under Windows 8 could be something the original developers of the API never got around to preparing for.

The reason why this is important is that working on an API that is no-longer supported, means that it's likely this will not be the only issue that you come across while developing games, and last thing we want is someone sacking something they might have really enjoyed because of frustration with continuous unfixable bugs! In the MSDN post someone recommended SlimDX, id like to second that as SlimDX is a valid, stable and well maintained replacement, plus it works with vb.net.


#4949197 Help!Where can I find some more SlimDX.Direct3D11 tutorials or simples?

Posted by Tarika on 14 June 2012 - 10:54 AM

On the SlimDX documentation section they state this:

Apart from the SlimDX samples, there are hundreds of samples written by companies and individuals in C++, Managed DirectX, or XNA that demonstrate all kinds of useful things. Although these cannot be copied verbatim to work with SlimDX, the code is typically very similar and we encourage people to use techniques learned from these samples with SlimDX. The differences are generally quite minor, and so they can be a great additional resource for SlimDX developers.

So your best bet is to google dx11 tutorials: http://lmgtfy.com/?q...ctx 11 tutorial


#4945996 Getting vertex position at certain point

Posted by Tarika on 03 June 2012 - 09:09 PM

This all depends on how your mesh is stored, what language you are using, and what you wish to achieve by this. Can you elaborate on what you wish to achieve, and which language you are using so members here can more easily answer.


#4941119 GraphicsDevice handle in a custom content processor

Posted by Tarika on 17 May 2012 - 11:51 PM

Thanks for the reply, it took me a while to figure it out but you pointed me in the right direction Posted Image

For anyone else looking for the solution, here is what I came up with:

IGraphicsDeviceService obj = content.ServiceProvider.GetService(typeof(IGraphicsDeviceService)) as IGraphicsDeviceService;
GraphicsDevice device = obj.GraphicsDevice;