• Welcome to Theos PowerBasic Museum 2017.

I need help with SDL_TTF

Started by Marco Lugo, December 06, 2008, 08:33:34 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Marco Lugo


#COMPILE EXE
#OPTIMIZE SPEED
#REGISTER ALL

#DIM ALL

#INCLUDE ONCE "SDL.inc"
#INCLUDE ONCE "SDL_ttf.inc"

%WINDOW_WIDTH = 640
%WINDOW_HEIGHT = 480
$WINDOW_TITLE = "SDL Start"



FUNCTION PBMAIN () AS LONG

    IF (SDL_Init(%SDL_INIT_VIDEO) < 0) THEN
        MessageBox 0, "Unable to init SDL: " + SDL_GetError(), "Error!", %MB_OK
        EXIT FUNCTION
    END IF

    IF (TTF_Init < 0) THEN
        MessageBox 0, "Unable to init TTF: " + SDL_GetError(), "Error!", %MB_OK
        EXIT FUNCTION
    END IF

    DIM screen AS SDL_Surface POINTER

    screen = SDL_SetVideoMode(%WINDOW_WIDTH,%WINDOW_HEIGHT,16,%SDL_HWSURFACE OR %SDL_DOUBLEBUF)

    IF ( screen = 0) THEN
        MessageBox 0, "Unable to set 640x480 video: " + SDL_GetError(), "Error!", %MB_OK
        SDL_Quit
        EXIT FUNCTION
    END IF

    'set the title of the window
    SDL_WM_SetCaption($WINDOW_TITLE,$NUL)

    DIM myfont AS TTF_Font POINTER

    myfont = TTF_OpenFont("ARIAL.TTF", 12)

    DIM foregroundColor AS SDL_Color
    DIM backgroundColor AS SDL_Color

    foregroundColor.r =  255
    foregroundColor.g =  255
    foregroundColor.b =  255

    backgroundColor.r =  0
    backgroundColor.g =  0
    backgroundColor.b =  255

    DIM textSurface AS SDL_Surface POINTER
    textSurface = TTF_RenderText_Shaded(myfont, "This is my text.",foregroundColor,backgroundColor)

    DIM textLocation AS SDL_Rect
    textLocation.x = 100
    textLocation.y = 100
    textLocation.w = 0
    textLocation.h = 0
   
   DIM done AS BYTE

    done = 0

    DIM ev AS SDL_Event

    WHILE (done = 0)

        WHILE (SDL_PollEvent(VARPTR(ev)))
            SELECT CASE ev.type
                CASE %SDL_QUIT
                    done = 1
                    EXIT LOOP
                CASE %SDL_KEYDOWN
                    IF (ev.key.keysym.sym = %SDLK_ESCAPE) THEN
                        done = 1
                        EXIT LOOP
                    END IF
            END SELECT
        WEND

        SDL_FillRect(screen, 0, SDL_MapRGB(@screen.format, 0, 0, 0))

        'draw bitmap
        SDL_BlitSurface(textSurface, 0, screen, VARPTR(textLocation))

        'DRAWING ENDS HERE

        'finally, update the screen :)
        SDL_Flip(screen)

    WEND
   
    SDL_FreeSurface(textSurface)
    TTF_CloseFont(myfont)
   
    TTF_Quit
   
    SDL_Quit

    MessageBox 0, "Exited cleanly without errors!", "Info", %MB_OK
   
END FUNCTION


This is the conversion of a small C program that open a window and draw a text in it.
I have an error because the structure TTF_Font isn't definite in the "SDL_ttf.inc"  :(
How i can fix this problem?

Thanks

Patrice Terrier

#1
From the top of my head SDL doesn't work with VISTA.

If you are looking to use TTF with OpenGL why not using OpenGL directly instead of another encapsulation.

The BassBox, BBPLUGIN.INC already provide such feature to create GL texture form True Type Font.

Search for BBP_BuildGLfont

Added:


%GL_LIGHTING         = &H0B50
%GL_TEXTURE_2D       = &H0DE1
%GL_MODELVIEW        = &H1700
%GL_PROJECTION       = &H1701
%GL_LIST_BIT         = &H00020000
%GL_UNSIGNED_BYTE    = &H1401

#IF %BBP_NO_GLFONT = 0
    TYPE ZGLFONT
        fontName   AS ASCIIZ * 64 ' The True Type font name.
        fontBase   AS LONG        ' The OpenGL FontBase (do not set this).
        charStart  AS LONG        ' OpenGL starting ASCII character (usually 32).
        charNum    AS LONG        ' OpenGL number of glyphe characters to create(usually 96).
        fontHeight AS LONG        ' The font size (72 DPI).
        fontWeight AS LONG        ' The font weigt.
        fontHandle AS LONG        ' The Win32 font handle
    END TYPE
   
    TYPE SIZEL
        cx AS LONG
        cy AS LONG
    END TYPE
   
    TYPE RECT
        nLeft AS LONG
        nTop AS LONG
        nRight AS LONG
        nBottom AS LONG
    END TYPE
   
    %FW_BOLD             = 700
    %OUT_TT_PRECIS       = 4
    %ANTIALIASED_QUALITY = 4
   
    DECLARE FUNCTION GetStockObject LIB "GDI32.DLL" ALIAS "GetStockObject" (BYVAL nIndex AS LONG) AS DWORD
    DECLARE FUNCTION GetDeviceCaps LIB "GDI32.DLL" ALIAS "GetDeviceCaps" (BYVAL hdc AS DWORD, BYVAL nIndex AS LONG) AS LONG
    DECLARE FUNCTION CreateFont LIB "GDI32.DLL" ALIAS "CreateFontA" (BYVAL nHeight AS LONG, BYVAL nWidth AS LONG, BYVAL nEscapement AS LONG, BYVAL nOrientation AS LONG, BYVAL fnWeight AS LONG, BYVAL fdwItalic AS DWORD, _
            BYVAL fdwUnderline AS DWORD, BYVAL fdwStrikeOut AS DWORD, BYVAL fdwCharSet AS DWORD, BYVAL fdwOutputPrecision AS DWORD, BYVAL fdwClipPrecision AS DWORD, BYVAL fdwQuality AS DWORD, _
            BYVAL fdwPitchAndFamily AS DWORD, lpszFace AS ASCIIZ) AS DWORD
    DECLARE FUNCTION glGenLists LIB "opengl32.dll" ALIAS "glGenLists" (BYVAL range&) AS DWORD
    DECLARE FUNCTION wglUseFontBitmaps LIB "OPENGL32.DLL" ALIAS "wglUseFontBitmapsA" (BYVAL hdc AS DWORD, BYVAL dFirst AS DWORD, BYVAL dCount AS DWORD, BYVAL dListBase AS DWORD) AS LONG
    DECLARE FUNCTION GetTextExtentPoint32 LIB "GDI32.DLL" ALIAS "GetTextExtentPoint32A" (BYVAL hdc AS DWORD, lpsz AS ASCIIZ, BYVAL cbString AS LONG, lpSize AS SIZEL) AS LONG
    DECLARE SUB glDeleteLists LIB "opengl32.dll" ALIAS "glDeleteLists" (BYVAL list AS DWORD, BYVAL range&)
    DECLARE FUNCTION GetClientRect LIB "USER32.DLL" ALIAS "GetClientRect" (BYVAL hwnd AS DWORD, lpRect AS RECT) AS LONG
    DECLARE FUNCTION glIsEnabled LIB "opengl32.dll" ALIAS "glIsEnabled" (BYVAL cap AS DWORD) AS BYTE
    DECLARE SUB glDisable LIB "opengl32.dll" ALIAS "glDisable" (BYVAL cap AS DWORD)
    DECLARE SUB glMatrixMode LIB "opengl32.dll" ALIAS "glMatrixMode" (BYVAL mode AS DWORD)
    DECLARE SUB glPushMatrix LIB "opengl32.dll" ALIAS "glPushMatrix" ()
    DECLARE SUB glLoadIdentity LIB "opengl32.dll" ALIAS "glLoadIdentity" ()
    DECLARE SUB glOrtho LIB "opengl32.dll" ALIAS "glOrtho" (BYVAL nleft AS DOUBLE, BYVAL nright AS DOUBLE, BYVAL bottom AS DOUBLE, BYVAL top AS DOUBLE, BYVAL zNear AS DOUBLE, BYVAL zFar AS DOUBLE)
    DECLARE SUB glColor3f LIB "opengl32.dll" ALIAS "glColor3f" (BYVAL red AS SINGLE, BYVAL green AS SINGLE, BYVAL blue AS SINGLE)
    DECLARE SUB glRasterPos2i LIB "opengl32.dll" ALIAS "glRasterPos2i" (BYVAL x&, BYVAL y&)
    DECLARE SUB glPushAttrib LIB "opengl32.dll" ALIAS "glPushAttrib" (BYVAL mask AS DWORD)
    DECLARE SUB glListBase LIB "opengl32.dll" ALIAS "glListBase" (BYVAL nbase AS DWORD)
    DECLARE SUB glCallLists LIB "opengl32.dll" ALIAS "glCallLists" (BYVAL n&, BYVAL ntype AS DWORD, lists AS ANY)
    DECLARE SUB glPopAttrib LIB "opengl32.dll" ALIAS "glPopAttrib" ()
    DECLARE SUB glPopMatrix LIB "opengl32.dll" ALIAS "glPopMatrix"

    FUNCTION BBP_DefaultFont() AS LONG
        STATIC hDefault AS LONG
        IF hDefault = 0 THEN hDefault = GetStockObject(12)      ' %ANSI_VAR_FONT
        FUNCTION = hDefault
    END FUNCTION
   
    SUB BBP_BuildGLfont (BYVAL hDC AS LONG, UseFont AS ZGLFONT)
        LOCAL OldFont AS LONG, lpSize AS SIZEL, hFont AS LONG, LenFontName AS LONG
   
        UseFont.charStart = 32
        UseFont.charNum   = 96
        UseFont.fontBase  = glGenLists(UseFont.charNum)         ' Storage For 96 Characters
        LenFontName = LEN(UseFont.fontName)
        IF hDC = 0 THEN LenFontName = 0
        IF LenFontName THEN
           IF UseFont.fontHeight < 1 THEN UseFont.fontHeight = 10
           UseFont.fontHeight = -1 * (UseFont.fontHeight * GetDeviceCaps(hDC, 90)) \ 72
           IF UseFont.fontWeight = 0 THEN UseFont.fontWeight = %FW_BOLD
           hFont = CreateFont(UseFont.fontHeight, _             ' Height Of Font
                              0, _                              ' Width Of Font
                              0, _                              ' Angle Of Escapement
                              0, _                              ' Orientation Angle
                              UseFont.fontWeight, _             ' Font Weight
                              0, _                              ' Italic
                              0, _                              ' Underline
                              0, _                              ' Strikeout
                              0, _                              ' %ANSI_CHARSET Character Set Identifier
                              %OUT_TT_PRECIS, _                 ' Output Precision
                              0, _                              ' %CLIP_DEFAULT_PRECIS Clipping Precision
                              %ANTIALIASED_QUALITY, _           ' Output Quality
                              0, _                              ' %FF_DONTCARE OR %DEFAULT_PITCH Family And Pitch
                              UseFont.fontName)                 ' Font Name
        ELSE
           hFont = BBP_DefaultFont()
        END IF
        IF hFont THEN
           OldFont = SelectObject(hDC, hFont)                   ' Selects our font
         ' Builds 96 Characters Starting At Character 32
           CALL wglUseFontBitmaps(hDC, UseFont.charStart, UseFont.charNum, UseFont.fontBase)
         ' Get font height
           CALL GetTextExtentPoint32(hDC, "W", 1, lpSize)
           UseFont.fontHeight = lpSize.cy
           CALL SelectObject(hDC, oldfont)                      ' Restore previous selected font
           IF LenFontName THEN UseFont.fontHandle = hFont       ' Save the Win32 font handle
        END IF
    END SUB
   
    SUB BBP_DeleteGLFont (UseFont AS ZGLFONT)
        IF UseFont.fontBase THEN
           CALL glDeleteLists(UseFont.fontBase, UseFont.charNum)       ' Delete All 96 Characters
           IF UseFont.fontHandle THEN DeleteObject(UseFont.fontHandle) ' The Win32 font handle
        END IF
    END SUB
   
    SUB BBP_DrawGLText (BYVAL glWnd AS LONG, UseFont AS ZGLFONT, BYVAL x AS LONG, BYVAL y AS LONG, zTxt AS ASCIIZ, BYVAL ARGB AS LONG)
        LOCAL rc AS RECT, A, R, G, B AS BYTE, cAlpha, cRed, cGreen, cBlue AS SINGLE, WasTexture, WasLighting AS LONG
       
        CALL GetClientRect(glWnd, rc)
   
        WasTexture = glIsEnabled(%GL_TEXTURE_2D)
        IF WasTexture THEN CALL glDisable(%GL_TEXTURE_2D)        ' Turn off textures, didn't want our text textured
        WasLighting = glIsEnabled(%GL_LIGHTING)
        IF WasLighting THEN CALL glDisable(%GL_LIGHTING)         ' Disable Lighting
   
        CALL glMatrixMode(%GL_PROJECTION)
        CALL glPushMatrix()
        CALL glLoadIdentity()
        CALL glOrtho(0.0, rc.nRight, 0.0, rc.nBottom, -1.0, 1.0)
        CALL glMatrixMode(%GL_MODELVIEW)
        CALL glPushMatrix()
        CALL glLoadIdentity()
   
      ' Set color
        CALL BBP_SplitColorARGB(ARGB, A, R, G, B)
        cAlpha = A + 1: cRed = R + 1: cGreen = G + 1: cBlue = B + 1
        CALL glColor3f(cRed / 256, cGreen / 256, cBlue / 256)
      ' Set X,Y location
        CALL glRasterPos2i(x, rc.nBottom - (UseFont.fontHeight - 4) - y)
      ' Draw the text
        CALL glPushAttrib(%GL_LIST_BIT)                           ' Pushes The Display List Bits
             CALL glListBase(UseFont.fontBase - UseFont.charStart)' Sets The Base Character to 0
             CALL glCallLists(LEN(zTxt), %GL_UNSIGNED_BYTE, zTxt) ' Draws The Display List Text
        CALL glPopAttrib()                                        ' Pops The Display List Bits
   
        CALL glPopMatrix()
        CALL glMatrixMode(%GL_PROJECTION)
        CALL glPopMatrix()
        CALL glMatrixMode(%GL_MODELVIEW)
   
        IF WasTexture THEN CALL glEnable(%GL_TEXTURE_2D)          ' Enable texture mode
        IF WasLighting THEN CALL glEnable(%GL_LIGHTING)           ' Enable Lighting
   
        CALL glColor3f(1, 1, 1)                                   ' Back to default color
   
    END SUB
#ENDIF


Note: There are several examples showing how to use it, in the different BassBox plugins source code.

...

Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com

José Roca

Quote
I have an error because the structure TTF_Font isn't definite in the "SDL_ttf.inc" How i can fix this problem?

There is no structure to declare. It is an opaque holder. It is defined as:


typedef struct _TTF_Font TTF_Font;


Simply declare it as DWORD.


DIM myfont AS DWORD


C and BASIC don't use the same syntax.

Marco Lugo