Tuesday, March 02, 2010

It speaks... Well, writes...

For detailed changes click here.

After all my work on the GUI, I got a little bored of it and decided to work on something with a little bit more meat and could actually be finished quickly. So I chose to work on my RPG-esque (actually it's near enough a complete copy) TextBox class. It was only after a few hours of head ache I realised this was just another form of GUI and wouldn't be as easy to implement as I thought. If you don't know what I'm talking about, check out the example pic below from Zelda Ocarina of Time to get an idea.


I wanted to implement a flexible TextBox class that very easy to get going when I wanted to display it. It needed to do the following:
  • Adjust it's height based on the amount of lines I wanted to display (and shrink if the text was smaller than that amount)
  • Take the whole text as one single string (not line by line) and be able to calculate how many lines there would be based on font width
  • Have support to colour characters\words\sentences through html like tag system within the text string
  • Have support to change the font of characters\words\sentences through html like tag system within the text string
  • Have support to force a new line when needed
  • Image support, either right\left aligned
  • Text alignment - left \ centre (I did right as well but it looked crap and I will never use it so it's removed)
  • Display text character by character based on delay time
  • Speed up the character by character delay by holding down a button
  • Display a whole line in one go without the effect of character by character
So yes, I wasn't asking to much... Especially since I haven't really worked with strings before to this extent. In fact the only thing I've done with strings in the past is assign them and display them (and maybe check their width). So it was quite a nice little learning curve. Check out the latest vid below to see how it turned out.



As you can see I've got everything I wanted apart from the image support (which is coming!) and I'm pretty happy with the results. At the moment it really does feel a little un-optimised though. For instance to prints this text string with the word "RPG" being red and in a different font and adding a new line before it I would type:

"/n<red><f>RPG<-> Text Boxes are more hassle than they look..."

When the string is parsed, new line symbols (/n - not \n) are removed and the text width is calculated by stripping the tags and then re-adding them after. Only during the drawing section of this text are the tags evaluated. Which seems wrong, because during that drawing, the logic is also reading the string character by character. I tried to find a smart way of doing this only during the logic cycle but couldn't come up with anything other than creating another array the same size as the string and keeping information about the tags on a per character basis, and that really did seem like more hassle than it was worth.

It works though which is the main thing, and with clever use of the:


int al_color_name_to_rgb(char const *name, float *r, float *g, float *b)

command in Allegro 4.9 my textbox now supports all these colours with just one simple routine. Yes I am really grateful for the hard work the Allegro Devs do, because it's introducing commands like those that make life a lot easier. I hope one day I will be good enough to contribute something back.

I think that's enough of me going on for one post, so I will leave it here. My next major task is going to be cleaning up my existing code, and completing the Map Editor to 100% or near enough. College finished early today and I thought I would spend the time in the library fleshing out my game design, so now I'm pretty certain what I will and won't need which can only be a good thing. I would like to talk about the game more, but I really would like to have something to show about it first.

Thanks for reading =)

No comments:

Post a Comment