Deadlyevilryu's Blog

Trying to become a video game developer

StarField Battle 2 is almost released :)

I have my XNA game StarField Battle 2 in peer review now.  It is 50% of the way through peer review. 🙂  I can’t wait I am so excited.  I will be giving away tokens that can be used on Xbox live marketplace to get a free copy of the game to some of my followers on Twitter so if you want a change to get a free token follow me (@DeadlyEvilRyu).  In other xna project news I have done some more work on my next project Anaconda Wars.  It will have single player local and network multiplayer modes.  That game is going to be one of the few games that I make that will be a retro clone.  I really don’t want to make to many retro clones.  After Anaconda Wars is released I will probably start making a RPG, but I don’t know yet.  Other than that I haven’t been doing any other xna programing, and I don’t think I will be doing any more tutorials, but we will see when we get to that point.

Advertisements

June 1, 2010 Posted by | XNA | | Leave a comment

Started a new Project (Anaconda Wars)

I started working on a new game called Anaconda Wars.  It is a remake of the classic snake game.  I know everyone has played that game or some version of it on a cell phone or the internet.  Right now I have 2 game modes one with the classic rules and one with a variation of the classic rules.  I am planing more.  Here is a video of the prototype.

In other news I put Starfield Battle 2 in peer review so when and if it passes I will let you guys know.

April 15, 2010 Posted by | Gaming, XNA | | Leave a comment

XNA 2D HLSL Tutorial 1: Part 4

Ok This is the final part of the 2D HLSL tutorial 1.  Hopefully you have not been confused by any of the code that I showed you.  So without further delay lets get into what we are doing for this part in the tutorial.  So far I have only showed you how to create a single light with the custom shader.  Here is the fun part what about more than one light.  Well it can be done with few additions to the code we have now.  But there is a problem we are working with pixel shader model 2.0  This limits us to working with maybe 2 or 3 lights.  Its not bad if you only need 2 or 3 lights per scene.  If you use PS 3.0 you can many more lights.  I have experimented with it and I can get about 30 – 50 lights no problem.  If you have a PC that doesn’t support PS 3.0 that’s OK I will only be showing an example of code in PS 2.0, but that doesn’t mean you can’t try and test it on the 360.  It works fine for the 360 if you write your custom effect in PS 3.0. If you don’t have a texture that you can use I have provided a Color Map and Depth map for your use. I should have posted those for use in the earlier post for this tutorial, but I forgot to upload them.

First the code for the pixel shader:

struct Light
{
	float3 position;
	float4 color;
	float power;
};

texture colortexture;
texture normaltexture;

int numberOfLights;
Light lights[3];

float ambient;
float4 ambientColor;

float screenWidth;
float screenHeight;

sampler ColorMap = sampler_state
{
	Texture = <colortexture>;
};

sampler NormalMap = sampler_state
{
	Texture = <normaltexture>;
};

float4 FlatColorPS(float2 texCoords : TEXCOORD0) : COLOR
{
    return ambientColor * ambient;
}

float4 AmbientLightPS(float2 texCoords : TEXCOORD0) : COLOR
{
	float4 base = tex2D(ColorMap, texCoords);

	return (base) + (ambientColor * ambient);
}

float4 CalculateLight(Light light, float4 base, float3 normal,
					float3 pixelPosition)
{
	float3 direction = light.position - pixelPosition;
	float distance = 1 / length(light.position - pixelPosition) * light.power;
	float amount = max(dot(base + normal, normalize(distance)), 0);

	return base * distance * amount * light.color * ambient;
}

float4 DeferredNormalPS(float2 texCoords : TEXCOORD0) : COLOR
{
	float4 base = tex2D(ColorMap, texCoords);
	float3 normal = tex2D(NormalMap, texCoords);

	float3 pixelPosition = float3(screenWidth * texCoords.x,
							screenHeight * texCoords.y,0);

	float4 finalColor = (base * ambientColor * ambient);
	for (int i=0;i<numberOfLights;i++)
	{
		finalColor += CalculateLight(lights[i], base, normal, pixelPosition);
	}

	return finalColor;
}

technique Simple
{
    pass Pass1
    {
        PixelShader = compile ps_2_0 FlatColorPS();
    }
}

technique Colored
{
    pass Pass1
    {
        PixelShader = compile ps_2_0 AmbientLightPS();
    }
}

technique Deferred
{
	pass Pass0
	{
		PixelShader = compile ps_2_0 FlatColorPS();
	}

    pass Pass1
    {
        PixelShader = compile ps_2_0 DeferredNormalPS();
    }
}

Ok so what is so different from the last custom effect is we have added in a light structure so that we can keep track of multiple lights. We added a CalculateLight function/Method (Whatever you want to call it.) This will calculate the distance, direction, and amount of light that is generated by each light. Then it will return the base color of the pixel the light is hitting *the distance of the light from the pixel * the amount of light * the color of the light * the ambient factor. Each light will keep track of its own power, position, and color. So now lets move on to the game code.

First we need to create a class for the lights

namespace lightDemo2
{
    public class Light
    {
        public Vector3 position;
        public Vector4 color;
        public float power;

        internal void UpdateEffect(EffectParameter effectParameter)
        {
            effectParameter.StructureMembers["position"].SetValue(position);
            effectParameter.StructureMembers["color"].SetValue(color);
            effectParameter.StructureMembers["power"].SetValue(power);
        }
    }
}

Ok so the only thing that you need to know about here is the UpdateEffect Method. We will be passing in a EffectParameter so that we can manipulate the Light in the custom effect file. So for example if we call effectParameter.StructureMembers[“position”].SetValue(position); this will pass in the position of the light to the pixel shader.

Ok now we add code to the game class.

RenderTarget2D gameRT;
RenderTarget2D normalRT;

Effect effect;
EffectParameter lightParameter;

Texture2D colorTexture;
Texture2D normalTexture;

int currentLight = 0;
Light[] lights = new Light[3];

KeyboardState oldState;

Ok so we have some know variables that we are adding. We are now going to use an int called currentLight to keep track of what light we will be updating the position and power for. Also we will be using a Light array called lights and we will set the size to 3.

In the LoadContent Method add:

effect = Content.Load<Effect>("CustomEffect");

colorTexture = Content.Load<Texture2D>("cloud");
normalTexture = Content.Load<Texture2D>("cloud_depth");

lights[0] = new Light();
lights[0].position = new Vector3(20, 30, 0);
lights[0].power = 50f;
lights[0].color = new Vector4(1, 0, 0, 1);

lights[1] = new Light();
lights[1].position = new Vector3(300, 300, 0);
lights[1].power = 50f;
lights[1].color = new Vector4(0, 1, 0, 1);

lights[2] = new Light();
lights[2].position = new Vector3(700, 500, 0);
lights[2].power = 50f;
lights[2].color = new Vector4(0, 0, 1, 1);

PresentationParameters pp = GraphicsDevice.PresentationParameters;
int width = pp.BackBufferWidth;
int height = pp.BackBufferHeight;
SurfaceFormat format = pp.BackBufferFormat;

gameRT = new RenderTarget2D(GraphicsDevice, width, height, 1, format);
normalRT = new RenderTarget2D(GraphicsDevice, width, height, 1, format);

The only thing that we are adding here is the setting up of the lights.

In the Update Method we will be adding some input code to control the lights.

KeyboardState newState = Keyboard.GetState();

if (newState.IsKeyDown(Keys.Enter) &&
    oldState.IsKeyUp(Keys.Enter))
{
     currentLight++;

     if (currentLight > lights.Length - 1)
         currentLight = 0;
}

if (newState.IsKeyDown(Keys.A))
     lights[currentLight].power -= 5f;
if (newState.IsKeyDown(Keys.S))
     lights[currentLight].power += 5f;
if (newState.IsKeyDown(Keys.Left))
     lights[currentLight].position.X -= 5f;
if (newState.IsKeyDown(Keys.Right))
     lights[currentLight].position.X += 5f;
if (newState.IsKeyDown(Keys.Up))
     lights[currentLight].position.Y -= 5f;
if (newState.IsKeyDown(Keys.Down))
     lights[currentLight].position.Y += 5f;

oldState = newState;

Now finally The Draw Method:

GraphicsDevice.Clear(Color.CornflowerBlue);

RenderNormalTarget();
RenderGameTarget();

GraphicsDevice.SetRenderTarget(0, null);
GraphicsDevice.Clear(Color.Black);

effect.CurrentTechnique = effect.Techniques["Deferred"];
effect.Parameters["screenWidth"].SetValue(GraphicsDevice.Viewport.Width);
effect.Parameters["screenHeight"].SetValue(GraphicsDevice.Viewport.Height);
effect.Parameters["colortexture"].SetValue(gameRT.GetTexture());
effect.Parameters["normaltexture"].SetValue(normalRT.GetTexture());
effect.Parameters["ambient"].SetValue(0.01f);
effect.Parameters["ambientColor"].SetValue(new Vector4(1, 1, 1, 1));
effect.Parameters["numberOfLights"].SetValue(lights.Length);

lightParameter = effect.Parameters["lights"];
for (int i = 0; i < lights.Length; i++)
{
     Light l = lights[i];
     l.Update(lightParameter.Elements[i]);
}

foreach(EffectPass pass in effect.CurrentTechnique.Passes)
{
     spriteBatch.Begin(SpriteBlendMode.None, SpriteSortMode.Immediate, SaveStateMode.None);
     effect.Begin();
     pass.Begin();
     spriteBatch.Draw(gameRT.GetTexture(), Vector2.Zero, Color.White);
     spriteBatch.End();
     pass.End();
     effect.End();
}

Ok so now if you run the code you should be able to use the arrow keys to more a light, space to change lights, and A/Z to change a lights power. OK now that I have shown you how to do 2D lighting with deferred normal mapping you can do all sorts of things. Go nuts, and use your imagination to come up with some cool game concepts, or just create a really cool 2D scene for a level in your game.

If you found this helpful leave a comment, or tell me what I could do to make any tutorials that I do in the future helpful to you.

November 20, 2009 Posted by | XNA | | 9 Comments

XNA 2D HLSL Tutorial 1: Part 2a

I forgot to include the code for the custom effects file in the last post.  LOL 🙂 anyway.  The only thing that we are adding here is we are returning a base color added to the ambientColor * ambient.  See its really simple and easy to code.

texture colortexture;

float ambient;
float4 ambientColor;

sampler ColorMap = sampler_state
{
 Texture = <colortexture>;
};

float4 AmbientLightPS(float2 texCoords : TEXCOORD0) : COLOR
{
 float4 base = tex2D(ColorMap, texCoords);

 return (base) + (ambientColor * ambient);
}

technique Colored
{
 pass Pass1
 {
 PixelShader = compile ps_1_1 AmbientLightPS();
 }
}

November 19, 2009 Posted by | XNA | | Leave a comment

XNA 2D HLSL Tutorial 1: Part 2

In my last post I showed how to use a custom fx file to apply a pixel shader to a render target.  This time I will show how to add in a texture that will be loaded in and apply the pixel shader to that texture.  If you read my last post you may have noticed a Texture2D field called colorTexture.  This will be the texture that is rendered to the render target and displayed on screen with the ambient lighting.  So first lets get that texture into the project

You can just add this to your loadContent Method.

 

colorTexture = Content.Load<Texture2D>("circuit_board");

 

Next in the RenderGameTarget Method in between the spritebatch.begin and spritebatch.end calls add.

spriteBatch.Draw(colorTexture, Vector2.Zero, Color.White);

 

This will draw our colorTexture and any other texture that is placed in RenderGameTarget method to the render target.

Next in the Draw Method if you haven’t added it in.

effect.CurrentTechnique = effect.Techniques["Colored"];
effect.Parameters["colortexture"].SetValue(gameRT.GetTexture());

First change the effect.CurrentTechnique to Colored.  We will add that in the custom shader in a minute.  and set the colortexture in the shader to gameRT.GetTexture.  This will pass in the game render target to the custom shader.  So now you have a texture being blended with your ambient and ambientColor.  Also you can add as many textures as you want to the RenderGameTarget, and they will all get blended properly.  This post is kind of short, and I could have added this part in the last post, but I didn’t think about it.  next time we will be adding in lighting with deferred normal mapping.

November 19, 2009 Posted by | XNA | | 1 Comment

XNA 2D HLSL Tutorial 1: Part 1

Someone asked me about documentation that would help them learn how to do 2d lighing in xna.  When I learned how to do the lighting I had to find out how to do most of that myself.  I did find this helpful.  Nick Gravelyn’s Deferred Normal Mapping Tutorial

I’m not going to go into how a pixel shader or any of the code works.  IMO its really simple once you start to understand it a bit.  So first the simple stuff.

I will start with the effect file.  I called mine CustomEffect.FX you can call yours whatever you want.

texture colortexture;

float ambient;
float4 ambientColor;

sampler ColorMap = sampler_state
{
	Texture = <colortexture>;
};

float4 FlatColorPS(float2 texCoords : TEXCOORD0) : COLOR
{
    return ambientColor * ambient;
}

technique Simple
{
    pass Pass1
    {
        PixelShader = compile ps_1_1 FlatColorPS();
    }
}

What this pixel shader will do is tint your screen a color that is provided to the pixel shader by the ambientColor and multiply it by the ambient power. so if you ambientColor = Vector4(1,1,1,1) and your ambient = 0.5f the screen will be grey.

Next we add it to the game.

Here is the game code.

RenderTarget2D gameRT;

Effect effect;

Texture2D colorTexture;

We will be rendering a texture to a Render Target so that way it will be easy to add other effects later if you want to.

in your LoadContent Method add:

effect = Content.Load<Effect>("CustomEffect");

PresentationParameters pp = GraphicsDevice.PresentationParameters;
int width = pp.BackBufferWidth;
int height = pp.BackBufferHeight;
SurfaceFormat format = pp.BackBufferFormat;

gameRT = new RenderTarget2D(GraphicsDevice, width, height, 1, format);

This will load your effect file and create your Render Target, and set the render target to the screens width, height, and surface format.

private void RenderGameTarget()
{
GraphicsDevice.SetRenderTarget(0, gameRT);
GraphicsDevice.Clear(Color.TransparentBlack);

spriteBatch.Begin();
spriteBatch.Draw(colorTexture, Vector2.Zero, Color.White);
spriteBatch.End();
}

This method will be where you place all your texture that will be drawn onto the screen right now we have none, but that’s OK.

In your Draw Method Add

GraphicsDevice.Clear(Color.CornflowerBlue);

RenderGameTarget();

GraphicsDevice.SetRenderTarget(0, null);
GraphicsDevice.Clear(Color.Black);

effect.CurrentTechnique = effect.Techniques["Colored"];
effect.Parameters["colortexture"].SetValue(colorTexture);
effect.Parameters["ambient"].SetValue(0.4f);
effect.Parameters["ambientColor"].SetValue(new Vector4(0, 0, 1, 1));

foreach(EffectPass pass in effect.CurrentTechnique.Passes)
{
     spriteBatch.Begin(SpriteBlendMode.None, SpriteSortMode.Immediate, SaveStateMode.None);
     effect.Begin();
     pass.Begin();
     spriteBatch.Draw(gameRT.GetTexture(), Vector2.Zero, Color.White);
     spriteBatch.End();
     pass.End();
     effect.End();
}

