HyperLamer
Posts: 178/8210 |
[edit] Doh, stupid me. I'd merely mucked up the drawing procedure (each time through the For loop that draws the pixel, I needed to increment the X coord, whereas I only did it once, so I drew all 8 pixels of a row in the same place. It looked like total garbage and didn't crash because of the use of pointers simply reading random crap outta RAM.
So, since it works now, here's the full code:
'Increment a Long value as if unsigned Function IncLongUnsigned(Value As Long) As Long If Value < &H7FFFFFFF And Value >= 0 Then IncLongUnsigned = Value + 1 ElseIf Value > -2147483648# Then IncLongUnsigned = Value - 1 ElseIf Value = &H7FFFFFFF Then IncLongUnsigned = -2147483648# ElseIf Value = -2147483648# Then IncLongUnsigned = &H7FFFFFFF End If End Function
'Params: SrcArrayPtr=Pointer to source byte array (first byte of tile, 16 bytes) 'Required API Declarations: CopyMemory(), SetPixel() Sub GB2BPPToRGB(SrcArrayPtr As Long, DestDC As Long, Optional XPos As Long, Optional YPos As Long, Optional C1 As Long = &HFFFFFF, Optional C2 As Long = &HAFAFAF, Optional C3 As Long = &H5F5F5F, Optional C4 As Long = 0) Dim ArrayPtr As Long, Bits(0 To 7) As Byte, C As Integer, Colour(0 To 3) As Long Dim XP As Byte, YP As Byte 'Draw coords Dim P1 As Byte, P2 As Byte 'Pixel data Colour(0) = C1 'Set the colour array Colour(1) = C2 Colour(2) = C3 Colour(3) = C4 TempByte = 128 For C = 7 To 0 Step -1 'Set the Binary place values Bits(C) = TempByte TempByte = TempByte \ 2 Next C ArrayPtr = SrcArrayPtr Do CopyMemory ByVal VarPtr(P1), ByVal ArrayPtr, 1 'Get 2 bytes from the array ArrayPtr = IncLongUnsigned(ArrayPtr) 'Since we can't pass an array and Variants suck, we pass a pointer CopyMemory ByVal VarPtr(P2), ByVal ArrayPtr, 1 ArrayPtr = IncLongUnsigned(ArrayPtr) For C = 7 To 0 Step -1 TempByte = P1 'Get specific bits of each byte TempByte2 = P2 TempByte = (TempByte And Bits(C)) / (2 ^ C) TempByte2 = (TempByte2 And Bits(C)) / (2 ^ C) TempByte3 = (TempByte2 * 2) + TempByte 'TempByte3=Colour # 'Debug.Print TempByte, TempByte2 SetPixel DestDC, XP + XPos, YP + YPos, Colour(TempByte3) 'Draw the pixel XP = XP + 1 'Inc the x position Next C XP = 0 YP = YP + 1 'Inc the y position Loop Until YP = 8 End Sub
Example use: GB2BPPToRGB VarPtr(TileData(1)), Me.hdc, 8, 12, &hFFFFFF, &h0000FF, &h00FF00, &hFF0000 ^ Will draw on the current form starting at X position 8, Y position 12, using the colours white, red, green and blue (Windoze uses BGR). TileData should be an array of 16 bytes. (You don't have to use the first entry of the array, just the first byte of the graphic, it can be VarPtr(TileData(5465642)) for all the code cares. )
The included function IncLongUnsigned is merely for, as the name implies, incrementing a Long as if it were unsigned (since if I just add one to the pointer and it's above 0x7FFFFFFF VB interprets it as negative and I end up subtracting, and of course there's the potential for overflow). Use it however you want. |