Screen Fades and Audio playback

BlitzMax Forums/BlitzMax Beginners Area/Screen Fades and Audio playback

Please would someone tell me how to (if its not too hard)

1 - Fade a screen to black
2- Fade a screen to white
3 - start playback of a sound part way through e.g. specify the position

Thank you very much.

Best way to do a screen fade is draw a rect Setcolor 0,0,0 the full width of the screen with a varying Alpha level. Works like a charm except for a small rendering hit. ;)

Agreed. No idea on the sound thing though...

you can play "parts" of a file via fmod if that is an option?

Thank you very much Grey Alien and MGE - Does the screen fade work for fading to white too?

Grisu - I have fmod - but I didn't realise I could play parts of a file. I found a command to Get the current position but not to set it - what command do I need to use? (I will check the documenatation again anyway because I probably missed it)

Thank you everyone!

Yes, the screen fade thing works for any colour. All you're doing is drawing your screen as usual and then drawing a rectangle of any colour of your choosing on top of it, but varying the alpha level for each frame.

As for the sound thing, there is no way of doing this with the built in audio commands. But, as pointed out, you should be able to do this via FMod or any other third party audio module. I haven't used any of these myself, so can't give you the exact command(s) you need to use.

As for the sound thing, there is no way of doing this with the built in audio commands.
There is...But it ain't pretty. ;o)

Here's some code I put in the Code Archives some time ago:

It can do fade to black, fade to white, as well as a cross-fade from one image to another.

' BlitzMax Fader, by Marc van den Dikkenberg
'
' - Press any key to progress to the next fade effect.
' - Press Escape to exit.
'
' Note: the 'perc' variable is used to define the intensity of an effect percentage-wise, and
' and should be passed a value between 0 and 100 (percent).
' Use any two full-screen images to see these effects.


Graphics 800,600,32,60
SetColor 255,255,255
Local Counter:Int

pic1=LoadImage("pic1.png")
pic2=LoadImage("pic2.jpg")

DrawImage (pic1,0,0)
Flip
WaitKey()

While Not KeyHit(Key_Escape)
	' do a crossfade
	For Counter=0 To 100
		CrossFade(pic1,pic2,Counter)
		Flip
	Next
	WaitKey()
	If KeyDown(key_escape) Then Exit

	' ...and back again!
	For Counter=0 To 100
		CrossFade(pic2,pic1,Counter)
		Flip
	Next
	WaitKey()
	If KeyDown(key_escape) Then Exit

	' Fade to white
	For counter=0 To 100
		DrawImage (pic1,0,0)
		FadeToWhite(counter)
		Flip
	Next
	WaitKey()
	If KeyDown(key_escape) Then Exit

	' Re-draw the image
	DrawImage (pic1,0,0)
	Flip
	WaitKey()
	' ...and fade to black
	For Counter=0 To 200 
		DrawImage (pic1,0,0)
		FadeToBlack(Counter/2)
		Flip
	Next
	WaitKey()
	If KeyDown(key_escape) Then Exit
Wend

Function FadeToWhite(perc:Float)
	If perc>100 Then
		perc=100 
	ElseIf perc<0 Then
		perc=0
	End If
	If perc>0 Then 
		SetColor 255,255,255
		SetBlend (alphablend)
		SetAlpha (perc/100)
		DrawRect (0,0,GraphicsWidth(),GraphicsHeight())
	End If 
End Function

Function FadeToBlack(perc:Float)
	If perc>100 Then
		perc=100 
	ElseIf perc<0 Then
		perc=0
	End If
	If perc>0 Then 
		SetColor 0,0,0
		SetBlend (alphablend)
		SetAlpha (perc/100)
		DrawRect (0,0,GraphicsWidth(),GraphicsHeight())
	End If 
End Function

Function CrossFade(pic1:timage,pic2:timage,perc:Float)
	If perc<1 Then
		perc=1
	ElseIf perc>100 Then
		perc=100
	End If
	SetBlend (SolidBlend)
	SetColor 255,255,255
	DrawImage (pic1,0,0)
	SetBlend (Alphablend)
	SetAlpha (perc/100)
	DrawImage (pic2,0,0)
End Function


