2D Tiles in D3D
2D Tiles in D3D
2D Tiles in D3D
2D Tiles in D3D
2D Tiles in D3D
2D Tiles in D3D 2D Tiles in D3D 2D Tiles in D3D 2D Tiles in D3D 2D Tiles in D3D 2D Tiles in D3D 2D Tiles in D3D 2D Tiles in D3D
2D Tiles in D3D 2D Tiles in D3D
2D Tiles in D3D
Go Back  Xtreme Visual Basic Talk > > > > 2D Tiles in D3D


Reply
 
Thread Tools Display Modes
  #1  
Old 09-16-2006, 05:15 PM
beefmonky's Avatar
beefmonky beefmonky is offline
Regular
 
Join Date: Jun 2004
Posts: 66
Default 2D Tiles in D3D


Hi, I wrote a 2D tile RPG game using DirectDraw. I'm trying to rewrite it with D3D9 with VB.NET 2005. It's very hard to find good resources on using d3d for 2d graphics... I'm able to render sprites to the screen, but my question is... if i want to display multiple tiles to the screen (25 x 18), do I have to make multiple quads? one quad representing one tile? then place a texture on each quad. or do i create one large quad (the size of the entire screen) and somehow render my tiles to it?

Also, another question... using directdraw's surfaces, i was able to load a large sprite sheet into a surface, then reference a rectangle to "blit" to the screen. in d3d, do i use textures for this? i hear that you shouldn't use textures larger than 256x256, and avoid using non-square textures, this would make things far more difficult. having to load multiple sprite sheets as textures and all.

anyway, any guidance or help would be much appreciated
Reply With Quote
  #2  
Old 09-17-2006, 05:28 AM
Iceplug's Avatar
Iceplug2D Tiles in D3D Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: Iowa, USA
Posts: 16,583
Default

Hmm... perhaps you could use a vertexbuffer to have a fixed mesh of tiles - make 25 columns of squares with vertices and 18 rows of squares as well. Give them appropriate texture coordinates and then, don't forget to draw them. (Don't forget to make them transformed)

The 256x256 limitation is, I think, the largest texture size you should need for a full-body texture. Any more than that is detail that will go unnoticed. But, I could be wrong.
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
  #3  
Old 09-17-2006, 08:53 PM
beefmonky's Avatar
beefmonky beefmonky is offline
Regular
 
Join Date: Jun 2004
Posts: 66
Default

Quote:
Originally Posted by Iceplug
Hmm... perhaps you could use a vertexbuffer to have a fixed mesh of tiles - make 25 columns of squares with vertices and 18 rows of squares as well. Give them appropriate texture coordinates and then, don't forget to draw them. (Don't forget to make them transformed)

The 256x256 limitation is, I think, the largest texture size you should need for a full-body texture. Any more than that is detail that will go unnoticed. But, I could be wrong.
thanks for your reply. i'm making a lot of progress thanks to your help... although since my last post a few new questions have arose... if all textures MUST be square, how do people make 2d games with characters that are taller than they are wide?

also, is there a direct way to load a bitmap, then split the bitmap into textures... as it is now... i'm loading the bmp, using a graphics object to draw 64x64 sections (my individual tiles) to a temporary bitmap, then using texture.frombitmap to convert the tempBMP to a texture. doing it that way takes probably about 30 seconds to load 120 tiles... that's a bit slow.

anyway, thanks again for your help!
Reply With Quote
  #4  
Old 09-17-2006, 11:58 PM
psychotomusSim psychotomusSim is offline
Centurion
 
Join Date: Apr 2006
Posts: 192
Default

all textures don't have to be square, they can be rectangle =)
__________________
Game Maker Forums - Game Programming Forums
Online RPG Creator - Create your own Web Game
Reply With Quote
  #5  
Old 09-18-2006, 04:14 AM
Rockoon's Avatar
Rockoon Rockoon is offline
Joseph Koss

* Guru *
 
Join Date: Aug 2003
Location: Unfashionable End
Posts: 3,615
Default

Quote:
Originally Posted by beefmonky
thanks for your reply. i'm making a lot of progress thanks to your help... although since my last post a few new questions have arose... if all textures MUST be square, how do people make 2d games with characters that are taller than they are wide?

