GLBasic forum

Codesnippets => Code Snippets => Topic started by: PeeJay on 2008-Feb-02

Title: Bitmapped Font Functions - Everything but the kitchen sink!
Post by: PeeJay on 2008-Feb-02
Okay, I've been having a play today, and have written a set of functions for manipulating bitmapped fonts, as follows:-

Draw Text (with or without kerning)
Centralise Text (with or without kerning)
Right Justify Text (with or without kerning)

Word Wrap Text (with or without kerning)
Word Wrap and Centralise Text (with or without kerning)
Word Wrap and Right Justify Text (with or without kerning)

Full Justification of Text (kerned only - without kerning would be too ugly!)

All of these can show text at any position on the screen, use any point to justify to, and use any width for word wrap.

As it's a bit big (since I have split everything into separate functions so you can pick and choose what you want to use) I have upped it to zshare - http://www.zshare.net/download/7127057ae41080/ - complete with a load of demo code and a font so you can see it in action.
Title: Bitmapped Font Functions - Everything but the kitchen sink!
Post by: D2O on 2008-Feb-02
:booze:  :good:  :nw:
Title: Bitmapped Font Functions - Everything but the kitchen sink!
Post by: AndyH on 2008-Feb-02
Sounds ace Peejay - What would be really cool would be a font drawing routing using POLYVECTOR to draw the text instead.  Then you could add a colour blend parameter.

One of the strong features of Max are some commands that modify all subsequent drawing commands.  SETCOLOR is one of them, set it to WHITE and what you draw comes out at normal pixel colours.  Set at any other RGB value and it is masked, so SETCOLOR 255,0,0 will tint everything you draw red.  This applies to displaying text too.  I think GLB could benefit from this too, and could be possible to do with POLYVECTOR which takes a colour parameter for each corner.  BTW, BMax also has SetBlend, SetAlpha, SetScale, SetRotation and SetHandle which I use a lot.  Would be nice to have some of these in GLB too, which I think could be mostly possible (the exception being the differences between GLB's ALPHAMODE command and BMax's SetAlpha/SetBlend combination).
Title: Bitmapped Font Functions - Everything but the kitchen sink!
Post by: Kitty Hello on 2008-Feb-03
Polyvector-Print:
http://www.glbasic.com/forum/viewtopic.php?id=244
Title: Bitmapped Font Functions - Everything but the kitchen sink!
Post by: Moru on 2008-Apr-22
I knew I should have downloaded it when I saw it first, now it's gone. Any chance of uploading it again to some better host that will actually keep it online for a while?
Title: Bitmapped Font Functions - Everything but the kitchen sink!
Post by: PeeJay on 2008-Apr-22
Never let it be said that I don't do requests! You can download it from //www.peejays-remakes.co.uk/Bitmap%20Font%20Routines.zip - and believe me, it is well worth the download (there is a simple demo included to show what it can do)
Title: Bitmapped Font Functions - Everything but the kitchen sink!
Post by: Moru on 2008-Apr-22
Thankyou very much, this is what I will want later, was just too lazy to write it yet again after doing it once in STOS basic... :-)
Title: Bitmapped Font Functions - Everything but the kitchen sink!
Post by: PeeJay on 2008-Apr-22
No problem. If you want to adapt it to use PolyVector (thus allowing colour effects), it is a simple edit in the DrawText() function - this is the only function that actually draws the characters to the screen
Title: Bitmapped Font Functions - Everything but the kitchen sink!
Post by: Moru on 2008-Apr-22
So it calculates the kerning live on the first load? My program took 10-20 minutes for calculating kerning... poor old 8 MHz Atari with no direct access to pixels :-)
Title: Bitmapped Font Functions - Everything but the kitchen sink!
Post by: Kitty Hello on 2008-Apr-23
Save it to a seperate file and load from there then.
8MHz? How much ram? 64k?
Title: Bitmapped Font Functions - Everything but the kitchen sink!
Post by: Moru on 2008-Apr-23
512 kB but program was run in STOS Basic. The screen was interleaved bitmap so accessing individual pixels was not a fast task. The delay in making the kerning table is not very noticable in PeeJays code so not a problem :-)

Btw, your code is very easy to read and understand, PeeJay. And plenty of nice comments :-)

