PDA

Click to See Complete Forum and Search --> : Tile Game Bug


git
Mar 13th, 2001, 06:34 AM
Hiyas,

I've written a basic Isometric game... Here's the revelant source code so far...

Option Explicit
Dim MapXSize As Integer
Dim MapYSize As Integer
Dim MapZSize As Integer
Dim X As Integer
Dim Y As Integer
Dim Z As Integer
Dim XOffSet As Integer
Dim YOffSet As Integer
Dim ZOffSet As Integer
Dim Map() As Integer
Dim CityMap(500, 500) As Integer

Private Declare Function BitBlt Lib "gdi32" (ByVal HeyhDestDC As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyUp Then
XOffSet = XOffSet - 1
ElseIf KeyCode = vbKeyDown Then
XOffSet = XOffSet + 1
ElseIf KeyCode = vbKeyLeft Then
YOffSet = YOffSet - 1
ElseIf KeyCode = vbKeyRight Then
YOffSet = YOffSet + 1
End If
BitBltMap
TacPic.Refresh
OffSetLabel.Caption = XOffSet & " - " & YOffSet
End Sub

Private Sub Form_Load()
Randomize
XOffSet = 0
YOffSet = 0
MapXSize = 10
MapYSize = 10
MapZSize = 1
ReDim Map(MapXSize, MapYSize, MapZSize) As Integer
For X = 1 To MapXSize
For Y = 1 To MapYSize
For Z = 1 To MapZSize
Map(X, Y, Z) = 1 + Fix(2 * Rnd)
Next Z
Next Y
Next X
BitBltMap
TacPic.Refresh
End Sub

Private Sub BitBltMap()
Dim TempX As Integer
Dim TempY As Integer
On Error Resume Next
TacPic.Cls
For Y = 1 To 22
For X = 0 To 16
TempX = ((XOffSet + Y) + (YOffSet + (X - (MapYSize \ 2))))
TempY = ((XOffSet + Y) - (YOffSet + (X - (MapYSize \ 2))))
If Not TempX < 1 And Not TempY < 1 And Not TempX > MapXSize And Not TempY > MapYSize Then
BitBlt TacPic.hDC, (X * 40) - 20, (Y * 20) - 20, 40, 19, TileSource.hDC, (Map(TempX, TempY, 1) - 1) * 40, 19, vbSrcAnd
BitBlt TacPic.hDC, (X * 40) - 20, (Y * 20) - 20, 40, 19, TileSource.hDC, (Map(TempX, TempY, 1) - 1) * 40, 0, vbSrcPaint
End If
Next X
For X = 1 To 16
TempX = ((XOffSet + Y) + (YOffSet + (X - (MapYSize \ 2))))
TempY = ((XOffSet + Y) - (YOffSet + (X - (MapYSize \ 2))))
If Not TempX - 1 < 1 And Not TempY < 1 And Not TempX - 1 > MapXSize And Not TempY > MapYSize Then
BitBlt TacPic.hDC, (X * 40) - 40, (Y * 20) - 30, 40, 19, TileSource.hDC, (Map(TempX - 1, TempY, 1) - 1) * 40, 19, vbSrcAnd
BitBlt TacPic.hDC, (X * 40) - 40, (Y * 20) - 30, 40, 19, TileSource.hDC, (Map(TempX - 1, TempY, 1) - 1) * 40, 0, vbSrcPaint
End If
Next X
Next Y
End Sub

It blits perfectly, but how do I find the x,y position of the mouse on the actual isometric map? (I'm probably not going to use z now) I've tried lots of formulas but I can't figure out how to 'reverse' my blitting formula.

Btw I tried to use the code vB codes but they didn't seem to work.

Anyone?

Thanks,

-Git

Sastraxi
Mar 13th, 2001, 08:03 AM
Here it is for anyone who wants to help :)
Sorry, git, but you can put the code tags around it to preserve formatting. I'll post in a sec.


Option Explicit
Dim MapXSize As Integer
Dim MapYSize As Integer
Dim MapZSize As Integer
Dim X As Integer
Dim Y As Integer
Dim Z As Integer
Dim XOffSet As Integer
Dim YOffSet As Integer
Dim ZOffSet As Integer
Dim Map() As Integer
Dim CityMap(500, 500) As Integer

Private Declare Function BitBlt Lib "gdi32" (ByVal HeyhDestDC As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyUp Then
XOffSet = XOffSet - 1
ElseIf KeyCode = vbKeyDown Then
XOffSet = XOffSet + 1
ElseIf KeyCode = vbKeyLeft Then
YOffSet = YOffSet - 1
ElseIf KeyCode = vbKeyRight Then
YOffSet = YOffSet + 1
End If
BitBltMap
TacPic.Refresh
OffSetLabel.Caption = XOffSet & " - " & YOffSet
End Sub

Private Sub Form_Load()
Randomize
XOffSet = 0
YOffSet = 0
MapXSize = 10
MapYSize = 10
MapZSize = 1
ReDim Map(MapXSize, MapYSize, MapZSize) As Integer
For X = 1 To MapXSize
For Y = 1 To MapYSize
For Z = 1 To MapZSize
Map(X, Y, Z) = 1 + Fix(2 * Rnd)
Next Z
Next Y
Next X
BitBltMap
TacPic.Refresh
End Sub

Private Sub BitBltMap()
Dim TempX As Integer
Dim TempY As Integer
On Error Resume Next
TacPic.Cls
For Y = 1 To 22
For X = 0 To 16
TempX = ((XOffSet + Y) + (YOffSet + (X - (MapYSize \ 2))))
TempY = ((XOffSet + Y) - (YOffSet + (X - (MapYSize \ 2))))
If Not TempX < 1 And Not TempY < 1 And Not TempX > MapXSize And Not TempY > MapYSize Then
BitBlt TacPic.hDC, (X * 40) - 20, (Y * 20) - 20, 40, 19, TileSource.hDC, (Map(TempX, TempY, 1) - 1) * 40, 19, vbSrcAnd
BitBlt TacPic.hDC, (X * 40) - 20, (Y * 20) - 20, 40, 19, TileSource.hDC, (Map(TempX, TempY, 1) - 1) * 40, 0, vbSrcPaint
End If
Next X
For X = 1 To 16
TempX = ((XOffSet + Y) + (YOffSet + (X - (MapYSize \ 2))))
TempY = ((XOffSet + Y) - (YOffSet + (X - (MapYSize \ 2))))
If Not TempX - 1 < 1 And Not TempY < 1 And Not TempX - 1 > MapXSize And Not TempY > MapYSize Then
BitBlt TacPic.hDC, (X * 40) - 40, (Y * 20) - 30, 40, 19, TileSource.hDC, (Map(TempX - 1, TempY, 1) - 1) * 40, 19, vbSrcAnd
BitBlt TacPic.hDC, (X * 40) - 40, (Y * 20) - 30, 40, 19, TileSource.hDC, (Map(TempX - 1, TempY, 1) - 1) * 40, 0, vbSrcPaint
End If
Next X
Next Y
End Sub

Sastraxi
Mar 13th, 2001, 08:10 AM
Well ya got me. I don't know how to do so... I guess it would've helped had I written the code originally.... I'll take a few 'educated' guesses, though...

And also ERROR in the code, in the second two blits you use (y*20)-30 while in the first two you use (y*20)-20.

Okay... get the position of the mouse. (in pixels)
Then.... I don't know, to tell the truth...

I can't seem to get it... Sorry about that. Maybe rewriting your code is the best way. (sometimes that helps me) If you have a really problematic piece of code, rem it out and start fresh.

Sorry I couldnt be more of a help.

Mar 14th, 2001, 10:45 AM
This is an easy one. All you have to do is get the current mouse pos(GetCursorPos API). Then, just add XOffset to CursorPos.X, and YOffset to CursorPos.Y. So:

Declare blah blah blah GetCursorPos blah ()
Private Type POINTAPI
blah as blah
blah as blah
End Type
'//Suggestion: Fill in those two lines with real code =)

Private Sub GetActualCursorPos(CursorPos as POINTAPI)
GetCursorPos(CursorPos)
CursorPos.X = CursorPos.X + XOffset
CursorPos.Y = CursorPos.Y + YOffest
End Sub


One thing, though. If those offset values are for an entire tile, then just multiply XOffset by TileSizeX and YOffset by TileSizeY.

Z.

Sastraxi
Mar 14th, 2001, 06:32 PM
Well then Zaei, I guess I just wasnt thinking. Hmm... I wonder why I didn't get that? Maybe it was because I didn't understand the code. Ah well, good luck git, and good job Zaei!

I'm just an email away - Sastraxi

git
Mar 14th, 2001, 10:11 PM
Still not working - it's not as simple as adding the offsets to the mouse positions... =/

I've revised the BitBlt sub so it runs better and looks easier. can anyone help me now...?


Private Sub BitBltMap()
Dim TempX As Integer
Dim TempY As Integer
GameForm.Cls
For Y = 1 To 22
For X = 0 To 16
TempX = XOffSet + Y + YOffSet + X - (MapYSize / 2)
TempY = XOffSet + Y - YOffSet - X + (MapYSize / 2)
If Not TempX < 1 And Not TempY < 1 And Not TempX > MapXSize And Not TempY > MapYSize Then
BitBlt GameForm.hDC, (X * 40) - 20, (Y * 20) - 20, 40, 19, TileSource.hDC, (Map(TempX, TempY, 1) - 1) * 40, 19, vbSrcAnd
BitBlt GameForm.hDC, (X * 40) - 20, (Y * 20) - 20, 40, 19, TileSource.hDC, (Map(TempX, TempY, 1) - 1) * 40, 0, vbSrcPaint
End If
Next X
For X = 1 To 16
TempX = XOffSet + Y + YOffSet + X - (MapYSize / 2) - 1
TempY = XOffSet + Y - YOffSet - X + (MapYSize / 2)
If Not TempX < 1 And Not TempY < 1 And Not TempX > MapXSize And Not TempY > MapYSize Then
BitBlt GameForm.hDC, (X * 40) - 40, (Y * 20) - 30, 40, 19, TileSource.hDC, (Map(TempX, TempY, 1) - 1) * 40, 19, vbSrcAnd
BitBlt GameForm.hDC, (X * 40) - 40, (Y * 20) - 30, 40, 19, TileSource.hDC, (Map(TempX, TempY, 1) - 1) * 40, 0, vbSrcPaint
End If
Next X
Next Y
End Sub


Thanks =)

-Git

kedaman
Mar 15th, 2001, 12:48 AM
i hate looking at others tile blitting engines, because they usually don't make sense. I see though there are lots of performance improvements you could do.

well, i'm just going to put the general algoritm for pixel -flat isometrical coordinates, and let you have your specifics added yourself.

ax=px/tilew
ay=py/tileh
ix=ax+ay
iy=ax-ay

pixel offsets are added to px,py
tile offsets are added to ax,ay