Author Topic: Draw bitmap text at an angle?  (Read 223 times)

Offline wombats

  • Jr. Member
  • **
  • Posts: 25
Draw bitmap text at an angle?
« on: August 08, 2018, 01:37:14 PM »
I want to be able to draw my bitmap font at the angle set by SetRotation(), like DrawText().

Does anyone know how I can do that?

Code: [Select]
Type Character
Field id:Int
Field x:Int
Field y:Int
Field w:Int
Field h:Int
Function Create:Character(char:String, x:Int, y:Int, w:Int, h:Int)
Local c:Character = New Character
c.id = Asc(char)
c.x = x
c.y = y
c.w = w
c.h = h
Return c
EndFunction
EndType

Global font:TImage = LoadImage("font.png", 0)

Global Chars:TList = New TList

Local A:Character = New Character.Create("a", 0, 0, 14, 31)
Chars.AddLast(A)

Local B:Character = New Character.Create("c", 18, 0, 15, 31)
Chars.AddLast(B)

Local C:Character = New Character.Create("c", 36, 0, 11, 31)
Chars.AddLast(C)

Local Space:Character = New Character.Create(" ", 48, 0, 11, 31)
Chars.AddLast(Space)

Function DrawBitmapText(t:String, x:Int, y:Int)
Local x1:Int = x
For Local i:Int = 1 To t.length
For Local char:Character = EachIn Chars
If char.id = Asc(Mid(t, i, 1))
DrawSubImageRect(font, x1, y, char.w, char.h, char.x, char.y, char.w, char.h)
x1:+char.w
EndIf
Next
Next
EndFunction

Graphics 640, 480

Repeat

SetClsColor(255, 255, 255)

Cls

SetRotation(45)

SetColor(255, 255, 255)
DrawBitmapText("abcabc", 50, 50)

SetColor(0, 0, 0)
DrawText("abcabc", 300, 50)

Flip

Until KeyHit(KEY_ESCAPE) Or AppTerminate()

Offline col

  • Sr. Member
  • ****
  • Posts: 405
Re: Draw bitmap text at an angle?
« Reply #1 on: August 08, 2018, 02:32:03 PM »
Like this?
If you are using virtual resolutions then you'll need to factor those 'scalings' into the equation too.

Code: [Select]
Function DrawBitmapText(t:String, x:Int, y:Int)
Local r:Float = GetRotation()
Local sx:Float, sy:Float
GetScale(sx, sy)

For Local i:Int = 1 To t.length
For Local char:Character = EachIn Chars
If char.id = Asc(Mid(t, i, 1))
DrawSubImageRect(font, x, y, char.w, char.h, char.x, char.y, char.w, char.h)
x :+ Cos(r) * char.w * sx
y :+ Sin(r) * char.w * sy
EndIf
Next
Next
EndFunction
Any bugs in my code are proof of its hand-coded nature.
https://github.com/davecamp

Offline wombats

  • Jr. Member
  • **
  • Posts: 25
Re: Draw bitmap text at an angle?
« Reply #2 on: August 08, 2018, 07:57:46 PM »
Perfect. Thank you!