also, is there a direct way to load a bitmap, then split the bitmap into textures... as it is now... i'm loading the bmp, using a graphics object to draw 64x64 sections (my individual tiles) to a temporary bitmap, then using texture.frombitmap to convert the tempBMP to a texture. doing it that way takes probably about 30 seconds to load 120 tiles... that's a bit slow.

anyway, thanks again for your help!
Textures dont always have to be square - older hardware required square power-of-two textures, a bit more modern hardware allowed rectangles but still power-of-two - some of the newest hardware could care less about the power-of-2 nature .. its a safe bet however to use square power-of-two's

Also, you dont have to use the entire texture. You can play with the texture coordinates on the vertices to use any portion of a texture that you want.

You might want to try loading up your big bitmap as a texture (a Texture Atlas is the proper term.. the file format is irrelevant) and then using render-to-texture to split up the atlas into distinct textures (or you can use it as is and just play with the texture coordinates in the vertices)
Reply With Quote
  #6  
Old 09-18-2006, 05:14 PM
Iceplug's Avatar
Iceplug2D Tiles in D3D Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: Iowa, USA
Posts: 16,583
Default

I would just use proper texture coordinates rather than using a Bitmap - is there some case in which you cannot use texture coordinates? (Are you doing point sprites?)
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
  #7  
Old 09-18-2006, 05:26 PM
beefmonky's Avatar
beefmonky beefmonky is offline
Regular
 
Join Date: Jun 2004
Posts: 66
Default

Quote:
Originally Posted by Iceplug
I would just use proper texture coordinates rather than using a Bitmap - is there some case in which you cannot use texture coordinates? (Are you doing point sprites?)
i'm not sure what you mean by point sprites... i'm simply trying to create a sub that mimics the behavior of directdraw

Code:
Public Sub Draw(ByVal lDevice As Device, ByVal Tex As Texture, ByVal X As Integer, ByVal Y As Integer, ByVal Width As Integer, ByVal Height As Integer) Dim Vertices() As CustomVertex.PositionTextured Dim vBuffer As VertexBuffer vBuffer = New VertexBuffer(GetType(CustomVertex.PositionTextured), 4, lDevice, Usage.WriteOnly, CustomVertex.PositionTextured.Format, Pool.Default) Vertices = vBuffer.Lock(0, LockFlags.None) Vertices(0) = New CustomVertex.PositionTextured(X, Y, 1, 0, 0) Vertices(1) = New CustomVertex.PositionTextured(X + Width, Y, 1, 1, 0) Vertices(2) = New CustomVertex.PositionTextured(X, Y + Height, 1, 0, 1) Vertices(3) = New CustomVertex.PositionTextured(X + Width, Y + Height, 1, 1, 1) vBuffer.Unlock() lDevice.VertexFormat = CustomVertex.PositionTextured.Format lDevice.SetStreamSource(0, vBuffer, 0) lDevice.SetTexture(0, Tex) lDevice.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2) End Sub


that's what i have so far... i would like to be able to supply the sub with a srcRect like in directdraw also... but trying to convert the srcRect to the tu, tv coordinates is proving to be quite confusing...

before i go through all of this trouble... will this method be slower than directdraw's "draw" or "drawfast" method?

the reason i'm trying to make this like DD is because i have written a tile based game in directdraw that i'm trying to migrate to d3d. if i could get this sub working it would make things a lot easier... thanks guys for your help
Reply With Quote
  #8  
Old 09-18-2006, 05:29 PM
Iceplug's Avatar
Iceplug2D Tiles in D3D Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: Iowa, USA
Posts: 16,583
Default

Well, if you're trying to mimic DirectDraw, using a Position vertex is not the best choice - use transformed vertices instead - that way your picture will be automatically drawn to screen coordinates.

Also (you may be doing this already) if you make small squares for the texture, you can make tiles for them, which you can't do (efficiently) with one big 4 vertex square covering the screen.
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
  #9  
Old 09-18-2006, 07:54 PM
beefmonky's Avatar
beefmonky beefmonky is offline
Regular
 
Join Date: Jun 2004
Posts: 66
Default

Quote:
Originally Posted by Iceplug
Well, if you're trying to mimic DirectDraw, using a Position vertex is not the best choice - use transformed vertices instead - that way your picture will be automatically drawn to screen coordinates.

