OK here is my final code which is LOADS faster than my start code. It works best over a duration of 1 second for asmooth transition. Note you can press any key to abort the fade.
The array of pixels makes a huge difference. There is a small delay as it is filled but this really isn't noticeable. This code is still slow in windowed mode due to WritePixelFast (haha) but in full screen mode it rocks. Thanks everyone esp. sswift.
To use it, call ccSetupFadeScreen() first, then all you need to do is call ccFadeScreen(x) where x is the number of seconds that you wish the fade to last. Don't forget to set the screen width and height constants up properly!
The array of pixels makes a huge difference. There is a small delay as it is filled but this really isn't noticeable. This code is still slow in windowed mode due to WritePixelFast (haha) but in full screen mode it rocks. Thanks everyone esp. sswift.
; ----------------------------------------------------------------------------- ; ccFadeScreen ; ----------------------------------------------------------------------------- Const ScreenWidth% = 800 Const ScreenHeight% = 600 Dim ScreenPixels%(ScreenWidth*ScreenHeight) Function ccSetupFadeScreen() ;call this to grab the BackBuffer to an array of pixels ready for fading Local counter = 0 Local x,y LockBuffer BackBuffer() For y = 0 To screenheight-1 For x = 0 To screenwidth-1 ScreenPixels(counter) = ReadPixelFast(x, y) Counter = Counter + 1 Next Next UnlockBuffer BackBuffer() End Function Function ccFadeScreen(Time_To_Fade#) ;pass a time in seconds Local Start_Time = MilliSecs() FlushKeys ;this stops the last key press bypassing the Fade Screen function Repeat Time_Passed = MilliSecs() - Start_Time Seconds_Passed# = Time_Passed / 1000.0 ; Take x seconds to fade to black. ccFadeScreenToVal(Time_To_Fade, Seconds_Passed#) ;this function calls Flip True Until GetKey() <> 0 Or Seconds_Passed >= Time_To_Fade ;allow user to escape the fade with a key press End Function Function ccFadeScreenToVal(Time_To_Fade#, Time_Elapsed#) Local X, Y Local RGB Local R, G, B Local Multiplier# Local counter = 0 ;work on a temporary image not BackBuffer Local fstemp = CreateImage(ScreenWidth, ScreenHeight) SetBuffer ImageBuffer(fstemp) ;work out fade level Multiplier# = 1.0 - (Time_Elapsed# / Time_To_Fade#) If (Multiplier# < 0) Then Multiplier# = 0 LockBuffer For Y = 0 To ScreenHeight-1 For X = 0 To ScreenWidth-1 RGB = ScreenPixels(counter) counter = counter + 1 R = (RGB Shr 16) And 255 G = (RGB Shr 8 ) And 255 B = (RGB ) And 255 R = R * Multiplier# G = G * Multiplier# B = B * Multiplier# RGB = (R Shl 16) Or (G Shl 8) Or B WritePixelFast X, Y, RGB Next Next UnlockBuffer ;now show the faded image SetBuffer BackBuffer() ClsColor 0,0,0 ;precaution Cls ;required DrawImage fstemp, 0,0 Flip True ;clean up FreeImage fstemp End Function
To use it, call ccSetupFadeScreen() first, then all you need to do is call ccFadeScreen(x) where x is the number of seconds that you wish the fade to last. Don't forget to set the screen width and height constants up properly!