First we clear the GraphicsDevice to a Color then we render the textures to be drawn.  Next we set the render target to null and clear the GraphicsDevice again.  After that is done we set our effect parameters to pass in the ambientColor you ambient to the effect file then we loop through the effect passes in the effect file and draw the render target for the game sprites.  Right now all it shows is a blank screen with a flat color.  For example if you have a ambientColor of Vector4(1,0,0,1) and set ambient to 1f your screen will be red.

Next time I will show how to add textures to the screen.  I hope this helps you out with your graphics programing.  It took me a few days to learn how to do this, and I think It helped me a lot.

November 19, 2009 Posted by | XNA | | Leave a comment

The do’s and don’ts of Game Development.

Ok StarField Battle 4 months ago would have never passed peer review, or would have never been allowed on XBLIG.  I am going to tell you from memory when was wrong with the game.  I am glad that the reviewers and play testers helped me find all the bugs that were in the game.  First I will tell you what not to do when making a game.

What not to do:

  1. Do not submit a game to peer review before testing (I didn’t do this but it had to be said)
  2. Do not submit a game to play test and have the play testers look for the bugs for you.
  3. Don’t just think that if you get all good feedback in play test that it is OK to go straight to peer review.
  4. DON’T RELEASE YOUR DAMN GAME THE SAME TIME A CALL OF DUTY GAME COMES OUT.

What to do:

  1. Play test your own game and try to break it as much as possible.  If you can’t find any bugs, code errors,  or glitches in the game then submit to play test and ask the play testers to help out.
  2. Try to market your game as much as you can.
  3. Try to get your friends to give you feedback on your current version of the game.  Maybe they will give you ideas on features or game play mechanics that can better the game.
  4. Give yourself lots of time to work on the game.  I originally started the game in April and had until August to finish.  I didn’t finish, and well here we are.  If I could I would go back in time, and submit my current version of the game to DBP.

These are just a few thinks that I could think of, but there are probably more.  Stanfield Battle If far from perfect, but I am proud of my work and hopefully the game does well.  I am planning on making another game but don’t know when I will release it.  Maybe before December or maybe I will just wait and put more work into it.  I had a lot of fun working on my game and can’t wait to make another one.

November 12, 2009 Posted by | XNA | , | Leave a comment

Finally Released.

My Xbox Live Indie Game StarField Battle has been released finally.  🙂  Its been a long 8 months for a game that was originaly supposed to be for the Dream Build Play Competition.  I wish I had submitted the version that I have now for DBP.  The version I submitted had so many bugs and problems with it that it was sad. In fact In my next post I will tell you the do’s and don’ts of game development.

November 12, 2009 Posted by | Gaming, XNA | , | Leave a comment

Started work on a Game Engine.

Yesterday I started working on a 2 d game engine.  Mainly because I was tired of writing code that would handle test cases from The Evil Check List and so the game engine was conceived.

Current features

  • Screen Manager
  • Input Manager (allows for multiple control schemes)
  • Audio Manager
  • Storage Device Manager

Planed features

  • Handle player sign in
  • Networking
  • High Score Table

I don’t know what other features I could add that I would use over and over, but I will think of more.

November 6, 2009 Posted by | Game Engine, XNA | , , | Leave a comment

Thinking of new ideas for a new game

I have 2 games in play test on the XNA Creators Club website right now one will finish tomorrow and I probably will put it into peer review unless I can think of something else that I can add to it.  The other one was for a XNA 7 Day Challenge
http://xna7day.com/

I don’t know what kind of game I will make for my next one even though the one for the 7 Day Challenge isn’t really finished.  I may finish and release it before my 4 month CC membership runs out next month.

November 3, 2009 Posted by | XNA | | Leave a comment