Sign in to follow this  
web_scripter

DirectX isometric and C#

Recommended Posts

web_scripter    127
I currently use DirectX 9.0 and C# to render isometric maps. here is my code for creating and rendering isometric maps.
Device device = null;
ArrayList terrain = null;
VertexBuffer vertexBuffer = null;
ArrayList map1;
ArrayList map2;
ArrayList map3;
ArrayList arr = null;

public bool InitializeGraphics()
{
        try
	{
	PresentParameters presParams = new PresentParameters();
	presParams.Windowed = true;
	presParams.SwapEffect = SwapEffect.Discard;
	device = new Device( 0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presParams );
		
	device.DeviceReset += new EventHandler( device_DeviceReset );
	this.device_DeviceCreate( device, null );
	this.device_DeviceReset( device, null );
	return true;
	}
	catch( DirectXException ) { return false; }

}

void device_DeviceCreate( object sender, EventArgs e )
		{
			Device dev = sender as Device;

			map1 = null;
			map2 = null;
			map3 = null;
			terrain = null;
			if(vertexBuffer != null)
				vertexBuffer.Dispose();
			vertexBuffer = null;

			arr = new ArrayList();
			terrain = new ArrayList();
			//vertexBuffer.Created += new System.EventHandler( this.createVB );
			//vertexBuffer2.Created += new System.EventHandler( this.createVB2 );
			Random rand = new Random();
			bool offset = false;
			for( float x = -5; x < 5; x += 1f )
			{
				if(offset)
				{
					offset = false;
				}
				else
				{
					offset = true;
				}
				for( float y = -4; y < 4; y += 1 )
				{
					//terrain generation
					int map = rand.Next( 15 );
					if( map < 5 )
						terrain.Add( 1 );
					else if( map >= 5 && map < 10 )
						terrain.Add( 0 );
					else
						terrain.Add( 2 );
					float yoffset = y;
					if(offset)
						yoffset += 0.5f;
				
											CustomVertex.PositionTextured[] verts = this.createVBTextured( x, yoffset );
					
						
						vertexBuffer = new VertexBuffer( typeof( CustomVertex.PositionTextured ), 4, device, Usage.Dynamic | Usage.WriteOnly, CustomVertex.PositionTextured.Format, Pool.Default );
						vertexBuffer.SetData( verts, 0, LockFlags.None );
						arr.Add( vertexBuffer );
					
				}
				map1 = new ArrayList();
				map2 = new ArrayList();
				map3 = new ArrayList();

				int count = terrain.Count;
				for( int i = 0; i < count; i++ )
				{
					switch( (int)terrain[ i ] )
					{ 
						case 0:
							map1.Add( i );
							break;
						case 1:
							map2.Add( i );
							break;
						case 2:
							map3.Add( i );
							break;
					}
				}
			}
		}
CustomVertex.PositionTextured[] createVBTextured( float xoffset, float yoffset )
		{
			CustomVertex.PositionTextured[] verts = new CustomVertex.PositionTextured[ 4 ];

			verts[ 0 ] = new CustomVertex.PositionTextured( -1.0f + xoffset, 0.0f + yoffset,  0.0f, 0.0f, 0.0f );
			verts[ 1 ] = new CustomVertex.PositionTextured( 0.0f + xoffset,  -0.5f + yoffset, 0.0f, 0.0f, 1.0f );
			verts[ 2 ] = new CustomVertex.PositionTextured( 1.0f + xoffset,  0.0f + yoffset,  0.0f, 1.0f, 1.0f );
			verts[ 3 ] = new CustomVertex.PositionTextured( 0.0f + xoffset,  0.5f + yoffset,  0.0f, 1.0f, 0.0f );

			return verts;
		}
