Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Printing to image handle
#1
Lightbulb 
I made this small function to create image handles with text and transparency, I use a variation of this in my games for some time now, I figured someone might want to use this in the future.
To run this you'll need the the font used (mouse.ttf), the font name being Mouse Memoirs.


[Image: Function-Test-Preview.png]


If you have an idea or need help with something feel free to DM me, preferably on Discord.
Big Grin

Code: (Select All)

'Setting the screen so its possible to go back in the function
Dim Shared MainScreen
MainScreen = _NewImage(550, 650, 32)
Screen MainScreen
_Dest MainScreen
'Dims fonts with different sizes.
Dim Shared FontSizes(1024)
For i = 1 To 1024
    FontSizes(i) = _LoadFont("assets/begs world/mouse.ttf", i, "")
Next
Dim text(20) As Long
text(0) = _NewImage(16, 32, 32)
text(1) = CreateImageText(text(1), "Hello, Welcome to my demo!", 48)
text(2) = CreateImageText(text(2), "This is a demo of my text function", 42)
text(3) = CreateImageText(text(3), "it's possible to do alot of things", 42)
text(4) = CreateImageText(text(4), "Like this!", 90)
text(5) = CreateImageText(text(5), "or this...", 21)
text(6) = CreateImageText(text(6), "I used this in my menu engine and games Big Grin", 38)
text(7) = CreateImageText(text(7), "It's pretty flexible too!", 60)
text(8) = CreateImageText(text(8), "You can generate text in real time like this:", 30)
text(9) = CreateImageText(text(9), "!OoOoOoOoOo!", 30) 'This one will change size once in the LOOP
text(10) = CreateImageText(text(10), "Neat, right?", 60)
text(11) = CreateImageText(text(11), "This is the end, bye!", 85)

Intro = _Height + 64
Beep
Do
    Cls: _Limit 60: _KeyClear
    Intro = Intro - 1
    o = 0
    delay = delay - 1
    For i = 1 To 11
        If i = 9 And delay < 0 Then delay = 2: text(9) = CreateImageText(text(9), "!OoOoOoOoOo!", Int(Rnd * 80) + 10) ' The real time generating part, pretty simple.

        ' Line to display the actual size of the images:
        'Line ((_Width / 2) - _Width(text(i)) / 2, Intro + o)-((_Width / 2) + _Width(text(i)) / 2, Intro + o + _Height(text(i))), _RGB32(64, 64, 64), BF

        _PutImage ((_Width / 2) - _Width(text(i)) / 2, Intro + o), text(i) 'Displaying the images in the right place.

        If i <> 9 Then 'This exception is so texts after the 9th don't jitter
            o = o + _Height(text(i))
        Else
            o = o + 100
        End If

    Next
    _Display
Loop While Intro > -700
Beep
System

'This is the function I made.
' Handle is the image you want to create, make sure you do "  Image = CreateImageText(Image, "blabla", 20)  ".
' Why putting Image 2 times? So you don't fill your computer's memory generating a lot of images with the same Handle like in text(9).
Function CreateImageText (Handle As Long, text As String, textsize As Integer) 'Function written by Bhsdfa
    If Handle <> 0 Then _FreeImage Handle ' Making sure the Handle is free.
    If text = "" Then text = " " ' If text = "" it will generate an error
    _Font FontSizes(textsize)
    thx = _PrintWidth(text) 'thx and thy are used to set image resolution.
    thy = _FontHeight(FontSizes(textsize))
    Handleb = _NewImage(thx, thy, 32) 'Why Handleb? For some reason it doesn't work creating the normal Handle, there need to be one more step.
    _Dest Handleb
    _ClearColor _RGB32(0, 0, 0): _PrintMode _KeepBackground: _Font FontSizes(textsize): _PrintString (0, 0), text, Handleb ' Prints to Handleb
    Handle = _NewImage(thx, thy, 32)
    _Dest MainScreen
    _PutImage (0, 0), Handleb, Handle ' From HandleB to normal Handle.
    _Font FontSizes(20) ' Resets the font back to normal, you can switch this in your code to your normal font.
    If Handleb <> 0 Then _FreeImage Handleb ' Frees HandleB to prevent filling up memory.
    CreateImageText = Handle
End Function


Attached Files
.ttf   mouse.ttf (Size: 54.62 KB / Downloads: 11)
Reply
#2
Oh nice, I loaded an array of font sizes in my "very simple (vs) GUI" also but skipped making images before printing.
b = b + ...
Reply




Users browsing this thread: 1 Guest(s)