03-05-2024, 07:59 PM
You often need to place text in games.
Unfortunately, _printstring and _printwidth are significantly slow.
I have always used printing by either using the letters based on a font set or transforming an existing sentence into a hardware image.
Since I always had ideas when displaying the text, I always had to adapt the long-used solutions.
Now I tried to create a universal solution so that I no longer have to deal with this.
First you need a newimage,,32 image that uses the display. So that the screen is not SCREEN 0. After.
Let's define the most important parameters of font management!
'm_fontsettings'
with this, we determine which characters will be used to mark the beginning and end of the commands in the print string in the future, as well as determine whether the hardware letter-image creation engine generates software (32) or hardware (33) images.
Let's create fonts!
There are two options. When defining a letter with a color, or when with an image-texture.
-------------------------------------------------- -------------------------------------------------- ------------------------------
We create a font with 'm_fontadd' - color
for example
m_fontadd 0, "youngfrankexpand.ttf", _RGBA32(238, 194, 194, 255), _RGBA32(255, 0, 0, 100), 10, 150, 1, 1
parameters in order:
1.index
2.ttf file location
3. letter color (feel free to use the alpha value if you want a transparent letter)
The color of the shadow of letter 4 (if you don't want a shadow, then alpha should be 0, or simply write 0 here)
Shifting the shadow of the 5th letter. moves the shadow to the right and down relative to the letter by that many pixels
6. letter size/quality. here we define the quality of the hardware creation of the letter. The height of the image where the letter image is created.
7. is the multiplier of the horizontal size of the letter. by default 1, then we get the look of the original font. The letter can be stretched (eg 1.1 or more) or compressed (0.9 or less).
8. letter spacing. horizontal spread of letter spacing. 1 if we want the natural appearance. You can stretch the letter spacing with gaps (for example, 1.1 or more) or compress them (0.9 or less).
-------------------------------------------------- -------------------------------------------------- ------------------------------------------
'm_fontaddpic' - we create a font with an image texture
for example
m_fontaddpic 2, "youngfrankexpand.ttf", qb64logo, 255, 5, _RGBA32(0, 0, 0, 180), 10, 200, 2, 1
parameters:
1.index
2.ttf file location
3.'handle', a software (32) existing image, for example 'qb64logo = _LoadImage("qb64logo.jpg", 32)'
4. the alpha value of the image
5. the image size multiplier. Inserts a mosaic image from the image into the letter. The higher the number, the smaller the texture will be.
6. The color of the shadow (if we don't want a shadow, then alpha should be 0, or simply write 0 here)
7. Shifting the shadow of letter 7. moves the shadow to the right and down relative to the letter by that many pixels
8. letter size/quality. here we define the quality of the hardware creation of the letter. The height of the image where the letter image is created.
9. is the multiplier of the horizontal size of the letter. by default 1, then we get the look of the original font. The letter can be stretched (eg 1.1 or more) or compressed (0.9 or less).
10. letter spacing. horizontal spread of letter spacing. 1 if we want the natural appearance. You can stretch the letter spacing with gaps (for example, 1.1 or more) or compress them (0.9 or less).
With this system, we can define any number of fonts.
Use, display
-------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -----
Any placement
m_printstring 1, 200, "<fh80><fi0>mid<fh30><fi1>mini<fh200>BIG", 1, 0
parameters:
1.X position
2.Y position
3.TEXT-STRING-COMMAND
4. rotation 0=from the left edge of the image 1=from the center of the image 2=from the right edge of the image
5. magnitude of rotation. A value of 0 means no rotation
-------------------------------------------------- -------------------------------------------------- -------------------------------------------------- ----
Placement in the horizontal center of the screen
m_printstring_center 300, "<fi1><fh80>welcome<fh50> here (Center)", 0, 0
parameters:
1.Y position
2.TEXT-STRING-COMMAND
3. rotation from where? 0/1/2 (see above)
4. amount of rotation (see above)
-------------------------------------------------- -------------------------------------------------- -------------------------------------------------- ---
Aligns to the right side of the screen
m_printstring_right 700, "<fi2><fh100>QB64 logo", 20, 1, .1
parameters:
1. Y position
2. TEXT-STRING-COMMAND
3. margin - leaves this many pixels near the right edge of the screen
4. rotation from where? 0/1/2 (see above)
5. amount of rotation (see above)
-------------------------------------------------- -------------------------------------------------- -------------------------------------------------- ------
TEXT-STRING-COMMAND
In this string, we can pass the text to be displayed and the commands.
Commands: if possible, indicate at the beginning of the string which font and which size we want to print!
<fh80> - Switch to font 80! (fh = font height)
<fi1> - Switch to the font with index 1! (fi=pound index)
The texts to be displayed are placed in the section between the commands.
Example:
m_printstring 1, 200, "<fh80><fi0>mid<fh30><fi1>mini<fh200>BIG", 1, .2
What this command does is display this in column 1, line 200 of the screen:
Change to pixel size 80, change to font index 0 and write 'mid', then change to pixel size 30 and change to font index 1 and write 'mini', then change 200- as pixel size and write 'BIG', and rotate all of this from the center of the text to 0.2 radian degrees!
-------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -----
When we have defined a TEXT-STRING-COMMAND, we have the option of querying its size.
x$ = "<fh80><fi0>mid<fh30><fi1>mini<fh200>BIG"
pixelsize length = m_printwhidht (x$)
pixelSize height = m_printheight (x$)
The height naturally returns the size of the largest font size used.
-------------------------------------------------- -------------------------------------------------- ------------------------------
some thoughts about the system:
-when you create a font, the program stores its characteristics. Later, when we need to display a character, it checks to see if you have already taken a hardware image of it. If you don't have it yet, make it. This is good because it saves memory. It only creates a character when we use it. For example, when displaying the text 'Hello', it takes a hardware image only 4 times. The letter 'l' is only read out for the second time. This solution is very fast and saves memory.
-When we use a TEXT-STRING-COMMAND, any display subroutine or size query function (m_printwidth,m_printheight) is forced to assemble, prepare, build for the location of the characters. If the same TEXT-STRING-COMMAND is used several times, the program will not calculate it again. Lots of time saved.
- Important! when we enter a coordinate, it does not indicate the upper left corner of the text (like _printstring) !!! Each coordinate point starts from the lower left corner of the text!