Edit: Never mind the question, stupid typo :-)
Title: Bitmapped Font Functions - Everything but the kitchen sink!
Post by: PeeJay on 2008-Apr-23
Hehe, I don't usually code like that - comments are a luxury I only bother including when sharing code, or else it just means more typing! :D

Of course, you can always save the kerning information in a separate file (or now, store them in DATA statements) but I chose to use that method since it means it will work with any bitmapped fonts with minimal changes (just the font width and height variables)
Title: Bitmapped Font Functions - Everything but the kitchen sink!
Post by: Moru on 2008-Apr-23
Happily contributing back :-)
New version with POLYVECTOR support, hope I got it right :-)

http://gamecorner.110mb.com/games/bitmap_font_routines.zip (http://gamecorner.110mb.com/games/bitmap_font_routines.zip)
Title: Bitmapped Font Functions - Everything but the kitchen sink!
Post by: PeeJay on 2008-Apr-24
Ooooh, looks good to me! I think the only thing I would have done different would have been to pass colours to the PolyVector function, so that you could draw in any colours you like, rather than having them hard coded, but again, that's a simple edit for whoever wants to use the code.

I always try to bear in mind that coding is about trying to make code more and more idiot proof, while the universe is trying to make more and more idiots. So far, the universe is winning :D
Title: Bitmapped Font Functions - Everything but the kitchen sink!
Post by: Kitty Hello on 2008-Apr-24
LOL! Indeed. Dad of a friend is a teacher for computer classes. He once said, that the easier the computers are to use, the more people act dumb. In 80s you could teach BASIC or PASCAL at school. Today you can be happy to teach Wordpad.
Title: Bitmapped Font Functions - Everything but the kitchen sink!
Post by: Kitty Hello on 2008-Apr-24
LOL! Indeed. Dad of a friend is a teacher for computer classes. He once said, that the easier the computers are to use, the more people act dumb. In 80s you could teach BASIC or PASCAL at school. Today you can be happy to teach Wordpad.
Title: Bitmapped Font Functions - Everything but the kitchen sink!
Post by: Moru on 2008-Apr-24
I wasn't sure how to do the colours so they would be setable in an easy fashion. Now after a good nights sleep I figured I'll just make a global array holding all the corners values and two functions to easier set the colors. Also added a function for storing a color and restoring it again. Can be rewritten to store many colors and just access them with a word or a number if one feels like it :-)

Forgot to mention that for full color-freedom you need a font that is white, otherwise you can't set some colors because they will just filter away the colors not needed and leaving you with black :-)
For example a red font will not be possible to make blue, if I got it right.
http://gamecorner.110mb.com/games/bitmap_font_routines.zip (http://gamecorner.110mb.com/games/bitmap_font_routines.zip)

...wordpad, what is that? :-)
Title: Bitmapped Font Functions - Everything but the kitchen sink!
Post by: Moru on 2008-Apr-28
Is it possible to see the ASCII code of characters above ascii 127? If I use asc() I get negative numbers when I try to convert Ã...ÄÖ. And yes I know that ascii only covers the first 127 characters but what am I supposed to use for the rest of the swedish and other foreign characters?
Title: Bitmapped Font Functions - Everything but the kitchen sink!
Post by: Kitty Hello on 2008-Apr-28
They are mapped to signed char.
so,
Code (glbasic) Select
a = ASC(i)
if a<0 then a=256-a
should give you what you need.
Is that stupid? I'll better change that, so it returns positive numbers in the first place, OK?
Title: Bitmapped Font Functions - Everything but the kitchen sink!
Post by: Moru on 2008-Apr-28
Sounds good to me, signed char isn't very useful in this case :-)
Title: Bitmapped Font Functions - Everything but the kitchen sink!
Post by: Kitty Hello on 2008-Apr-28
Update online.
Title: Bitmapped Font Functions - Everything but the kitchen sink!
Post by: Moru on 2008-Apr-28
Great job Gernot thanks! Now we can use swedish characters too, and the standard fonts from the fontcreator whithout need of creating a specially formated font file.

I also fixed a bug with the unkerned printing, When you put an "i" and a "w" next to eachothers they looked welded together.

No more excuses for not printing with proportional fonts! I also tried it on our GP2X and it ran just fine, no visible slowdowns to calculate the kerntables even though it has to do double the work now.

The source file contains full examples with comments, it's very easy to learn how to use this code.

Proportional bitmap fonts in GL-Basic (http://gamecorner.110mb.com/games/bitmap_font_routines.zip)
Title: Bitmapped Font Functions - Everything but the kitchen sink!
Post by: Kitty Hello on 2008-Apr-28
Aces! Thanks.
Title: Bitmapped Font Functions - Everything but the kitchen sink!
Post by: PeeJay on 2008-Apr-28
If only my games were played with as long ..... :D

Great job
Title: Re: Bitmapped Font Functions - Everything but the kitchen sink!
Post by: Moru on 2008-Aug-08
Added shadow support (Thanks Hemlos for the idea :-)
this code is so easy to work that it took me 45 minutes to add support for shadows including the time it took to rewrite all the examples to use shadows. Thanks again, PeeJay for doing the hard work and making it free for us to use!
Title: Re: Bitmapped Font Functions - Everything but the kitchen sink!
Post by: Moru on 2008-Aug-09
The font is changeable with the built-in font editor of GL-Basic, that's why it takes a bit to load but on my computer and my GP2X it's more like 2 seconds, why is it taking 20 seconds for you?

Transparent mode should be adjusted with just putting in ALPHAMODE before you draw the font. You can also put in alphachannel with photoshop in the font if you want.

Do you have a baby? Do you even know what work-load is? :-)
Title: Re: Bitmapped Font Functions - Everything but the kitchen sink!
Post by: Hemlos on 2008-Aug-09
Quote from: Moru on 2008-Aug-09

Do you have a baby? Do you even know what work-load is? :-)

Do i ever...try teenagers.

Cool love the Schnipsel
Title: Re: Bitmapped Font Functions - Everything but the kitchen sink!
Post by: PeeJay on 2008-Aug-09
The program "stalls" at startup while it precalculates the kerning for the font - this could be exported to an external file to eliminate this, but it is currently done that way to work with ANY bitmapped font. If it's taking more than a couple of seconds, though, there's something not right somewhere!

Of course the font can be changed - simply load in an alternative font, and change the fontwidth and fontheight values.

At some point when I get bored I will revisit this set of routines and expand on them, as it seems a lot of people are finding them pretty useful (there's an understatement!) and now GL supports default parameters for function calls, it means I can make tweaks I couldn't do before (well, not if I wanted to keep the code really tidy!)

Title: Re: Bitmapped Font Functions - Everything but the kitchen sink!
Post by: Moru on 2008-Aug-09
I have been working on a precalculating routine but it seems the most time is taken from grabbing the sprites from the bitmap. Precalculation didn't do much for speed increase. On the other thand I'm running two 3D games in the background while testing this code and I'm still not getting any lower speed than 2-5 seconds

Marquee is almost done, just a logical bug that I need some peace to think thru.
Title: Re: Bitmapped Font Functions - Everything but the kitchen sink!
Post by: PeeJay on 2008-Aug-09
You should see a massive speed increase (to basically instantaneous!) using an external file for precalculation.

The kerning is simply calculating the width of each character, so by writing a separate routine, you can calculate these values, then save them out into a separate file. Then, in the main program, load this file in and put the values into the array - no need to calculate again.

This is one of the amends I have been planning, as well as adding the ability to use multiple character sets of multiple sizes
Title: Re: Bitmapped Font Functions - Everything but the kitchen sink!
Post by: Moru on 2008-Aug-09
You where right, speed improved vastly even from reading from a file, now it's taking about 2% of the old time to load. What would be better for future improvements and multiple fonts, data statements or binary files to save the kern data?

Updated the code, download either from my homepage or the link earlier in the thread.
Title: Re: Bitmapped Font Functions - Everything but the kitchen sink!
Post by: PeeJay on 2008-Aug-09
Data statements would be neater, as they would be hard coded into the project, and would save having extra files - however, it would mean that you could not write routines to use within any project, as each one would need it's own data coded in, whereas using a separate file means that another program could generate them in advance.

However, with that said, with a little careful coding, it would be possible to export the data into a .gbas named file with DATA statements - hmmm, that is giving me a few ideas ...... ;)
Title: Re: Bitmapped Font Functions - Everything but the kitchen sink!
Post by: Moru on 2008-Aug-09
My code is generating both data statements and binary files but I don't know how to dynamicly access a certain data statement. We need that if we want several fonts I think?
Title: Re: Bitmapped Font Functions - Everything but the kitchen sink!
Post by: PeeJay on 2008-Aug-09
Simple - while creating the data statements for each font, also add in a point for the restore command to refer to - then you can switch fonts in a fraction of a second. Alternatively, read all the data in one go, and store it in a 2D array - kern[font,character]
Title: Re: Bitmapped Font Functions - Everything but the kitchen sink!
Post by: Moru on 2008-Aug-09
What I meant was, can I have restore points that the user can choose ReadKern("my_font",5) or something similar. No I guess not. I will look into making it one big array later when I have time :-)
Title: Re: Bitmapped Font Functions - Everything but the kitchen sink!
Post by: PeeJay on 2008-Aug-10
I'm still thinking about my original code (to be honest, I haven't even looked at the revamped version, apart from the very first one where PV was used for colouring), and indeed, if I do look at doing a modification, I'll be using my old code and starting again, so we could end up with 2 different solutions to achieve the same thing (which is no bad thing!)

