Page 1 of 1

"Custom" side scrolling text - advanced version

PostPosted: Mon Apr 23, 2007 12:04 am
by morcior
In writing a side scrolling text function, I realised that for practical use in a game, it is useful to know when the text has finished typing so I've added call-backs to the code. This is a little more advanced that the other version so I've posted it seperately.

The set-up is the same. Copy the code to global code, and call txtDraw() in the draw event of your text actor.

The difference is when you come to write text. You now specify a call-back function to be called when the text has finished typing. You can define your call back functions in global code, eg:

Code: Select all
void my_callback()
{
    btn_next.transp = 0; // show "next" button
}


Now when you call the code to write some text, you can have the function you've just written called when the text finishes typing by adding a reference to the function:

Code: Select all
txtWrite(&txt_actor, "hello world!", my_callback);


If you don't want a function called when typing has finished, you can use "null":
Code: Select all
txtWrite(&txt_actor, "hello world!", null);



I also added a txtFadeOut(function) function that does what it says! It fades out the last text you wrote, and it also takes a function pointer for if you want to call an event when fading finishes. Again, use null if you want to take no action.

Code: Select all
txtFadeOut(null);



Finally, heres the code:

Code: Select all
char buf[512];
int pos = -3, ptimer = 0, fading = 0;
Actor *tgt = NULL;
void (*fin)() = NULL;

#define TYPE_SPEED 2
#define FADE_SPEED 0.05f
#define APPEND_CHAR "_"

void txtDraw()
{
   if (fading == 2) return;
   if (fading == 1) { if (tgt->transp < 1) { tgt->transp += FADE_SPEED; } else { fading = 2; fin(); return; } }
   if (ptimer < TYPE_SPEED) { ptimer++; return; }
   ptimer = 0;

   if (pos<0)
      if (pos==-3) { fin(); pos = -1; return; }
      else if (pos==-1) { pos = -2; strcpy(tgt->text, buf); return; }
      else if (pos==-2) { pos = -1; strcpy(tgt->text, buf); strcat(tgt->text, APPEND_CHAR); return; }
      else return;

   strncpy(tgt->text, buf, pos);
   strncat(tgt->text, APPEND_CHAR, 1);
   
   pos++;
   if (pos > strlen(buf)) pos = -3;
}

void txtWrite(Actor *ent, char *txt, void (*ftn)())
{
   ptimer = 0; pos = 1; fading = 0; tgt = ent; fin = ftn; tgt->transp = 0;
   sprintf(ent->text, " ");
   sprintf(buf, txt);
}

void txtClear(Actor *ent) { sprintf(ent->text, " "); }
void txtFadeOut(void (*ftn)()) { fading = 1; fin = ftn; }
void null() { return; }

Re: "Custom" side scrolling text - advanced version

PostPosted: Mon Jan 21, 2008 11:37 pm
by DarkParadox
i noticed that every one of this text functions crash if there used in more than one actor.
yours does it to.