BASIC

Author Topic: PrintPoly: Print using Polyvector  (Read 1542 times)

Offline AlienMenace

  • Mr. Polyvector
  • ***
  • Posts: 149
    • View Profile
PrintPoly: Print using Polyvector
« on: 2011-Oct-16 »
Print text using Polyvector

PrintPoly(text$, x, y, fontsprite, color, centered)
centered=1, not centered=0

Hope somebody finds this useful.
Please feel free to make comments/suggestions. :)

Code: GLBasic [Select]
// --------------------------------- //
// Project: PolyFont BY GulfRacer
// Start: Sunday, October 16, 2011
// IDE Version: 10.106

LOCAL screen_x, screen_y
GETSCREENSIZE screen_x, screen_y

SETCURRENTDIR("Media")
LOADSPRITE "font.png",1000 // LOAD YOUR FONT AS A SPRITE

WHILE TRUE

PrintPoly("** This is a PrintPoly test! **",screen_x/2, 100, 1000, RGB(255,0,0),1) // WARNING: USE COLOR RGB(255,255,255) FOR MOBILE DEVICES
PrintPoly("This text is centered!",screen_x/2,150,1000, RGB(0,255,0),1)
PrintPoly("This text is not!",screen_x/2,200,1000, RGB(0,0,255),0)

SHOWSCREEN
WEND

FUNCTION PrintPoly: text$, x, y, font, color, center
LOCAL font_sizex, font_sizey, char_row, char_col, text_length
LOCAL loop_foo, cur_char$, cur_char_code
LOCAL texture_x, texture_y, cols, rows, center_x=0

text_length=LEN(text$)

IF text_length>0

        GETSPRITESIZE font,font_sizex,font_sizey
       
        cols=font_sizex/16 // GET THE COLUMN SIZE IN PIXELS
        rows=font_sizey/8 // GET THE ROW SIZE IN PIXELS

        IF center=1 THEN center_x=text_length*cols/2
               
        FOR loop_foo=0 TO text_length-1
               
                cur_char$=MID$(text$,loop_foo,1) // GET THE CURRENT CHARACTER          
                cur_char_code=ASC(cur_char$) // GET THE CURRENT CHARACTER CODE         
                char_row=INTEGER(cur_char_code/16) // FIND THE ROW IN THE FONT MATRIX THE CHARACTER BELONGS TO         
                char_col=INTEGER(MOD(cur_char_code,char_row*16)) // FIND THE COLUMN IN THE FONT MATRIX THE CHARACTER BELONGS TO                
                texture_x=char_col*cols // LOCATE THE TEXTURE PIXEL X LOCATION IN THE FONT MATRIX              
                texture_y=char_row*rows // LOCATE THE TEXTURE PIXEL y LOCATION IN THE FONT MATRIX
       
                STARTPOLY font         
       
                POLYVECTOR x-center_x+(loop_foo*cols),y,texture_x,texture_y,color
                POLYVECTOR x-center_x+(loop_foo*cols),y+rows,texture_x,texture_y+rows,color
                POLYVECTOR x-center_x+(loop_foo*cols)+cols,y+rows,texture_x+cols,texture_y+rows,color
                POLYVECTOR x-center_x+(loop_foo*cols)+cols,y,texture_x+cols,texture_y,color
       
                ENDPOLY
                               
        NEXT
               
ENDIF

ENDFUNCTION

 
Apps published: 3

Offline Wampus

  • Prof. Inline
  • *****
  • Posts: 1004
    • View Profile
Re: PrintPoly: Print using Polyvector
« Reply #1 on: 2011-Oct-16 »
Simple, clear and concise. Easy to expand for more specialised needs. Thanks.