Also (you may be doing this already) if you make small squares for the texture, you can make tiles for them, which you can't do (efficiently) with one big 4 vertex square covering the screen.
I looked around a a bit for transformed vertices... i read that you can't apply lighting or other effects to it? i haven't been able to find a good example of how it works, i did try playing around with the tu,tv coordinates like rockoon suggested, it's grabbing parts of the tile map, but it's not getting the part i would like... here's my new draw sub:

Code:
Public Sub Draw(ByVal lDevice As Device, ByVal srcTexture As Texture, ByVal srcRect As Rectangle, ByVal destRect As Rectangle) Dim Vertices() As CustomVertex.PositionTextured Dim vBuffer As VertexBuffer vBuffer = New VertexBuffer(GetType(CustomVertex.PositionTextured), 4, lDevice, Usage.WriteOnly, CustomVertex.PositionTextured.Format, Pool.Default) Vertices = vBuffer.Lock(0, LockFlags.None) Dim TU1, TU2, TV1, TV2 As Double Dim texDesc As SurfaceDescription texDesc = srcTexture.GetLevelDescription(0) TU1 = srcRect.Left / texDesc.Width TU2 = srcRect.Right / texDesc.Width TV1 = srcRect.Top / texDesc.Height TV2 = srcRect.Bottom / texDesc.Height 'Top Left Vertices(0) = New CustomVertex.PositionTextured(destRect.Left, destRect.Top, 1, TU1, TV1) 'Top Right Vertices(1) = New CustomVertex.PositionTextured(destRect.Right, destRect.Top, 1, TU2, TV1) 'Bottom Left Vertices(2) = New CustomVertex.PositionTextured(destRect.Left, destRect.Bottom, 1, TU1, TV2) 'Bottom Right Vertices(3) = New CustomVertex.PositionTextured(destRect.Right, destRect.Bottom, 1, TU2, TV2) vBuffer.Unlock() lDevice.VertexFormat = CustomVertex.PositionTextured.Format lDevice.SetStreamSource(0, vBuffer, 0) lDevice.SetTexture(0, srcTexture) lDevice.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2) End Sub

it's grabbing the wrong tile from my tile map, and it looks stretched some when it's rendered, and i can't figure out why =/
Reply With Quote
  #10  
Old 09-22-2006, 06:58 PM
Iceplug's Avatar
Iceplug2D Tiles in D3D Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: Iowa, USA
Posts: 16,583
Default

You cannot apply lighting to a transformed vertex because it does not actually exist in the 3D world. However, if you want to do some thing similar to lighting, you can use a TransformedColored vertex to add a color.

Care must be taken to make sure that your original bitmap has dimensions that are powers of 2 - it may also be possible that your bitmap may be stretched to fit the size of the texture that you specified when you made the texture. Check your texDesc values as well.
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
  #11  
Old 09-22-2006, 10:08 PM
beefmonky's Avatar
beefmonky beefmonky is offline
Regular
 
Join Date: Jun 2004
Posts: 66
Default

Quote:
Originally Posted by Iceplug
You cannot apply lighting to a transformed vertex because it does not actually exist in the 3D world. However, if you want to do some thing similar to lighting, you can use a TransformedColored vertex to add a color.

Care must be taken to make sure that your original bitmap has dimensions that are powers of 2 - it may also be possible that your bitmap may be stretched to fit the size of the texture that you specified when you made the texture. Check your texDesc values as well.
Wow you are absolutely right it was the texDesc values were off. I loaded the texture and didn't specify the size (thinking it would use the bitmaps actual size) apparently it uses by default 1024x1024 unless otherwise specified. Thanks a lot for pointing that out!
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
2D Tiles in D3D
2D Tiles in D3D
2D Tiles in D3D 2D Tiles in D3D
2D Tiles in D3D
2D Tiles in D3D
2D Tiles in D3D 2D Tiles in D3D 2D Tiles in D3D 2D Tiles in D3D 2D Tiles in D3D 2D Tiles in D3D 2D Tiles in D3D
2D Tiles in D3D
2D Tiles in D3D
 
2D Tiles in D3D
2D Tiles in D3D
 
-->