Author Topic: SDL: Drawing Text with TTF  (Read 2961 times)

0 Members and 1 Guest are viewing this topic.

Offline José Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 2481
  • User-Rate: +204/-0
SDL: Drawing Text with TTF
« on: July 27, 2008, 11:19:48 PM »
 
The following example draws text at random positions using the SDL_TTG library.

DLL dependencies: SDL.DLL, SDL_TTF.DLL, libfreetype-6.dll.

Code: [Select]
' SED_PBWIN - Use the PBWIN compiler
#COMPILE EXE
#DIM ALL
#INCLUDE "SDL_TTF.INC"

%SCR_WIDTH  = 320
%SCR_HEIGHT = 240
%SCR_BPP    = 16

$FONTFILE   = "VERA.TTF"
%FONTSIZE   = 36 ' in points
$FONTTEXT   = "tff!"

SUB SDL_TTF_DrawText (BYVAL pscreen AS SDL_Surface PTR, _
   BYVAL x AS LONG, BYVAL y AS LONG, BYREF clr AS SDL_Color, _
   BYREF szText AS ASCIIZ, BYVAL pFont AS DWORD)

   LOCAL psurface AS SDL_Surface PTR
   LOCAL rc AS SDL_RECT

   psurface = TTF_RenderText_Solid(pfont, szText, clr)

   rc.x = x
   rc.y = y
   rc.w = @psurface.w
   rc.h = @psurface.h
   SDL_UpperBlit(psurface, %NULL, pscreen, VARPTR(rc))

   SDL_FreeSurface(psurface)

END SUB

FUNCTION PBMAIN () AS LONG

   LOCAL pscreen AS SDL_Surface PTR
   LOCAL pFont AS DWORD
   LOCAL i, w, h AS LONG
   LOCAL clr AS SDL_Color

   ' // Initialize SDL's subsystems - in this case, only video.
   IF SDL_Init(%SDL_INIT_VIDEO) < 0 THEN
      MSGBOX "Unable to init SDL: " & SDL_GetError()
      EXIT FUNCTION
   END IF

   ' // Attempt to create a 640x480 window with 32bit pixels.
   pscreen = SDL_SetVideoMode(%SCR_WIDTH, %SCR_HEIGHT, %SCR_BPP, 0)

   ' // If we fail, return error.
   IF pscreen = %NULL THEN
      SDL_Quit
      MSGBOX "Unable to set the video mode: " & SDL_GetError()
      EXIT FUNCTION
   END IF

   ' // Set the window title
   SDL_WM_SetCaption("SDL_TTF Demo", "")

   ' Initialize SDL_ttf
   IF TTF_Init < 0 THEN
      SDL_Quit
      EXIT FUNCTION
   END IF

   pFont = TTF_OpenFont($FONTFILE, %FONTSIZE)

   IF pFont = %NULL THEN
      TTF_Quit
      SDL_Quit
      EXIT FUNCTION
   END IF

   TTF_SizeText(pFont, BYCOPY $FONTTEXT, w, h)

   LOCAL done AS LONG
   LOCAL uevent AS SDL_Event
   WHILE done = %FALSE
      ' // Draw text
      FOR i = 1 TO 25
         clr.r = RND * 256
         clr.g = RND * 256
         clr.b = RND * 256
         SDL_TTF_DrawText pscreen, RND * (%SCR_WIDTH + w * 2) - w, _
             RND * (%SCR_HEIGHT + h * 2) -h, clr, BYCOPY $FONTTEXT, pFont
      NEXT
      SDL_Flip pscreen
      SDL_PumpEvents
      ' // Poll for events, and handle the ones we care about.
      WHILE SDL_PollEvent(VARPTR(uevent))
         SELECT CASE uevent.type
            CASE %SDL_KEYDOWN
            CASE %SDL_KEYUP
               ' // Quit if escape is pressed
               IF uevent.key.keysym.sym = %SDLK_ESCAPE THEN
                  done = %TRUE
                  EXIT LOOP
               END IF
            CASE %SDL_QUIT
               done = %TRUE
               EXIT LOOP
         END SELECT
      WEND
   WEND

   ' Shut down
   TTF_Quit
   SDL_Quit

END FUNCTION
« Last Edit: August 07, 2011, 08:05:52 PM by José Roca »