It is possible to change the color of text displayed in GLBasic?
I am trying the equivalent of this Javascript:
ctx.fillStyle = RGB(255,255,0); // ???????????
ctx.fillText("Presents", 160, 240); // PRINT "Presents", 160, 240
make different fonts in different colours and switch. There's another option to use POLYVECTOR, but it's a bit complicated (search the forums) and might not work on all Android phones (driver issues).
Kitty Hello,
I created 8 text fonts BMP files with different colors.
On windows it works fine, the text switch to the desired SETFONT command.
On the Pre 2 and HP Touchpad it does not work the way it should, the text do not switch.
Here is the code I am using.
LOAD "fonts/APDfont_Red.bmp",0
LOAD "fonts/APDfont_Green.bmp",1
LOAD "fonts/APDfont_Blue.bmp",2
LOAD "fonts/APDfont_Cyan.bmp",3
LOAD "fonts/APDfont_Orange.bmp",4
LOAD "fonts/APDfont_Grey.bmp",5
LOAD "fonts/APDfont_Yellow.bmp",6
LOAD "fonts/APDfont_White.bmp",7
// In the main loop of my game I call a scoreUpdate
SUB scoreUpdate
SETFONT 0
PRINT "hiscore:",40,50
SETFONT 0
PRINT "score:",120,50
SETFONT 0
PRINT "level:",40,50
SETFONT 0
PRINT "life:",40,50
ENDSUB
where is the setfont 1, or setfont 2 to change bitmap?
It can not work even in Windows.
When I posted the code, I typed it and did not copy from the computer because I wrote it from my tablet.
Here is the correct code, I does work in windows but does not work on the TP and Pre 2.
LOADFONT "fonts/APDfont_Red.bmp",0
LOADFONT "fonts/APDfont_Green.bmp",1
LOADFONT "fonts/APDfont_Blue.bmp",2
LOADFONT "fonts/APDfont_Cyan.bmp",3
LOADFONT "fonts/APDfont_Orange.bmp",4
LOADFONT "fonts/APDfont_Grey.bmp",5
LOADFONT "fonts/APDfont_Brown.bmp",6
LOADFONT "fonts/APDfont_Yellow.bmp",7
LOADFONT "fonts/APDfont_White.bmp",8
// ------------------------------------------------------------- //
// --- SCOREUPDATE ---
// ------------------------------------------------------------- //
SUB scoreUpdate:
SETFONT 0 // red
PRINT hiscore,40,50
SETFONT 1 // green
PRINT score,120,50
SETFONT 3 // cyan
PRINT level,200,50
SETFONT 2 // blue
PRINT balls,280,50
SETFONT 7 // white
IF (score > hiscore)
hiscore = score
IF (scoreup)
scoreup = FALSE
IF sound
play(2) //Fame
ENDIF
ENDIF
ENDIF
ENDSUB // SCOREUPDATE
I dont see anything wrong on this code, it has to work.
I have a few apps with many fonts and they work as expected on pre2...
Try saving the fonts as png instead of bmp, I can not thing anything else...
did you checked for the filenames? The must match 100%, big letters is not the same as small letters on other systems than Windows......
Or not setting mediapath$...
Thanks, I will check file names and if that is not the problem I will create the font files in png.
I found the problem!
The file names in the "distribute\WebOS" folder was not the same as the ones in Windows.
It took me a while to realize that fonts, sounds and other files I have to manually copy from the GLBasic folder to the "distribute\WebOS".
:D
Hi
I need this functionality myself quite soon, so I made a little function to do colored printing of a font.
Feel free to improve and expand :)
// --------------------------------- //
// Project: colorfont
// Start: Saturday, September 24, 2011
// IDE Version: 10.113
TYPE TColor
Initialized = FALSE
Name$
ABGR%
Red%
Green%
Blue%
Alpha%
FUNCTION Set: color%, alpha = 255, name$ = "unknown"
self.Red = bAND(color, 0xff)
self.Green = bAND(ASR(color,8), 0xff)
self.Blue = bAND(ASR(color,16), 0xff)
self.Alpha = alpha
self.Name$ = name$
self.ABGR = bOR(RGB(self.Red,self.Green, self.Blue), ASL(self.Alpha, 24))
self.Initialized = TRUE
ENDFUNCTION
ENDTYPE
TYPE TText
lineWidth%
lineHeight%
fontWidth%
fontHeight%
screenId%
spriteId%
originalColor AS TColor
Color AS TColor
FUNCTION Init: org_color% = 0xffffffff, sprite_id% = -1, screen_id% = 31
self.originalColor.Set(org_color)
self.Color.Set(org_color)
GETFONTSIZE self.fontWidth, self.fontHeight
self.lineHeight = self.fontHeight + 1 // Need this TO NOT crash on draw-functions
IF sprite_id = -1
self.spriteId = GENSPRITE()
ELSE
self.spriteId = sprite_id
ENDIF
self.screenId = screen_id
ENDFUNCTION
FUNCTION setColor: color%
self.Color.Set(color%)
ENDFUNCTION
FUNCTION Write: txt$, xpos%, ypos%
LOCAL abgr%[]
self.lineWidth = LEN(txt$) * self.fontWidth
CREATESCREEN self.screenId, self.spriteId, self.lineWidth, self.lineHeight
USESCREEN self.screenId
DRAWRECT 0,0, self.lineWidth, self.lineHeight, RGB(255,0,128)
PRINT txt$ , 0, 0
SPRITE2MEM(abgr[], self.spriteId)
FOR x = 0 TO self.lineWidth * self.fontHeight
IF abgr[x]=self.originalColor.ABGR
abgr[x]=self.Color.ABGR
ENDIF
NEXT
MEM2SPRITE(abgr[], self.spriteId, self.lineWidth, self.lineHeight)
USESCREEN -1
DRAWSPRITE self.spriteId, xpos, ypos
ENDFUNCTION
ENDTYPE
SETSCREEN 320, 240, FALSE
DRAWRECT 0,0, 320,240, RGB(80,80,80)
LOCAL txt AS TText
txt.Init()
txt.setColor(RGB(0,255,255))
txt.Write( "This is a test", 10, 10)
txt.setColor(RGB(255,0,0))
txt.Write( "Another Color :)", 40, 30)
SHOWSCREEN
MOUSEWAIT
You shouldn't need to manually copy stuff to the HP - it should all be in the package...