void device_DeviceReset( object sender, EventArgs e )
		{
			Device dev = (Device)sender;
			dev.RenderState.CullMode = Cull.None;
			dev.RenderState.Lighting = false;
			
		}
	public void Render()
		{
			if( device == null )
				return;
			if( pause )
				return;
			//Clear the backbuffer to a blue color 
			device.Clear( ClearFlags.Target, System.Drawing.Color.Pink, 1.0f, 0 );
			// Setup the world, view, and projection matrices
			SetupMatrices();
			//Begin the scene
			device.BeginScene();

			device.VertexFormat = CustomVertex.PositionTextured.Format;
			device.SetTexture( 0, null );
			
			int length = map1.Count;
			device.SetTexture(0, new Texture(device,new Bitmap(@"G:\map.bmp"), 0, Pool.Managed));
			for( int i = 0; i < length; i++ )
			{
				int index = (int)map1[ i ];
				if( index < arr.Count )
				{
					VertexBuffer vb = arr[ index ] as VertexBuffer;
					device.SetStreamSource( 0, vb, 0 );
					device.DrawPrimitives( PrimitiveType.TriangleFan, 0, 2 );
					
				}
			}
			device.SetTexture(0,null);
			
			length = map2.Count;
			device.SetTexture(0, new Texture(device,new Bitmap(@"G:\map2.bmp"), 0, Pool.Managed));
			for( int i = 0; i < length; i++ )
			{
				int index = (int)map2[ i ];
				if( index < arr.Count )
				{
					VertexBuffer vb = arr[ index ] as VertexBuffer;
					device.SetStreamSource( 0, vb, 0 );
					device.DrawPrimitives( PrimitiveType.TriangleFan, 0, 2 );
				}
			}
			device.SetTexture(0, null);

			length = map3.Count;
			device.SetTexture( 0, new Texture( device, new Bitmap( @"G:\map3.bmp" ), 0, Pool.Managed ) );
			for( int i = 0; i < length; i++ )
			{
				int index = (int)map3[ i ];
				if( index < arr.Count )
				{
					VertexBuffer vb = arr[ index ] as VertexBuffer;
					device.SetStreamSource( 0, vb, 0 );
					device.DrawPrimitives( PrimitiveType.TriangleFan, 0, 2 );
				}
			}			
			//Tile tile = new Tile();
			//tile.drawTiles(device);
			device.SetTexture(0, null);
			

			//End the scene
			device.EndScene();
			device.Present();
			this.Invalidate();
		}
private void SetupMatrices()
		{
			int iTime = Environment.TickCount % 1000;
			float fAngle = iTime * ( 2.0f * (float)Math.PI ) / 1000.0f;
			
			device.Transform.World = Matrix.RotationYawPitchRoll( 0, 0, 0 );
			device.Transform.View = Matrix.LookAtLH( new Vector3( 0.0f + CameraX, 3.0f, -5.0f + CameraY ), new Vector3( 0.0f + CameraX, 0.0f, 0.0f + CameraY ), new Vector3( 0.0f, 1.0f, 0.0f ) );
			device.Transform.Projection = Matrix.OrthoLH( 8.0f, 8.0f, 1.0f, 100.0f );
			//device.Transform.Projection = Matrix.PerspectiveFovLH( (float)Math.PI / 4, (float)this.Width / this.Height,
			//	1.0f, 1000.0f );
			//device.Transform.View = Matrix.LookAtLH( new Vector3( 200.0f, 150.0f, 362.13f ),
			//	new Vector3( 200.0f, 150.0f, 0.0f ), new Vector3( 0.0f, -1.0f, 0.0f ) );

			//device.Transform.Projection = Matrix.PerspectiveFovLH( (float)Math.PI / 4, 1.0f, 1.0f, 100.0f );
		}


well that is my code. And my question is am i creating and rendering my tiles correctly or should i be doing it another way? Also i am trying to add elevation in my tiles so it would look more like real terrain. Also i need to figure out how to select tiles with the mouse. I'm probably doing this all wrong but this is my first time creating an isometric / tile based game and have no idea how to organize my tiles and map efficiently. All help is greatly appreciated. Thanks in advance [Edited by - web_scripter on May 26, 2005 9:46:36 AM]

Share this post


Link to post
Share on other sites
paulecoyote    1065
your code is interesting but could you edit your post using the "source" code tags as in the faq? http://www.gamedev.net/community/forums/faq.asp

Then it will format it properly, making it easier to read.

Selecting stuff in a 3d from 2d is called "picking". There is a utility function called "Project" and "Unproject" somewhere in the sdk... if you do a [google] for c# and picking I'm sure you'll find something.

http://msdn.microsoft.com/archive/default.asp?url=/archive/en-us/directx9_m/directx/ref/ns/microsoft.directx/s/vector3/m/unproject0.asp

Share this post


Link to post
Share on other sites
web_scripter    127
Thanks. I've seen picking before. but, i used it in a 3d perspective world with meshes. So i thought it might not work in an orthogonal world with vertexbuffers.

I've come up with a different way of getting screen cordinates into tile cordinates. You just use two formulas

double newX = mouseX*Math.Cos(angle) + mouseY*Math.Cos(angle);
double newY = -mouseX*Math.Sin(angle) + mouseY*Math.Sin(angle);

^y
|
|__>x
My angle is 45 degrees. becuase 0 degress would be the diagram above and 45 degress would be tilting that diagram 45 would tilt the diagram to the right.

So you convert screen coordinates into (i guess you would call it)isometric coordinates. then you just divide each one by the tile size. so if your tiles are 24X12. you would do this:

int tileX = newX / 24;
int tileT = newY / 12;

That's as far as i could get. I don't know how to get the coordinates connected with the tiles.How would you go about doing this?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this