Redefining internal font (a.k.a. custom characters) in graphic mode

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
angros47
Posts: 2321
Joined: Jun 21, 2005 19:04

Redefining internal font (a.k.a. custom characters) in graphic mode

Post by angros47 »

I found this code: viewtopic.php?f=7&t=20766 and I realized that it is possible to access the internal font used in the graphic mode. So, I wrote this:

Code: Select all

type Font
  w as long
  h as long
  d as any ptr
end type

enum
  FB_FONT_8 = 0,
  FB_FONT_14
  FB_FONT_16
end enum

extern Fonts(2)  alias "__fb_font"  as Font


sub EditChar cdecl (byref f as Font, c as ubyte, ...)
	Dim As cva_list args
	cva_start(args,c)
	dim as long y,yend,code
	dim row as ubyte ptr
	yend=f.h

	code=c:code*=f.h:row=f.d+code
	for y = 1 to yend
		*row=cva_arg(args, ubyte)
		row+=1
	next

end sub

'screenres 640,480 ',32
screen 12
editchar (Fonts(FB_FONT_16),asc("e"), 60, 66, 66, 129, 129, 66, 24, 24, 126, 24, 24, 24, 36, 36, 195)

dim a as string
print "Type something:"
input a
The program redefines the character "e", replacing it with a stick man. The approach is similar to the one used on real hardware (I used the same data set used here viewtopic.php?t=12168): the character has a color depth of one bit, and since each line is made by 8 pixels, the character is defined as a sequence of 8, 14 or 16 bytes

This approach is not related with the custom fonts that can be used with DRAW STRING. The main differences are:
1) Using DRAW STRING doesn't affect the character attribute map: so it's impossible to check the character later with the SCREEN function
2) Custom fonts used by DRAW STRING must be in the same format of the screen mode they are used on (if they are created in indexed 8 bit screen mode they can't be used on 32 bit real color mode, for example). This approach, instead, works on any screen mode
3) DRAW doesn't control scrolling. PRINT does. Also, as shown in the example, this approach allows to use custom fonts with INPUT as well
4) Fonts used with DRAW STRING contain color information in the font itself, so it is not possible to use the same font in different colors, you would need a new font for each color you want to use. The approach I used, instead, allows to use the COLOR command normally

I think this trick could be useful to port some basic programs written for old home computers (Commodore 64, Vic 20, Sinclair Spectrum, Amstrad CPC), since they often used character redefinition (as far as I know they all used 8x8 characters).
Last edited by angros47 on Jul 24, 2021 14:38, edited 1 time in total.
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Redefining internal font (a.k.a. custom characters) in graphic mode

Post by dodicat »

You missed the cdecl bit on the EditChar .
It is required by version 1.08 anyway on windows.
I like your selections of short code demos you post these days.
angros47
Posts: 2321
Joined: Jun 21, 2005 19:04

Re: Redefining internal font (a.k.a. custom characters) in graphic mode

Post by angros47 »

Thank you, fixed!
Post Reply