Author Topic: [SOLVED] PngFont Help Last ask before releasing my first demo!  (Read 3341 times)

0 Members and 1 Guest are viewing this topic.

Offline Omnikam

  • Atari ST
  • ***
  • Posts: 121
  • Karma: 4
    • View Profile
I had wanted to know how to adapt the following code to use larger fonts , I figured it out, much trial and error  :carrot:
Update2:
Ok i figured it out,  :updance: but i have a new problem, some of the expected characters are wrong, "H" when it shoulg be "G" etc I dont really understand the "asc(mid" funtion, so i think it will be beyond me to work out why its messing up, I really need help on this one, please  ???
Looking at the font png i noticed that if i use a G ill get the first next line being a H. Q gives me R which is also on the next line, i figure my Png font spacing might be off around the edge, ill look into that as a possable fault

 

Code: [Select]
; Updated to work with PB v4.02 and changed the code to work with DX Sprites indeed Images
; by va!n (thorsten will) in 2007
;
;============================================================================================

Global  xres.w , yres.w , loop.w ,xp.w , yp.w , t.s , tptr.w , sco.b

xres  = 800           ; ScreenWidth
yres  = 600           ; ScreenHeight
sco   = 0
tptr  = 1

t.s = "                   "
t.s = t.s + "I FIGURED OUT THE FONT             MY NEW PROBLEM         "
t.s = t.s + "WHY ARE SOME OF THE LETTERS WRONG,  MY SPELLING IS RIGHT "
t.s = t.s + "BUT ITS NOT DISPLAYING CORRECTLY,  WHY?   DOEST ANYONE KNOW?"
t.s = t.s + "                                 "

; -------- Init needed Stuff --------

InitSprite()                     ; Needed to init DX Screen and DX Drawings
InitKeyboard()                   ; Needed for handling DX keyboard inputs
UsePNGImageDecoder()             ; Needed to work with PNG Image

; -------- If Font successfully load, open Screen --------

window = OpenScreen( xres , yres , 32 , "PBWINDOW")      ; Open DirectX Screen

If window = 0                                            ; If creating DX screen failed,
  MessageRequester("Error", "Cant open Window", 0)       ; we alert an error and
  End                                                    ; stop the application
EndIf

; -------- Load Font Gfx as Sprite--------

font = LoadSprite (#PB_Any, "output.PNG")                ; Load the font PNG as Sprite

If font = 0                                              ; If loading Spritefailed,
  MessageRequester("Error", "Cant load font.PNG", 0)     ; we alert an error and
  End                                                    ; stop the application
EndIf

; -------- MainLoop --------

Repeat
      ClearScreen($0)                                   ; Clear Screen with color $0
  ExamineKeyboard()                                     ; Update for keyboard inputs

  cco = 0

  For cc = 0 To 25
    letter   = Asc(Mid(t.s, tptr+cc, 1))- 31
 
    yCharPos = letter / 10
    xCharPos = letter % 10
   
    If xCharPos = -1 : xCharPos = 9
    ElseIf xCharPos > 0 : xCharPos = xCharPos - 1
    EndIf
   
    ClipSprite(font, xCharPos*94, yCharPos*78, 70, 70)   ;the magic
    TransparentSpriteColor(font,#White)  ;sets the transparacy color of the font png
    DisplayTransparentSprite(font, sco+cco, 300+50*Sin((cc+cco+sco+m)/120))  ;
    cco = cco + 32
  Next
 
  m   = m   -5
  sco = sco -3
;     
  If sco < -32
    tptr = tptr + 1
    sco = sco + 32
  EndIf
;     
  If tptr > Len(t.s)-30
    tptr = 1
  EndIf
                                                       ; Stop drawing on DX Screen
  FlipBuffers()                                        ; Bring backround buffer to forground

Until KeyboardPushed (#PB_Key_Escape)                 ; Repeats until key ESCAPE has pressed
End


« Last Edit: October 30, 2015 by Omnikam »

Offline padman

  • Senior Member
  • Pentium
  • ********
  • Posts: 990
  • Karma: 260
    • View Profile
Re: PngFont Help Last ask before releasing my first demo!
« Reply #1 on: October 30, 2015 »
You are indeed on the right track, but your source image has weird dimensions.

917 pixels  / 10 chars horizontally gets you 91.7 pixel width. And 448 pixels / 6 chars vertically gets you 74.6 pixels height. So ever single char you are trying to cut out is 91.7x74.6. That's not gonna work. At least not that easily. And you will have huge borders around your font.

Make sure your font source picture looks anything like the one attached below. You can double, triple etc the size of the pic and you'll end up with a 64x64, 96x96 char which you can easily use for your needs. Assuming that the rest of Shockwave's code is untouched you should be ready to go ;)


Code: [Select]
ClipSprite(font, xCharPos*32, yCharPos*32, 32, 32)
ClipSprite(font, xCharPos*64, yCharPos*64, 64, 64)  ; x 2
ClipSprite(font, xCharPos*96, yCharPos*96, 96, 96)  ; x 3



Edit: Removed [Almost Solved] part from topic titles in all answers.
« Last Edit: October 30, 2015 by padman »
Challenge Trophies Won:

Offline padman

  • Senior Member
  • Pentium
  • ********
  • Posts: 990
  • Karma: 260
    • View Profile
Re: PngFont Help Last ask before releasing my first demo!
« Reply #2 on: October 30, 2015 »
Replace this part

Code: [Select]
letter   = Asc(Mid(t.s, tptr+cc, 1))- 31
 
    yCharPos = letter / 10
    xCharPos = letter % 10
   
    If xCharPos = -1 : xCharPos = 9
    ElseIf xCharPos > 0 : xCharPos = xCharPos - 1
    EndIf


with

Code: [Select]

    letter   = Asc(UCase(Mid(t.s, tptr+cc, 1)))-32
 
    yCharPos = letter / 10
    xCharPos = letter % 10


Can't test it, but that should do the trick.

« Last Edit: October 30, 2015 by padman »
Challenge Trophies Won:

Offline Omnikam

  • Atari ST
  • ***
  • Posts: 121
  • Karma: 4
    • View Profile
Re: PngFont Help Last ask before releasing my first demo!
« Reply #3 on: October 30, 2015 »
 :clap:
Your the Man Padman, yes that did the trick, my only question is why did that work?
what did your code change actually do? I understand if your busy, perhaps when you have time to explain it  :cheers:
« Last Edit: October 30, 2015 by padman »