http://GameProgrammer.Com

Programming

GP Mailing List
     Thread Index
     Date Index

ATXGPSIG List
     Thread Index
     Date Index

Google
>

Home

Wise2Food



[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Word Wrap



I haven't fully checked your code out but I'll go ahead and put my up now.  
It seems, though, that we are using different implementations of it so the 
way we code it is different; but that's cool.  Anyway, here it is(with 
comments):

/*-------------------------
     TEXT BOX FUNCTION---Thursday February 08, 2001-----Success
--------------------------*/
void tbox(char *text) {

//------SET UP LOOP VARIABLES AND OTHER VARIABLES------

int totchars = strlen(text);             //LENGTH OF STRING TO PRINT-'text'
int numfields =(int)((totchars/50) + 1);  //----Evaluation 1----
char buffer[numfields][65];    //'text' string but with \n's and other stuff
int i=0,j,read=0;      //i & j-loop counters, read-keeps track of number
                      //of characters read from text so loops can end
//FIND NUMBER OF FIELDS FOR
//MY ARRAY CALLED BUFFER



//-------COPY PROCESS-------

while(i<numfields && read<totchars){ //WHILE i IS LESS THAN NUMBER OF 
STRINGS
j=1;     //-----Evaluation 2-----      AND WE HAVEN'T READ ALL OF 'text'
strncpy(&buffer[i][0], "|", 1);
      while(j<51 && read<totchars){
      strncpy(&buffer[i][j], text++, 1);
      read++;
      j++;
      }
strncpy(&buffer[i][j],"          |\n",13);
i++;      //increment to next string
}


/*----------------------------
   !!!!!EVALUATION TIME!!!!
-----------------------------
1.)Here, I divide numfields by 50, which is the length the strings will be 
in
my text box.  I convert it to an int and add one to it for two reasons: 
1.)If
the division ended up something like 3.37544 or whatever, during the
conversion to an int, it might just give me 3(which would be 150 
characters).
Therefore I add one just to make sure I get all of my characters. 2.)If the
string is less than 50 characters long, meaning the number would be less 
than
one, it could give me zero and so pretty much the same reason as above, I 
add
one.
2.)This is where all the dirty work is done.  Upon every iteration of i, j
must be reset to 1. i is the placeholder for which string (1,2,3 or etc) I 
am
copying. j is the placeholder for which part of each string I am copying
(buffer[1][(j-this could be anywhere from 1-50 inclusive.)]) I just said 
that
j could be anywhere from 1 to 50 inclusive. VERY IMPORTANT!! An arrays
starting place is always 0! But, I use the zero place for the side of the
text box '|'. Also, it can only reach 50, because after that I add a 
newline,
some space and the end and the right side of the text box '|'. That's why
after j reaches 50 (the loop runs while j<50 or the read<totchars, in which
read keeps track of how many total characters I have read and is never 
reset,
until the end of the function of course! :>) Note that both loops end when
I have read the total number of characters(read starts at zero so counting
the zero place, 1 less than totchars plus the zero is actually equal to
tochars)not just one of them. That means the j loop will exit and then the i
loop will exit), j must be reset to 1, which is the startin place for it to
read string data into the next string in buffer. And that wraps it up! :>)*/

//Time to print the box and the info! :>)
int a;
cout << " _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _" << 
endl;

for(a=0;a<numfields;a++)
cout << buffer[a];


cout << " - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -" << 
endl;

}//end function tbox


The code should be fully compilable, but it just doesn't work how I wish it 
would.  This is the unrevised version.  I was taking a shower and thinking 
about the problem and I think I may have found part of a solution to it.  I 
just don't have time to code it right now.  Hopefully I'll be able to send a 
revised version in a few days.




_________________________________________________________________
Join the world’s largest e-mail service with MSN Hotmail. 
http://www.hotmail.com

==^================================================================
This email was sent to: list@gameprogrammer.com

EASY UNSUBSCRIBE click here: http://topica.com/u/?bUrGcz.bV8N8c
Or send an email to: gameprogrammer-unsubscribe@topica.com

T O P I C A -- Register now to manage your mail!
http://www.topica.com/partner/tag02/register
==^================================================================