Author Topic: Wavy and Stretchy Text [BB2D]  (Read 1696 times)

0 Members and 1 Guest are viewing this topic.

Offline mike_g

  • Amiga 1200
  • ****
  • Posts: 435
  • Karma: 34
    • View Profile
Wavy and Stretchy Text [BB2D]
« on: August 20, 2007 »
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:
Code: [Select]
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 :)
« Last Edit: August 21, 2007 by mike_g »

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: Wavy and Stretchy Text
« Reply #1 on: August 20, 2007 »
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:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17378
  • Karma: 497
  • evil/good
    • View Profile
    • My Homepage
Re: Wavy and Stretchy Text
« Reply #2 on: August 20, 2007 »
Yep, cheers Mike!
Shockwave ^ Codigos
Challenge Trophies Won:

Offline va!n

  • Pentium
  • *****
  • Posts: 1418
  • Karma: 109
    • View Profile
    • http://www.secretly.de
Re: Wavy and Stretchy Text
« Reply #3 on: August 20, 2007 »
Very nice!  :goodpost:
k++
- 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:

Offline mike_g

  • Amiga 1200
  • ****
  • Posts: 435
  • Karma: 34
    • View Profile
Re: Wavy and Stretchy Text
« Reply #4 on: August 20, 2007 »
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 ;)

Offline va!n

  • Pentium
  • *****
  • Posts: 1418
  • Karma: 109
    • View Profile
    • http://www.secretly.de
Re: Wavy and Stretchy Text
« Reply #5 on: August 20, 2007 »
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: