- TextDump/WordWrap/TextDisp Routine for DoorsCS7!
- 15 Apr 2012 06:05:41 pm
- Last edited by JoeYoung on 18 Apr 2012 05:09:14 am; edited 5 times in total
EDIT: April 18, 2012
Here's what I've been working on this past week or so, the almost perfected version of my text displaying code that I hope to use in future games, like Pokemon. This program, like Doors, will work on 2.43 OFW as well as newer Mathprint versions!
This small section of code performs several functions which I believe are important for BASIC projects involving a great deal of text. It uses several BASIC libraries currently supported by the last official version of DoorsCS7. The code below is a sample program demonstrating how it might be implemented in a game. It operates in four steps.
1) The string to be displayed is stored into Str1 from the archived appvar TEXTDUMP using LOOKUPINDEX. TEXTDIND is the respective index file it uses to recall those lines quickly. This text dump only has ten lines, but it should work almost equally fast in text dumps with lines that are numbered in the hundreds, or maybe a thousand.
2) The program then searches the string for any Greek letters, which are currently serving as placeholders for various strings stored in TEXTTEMP. It is demonstrated in line two, where the alpha symbol is replaced with the proper name PLAYER.
3) The program then quickly scours the document for the locations of all SPACES in the string and stores the first letter of every word to a string. This is the longest step, but the wait is almost unnoticeable for strings of even a decent length. Line 5 demonstrates an extremely long string, but the wait was only a second or so on a TI84+SE (586 characters, nearly 100 words).
4) In the last step, the program displays every word individually, lending an impression that say, an in-game character is talking. It correctly demonstrates word-wrapping, and even tends to complete a sentence separately before moving on to the next one. After both rows are filled, the programs waits for a keypress before resuming. After the message has been displayed, it returns the screen to exactly the way it was. Provided the appvars are all present in the archive, the only input required was the number corresponding to the line in the text dump.
I am sure there are a load of ways people could use this, and the code I've written is out there and you should feel perfectly fine using it or changing it to suit your needs. Altering the text width or the number of rows, for example, should require relatively few changes to the programming. It is by no means a requirement, but it'd make me happy to see a little credit attributed to me, this is my first contribution in some time.
The text dump requires no linebreak characters whatsoever to function. In this version, however, the routine would hang up if it encountered a word too long to display on one line (i.e. 90 pixels or about ~23 letters). There are a few small bugs, but nothing too broken. The only thing I really noticed is that the program cannot tell if a period denotes the end of a sentence if it appears before a space or if it is simply an abbreviated term. I'd probably get lazy and try to fix it by making St. Louis -> St.Louis, etc.
The textdump contains ten examples, including two in another language, try them out! I'd advise not inputting any other number besides those ten integers in this particular program, I will not be responsible for the possible resultant ram clear and instead I will laugh at you.
SourceCoder can do nothing for it, but I still think that with the new additions it could stand some improvement, so any helpful criticisms would be welcomed! A downloaded is included with the program and the appvars I used to demonstrate it. Doors is a separate application by KermMartian and needs to be downloaded here:
http://www.cemetech.net/projects/item.php?id=32
DOWNLOAD PRGM_X.ZIP HERE!
BASIC Code wrote:
prgmX - 521 bytes
:Input "1-10:",B
:det(5,"rowSwap(TEXTDUMP","TEXTDIND",B,0→Str1
:For(A,1,1
:¦ sub("αβχδΔεγΠλμΩ[phat]φρσƩτ",A,1→Str2
:¦ While inString(Str1,Str2,1
:¦ ¦ inString(Str1,Str2,1→B
:¦ ¦ sub(Str1,1,B-1)+det(5,"rowSwap(TEXTTEMP",A,1)+sub(Str1,B+1,length(Str1)-B→Str1
:¦ End
:End
:real(9,2,10,1
:real(12,6,0,47,95,63
:real(12,7,2,49,93,61,1
:{1+length(Str1→L1
:DelVar B
:1→C
:While C
:¦ inString(Str1," ",max(1,B→C
:¦ 1+C→L1(1+dim(L1
:¦ 1+L1(dim(L1→B
:End
:SortA L1
:DelVar B
:DelVar D
:For(A,1,dim(L1-1
:¦ If A≠dim(L1-1
:¦ Then
:¦ ¦ sub(Str1,L1(A),L1(A+1)-L1(A→Str2
:¦ ¦ sum(0,Str2→C
:¦ ¦ If D+C>90:Then
:¦ ¦ ¦ If B:Then
:¦ ¦ ¦ ¦ Repeat getKey
:¦ ¦ ¦ ¦ End
:¦ ¦ ¦ ¦ real(12,7,2,49,93,61,1
:¦ ¦ ¦ End
:¦ ¦ ¦ B=0→B
:¦ ¦ ¦ DelVar D
:¦ ¦ End
:¦ ¦ identity(10,16,3+D,49+6B,Str2
:¦ ¦ D+C→D
:¦ ¦ sub(Str2,length(Str2)-1,1→Str2
:¦ ¦ If Str2="." or Str2="?" or Str2="!"
:¦ ¦ Then
:¦ ¦ ¦ 1→B
:¦ ¦ ¦ 90→D
:¦ ¦ End
:¦ End
:End
:Repeat getKey
:End
:real(3,0,0,1,1
Generated by SourceCoder, © 2005-2012 Cemetech
:Input "1-10:",B
:det(5,"rowSwap(TEXTDUMP","TEXTDIND",B,0→Str1
:For(A,1,1
:¦ sub("αβχδΔεγΠλμΩ[phat]φρσƩτ",A,1→Str2
:¦ While inString(Str1,Str2,1
:¦ ¦ inString(Str1,Str2,1→B
:¦ ¦ sub(Str1,1,B-1)+det(5,"rowSwap(TEXTTEMP",A,1)+sub(Str1,B+1,length(Str1)-B→Str1
:¦ End
:End
:real(9,2,10,1
:real(12,6,0,47,95,63
:real(12,7,2,49,93,61,1
:{1+length(Str1→L1
:DelVar B
:1→C
:While C
:¦ inString(Str1," ",max(1,B→C
:¦ 1+C→L1(1+dim(L1
:¦ 1+L1(dim(L1→B
:End
:SortA L1
:DelVar B
:DelVar D
:For(A,1,dim(L1-1
:¦ If A≠dim(L1-1
:¦ Then
:¦ ¦ sub(Str1,L1(A),L1(A+1)-L1(A→Str2
:¦ ¦ sum(0,Str2→C
:¦ ¦ If D+C>90:Then
:¦ ¦ ¦ If B:Then
:¦ ¦ ¦ ¦ Repeat getKey
:¦ ¦ ¦ ¦ End
:¦ ¦ ¦ ¦ real(12,7,2,49,93,61,1
:¦ ¦ ¦ End
:¦ ¦ ¦ B=0→B
:¦ ¦ ¦ DelVar D
:¦ ¦ End
:¦ ¦ identity(10,16,3+D,49+6B,Str2
:¦ ¦ D+C→D
:¦ ¦ sub(Str2,length(Str2)-1,1→Str2
:¦ ¦ If Str2="." or Str2="?" or Str2="!"
:¦ ¦ Then
:¦ ¦ ¦ 1→B
:¦ ¦ ¦ 90→D
:¦ ¦ End
:¦ End
:End
:Repeat getKey
:End
:real(3,0,0,1,1
Generated by SourceCoder, © 2005-2012 Cemetech
==========OLD=POST===========================
Code:
:{1+length(Str1→{L1}
:DelVar B1→C
:While C
:inString(Str1," ",max(1,B→C
:1+C→{L1}(1+dim({L1}
:1+{L1}(dim({L1}→B
:End
:SortA {L1}
:1→B:DelVar DFor(A,1,dim({L1})-1
:sum(0,sub(Str1,{L1}(A),{L1}(A+1)-{L1}(A→C
:If D+C>90:Then
:B+1→B:DelVar D{L1}(A→{L1}(B
:End:D+C→D:End
:max({L1}→{L1}(B+1
:1+B→dim({L1}
This program uses a Doors lib command called StringWidth:
http://dcs.cemetech.net/index.php?title=BasicLibs:StringWidth
Arguments: sum(0,"STRING"
Outputs: The width in pixels of "STRING" is returned in Ans from this routine, and can be used as Ans or stored in a variable.
The above, named prgmW, uses only 182 bytes on the calculator. I'm wondering if it is possible to do better? It takes the string in Str1 and outputs a list of starting positions to be used with sub( on the graphscreen. I worked very hard to optimize this because it is only one piece out off many to manage pasting text to the screen, so it needs to be fast. Anybody think they can do it better?