Advertisement

Direct3D for 2D tile rendering

Started by February 27, 2000 03:25 PM
11 comments, last by Zombie 24 years, 6 months ago
Thanks alot everyone!
I''ve decided to drop software support. If they don''t have a 3d graphics card, that''s their problem!
Actually, I might implement a nice and simple 2d D2D system. The idea is that:
a) you have a 3d graphics card. This Is Good. Use d3d. Have wonderfull graphics.
b) you don''t have a card. This Is Bad. Go buy one. The graphics are bearable, but without the great effects.
Is this a good idea? and if so, how should I implement this. I am thinking along the lines of a virtual base class. Is this a good idea?

Anyway, see ya everyone.

A coder with some unanswered problems.
To Bracket or Zombie, but anyone who can help actually.

I know its a long post but it has all the code sample specific to my question.

I pulled down the source for vb you made, compared it against my own and found that I had forgotten to But the D3DDEVICE option on the back buffer. :/ anyways with that fixed all worked well. Now i have a new question concerning this code

I made it so that by placing the mouse over the far left and right edges of the form it will change directions for the scrolling.

I added the ability to scroll up and down but this is making the screen jump alot.. I can tell its working but its not clean like the left to right motion.

here is a snipet from the code that checks the direction:

Select Case Direction
Case Is = 0
ScrollXOffset = ScrollXOffset - 1
If (ScrollXOffset < 1) Then
ScrollX = ScrollX + 1
ScrollXOffset = 127
If (ScrollX > 20) Then Direction = 1
End If
Case Is = 1
ScrollXOffset = ScrollXOffset + 1
If (ScrollXOffset > 127) Then
ScrollX = ScrollX - 1
ScrollXOffset = 0
If (ScrollX < 1) Then Direction = 0
End If
Case Is = 2 '' up
ScrollYOffset = ScrollYOffset + 1
If (ScrollYOffset > 127) Then
ScrollY = ScrollY - 1
ScrollYOffset = 0
If (ScrollY < 1) Then Direction = 3
End If
Case Is = 3 ''down
ScrollYOffset = ScrollYOffset - 1
If (ScrollYOffset < 1) Then
ScrollY = ScrollY + 1
ScrollYOffset = 127
If (ScrollY > 20) Then Direction = 3
End If
End Select


At this point I call the render sub.

RenderVertex ScrollX, ScrollY, ScrollXOffset, ScrollYOffset


and these vars turn into the following:

Sub RenderVertex(WorldX As Integer, WorldY As Integer, OffsetX As Integer, OffsetY As Integer)
Dim ScreenX As Integer
Dim ScreenY As Integer
Dim WhereX As Integer
Dim WhereY As Integer

d3_device.BeginScene
d3_device.SetTexture 0, dd_texture


WhereY = WorldY
WhereX = WorldX
ScreenY = -16

Do While ScreenY < ScreenHeight

If (WhereY Mod 2) = 1 Then
ScreenX = -64
Else
ScreenX = -94
End If

Do While ScreenX < ScreenWidth


Vertex(0).sx = ScreenX + OffsetX
Vertex(0).sy = ScreenY + 16 + OffsetY - Map(WhereX, WhereY).VertexHeight(0)
Vertex(1).sx = ScreenX + 32 + OffsetX
Vertex(1).sy = ScreenY + OffsetY - Map(WhereX, WhereY).VertexHeight(1)
Vertex(2).sx = ScreenX + 32 + OffsetX
Vertex(2).sy = ScreenY + 32 + OffsetY - Map(WhereX, WhereY).VertexHeight(2)
Vertex(3).sx = ScreenX + 64 + OffsetX
Vertex(3).sy = ScreenY + 16 + OffsetY - Map(WhereX, WhereY).VertexHeight(3)

Vertex(0).color = Map(WhereX, WhereY).VertexColor(0)
Vertex(1).color = Map(WhereX, WhereY).VertexColor(1)
Vertex(2).color = Map(WhereX, WhereY).VertexColor(2)
Vertex(3).color = Map(WhereX, WhereY).VertexColor(3)

Call d3_device.DrawPrimitive(D3DPT_TRIANGLESTRIP, D3DFVF_TLVERTEX, Vertex(0), 4, D3DDP_WAIT) '' D3DDP_DEFAULT
ScreenX = ScreenX + 64 ''(tile width)
WhereX = WhereX + 1
Loop
WhereY = WhereY + 1
WhereX = WorldX
ScreenY = ScreenY + 16 ''(half the tile height) }
Loop

d3_device.EndScene

End Sub

Sub MakeDemoMap()
Dim X As Integer, Y As Integer
Dim DeltaX As Integer '' Multiplier
Dim Value As Integer '''' Used to calculate height
Dim color As Long '' Used to calculate vertex shading
Dim Percentage As Integer
Dim testHeight As Integer
''Value = 100
For Y = 2 To 126

For X = 2 To 126

DeltaX = 1 - (Y Mod 2) '''' 1 for even, 0 for odd
''Value = Int((Rnd * 128) + 1) ''the bigger random value, the sharper ground.
Value = 10
''If x = 21 Then Value = 10 '' I want a flat walkway!
If Y < 10 Then Value = 128

''Percentage = Value * 200 ''the bigger number, the better lighting
Percentage = Value * 15
color = RGB(Percentage, Percentage, Percentage)


'' This tile''s west most vertex
Map(X, Y).VertexHeight(0) = Value
Map(X, Y).VertexColor(0) = color
Map(X, Y).h = testHeight
'' SW tile''s northern vertex
Map(X - DeltaX, Y + 1).VertexHeight(1) = Value
Map(X - DeltaX, Y + 1).VertexColor(1) = color

'' NW tile''s southern vertex
Map(X - DeltaX, Y - 1).VertexHeight(2) = Value
Map(X - DeltaX, Y - 1).VertexColor(2) = color

'' West Tile''s East most vertex
Map(X - 1, Y).VertexHeight(3) = Value
Map(X - 1, Y).VertexColor(3) = color


Map(X, Y).Texture = (X Mod 2)
Next X
Next Y


End Sub
Advertisement
Nevermind I figured it out. I had to change the ScrollYOffset value to 16- half the tile height.

No wonder it was jumpy

This topic is closed to new replies.

Advertisement