For my game I wrote this function which is called every time a portion of a background image is loaded. It takes a background image, does it's thing and spit out a coloured version of the same image. I'm real proud of it :D I feel it proves once again that *anyone* can learn to code, even a guy who's practically retarded when it comes to math (like I am). One problem though: whenever I draw the modified images, fps are cut in half! But the read/writepixel operations are already done with when I draw them. The modified images shouldn't draw any slower than ordinary ones, right?
I posted the function below if anyone wants to take a look at it and see if there's anything wrong.
[CODE]
Function Scenery_Light(f,r_value3D,g_value3D,b_value3D)
Local r# = 0
Local g# = 0
Local b# = 0
Local r_value
Local g_value
Local b_value
Local R_Modifier#
Local G_Modifier#
Local B_Modifier#
Local PixelR#
Local PixelR_Modifier#
Local width = 160
Local height = 400
r_value = 255-r_value3D
g_value = 255-g_value3D
b_value = 255-b_value3D
deltax = 0
deltay = 0
LightedBackgr = CreateImageBB(width,height)
LockBufferReadingBB(Layer3[f])
LockBufferWritingBB(LightedBackgr,64000)
For x = 0 To width-1
For y = 0 To height-1
If BitDepth = 8
PixelR = ReadPixelFastBB(x,y)
PixelR_Modifier = (r_value+g_value+b_value)/3
PixelR = PixelR - (PixelR_Modifier*(PixelR/255))
If PixelR > 255
PixelR = 255
ElseIf PixelR < 0
PixelR = 0
EndIf
ElseIf BitDepth = 16
r = 8*(PeekShort(pixelReadBank,2*x+y*ReadPixelPitch) Shr 11 And %11111) ;pos 11-15
g = 4*(PeekShort(pixelReadBank,2*x+y*ReadPixelPitch) Shr 5 And %111111) ;pos 5-10
b = 8*(PeekShort(pixelReadBank,2*x+y*ReadPixelPitch) And %11111);pos 0-4
ElseIf BitDepth = 32
rgba = PeekInt(pixelReadBank,4*x+y*ReadPixelPitch) And %111111111111111111111111
r = rgba Shr 16 And %11111111 ;pos 16-23
g = rgba Shr 8 And %11111111 ;pos 8-15
b = rgba And %11111111 ;pos 0-7
EndIf
R_Modifier = (r_value*(r/255))
G_Modifier = (g_value*(g/255))
B_Modifier = (b_value*(b/255))
r = r - R_Modifier
g = g - G_Modifier
b = b - B_Modifier
If r > 255
r = 255
ElseIf r < 0
r = 0
EndIf
If g > 255
g = 255
ElseIf g < 0
g = 0
EndIf
If b > 255
b = 255
ElseIf b < 0
b = 0
EndIf
If BitDepth = 8
WritePixelFastBB(x+deltax,y+deltay,PixelR)
Else
WritePixelFastBB(x+deltax,y+deltay,r,g,b)
EndIf
Next
Next
UnlockBufferWritingBB(LightedBackgr)
UnlockBufferWritingBB(Layer3[f])
FreeImageBB(Layer3[f])
Layer3[f] = CreateImageBB(width,height)
Layer3[f] = CopyImageBB(LightedBackgr,1)
FreeImageBB(LightedBackgr)
End Function
[/CODE]
I posted the function below if anyone wants to take a look at it and see if there's anything wrong.
[CODE]
Function Scenery_Light(f,r_value3D,g_value3D,b_value3D)
Local r# = 0
Local g# = 0
Local b# = 0
Local r_value
Local g_value
Local b_value
Local R_Modifier#
Local G_Modifier#
Local B_Modifier#
Local PixelR#
Local PixelR_Modifier#
Local width = 160
Local height = 400
r_value = 255-r_value3D
g_value = 255-g_value3D
b_value = 255-b_value3D
deltax = 0
deltay = 0
LightedBackgr = CreateImageBB(width,height)
LockBufferReadingBB(Layer3[f])
LockBufferWritingBB(LightedBackgr,64000)
For x = 0 To width-1
For y = 0 To height-1
If BitDepth = 8
PixelR = ReadPixelFastBB(x,y)
PixelR_Modifier = (r_value+g_value+b_value)/3
PixelR = PixelR - (PixelR_Modifier*(PixelR/255))
If PixelR > 255
PixelR = 255
ElseIf PixelR < 0
PixelR = 0
EndIf
ElseIf BitDepth = 16
r = 8*(PeekShort(pixelReadBank,2*x+y*ReadPixelPitch) Shr 11 And %11111) ;pos 11-15
g = 4*(PeekShort(pixelReadBank,2*x+y*ReadPixelPitch) Shr 5 And %111111) ;pos 5-10
b = 8*(PeekShort(pixelReadBank,2*x+y*ReadPixelPitch) And %11111);pos 0-4
ElseIf BitDepth = 32
rgba = PeekInt(pixelReadBank,4*x+y*ReadPixelPitch) And %111111111111111111111111
r = rgba Shr 16 And %11111111 ;pos 16-23
g = rgba Shr 8 And %11111111 ;pos 8-15
b = rgba And %11111111 ;pos 0-7
EndIf
R_Modifier = (r_value*(r/255))
G_Modifier = (g_value*(g/255))
B_Modifier = (b_value*(b/255))
r = r - R_Modifier
g = g - G_Modifier
b = b - B_Modifier
If r > 255
r = 255
ElseIf r < 0
r = 0
EndIf
If g > 255
g = 255
ElseIf g < 0
g = 0
EndIf
If b > 255
b = 255
ElseIf b < 0
b = 0
EndIf
If BitDepth = 8
WritePixelFastBB(x+deltax,y+deltay,PixelR)
Else
WritePixelFastBB(x+deltax,y+deltay,r,g,b)
EndIf
Next
Next
UnlockBufferWritingBB(LightedBackgr)
UnlockBufferWritingBB(Layer3[f])
FreeImageBB(Layer3[f])
Layer3[f] = CreateImageBB(width,height)
Layer3[f] = CopyImageBB(LightedBackgr,1)
FreeImageBB(LightedBackgr)
End Function
[/CODE]