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("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: 15)
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
#3
@Bhsdfa

You just doubled your reputation! Big Grin

+ 2

I love text stuff like this. I can't even recall if I have something similar in my one graphics WP I worked on some time ago.

Pete
Shoot first and shoot people who ask questions, later.
Reply
#4
(Yesterday, 07:46 PM)Pete Wrote: @Bhsdfa

You just doubled your reputation! Big Grin

+ 2

I love text stuff like this. I can't even recall if I have something similar in my one graphics WP I worked on some time ago.

Pete
Thanks Pete! Tongue
Reply
#5
Nicely done, +1. Thanks for sharing.


Reply
#6
This is the same style thing which I came up with ages ago and use for... EVERYTHING. Wink

Code: (Select All)
Screen _NewImage(800, 700, 32)
_ScreenMove 250, 0
For i = 1 To 4
HW(i) = TextToImage("Hello World", 16, &HFFFFFF00, 0, i)
Next


Cls
Print "The first thing to showcase with these two routines is just how simple it is to turn"
Print "text into an image with TextToImage."
Sleep
Print
Print "First, let's print it forwards:"
_PutImage (250, 48), HW(1)
Sleep
Print
Print "Then, let's print it backwards:"
_PutImage (250, 80), HW(2)
Sleep
Print
Print "Then, let's print it up to down:"
_PutImage (270, 112), HW(3)
Sleep
Locate 8, 40: Print "And let's finish with down to up:"
_PutImage (580, 112), HW(4)
Sleep
Locate 20, 1
Print
Print
Print "TextToImage simply transforms text into an image for us, with a few built in options"
Print "to it for the direction we want we text to print."
Print "It's as simple as a call with:"
Print " Handle = TextToImage(text$, fonthandle, fontcolor, backgroundcolor, mode"
Print
Print " text$ is the string which we want to print. (In this case 'Hello World'"
Print " fonthandle is the handle of the font which we _LOADFONT for use."
Print " (In this case, I choose the default _FONT 16.)"
Print " fontcolor is the color which we want our text in. (Here, it's YELLOW.)"
Print " backgroundcolor is the background which we want for the text time. (Clear this time.)"
Print " mode is how we decide to print forwards, backwards, up to down, or down to up."
Print
Print "Once we have an image handle, we can use that image just the same as we can with any other."
Print "For those who don't need to do anything more than display the text as an image,"
Print "feel free to use it as I have in the first part of this program with _PUTIMAGE."
Print
Print "Trust me -- TextToImage works just fine with _PUTIMAGE."
Print
Print "But.... If you need more..."
Sleep

Cls , 0

Print "There's always DisplayImage to help you out!"
DisplayImage HW(1), 300, 30, 1, 1, 0, 1
Print
Print "Display your image at a scale!"
Sleep
Print
Print "Twice as wide! ";
DisplayImage HW(1), 300, 60, 2, 1, 0, 1
Sleep
Print "Twice as tall! "
DisplayImage HW(1), 500, 60, 1, 2, 0, 1
Sleep
Print
Print "At an angle!"
DisplayImage HW(1), 280, 90, 1, 1, -45, 1
Sleep
Print: Print: Print: Print: Print
Print "Make it rotate!"
_Delay .2
_KeyClear
Do
Line (355, 155)-Step(100, 100), &HFF000000, BF
DisplayImage HW(1), 400, 200, 1, 1, angle, 0

angle = (angle + 1) Mod 360
_Limit 30
_Display
Loop Until _KeyHit
_AutoDisplay
Print
Print
Print
Print
Print "You can basically use DisplayImage just as you'd normally use RotoZoom, EXCEPT..."
Sleep
Print "You can choose which CORNER of the image you want to display at your coordinates."
Print
Line (350, 350)-Step(100, 100), -1, B
Circle (400, 400), 10, -1
Sleep
Print "Top Left corner! ";
DisplayImage HW(1), 400, 400, 2, 2, 0, 1
Sleep
Print "Bottom Left corner! ";
DisplayImage HW(1), 400, 400, 2, 2, 0, 2
Sleep
Print "Top Right corner! ";
DisplayImage HW(1), 400, 400, 2, 2, 0, 3
Sleep
Print "Bottom Right corner! "
DisplayImage HW(1), 400, 400, 2, 2, 0, 4
Sleep
_FreeImage HW(1)
HW(1) = TextToImage("Hello World", 16, &HFFFF0000, &HFF0000FF, 1)
Print "Or Centered!"
DisplayImage HW(1), 400, 400, 2, 2, 0, 0
Circle (400, 400), 10, -1
Sleep

Cls

Print "With TextToImage, you can turn text into an image... It's that simple!"
Print
Print "With DisplayImage, you have a ton of options for how to display ANY image"
Print " (NOT just for use with text images!!)"
Print
Print "Scale them, stretch them, rotate them, position them by various corners..."
Print
Print "Between these two routines, I generally don't need anything else when working"
Print " with images in my programs. Wink"
Print
Print
Print "And that's THE END of this demo. Post any questions you have on the forums for me!"





' (Image As Long, x As Integer, y As Integer, xscale As Single, yscale As Single,
' angle As Single, mode As _Byte)




Function TextToImage& (text$, font&, fc&, bfc&, mode As _Byte)
'text$ is the text that we wish to transform into an image.
'font& is the handle of the font we want to use.
'fc& is the color of the font we want to use.
'bfc& is the background color of the font.

'Mode 1 is print forwards
'Mode 2 is print backwards
'Mode 3 is print from top to bottom
'Mode 4 is print from bottom up
'Mode 0 got lost somewhere, but it's OK. We check to see if our mode is < 1 or > 4 and compensate automatically if it is to make it one (default).

If mode < 1 Or mode > 4 Then mode = 1
dc& = _DefaultColor: bgc& = _BackgroundColor
D = _Dest
F = _Font
T2Idown = CsrLin: T2Iright = Pos(0)
If font& <> 0 Then _Font font&
If mode < 3 Then
'print the text lengthwise
w& = _PrintWidth(text$): h& = _FontHeight
Else
'print the text vertically
For i = 1 To Len(text$)
If w& < _PrintWidth(Mid$(text$, i, 1)) Then w& = _PrintWidth(Mid$(text$, i, 1))
Next
h& = _FontHeight * (Len(text$))
End If

TextToImage_temp& = _NewImage(w&, h&, 32)
TextToImage = TextToImage_temp&
_Dest TextToImage_temp&
If font& <> 0 Then _Font font&
Color fc&, bfc&

Select Case mode
Case 1
'Print text forward
_PrintString (0, 0), text$
Case 2
'Print text backwards
temp$ = ""
For i = 0 To Len(text$) - 1
temp$ = temp$ + Mid$(text$, Len(text$) - i, 1)
Next
_PrintString (0, 0), temp$
Case 3
'Print text upwards
'first lets reverse the text, so it's easy to place
temp$ = ""
For i = 0 To Len(text$) - 1
temp$ = temp$ + Mid$(text$, Len(text$) - i, 1)
Next
'then put it where it belongs
For i = 1 To Len(text$)
fx = (w& - _PrintWidth(Mid$(temp$, i, 1))) / 2 + .99 'This is to center any non-monospaced letters so they look better
_PrintString (fx, _FontHeight * (i - 1)), Mid$(temp$, i, 1)
Next
Case 4
'Print text downwards
For i = 1 To Len(text$)
fx = (w& - _PrintWidth(Mid$(text$, i, 1))) / 2 + .99 'This is to center any non-monospaced letters so they look better
_PrintString (fx, _FontHeight * (i - 1)), Mid$(text$, i, 1)
Next
End Select
_Dest D
Color dc&, bgc&
_Font F
Locate T2Idown, T2Iright
End Function

Sub DisplayImage (Image As Long, x As Integer, y As Integer, xscale As Single, yscale As Single, angle As Single, mode As _Byte)
'Image is the image handle which we use to reference our image.
'x,y is the X/Y coordinates where we want the image to be at on the screen.
'angle is the angle which we wish to rotate the image.
'mode determines HOW we place the image at point X,Y.
'Mode 0 we center the image at point X,Y
'Mode 1 we place the Top Left corner of oour image at point X,Y
'Mode 2 is Bottom Left
'Mode 3 is Top Right
'Mode 4 is Bottom Right

Dim As Integer px(3), py(3), w, h, w1, h1
Dim sinr As Single, cosr As Single, i As _Byte
w = _Width(Image): h = _Height(Image)
w1 = w * xscale: h1 = h * yscale
Select Case mode
Case 0 'center
px(0) = -w1 / 2: py(0) = -h1 / 2: px(3) = w1 / 2: py(3) = -h1 / 2
px(1) = -w1 / 2: py(1) = h1 / 2: px(2) = w1 / 2: py(2) = h1 / 2
Case 1 'top left
px(0) = 0: py(0) = 0: px(3) = w1: py(3) = 0
px(1) = 0: py(1) = h1: px(2) = w1: py(2) = h1
Case 2 'bottom left
px(0) = 0: py(0) = -h1: px(3) = w1: py(3) = -h1
px(1) = 0: py(1) = 0: px(2) = w1: py(2) = 0
Case 3 'top right
px(0) = -w1: py(0) = 0: px(3) = 0: py(3) = 0
px(1) = -w1: py(1) = h1: px(2) = 0: py(2) = h1
Case 4 'bottom right
px(0) = -w1: py(0) = -h1: px(3) = 0: py(3) = -h1
px(1) = -w1: py(1) = 0: px(2) = 0: py(2) = 0
End Select
sinr = Sin(angle / 57.2957795131): cosr = Cos(angle / 57.2957795131)
For i = 0 To 3
x2 = (px(i) * cosr + sinr * py(i)) + x: y2 = (py(i) * cosr - px(i) * sinr) + y
px(i) = x2: py(i) = y2
Next
_MapTriangle (0, 0)-(0, h - 1)-(w - 1, h - 1), Image To(px(0), py(0))-(px(1), py(1))-(px(2), py(2))
_MapTriangle (0, 0)-(w - 1, 0)-(w - 1, h - 1), Image To(px(0), py(0))-(px(3), py(3))-(px(2), py(2))
END SUB
Reply
#7
@SMcNeill

I've developed a 5-Bell grading system!

Reviews are in and they're mixed...

The Bad News: Your rating for this demo is zero.

The Good news: You will be receiving your No-Bell Prize, in your in-basket, any day now! Big Grin

-------------------------

Kind of cool, but I think your corners are mixed up in that later part of the demo. Huh

Pete

- Leave it to a ding-dong like Pete to come up with a 5-Bell 'system'.
Reply
#8
(Yesterday, 11:53 PM)Pete Wrote: @SMcNeill

I've developed a 5-Bell grading system!

Reviews are in and they're mixed...

The Bad News: Your rating for this demo is zero.

The Good news: You will be receiving your No-Bell Prize, in your in-basket, any day now! Big Grin

-------------------------

Kind of cool, but I think your corners are mixed up in that later part of the demo. Huh

Pete

- Leave it to a ding-dong like Pete to come up with a 5-Bell 'system'.

It's a matter of perspective.

Poke a dot on the center of your screen.

Now align a piece of paper to the UPPER LEFT of that point, like a photo.

If we're talking upper left alignment of the photo, or goes BELOW and to the RIGHT of that point.

If we're talking about from the screen perspective, it'd go above and left, with the bottom right corner of the picture touching that point.

The corners aren't mixed up; your perspective is just reversed.  Wink
Reply
#9
!tihsllub emos s'tahT

Big Grin eteP
Reply




Users browsing this thread: 5 Guest(s)