ReadText from DataSection and Print2Screen !?

Advanced game related topics
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by MrVainSCL.


I just want to print some text on a 640 x 480 x 16 screen using the DrawText() command and the loaded Font! How can i read the text from a DataSection (Data$) and print it then to the users screen? Many thanks for help or just a small example...

"Mr.Vain of Secretly!" - Thorsten


;-------- Print Text on Screen --------

UseFont(2) ; Use Credits Font, we have always loaded :wink:

For prt = 0 To 7 ; We have 8 lines of text -1
; Read txt$ ; Read Data to Print
FrontColour (255,255,255) ; TextColor
Locate (320,240-24)
DrawText (txt$) ; How can i print out the txt$ ?

FlipBuffers()
Delay (dlay)
Next

;========================================
; D A T A - S E C T I O N
;========================================
DAT_CreditsText:

DataSection
Data$ "Text1"
Data$ "Text2"
Data$ ....
....
EndDataSection

MrVainSCL!
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by cfriedel.

I ended up doing it by taking a font, saving each letter as a sprite, and then superimposing them over my background. Not the easiest way to do text, but it seems to work. Basically what it does is read a string, convert each letter into ASCII, slap all the corresponding sprites together and then scrolls it from right to left. NOTE: Some of this code was lifted from an example. Also note that this forum reply tool causes some wrapping, so watch out for wrapping errors if you lift this code. Flavor to taste. Hope this helps, :)

Cliff


; Textscroll.pb
; By Cliff Friedel
;
; This program will draw text to a 640x480x16bit screen and scroll it across. Please note that this is not
; optimized (or even efficiently written). This was more of an exercise for me to figure out how the
; graphics commands worked. Please use at your own risk...
;

; First decide what text to write (use CAPS). Please note, this program only recognizes CAPS and the space.
; If you want to add the other ASCII characters, Please feel free.

text$ = "THIS IS A STUPID PROGRAM I WROTE IN ABOUT AN HOUR"

; Next, see if the machine has everything it needs to run this program
; and allocate memory for the sprites to be displayed

If InitSprite(27)=0
MessageRequester("Error","Error: You must have DirectX7 or Better to use this program.",0)
End
EndIf

;Now open the screen and begin loading in the sprites

If OpenScreen(640,480,16,"Sprite")
LoadSprite(0,"Graphics\65.bmp",0)
LoadSprite(1,"Graphics\66.bmp",0)
LoadSprite(2,"Graphics\67.bmp",0)
LoadSprite(3,"Graphics\68.bmp",0)
LoadSprite(4,"Graphics\69.bmp",0)
LoadSprite(5,"Graphics\70.bmp",0)
LoadSprite(6,"Graphics\71.bmp",0)
LoadSprite(7,"Graphics\72.bmp",0)
LoadSprite(8,"Graphics\73.bmp",0)
LoadSprite(9,"Graphics\74.bmp",0)
LoadSprite(10,"Graphics\75.bmp",0)
LoadSprite(11,"Graphics\76.bmp",0)
LoadSprite(12,"Graphics\77.bmp",0)
LoadSprite(13,"Graphics\78.bmp",0)
LoadSprite(14,"Graphics\79.bmp",0)
LoadSprite(15,"Graphics\80.bmp",0)
LoadSprite(16,"Graphics\81.bmp",0)
LoadSprite(17,"Graphics\82.bmp",0)
LoadSprite(18,"Graphics\83.bmp",0)
LoadSprite(19,"Graphics\84.bmp",0)
LoadSprite(20,"Graphics\85.bmp",0)
LoadSprite(21,"Graphics\86.bmp",0)
LoadSprite(22,"Graphics\87.bmp",0)
LoadSprite(23,"Graphics\88.bmp",0)
LoadSprite(24,"Graphics\89.bmp",0)
LoadSprite(25,"Graphics\90.bmp",0)
LoadSprite(26,"Graphics\bg.bmp",0)
LoadSprite(27,"Graphics\transparent.bmp",0)

; Initialize g to far right hand side of screen. this controls the horizontal positioning of the letters.

g = 640

; This is where all the good stuff happens. Please note this will run forever. I ought to put in some type
; of break clause here. Maybe later.

Repeat

; Draw and then clear the screen. This works a lot better if you do this before messing with your sprites
; then after. Otherwise you are going to end up with a lot of flicker.

FlipBuffers()
ClearScreen(0,0,0)

; Display the background (NOTE: This tiles the background sprite over and over until it fills the screen.)

For x = 0 To 640 Step 32
For y = 0 To 480 Step 32
DisplaySprite(26,x,y)
Next y
Next x
; This converts the text string into its respective sprites. Spaces end up less than 0, which is caught by
; the IF statement. These get assigned the transparent.bmp file
z=1
While z <= Len(text$)
spritenum=(Asc(Mid(text$,z,1)))-65
If spritenum < 0
spritenum = 27
EndIf
DisplayTransparentSprite(spritenum,(32*z)+g,200)
z=z+1
Wend
g = g-1

; This equation makes the letters go off the left and return on the right smoothly. Let me explain some of
; magic numbers here. 32 is the size of a letter's width. This is multiplied by the number of letters in
; text$. 41 is an extra space and 1/4, which gives it a little time to be off the screen before returning
; to the other side (so it doesn't pop off and pop on too soon). Multiplying by -1 is needed so the letters
; don't stop too soon and pop on the other side before they leave the screen. Finally, if g is less than
; this monstrosity, it resets itself to 640 and it all starts again. I must admit this is a little
; hocus pocus, but hey, it works.

If g < ((Len(text$)*32)+41)*-1
g = 640
EndIf
ForEver
EndIf
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Paul.

Version 2.60a allows you to write text directly to the graphics screen so you could do this...

Code: Select all

If InitSprite(1) = 0 Or InitKeyboard() = 0
  MessageRequester("Error", "Cannot Initialize DirectX_8", #MB_ICONERROR)
  End
EndIf

font1=LoadFont(0,"Arial",20)

If OpenScreen(640,480,16,"Text")
  Restore DAT_CreditsText
  For prt=0 To 3
    ClearScreen(0,0,0)
    
    StartDrawing(ScreenOutput() )
      DrawingMode(1)
      DrawingFont(font1)
      FrontColour(255,0,0)  
      Read text$
      tl=TextLength(text$)/2
      Locate(320-tl,220)  
      DrawText(text$)
    StopDrawing()
      
    FlipBuffers()
    Delay(2000)
  Next
EndIf

DataSection
DAT_CreditsText: 
Data$ "This is"
Data$ "a test" 
Data$ "to write text" 
Data$ "to the graphics screen"
EndDataSection
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by MrVainSCL.

thanks folks...

i still founded a way some time ago.. so its a bit to late :wink: - anyway many thanks! i dont want use any external gfx brushes :wink: i wanted use systemfont... yes, since the new update it will possible to print text direct on the screen... :wink:)

MrVainSCL!
Post Reply