• Welcome to Dark Bit Factory & Gravity.
 

Wavy and Stretchy Text [BB2D]

Started by mike_g, August 20, 2007

Previous topic - Next topic

0 Members and 0 Guests are viewing this topic.

mike_g

Heres some code for a couple of simple bitmap font text effects I made. You can get the font used in it here:
http://dbfinteractive.com/index.php?topic=311.0

It also works with the other bitmap fonts on this site. And heres the code:
Const GRAPHICS_W = 800
Const GRAPHICS_H = 600
Const FONT_W = 16
Const FONT_H = 16
Const CHARS$ = " !@#$%+'[]  ,-./0123456789:;(=)? ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Const LETTERS = 64
Const FONT_MASK = -16777216

Dim font(LETTERS*FONT_W, FONT_H)

Graphics GRAPHICS_W, GRAPHICS_H, 32, 2
SetBuffer BackBuffer()

BitmapFontLoad("Com.png")

Global old_time = MilliSecs()
Global frame_count, fps

inc=2: y=100: x=100
While Not KeyHit(1)
Cls
WobbleText(x, y, count, 50, "SOMETHING IS GOING ON")
WobbleText(x-20, y+100, count+5, 40, "ITS A WAVY TEXT DEMO !")

StretchyText(x, y+300, FONT_H, 50, "STRETCHY TEXT")

count=count+1
x=x+inc
If x+(20*16) >= GRAPHICS_W Or x < 0 Then inc=-inc
FPS()
Flip 0
Wend

Function StretchyText(x_pos, y_pos, min, max, txt$)
length = Len(txt$)
LockBuffer BackBuffer()
For char=0 To length-1
;GET THE POSITION IN FONT OF THE LETTER
letter$=Mid(txt$, char+1, 1)
For frame = 1 To LETTERS
If letter$ = Mid(CHARS$, frame, 1) Then Exit
Next
frame=frame-1

char_x = x_pos + char*FONT_W
;DRAW LETTER
For x=0 To FONT_W-1
height = (min+max+(Sin(char_x+x)*max)-1)
char_y = y_pos - (height / 2)
y_inc# = Float(FONT_H)/height

font_x = frame*FONT_W+x
font_y#=0
draw_x = char_x+x
For y=0 To height
draw_y = char_y+y
If font(font_x, font_y#) <> FONT_MASK
WritePixel(draw_x, draw_y, font(font_x, font_y#))
EndIf
font_y# = font_y#+y_inc#
Next
Next
Next
UnlockBuffer BackBuffer()
End Function



Function WobbleText(x_pos, y_pos, deg, intensity, txt$)
length = Len(txt$)
LockBuffer BackBuffer()

For char=0 To length-1
letter$=Mid(txt$, char+1, 1)

;GET THE POSITION IN FONT OF THE LETTER
For frame = 1 To LETTERS
If letter$ = Mid(CHARS$, frame, 1) Then Exit
Next
frame=frame-1

x_mod = char*FONT_W

;DRAW LETTER
For x=0 To FONT_W-1
wobble=Sin((char*FONT_W)+x+deg)*intensity
font_x = frame*FONT_W+x

For y=0 To FONT_H-1
draw_y = y_pos+y+wobble
draw_x = x_pos+x_mod+x

If font(font_x, y) <> FONT_MASK
WritePixel(draw_x, draw_y, font(font_x, y))
EndIf
Next
Next
Next
UnlockBuffer BackBuffer()
End Function

Function BitmapFontLoad(filename$)
tmp=LoadImage(filename$)
w=ImageWidth(tmp)
h=ImageHeight(tmp)
SetBuffer ImageBuffer(tmp)
LockBuffer
For y=0 To h-1
y_mod = y Mod FONT_H
For x=0 To w-1
font(((y/FONT_H)*w)+x, y_mod)=ReadPixelFast(x, y)
Next
Next
UnlockBuffer
SetBuffer BackBuffer()
FreeImage(tmp)
End Function


Function FPS()
If MilliSecs() > old_time+1000
old_time = MilliSecs()
fps=frame_count
frame_count=0
EndIf
Color 255, 255, 255
Text 0, 0, "FPS: "+fps
frame_count=frame_count+1
End Function

I'm working on porting these to C, but I am getting segfaults at the moment for poking about in the wrong bits of memory. Hopefully I should have that fixed in a bit tho :)

Clyde

Neat routines and thanks for sharing the source dude :)
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.


Challenge Trophies Won:

Shockwave

Shockwave ^ Codigos
Challenge Trophies Won:

va!n

- hp EliteBook 8540p, 4 GB RAM, Windows 8.1 x64
- Asus P5Q, Intel Q8200, 6 GB DDR2, Radeon 4870, Windows 8.1 x64
http://www.secretly.de
Challenge Trophies Won:

mike_g

Thanks guys :) They are kind of basic effects used a lot. But it also works as kind of a template for messing around with bitmap fonts, so hopefully I'll be able to come up with some slightly more original effects in the future ;)

va!n

nice to hear... i am still looking forward... keep on your great work!
- hp EliteBook 8540p, 4 GB RAM, Windows 8.1 x64
- Asus P5Q, Intel Q8200, 6 GB DDR2, Radeon 4870, Windows 8.1 x64
http://www.secretly.de
Challenge Trophies Won: