Load ttf Font as a resource not working

Just starting out? Need help? Post your questions and find answers here.
Pot Noodle
Enthusiast
Enthusiast
Posts: 202
Joined: Sat Feb 18, 2012 10:21 pm
Location: Leicestershire

Load ttf Font as a resource not working

Post by Pot Noodle »

Hi all,
I am trying to load a true type font as a resource in my game with,

Code: Select all

  OpenLibrary(0,"gdi32.dll")
  CallFunction(0,"AddFontMemResourceEx",?Font,?End_Font-?Font,0,@"1")
  CloseLibrary(0)
  LoadFont(0,"SFDigitalReadout-HeavyObliq",16)

;=========================================================
   DrawingFont(FontID(0))
   DrawText(530,320,"TEST")
   DrawingFont(#PB_Default)
;=========================================================

 DataSection;Fonts
   Font:   :IncludeBinary "Fonts\SFDigitalReadout-HeavyObliq.ttf"
   End_Font:
 EndDataSection
This is the basic's of it but it changes the font size but not the font :?
Any input would be most appreciated.
Last edited by Pot Noodle on Mon Sep 30, 2013 3:21 pm, edited 2 times in total.
P.N.
User avatar
DK_PETER
Addict
Addict
Posts: 904
Joined: Sat Feb 19, 2011 10:06 am
Location: Denmark
Contact:

Re: Load Game Font

Post by DK_PETER »

Hey PN

Normally you use a sheet with predefined characters. That way you're not limited to the fonts available
on a user's system

You can create font sheets with this: http://www.purebasic.fr/english/viewtop ... 27&t=51675

Best regards
Peter
Current configurations:
Ubuntu 20.04/64 bit - Window 10 64 bit
Intel 6800K, GeForce Gtx 1060, 32 gb ram.
Amd Ryzen 9 5950X, GeForce 3070, 128 gb ram.
Pot Noodle
Enthusiast
Enthusiast
Posts: 202
Joined: Sat Feb 18, 2012 10:21 pm
Location: Leicestershire

Re: Load Game Font

Post by Pot Noodle »

That way you're not limited to the fonts available on a user's system
Thats why i want to imbed the font in to the game.
Yes i like your idea but this is fine for a demo or game that requires glossy graphical fonts
but my game just use's a .ttf font for one number display so i think it's bit overkill for this job
but thanks anyways DK_PETER.
P.N.
User avatar
DK_PETER
Addict
Addict
Posts: 904
Joined: Sat Feb 19, 2011 10:06 am
Location: Denmark
Contact:

Re: Load ttf Font as a resource not working

Post by DK_PETER »

@PN

Anytime.
The Texture2Font can create very simple sheets too..

Like this:
image removed

Data text file for the above image:

Code: Select all

;Numbers of characters: 10
0123456789
;Image size (x*y): 240 * 57
;Image Offset width:  
24
;Image Offset height: 
57
All you need to do is to define the characters to use and pluck out the data you need from the data file.
Last edited by DK_PETER on Sun Aug 10, 2014 5:22 am, edited 1 time in total.
Current configurations:
Ubuntu 20.04/64 bit - Window 10 64 bit
Intel 6800K, GeForce Gtx 1060, 32 gb ram.
Amd Ryzen 9 5950X, GeForce 3070, 128 gb ram.
marroh
User
User
Posts: 72
Joined: Wed Aug 06, 2008 8:21 am

Re: Load ttf Font as a resource not working

Post by marroh »

Works on Windows 7 x86, Windows 8 x64

Code: Select all

EnableExplicit

Procedure CatchFont(*Memory, lMemSize)
  Protected lParam1 = 1
  ProcedureReturn AddFontMemResourceEx_(*Memory, lMemSize, 0, @lParam1)
EndProcedure
Procedure FreeCatchFont(lMemFont)
  ProcedureReturn RemoveFontMemResourceEx_(lMemFont)
EndProcedure

Procedure Main()
  Protected lMemFont = CatchFont(?cf1s, ?cf1e - ?cf1s)
  Protected lGuiFont = LoadFont(#PB_Any, "DOSEGA", 16) ; <<< Customize
  If lMemFont And lGuiFont
    If OpenWindow(0, #PB_Ignore, #PB_Ignore, 440, 80,"CatchFont Demo", #PB_Window_SystemMenu)
      SetGadgetFont(#PB_Default, FontID(lGuiFont))
      TextGadget(0,10,10,400,20,"CatchFont Demo")
      Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
      CloseWindow(0)
    Else
      MessageRequester("CatchFont Demo Error", "Couldn't open main window.", #MB_OK | #MB_ICONERROR)
    EndIf
  Else
    MessageRequester("CatchFont Demo Error", "Couldn't load font from memory.", #MB_OK | #MB_ICONERROR)
  EndIf
  FreeCatchFont(lMemFont)
  FreeFont(lGuiFont)
EndProcedure

Main() : End

DataSection
  cf1s:
  IncludeBinary "DOSEGA.ttf" ; <<< Customize
  cf1e:
EndDataSection
PureBASIC v5.41 LTS , Windows v8.1 x64
Forget UNICODE - Keep it BASIC !
Pot Noodle
Enthusiast
Enthusiast
Posts: 202
Joined: Sat Feb 18, 2012 10:21 pm
Location: Leicestershire

Re: Load ttf Font as a resource not working

Post by Pot Noodle »

Thanks marroh this works just fine.

Don't know why my code would not work but never mind it's all good now

Thanks again for your help. :D
P.N.
Post Reply