Is there a way to get the current number which was set using SETFONT #?

Previous topic - Next topic

CW

I am placing the finishing touches on a nifty function which may be useful with many different applications. I'd like to use both a custom directory and a custom font with the function, and than restore the original directory and font when the function exits. The directory is no problem, as we have the GETCURRENTDIR$() & SETCURRENTDIR() commands, but the font has me stumped.

If I use Fontkey% = GENFONT() in my function, is there a way to discover before I call it, from within the function, which font number is currently set so that I can restore it when I exit? Also, is there a way to UNLOAD(Fontkey) and free up the memory occupied by the custom font, which was set aside by the LOADFONT command using Fontkey = GENFONT?

Yes, I could require the programer to pass Fontkey to the function, but that seems so ugly and inelegant. It seems there should be a counterpart to GETCURRENTDIR$() for fonts.

Thanks!
-CW

Marmor

hi,
no getcurrentfont() command yet , u need a extra global parameter
to free your mem use loadsprite "", id

spacefractal

No, you cant.

You can workaround that by loading the font inside a function instead using that command directly, and then hold a static array with name of its and value used.

If you so call that function with illigal number (etc FONTLOAD("myfont.png", -1), then its will return the number of the file instead.

Im doing that a lots for images.
Genius.Greedy Mouse - Karma Miwa - Spot Race - CatchOut - PowerUp Elevation - The beagle Jam - Cave Heroes 2023 - https://spacefractal.itch.io/

CW

Thanks Marmor. That gives me something to work with.

Spacefractal, I'm not sure I follow what you mean by " loading the font inside a function instead using that command directly".

I assume you mean avoiding the LOADFONT command? If so are you talking about loading the font using LOADANIM from within the function, using FRAMEWIDTH and FRAMEHEIGHT equal to the font-width and font-height, and writing a simple word-processor engine around it to displaying each animation cell where they need to be placed to create text? I suppose that would work, but it seems an awful long way to go to get around the problem of not corrupting the original font which was set by the calling program.

If this isn't what you had in mind, please give me a more detailed description on your method for handling it.

Thanks! 
-CW

CW

Ah well, given the state of MY code, who am I to grouse about inelegant solutions?  :)
I'll just suck it up and require that the user pass the current font number so that everything can be left tidy when the function ends.

Thx all.
-CW

CW

P.S.
Marmor, I'm sure, intended to say Use loadfont "", id rather than loadsprite "", id

-CW

Marmor

yap i mean loadfont but  its he same technique for all "load" commands.

Darmakwolf

Well at the top of your program you can define the selected font like this:
global font_selected = 0




then when you change fonts, use this function instead of the normal one..,. then font_selected will always return the selected font ;)
function setfontX(fontID%)
setfont(fontID)
font_selected = fontID
endfunction

CW

Hi Darmakwolf.

Yeah, I thought of that. But I wanted to make the function as independent and stand-alone as possible, for other programers to use, without disturbing the calling program's fonts and other settings, and while keeping everything as encapsulated within the function as possible. Requiring a Global declaration makes imposition on how other programers code (away from the function) and violates the principle of encapsulation. The function would fail to stand alone. In the absence of a way for the function to detect the current font number, requiring that the calling program pass the current font number seems to be the best solution to leave things tidy when the function exits.

Still, I'm glad for the suggestion. Yours is not a bad solution were I the only one using the function, and it is how I would ordinarily handle that. Global variables, like GOTO commands, have their place. This may be one of them.

Cheers!
-CW

bigsofty

Off the top of my head and using Darmakwolf example as a start(and no globals)...

Code (glbasic) Select
FUNCTION setfontX: fontID%=-1
STATIC currFont%
IF fontID% = -1 THEN RETURN currFont%
SETFONT fontID
currFont% = fontID
ENDFUNCTION


So to set a font:-

    setfontX(myFontNum)

and to get the current font:-

    currentFont% = setfontX()

Untested though!  :S
Cheers,

Ian.

"It is practically impossible to teach good programming style to students that have had prior exposure to BASIC.  As potential programmers, they are mentally mutilated beyond hope of regeneration."
(E. W. Dijkstra)

CW

I like that Softy, and you can even use GENFONT() within the setfontX() function and return the latest index to the program. That static keeps everything local and avoids the Global issue.

It solves the problem. But then you have to ask all programers who want to use your main function (whatever that may do) to set all of their fonts through setfontX(), rather than the normal SETFONT function. Over the course of project development, would that become a hassle? How soon till the programer slips up and introduces another bug to be chased down? It's just easier to require that the current font be passed along each time your main function is called; with the compiler throwing an error on the spot and highlighting the line if it is forgotten. But yours is another way we could tackle the problem.
-CW

spacefractal

Im use a lots of those kind of helper functions, and what they should do to overide those sometimes limits.

Also im not even use the internal font system, because its was not flexible enouch (no scaling example, and not using offbuffer for scaling).

The function should do nicely the job done. Eventuelly you could also write a GetFontX() which just call SetFontX() to get the returned value for easier mind, etc:

Code (glbasic) Select

FUNCTION GetFontX:
   RETURN SetFontX()
ENDFUNCTION

Genius.Greedy Mouse - Karma Miwa - Spot Race - CatchOut - PowerUp Elevation - The beagle Jam - Cave Heroes 2023 - https://spacefractal.itch.io/

CW

Maybe what we are talking about is an entire FontX library of functions which include GetFontX(), SetFontX(), UseFontX(), PrintX(), GetWidthFontX(), GetHeightFontX(), ScaleFontX(), ColorFontX(), RotateFontX(), CenterPrintX() and other commands. An entire alternate method of printing to the screen which could bring all of your good ideas together in a single package. A programer who chooses to use the FontX() library would be much less likely to introduce forgetful bugs of the sort I was worried about into his code. FontX functions could be based on sprites of the character set for most of the functions. The trick would be writing an engine fast enough to make FontX a viable option. This may be a project for C++.
-CW

spacefractal

The idea is very good, since my mind the glbasic font system is limited, if you ask me. But its can been workaround, which us what codning is for.

Those commands you talk about require POLYVECTORS (the rotation and colors example). Here you need to been aware of not all systems like colored POLYVECTORS (iPad 1, iPad 3 with iOS6 example).

If source code is included with that such of system and there is bugs in that, either he will fix its, or he will report it. If you put in v0.9 or such, then they are are of those suchs of bugs can exists.

Personly im did my own font system, because im diddent use offbuffer for scaling, but scaled all graphics directly, that include the font. This was because offbuffer scaling was too slow when you use so much graphics as Im did for Greedy Mouse. Howver with lesser graphics, offbuffer is nice, and then you wont have the scaling issue.
Genius.Greedy Mouse - Karma Miwa - Spot Race - CatchOut - PowerUp Elevation - The beagle Jam - Cave Heroes 2023 - https://spacefractal.itch.io/

CW

Did you upload Greedy Mouse somewhere? I'd love to check it out.
-CW