Okay, I *think* this might clarify how the faster B+ pixel drawing works.
Here is Mark's lock_graphics demo with a bit more added to show how to add the correct colors. The functions slow things down a bit, but you can easily optimize it. Comments are welcome.
[CODE]
;Note that this is B+ code.
Const FORMAT_RGB565=1 ;16 bit 565
Const FORMAT_XRGB1555=2 ;16 bit 555
Const FORMAT_RGB888=3 ;24 bit
Const FORMAT_XRGB8888=4 ;32 bit
Graphics 640,480,16,1;fullscreen!
timer=CreateTimer(60)
Repeat
Select WaitEvent()
Case $103 ;key down
If EventData()=27 End
Case $4001 ;timer tick
tm=MilliSecs()
LockBuffer
bank=LockedPixels()
For y=0 To 479
offset=y*LockedPitch()
Select LockedFormat()
Case FORMAT_RGB565
For x=0 To 319
PokeShort bank,offset+x*4,Get16Bit565RGB(255,0,255)
PokeShort bank,offset+x*4+2,Get16Bit565RGB(255,0,255)
Next
Case FORMAT_XRGB1555
For x=0 To 319
PokeShort bank,offset+x*4,Get16Bit555RGB(255,0,0)
PokeShort bank,offset+x*4+2,Get16Bit555RGB(255,0,0)
Next
Case FORMAT_RGB888
For x=0 To 639
PokeInt bank,offset+x*4,GetRGB(255,0,0)
Next
Case FORMAT_XRGB8888
For x=0 To 639
PokeInt bank,offset+x*4,GetRGB(255,0,0)
Next
End Select
Next
UnlockBuffer
tm=MilliSecs()-tm
Text 0,0,tm
Text 100,0,LockedFormat()
Flip
End Select
Forever
End
Function GetRGB(r,g,b)
;24/32 Bit Conversion - ignore first 8 bits, which are reserved for alpha info
;r 00000000111111110000000000000000 = 00FF0000
;g 00000000000000001111111100000000 = 0000FF00
;b 00000000000000000000000011111111 = 000000FF
;Note: 32 bits = 4 bytes = 2 shorts = 1 int
Return b Or (g Shl 8) Or (r Shl 16)
End Function
Function Get16Bit555RGB(r,g,b)
;16 bit 555 Conversion - 1st bit is empty
;r 0111110000000000 = 7C00 = 7*16^3 + 12*16^2 = 31744
;g 0000001111100000 = 03E0
;b 0000000000011111 = 001F = 31
;Note: 16 bits = 2 bytes = 1 short
Return (r/8 Shl 10) Or (g/8 Shl 5) Or (b Shr 3)
End Function
Function Get16Bit565RGB(r,g,b)
;16 bit 565 conversion - middle green value gets 6 bits
;r 1111100000000000 = F800 = 63488
;g 0000011111100000 = 07E0
;b 0000000000011111 = 001F = 31
;Note: 16 bits = 2 bytes = 1 short
Return (r/8 Shl 11) Or (g/4 Shl 5) Or (b Shr 3)
End Function
[/CODE]
Here is Mark's lock_graphics demo with a bit more added to show how to add the correct colors. The functions slow things down a bit, but you can easily optimize it. Comments are welcome.
[CODE]
;Note that this is B+ code.
Const FORMAT_RGB565=1 ;16 bit 565
Const FORMAT_XRGB1555=2 ;16 bit 555
Const FORMAT_RGB888=3 ;24 bit
Const FORMAT_XRGB8888=4 ;32 bit
Graphics 640,480,16,1;fullscreen!
timer=CreateTimer(60)
Repeat
Select WaitEvent()
Case $103 ;key down
If EventData()=27 End
Case $4001 ;timer tick
tm=MilliSecs()
LockBuffer
bank=LockedPixels()
For y=0 To 479
offset=y*LockedPitch()
Select LockedFormat()
Case FORMAT_RGB565
For x=0 To 319
PokeShort bank,offset+x*4,Get16Bit565RGB(255,0,255)
PokeShort bank,offset+x*4+2,Get16Bit565RGB(255,0,255)
Next
Case FORMAT_XRGB1555
For x=0 To 319
PokeShort bank,offset+x*4,Get16Bit555RGB(255,0,0)
PokeShort bank,offset+x*4+2,Get16Bit555RGB(255,0,0)
Next
Case FORMAT_RGB888
For x=0 To 639
PokeInt bank,offset+x*4,GetRGB(255,0,0)
Next
Case FORMAT_XRGB8888
For x=0 To 639
PokeInt bank,offset+x*4,GetRGB(255,0,0)
Next
End Select
Next
UnlockBuffer
tm=MilliSecs()-tm
Text 0,0,tm
Text 100,0,LockedFormat()
Flip
End Select
Forever
End
Function GetRGB(r,g,b)
;24/32 Bit Conversion - ignore first 8 bits, which are reserved for alpha info
;r 00000000111111110000000000000000 = 00FF0000
;g 00000000000000001111111100000000 = 0000FF00
;b 00000000000000000000000011111111 = 000000FF
;Note: 32 bits = 4 bytes = 2 shorts = 1 int
Return b Or (g Shl 8) Or (r Shl 16)
End Function
Function Get16Bit555RGB(r,g,b)
;16 bit 555 Conversion - 1st bit is empty
;r 0111110000000000 = 7C00 = 7*16^3 + 12*16^2 = 31744
;g 0000001111100000 = 03E0
;b 0000000000011111 = 001F = 31
;Note: 16 bits = 2 bytes = 1 short
Return (r/8 Shl 10) Or (g/8 Shl 5) Or (b Shr 3)
End Function
Function Get16Bit565RGB(r,g,b)
;16 bit 565 conversion - middle green value gets 6 bits
;r 1111100000000000 = F800 = 63488
;g 0000011111100000 = 07E0
;b 0000000000011111 = 001F = 31
;Note: 16 bits = 2 bytes = 1 short
Return (r/8 Shl 11) Or (g/4 Shl 5) Or (b Shr 3)
End Function
[/CODE]