Help me with fading text...

BlitzMax Forums/BlitzMax Beginners Area/Help me with fading text...

Apollonius(Posted 2006) [#1]
Hi everyone!
1. I'm trying to have text fade in and fade out, how would you go about doing this?

After learning that...

2. Now how about having more then one? Fading at different time, like after the first one or in the middle of its fading.

>>>>>>>>>>>>>>>
Now to me this seems abit hard, I think I get the idea globally, like have timers(not sure how to work em) and like
have set a color
wait
set another color
wait
set another color..
ect

>> All the help is very appreciated! :)


ImaginaryHuman(Posted 2006) [#2]
SetBlend AlphaBlend
SetAlpha whatever
DrawImage


Apollonius(Posted 2006) [#3]
what about timing? (btw: text not img)


SillyPutty(Posted 2006) [#4]
it will work with text too


Apollonius(Posted 2006) [#5]
This is the code I have so far, but you know, I don't understand how to time it. Run this and press "N" a few times and this is the effect I want it to do except I want it to do it on its own and not have to press it... Can anyone show me an example?

Global azero#=0.1

SetGraphicsDriver GLMax2DDriver()
Graphics 800,600,0,60 'the 0 stands for debugging, 60 is the herts(FPS)

Repeat
Cls

SetBlend AlphaBlend
SetAlpha(1)
' Background
DrawImage g_background,0,0

' Text
SetColor(165,220,225)
SetAlpha(azero#)
DrawText "Hello World",10,10
If(KeyHit(key_n))Then
	If(azero# < 1)Then
		azero# = azero# + 0.1
	EndIf
EndIf

' Return to no Color
SetColor(255,255,255)

Flip
Until KeyHit(key_escape)
End



WendellM(Posted 2006) [#6]
Here's the sort of approach that I'd use:
Strict

Global azero#=0.1 ' if these need to be Global
Global azero2#=0.1
Local change# = 0.1
Local change2# = 0.1
Local timer:TTimer = CreateTimer(10) ' frequency in Hertz
Local timer2:TTimer = CreateTimer(20)

SetGraphicsDriver GLMax2DDriver()
Graphics 800,600,0,60 'the 0 stands for debugging, 60 is the herts(FPS)

Local g_background:TImage = LoadImage("image.jpg") ' YOUR IMAGE GOES HERE

Repeat

	Delay 5 ' give any other apps a few CPU cycles - reduce or remove if more speed needed
	PollEvent ' check for events without halting like WaitEvent
	Cls
	
	SetBlend AlphaBlend
	SetAlpha(1)
	' Background
	DrawImage g_background,0,0
	
	' Text
	SetColor(165,220,225)
	SetAlpha(azero#)
	DrawText "Hello World",10,10
	If EventSource() = timer Then ' timer has ticked
		azero# = azero# + change#
		If azero > 1.0 Then change = -change ' "ping pong" between getting brighter and getting darker
		If azero < 0.1 Then change = -change
	EndIf

	' Other text
	SetColor(255,0,0)
	SetAlpha(azero2#)
	DrawText "I blink twice as fast",10,24
	If EventSource() = timer2 Then
		azero2# = azero2# + change2#
		If azero2 > 1.0 Then change2 = -change2
		If azero2 < 0.1 Then change2 = -change2
	EndIf
			
	' Return to no Color or Alpha
	SetColor(255,255,255)
	SetAlpha 1.0
	
	Flip

Until KeyHit(key_escape)

End



Apollonius(Posted 2006) [#7]
Thanks this is awesome! Is there any tutorial on timers? It's been briefly explained to me before but I don't see the logic in them

Herts? Createtimer( ??? )?, and the frame per seconds how does that work with herts?

Dunno if I got this right, but when you do like CreateTimer(10), does that mean it ticks every 10 frames? or it ticks 10 times every frames? If thats so wouldnt it go too fast? This is confusing lol.


WendellM(Posted 2006) [#8]
Glad you like it. :) There may be a tutorial somewhere, but I don't know of any right offhand. Timers can have custom event hooks, but that's not necessary for basic operation.

All you really need to do is create them, then do a PollEvent in your main loop (or WaitEvent if you'd prefer your program coming to a halt while waiting for an event). If one of them has ticked over, the EventID() function will return that timer's ID. You can then do whatever it is you're doing when a timer ticks (in this case, increasing/decreasing alpha).

CreateTimer(10) ticks 10 times a second (another way of saying 10 Hertz). However, since the program above is only increasing/decreasing alpha by 0.1, it takes 10 of those ticks to go from full alpha to no alpha (or the other way around). So 10 ticks at 10 ticks per second = 1 second to completely change alpha. (There may be a minor fencepost issue so that it's not exactly 1 second, but close enough :) ).

CreateTimer(20) is twice as fast. It ticks 20 times a second but still only needs 10 ticks to completely change alpha. So 10 ticks at 20 ticks per second = 1/2 second to completely change alpha.

Graphics FPS isn't directly connected to timers' speeds. However, if the timers are near the rate or faster than the FPS, you'll get nasty strobing (and there may be a little stuttering as they get up around half as fast as the FPS). So, at 60 FPS (60 Hertz), you probably wouldn't want to go faster than CreateTimer(30) if your timer events are doing something drastic (like making images appear/disappear completely with every tick). In the example above, the alpha is being changed gradually rather than drastically, so a little stuttering wouldn't be too visible. But 30 "dramatic visual" timer events per second is near the ability of human vision to distinguish them individually anyway.

Oh, and the Delay that's used in the loop above is measured in milliseconds, not Hertz. So it delays 5 millisecs every loop to allow any other running program to do its thing. That's 5/1000 of a second, which is equivalent to 200 Hertz (1000/5). So, even if no other programs are running, 200 Hz is the fastest that anything in your program can run (that is, your main loop will only be executed a maximum of 200 times per second - fewer if it takes longer than 1/200 of a second to go through). This is fine in the program above, since nothing in it approaches that speed. I usually leave at least a Delay 1 (1000 Hertz) so that my programs don't completely hog the CPU (but if you're writing a really high-performance game that should hog the CPU and run as fast as possible, then leave out Delay).

Whew! I hope that clears up the basics of timing. :)


Apollonius(Posted 2006) [#9]
I think I'm starting to get it. Actions are generated every ticks, so the lesser ticks the slower the ticks happen and the more ticks you have the the more tick happen in a shorter time.

I think I grasph it now, yup I think I do! Thanks alot!

My little project will need to have about 100 random fades of text for the effect I wana great, think it can handle that?


WendellM(Posted 2006) [#10]
You're welcome. Yep, sounds like you have it.

100 different text strings on screen at once, all fading at different, random rates? In Blitz3D, Text was kind of slow, so that might have been a problem. But I think that BlitzMax's DrawText is significantly faster, so it could be OK. Only way to know for sure is to try it. :)

If each loop through 100 fades does end up taking too long, you could perhaps increase/decrease alpha by 0.2 rather than 0.1 each time and divide all of your timers' Hertz in half. The fades wouldn't be as smooth, but they'd be less frequent, and thus less taxing on the system. Another possibility is to have only one timer running at maybe 30 Hz, but increase/decrease every text string's alpha when it ticks by different amounts (perhaps ranging from 0.02 to 0.2 or thereabouts). You might try playing around with different approaches and see what works best for you.

Good luck!


Apollonius(Posted 2006) [#11]
I wana do like somekind of matrix effect with 0s and 1s, it's not alot of text.

dunno if i'll succeed but we'll see..

...


Apollonius(Posted 2006) [#12]
Update

I wrote about like over 200 lines, I'm getting closer and closer to soe result, but it sill seems so far. I have the write the same block of codes over and over. As I advance toward my goal, Blitz Max doesn't seem to have any problems with this, it handles it pretty well!


klepto2(Posted 2006) [#13]
If you need a something like this:
http://www.eastwestgames.de/file.php?id=156

I could make an easier example with 0s and 1s and post the code here or I could send it to you.

BTW: It isn't that difficult ;)


Apollonius(Posted 2006) [#14]
Pretty cool, my thing is somewhat I like, but it doesnt go from down to up, it actually doesnt move at all, I'm randomly fading in and out 0s and 1s that are placed on my bg. I haven't finished yet.. maybe it's not quite difficult for you but I'm a newb, this is my first little project lol