As for the inline DATA statements, yes, you can create restore points (just as I did for Manic Miner level data) thus:-

Code (glbasic) Select
STARTDATA KERN01
DATA .............
ENDDATA

STARTDATA KERN02
DATA ..........
ENDDATA


Unfortunately, as yet, GL doesn't support calculated restore points, so in the main code, you would need a SELECT / CASE system (again, as I used in ManicMiner) thus:-

Code (glbasic) Select
FUNCTION GetKern(fontnum)
SELECT fontnum
CASE 1; RESTORE KERN01
CASE 2; RESTORE KERN02
ENDSELECT

//now read the data for one font

ENDFUNCTION


However, I suspect I would be more likely to use the idea of reading all the data into one 2D array, since the chances of wanting more than 2 or 3 different fonts is highly unlikely (or whatever it is you are working on will just look a total mess!)
Title: Re: Bitmapped Font Functions - Everything but the kitchen sink!
Post by: PeeJay on 2008-Aug-10
1) The original links are dead - but I intend on adding a load of useful routines to my webby at some point. I had to stop direct linking to files from other websites, as I was being leeched to death! I will sort that out when I update the font routines (promise!)

2) Mine will work fine with arrays, for one simple reason - each character is only using one byte. So, for the 96 characters I was using, even if you need as many as 5 separate fonts (which is highly unlikely!), the array would still only be [5,96]. The biggest drain of resources will be having the graphics for the fonts preloaded, but that is a necessary evil, or you will be constantly making calls to the hard drive.

3) Since my solution was using bitmap images, you can use windoze fonts, though I would recommend doing it manually in a paint package, since the code relies on the characters being in a particular position within the image strip for the kern to work correctly.

I saw your font routines, but was rather concerned about the sheer amount of data you have - presumably you are storing the font pixel by pixel, which is great for the sparkly effect, but not so great for flexibility when it comes to using different fonts!

I have a WIP I want to finish before I look at revamping the font routines, so you're going to have to bear with me :)
Title: Re: Bitmapped Font Functions - Everything but the kitchen sink!
Post by: Moru on 2008-Aug-10
I just finnished the multi-font package, download at my homepage and come with suggestions. I would like to make the calling of the functions easier but I'm not sure if I should create extra functions that accept a type instead of lots of parameters, and then call the normal functions from there? What do others think?

Something like this mabe?
Code (glbasic) Select

font.color = rgb(255,255,0)
font.shadow = 2
font.shadowalpha = -.4

oDrawText("Testing", font)

function oDrawText: txt$, font as FONT
    DrawText(txt$, font.color, font.shadow, font.shadowalpha)
endfunction


Also, does anyone know why I can't have the color black when I draw the polyvectors? It just becomes transparent. Is that mabe because I'm using black as transparent color? I thought that was handled before you draw the texture?
Title: Re: Bitmapped Font Functions - Everything but the kitchen sink!
Post by: Kitty Hello on 2008-Aug-11
how about wrapping it in functions. That's the way all GLBasic functions work:

FontSetColor(font_index, RGB(0,0,0) )
FontSetShadow(font_index, TRUE, alpha)
FontDrawText("text", font_index)
Title: Re: Bitmapped Font Functions - Everything but the kitchen sink!
Post by: Moru on 2008-Aug-11
thats how we do it now, it's just that some functions ends up with more paramenters than others and after 14 it starts to be tricky to say the least :-)
Title: Re: Bitmapped Font Functions - Everything but the kitchen sink!
Post by: Kitty Hello on 2008-Aug-11
No, you must split such functions into several functions now. Try to find a partial group that makes sense.
Title: Re: Bitmapped Font Functions - Everything but the kitchen sink!
Post by: PeeJay on 2008-Aug-12
Just so you know, I have started tinkering, and have written the first program, which is the program to generate all the data into a nice gbas file ready for copy and pasting, or simply adding to your project. I have tried to make it as open ended as possible so it can deal with any number of fonts that you throw at it (though it will need some manual editing to tell the program how many fonts, and the character sizes of each font.)  No code to look at yet, though - you're going to have to be patient! :D
Title: Re: Bitmapped Font Functions - Everything but the kitchen sink!
Post by: Moru on 2008-Aug-12
Ok, I guess I create another thread so not to confuse our versions of the same code :-)
Title: Re: Bitmapped Font Functions - Everything but the kitchen sink!
Post by: Hemlos on 2008-Oct-12
Hi Peejay,
Im interested if youre doing more with your library?
This font library you made is really good stuff, and well thought...I Thought you might find interesting..Gernot added the BYREF option, for passing data in/out of functions now...you can pass data around like a football!

Hows your WIP coming along? Is it for GLBasic or the internet?
Title: Re: Bitmapped Font Functions - Everything but the kitchen sink!
Post by: PeeJay on 2008-Nov-05
I did make a good start on revamping the library, but then after a bit of a disagreement with another site, I haven't done any coding for a while. I'm sure I'll get back to it at some point though .....
Title: Re: Bitmapped Font Functions - Everything but the kitchen sink!
Post by: Ian Price on 2008-Nov-05
Hiya PeeJay. :D How are things?
Title: Re: Bitmapped Font Functions - Everything but the kitchen sink!
Post by: PeeJay on 2008-Nov-06
Ooooh, not too bad, thanks - to be honest, I've been quite glad of the break, as I was on a major coding hit - I mean, since switching to GLB last December, there was been 3 full games written, one nearly finished, and loads of little odds and sods, so I guess I deserve a holiday really!

I have been keeping half an eye on this place, but as I haven't fired up GL Basic for several weeks, I have to admit I haven't kept up with all the new developments, and the little extras that Gernot has been adding in. I'm sure I'll get round to hitting the coding buttons again before too long tho!  ;/
Title: Re: Bitmapped Font Functions - Everything but the kitchen sink!
Post by: Ian Price on 2008-Nov-06
Cool. Any game ideas starting to form?
Title: Re: Bitmapped Font Functions - Everything but the kitchen sink!
Post by: PeeJay on 2008-Nov-06
Oh yeah - I had a few ideas before I had my break anyway, but, to be honest, TriTangle will very likely be my last game as such. Given I am shit at graphics, and getting them done with contacts at RR was difficult enough - since I have parted company with that site, then then is absolutely no chance of getting any spriting done.
Title: Re: Bitmapped Font Functions - Everything but the kitchen sink!
Post by: Ian Price on 2008-Nov-06
If you have some small games in mind (perhaps suitable for GP2X, Wiz, Pandora rather than pc) then I could perhaps give yuo a hand? I work best at low res. and cartoon stylee - see my Shark game WIP for examples.
Title: Re: Bitmapped Font Functions - Everything but the kitchen sink!
Post by: PeeJay on 2008-Nov-06
Thanks for the offer, but since I only have a PC (and a DS), then coding for a GP2X seems a bit, er, pointless! :) Oddly enough (and don't laugh at this), one of the things I had been mulling over (and was even considering for the RR compo before the furore) was doing a remake of - are you ready for this - Vu-File! As databases go, it was perfect for what most people would ever need; it was a doddle to use, freeform, and you could design nice pretty index cards......
Title: Re: Bitmapped Font Functions - Everything but the kitchen sink!
Post by: Ian Price on 2008-Nov-06
I meant more the console type of resolution rather than anything bigger than 640x480 (unless scaled).

I can't say I've ever seen or used Vu-File,. I think the last database app. I used was DBase4 about 20 years ago!

Creating apps can be good too though - I recently did MaxANSI and started working on MaxMapper and enjoyed doing something other than games for a short time.