PDA

View Full Version : Text output pixel by pixel 8x8



Cybermonkey
30-10-2013, 02:14 PM
Hi, I wanted to hear what your suggestions are regarding the problem of text output if no font is available. I thought of an array which looks like this (e.g. A):


{0,0,0,0,0,0,0,0,
0,0,0,1,1,0,0,0,
0,0,1,0,0,1,0,0,
0,1,0,0,0,0,1,0,
0,1,1,1,1,1,1,0,
0,1,0,0,0,0,1,0,
0,1,0,0,0,0,1,0,
0,0,0,0,0,0,0,0}

etc. But I think that drawing always 64 pixels per character is a bit too time consuming.
Any ideas?
Ok, I think it will work if I update the screen after the complete text string is written. Since I can save a lot of time I might use the fontdata.inc of the Unit graph from Freepascal.
It starts like this:

{******************************************}
{ Bitmapped font data - unrolled for }
{ faster access. Each character is an }
{ 8x8 byte array representing the full }
{ bitmap. 0 = nothing, 1 = color }
{******************************************}


TYPE
TBitMapChar = array[0..7,0..7] of byte;

CONST
DefaultFontData: Array[#0..#255] of TBitmapChar = (
(
(0,0,0,0,0,0,0,0),
(0,0,0,0,0,0,0,0),
(0,0,0,0,0,0,0,0),
(0,0,0,0,0,0,0,0),
(0,0,0,0,0,0,0,0),
(0,0,0,0,0,0,0,0),
(0,0,0,0,0,0,0,0),
(0,0,0,0,0,0,0,0)),
...

So I can access the individual pixels via

DefaultFontData[char][x][y];

Super Vegeta
30-10-2013, 05:19 PM
Updating the screen takes a hellload of time. It's best to update after you're done, and - if your graph API allows it - only the parts that change. Since we're talking about low-res, I guess, if your characters are square enough, you could try converting the 8x8 map into a list of lines to draw from point ax,ay to bx,by. But I think nothing more than that.

AthenaOfDelphi
30-10-2013, 07:34 PM
I'm assuming it's an OpenGL based output mechanism. One option could be to render a texture from the data stored in your app so you effectively build a big in-memory image containing all the characters you have font data for in a nicely positioned location. And then simply use that as the source of textures for a series of polygons that are drawn when you want to draw the text.

Cybermonkey
30-10-2013, 09:04 PM
Oh, sorry, I had to tell you that I am using SDL2 for this task.

SilverWarior
31-10-2013, 12:24 PM
As Athena already pointed out making a predefined images/graphics for various charactes would be the best solution.
To learn more about this process search the web for "Bitmap Fonts".
Main advantage of Bitmap Fonts is that your font characters can actually be made from different colored pixels.
The main disadvantage of Bitmap Fonts is that in order to scale them nicely you would wanna use several diferent sized images for each characters to not loose to much quality by scaling them.

Cybermonkey
31-10-2013, 12:30 PM
Yes, I know about bitmap fonts and will implement some functions for them. But I also want a "built-in" font which needn't to be loaded. So, yes, I want both. ;)

SilverWarior
31-10-2013, 01:27 PM
Pardon me but I don't see why you will even need "built in" font. You can achieve same results using Bitmap Fonts. You can even change fonts bitmap pixel bx pixel if that is what you need.
Also wouldn't the use of "built in" font require you to render it pixel bx pixel which is much slower than rendering it as a whole graphics?

Cybermonkey
31-10-2013, 01:48 PM
Two reasons: first one needn't ship any external bitmap files. Second I can easily change the size of the font, for e.g. title, score etc. It's just for lazyness if one uses my interpreter so he/she needn't take care of that. Just a "drawtext" and that's it. But no one is forced to use the built-in font but can use his own bitmap font.
In my old version of EGSL I am using SDL_gfx which has a built-in bitmap font and I recognized that a lot of people use it for a simple text output (and even input).

SilverWarior
31-10-2013, 04:25 PM
Two reasons: first one needn't ship any external bitmap files. Second I can easily change the size of the font, for e.g. title, score etc.

1. You can still generate these bitmas programatically at apprication start if you want.
2. Why not use some TrueType font to Bitmap Font converter which would alow you to easily make BitmapFonts of any desired size. And since TrueType Fonts (athleast never versions) are vector based you will get best results when scaling them.

pitfiend
31-10-2013, 05:09 PM
why not embedding the bitmaps for different resolutions fonts? I think that rendering pixel per pixel is just a waste of time. you can even create you beautiful font in any good graphical tool and then convert it to text include files.

Cybermonkey
31-10-2013, 09:52 PM
I have no comparison but I think using the rectangles is fast enough. I made a test with 1000 x "Hello crazy World" with random colours, positions, alphablends and textsizes. The result is (on my machine here): 176 ms.

1218

Solstice Project
01-11-2013, 12:26 AM
Nevermind, the thread tree is confusing me ...

User137
03-11-2013, 12:05 AM
You could use that array to generate a bitmap font, and draw it as quads same way you would use other fonts. If i understood you are drawing each unique pixel per frame, and is something i'd not recommend for performance reasons.

AthenaOfDelphi
03-11-2013, 07:36 PM
My suggestion was to use the data you have to pre-render a bitmap during application startup (i.e. it only ever lives in memory) and then do exactly as User137 is suggesting... simply draw your letters as quads and stick the relevant section of your pre-rendered bitmap to it as the texture.

That's how I'd go about doing it. Clean, relatively efficient and simple.