And a somewhat related one: Fade to Monochrome.
Converts a full-color image to a black-and-white version, or desaturate it to a shade in between. (Interesting effect, but much more CPU intensive)

' BlitzMax Color-to-B&W Fade Routine
' Takes a full screen image, and smoothly desaturates it.
'
' *Great* for using when pausing a game!
'
' Freeware, (C) 2005 by xlsior/Marc van den Dikkenberg
'
' - Press any key to start the fade effect.
' - Press Escape to exit.

Strict

Graphics 800,600,0

Local pixcolor:Long	' Pixel colors as read from the pixmap
Local pixred:Int
Local pixgreen:Int
Local pixblue:Int     
Local pixavg:Int      ' Averaged RGB values
Local orgformat:Int   ' Keep track of the original PixelMap format, for conversion
Local monopix:Int     ' Colors in ARGB format
Local workimg:timage
Local workpix:tpixmap
Local bwimg:timage
Local sourceimg:timage

sourceimg:timage=LoadImage("d:/working/background3.jpg",DYNAMICIMAGE)
workimg:timage=CreateImage (GraphicsWidth(),GraphicsHeight(),1,DYNAMICIMAGE)

DrawImage sourceimg,0,0
'
' blah blah blah, run a program, draw things to the backbuffer, until
' you want to invoke the effect:
'

GrabImage (workimg:timage,0,0)
Flip

workpix:tpixmap=LockImage(workimg:timage)

orgformat=PixmapFormat(workpix)
If orgformat<>PF_RGB888 Then 
	' if the Pixmap in a different format than these routined expect, then convert:
	workpix:tpixmap=ConvertPixmap:tpixmap(workpix,PF_RGB888)
End If

For Local xx=0 To GraphicsWidth()-1
	For Local yy=0 To GraphicsHeight()-1
		pixcolor=ReadPixel(workpix,xx,yy)
		pixred=(pixcolor & $00FF0000) Shr 16
		pixgreen=(pixcolor & $FF00) Shr 8
		pixblue=(pixcolor & $FF)
		pixavg=(pixred*0.299)+(pixgreen*0.587)+(pixblue*0.114)
		pixred=pixavg Shl 16
		pixgreen=pixavg Shl 8
		pixblue=pixavg
		monopix=pixred | pixgreen | pixblue
		WritePixel(workpix,xx,yy,monopix)
	Next
Next

If orgformat<>PF_RGB888 Then
	' The pixmap was converted to a different format originally.
	' Now convert it back to what it was.
	workpix:tpixmap=ConvertPixmap:tpixmap(workpix,orgformat)
End If

bwimg:timage=LoadImage(workpix:tpixmap)
UnlockImage(workimg:timage)

' At this point we have the following:
' workpix     - A pixmap containing a black-and-white version
' bwimg       - An image containing a black-and-white version
' workimg     - The screen grabbed full-color version
' sourceimg   - The original image loaded from disk

WaitKey()

For Local counter=0 To 100 Step 2
	crossfade(workimg:timage,bwimg:timage,counter)
	Flip
	If KeyDown (key_Escape) Then Exit
Next

WaitKey()

For Local counter=0 To 100 Step 2
	crossfade(bwimg:timage,workimg:timage,counter)
	Flip
	If KeyDown (key_Escape) Then Exit
Next

WaitKey()

Function CrossFade(pic1:timage,pic2:timage,perc:Float)
	' Generic Crossfade routine, to fade between the full color and
	' Black-and-white versions of the image.
	' Parameters: sourcepic, destination pic, percentage faded
	If perc<1 Then
		perc=1
	ElseIf perc>100 Then
		perc=100
	End If
	SetBlend (SolidBlend)
	SetColor 255,255,255
	DrawImage (pic1,0,0)
	SetBlend (Alphablend)
	SetAlpha (perc/100)
	DrawImage (pic2,0,0)
End Function


For setting the position inside a file I use:
FSOUND_Stream_SetTime(shorttrailer, Value)

Wow! - Thank you to everyone for all the kind help.

I have tried your screen-fade routine xlsior - they are very good! You are right - the second routine will be very good for when the game is paused.

Grisu and Yan - Thanks for the help with the audio- playback. I will try both methods.