Page 1 of 1

CTR - Custom Text Renderer

PostPosted: Mon May 18, 2020 1:56 pm
by lcl
I thought I'd post some of my Game Editor projects to the forums. Here's the first one.

CTR - Custom Text Renderer for Game Editor

An alternative way to render text in Game Editor.

This text renderer doesn't use Game Editor's built-in text actors at all, but instead, uses normal actors for displaying the text. Every character is a unique clone.
The system requires the fonts to be in an image format. For creating the font images and configuring the font settings (letter spacing, word spacing, line spacing and indentation) use the Font Tool that is provided together with the system.

    Download (hosted on GitHub)

    Contents:
    - source code of the system
    - 4 example .ged projects
    - Font Tool for generating image fonts
    - a template project that can be merged to other .ged projects

    Also available on GitHub

Features
  • Automatic text alignment (left / center / right)
  • Automatic line wrapping (allows fitting text in an area of a given width)
  • Custom color coding (allows rendering text in multiple colors)
  • Fitting text in an area of a given size (allows fitting large paragraphs of text in small windows by using scrollable text)
  • Optimized performance (text is only updated on request)
A video demonstrating some of the features listed above:



Getting started

Extract the contents of the .zip and merge the CTR_template.ged to your game. You may need to make sure that there is no name collisions between your game and the template, as the actors, variables and functions of the template are not prefixed in any way yet.

When working with the project in Game Editor, make sure to configure the key used for exiting the game in a_textHandler's KeyDown and KeyUp events to something you can easily press. This is to make sure that all the allocated memory is freed upon exiting the 'Game Mode'.

I can provide more detailed instructions on the usage of the system if requested to. :)

Supported escape sequences

  • \n - normal line break, if automatic line wrapping is in use, this may be ignored
  • \v - forced line break (actually stands for vertical tab, but has been repurposed here)
  • \t - tabulation

Custom escape sequences

The system also includes some custom escape sequences. All these sequences start with '$'.
  • $$ - prints a single '$'
  • $_ - a non-breaking space, is not allowed to be replaced with a newline for line wrapping
  • $c000000 - deploys color override: all following text will be rendered using the specified color
    • Each pair of characters after c represents a hexadecimal value between 0-255, first pair is for red, second for green, last one for blue
    • Example: $c00ffff is interpreted as (0, 255, 255) i.e. cyan
  • $x - a finishing escape sequence, the character after x determines what effect is to be finished
    • $xc - stops color override: all following text will be rendered using the normal color of the text in question

License

Yeah, there's a license among the files. :P That's just me trying to get to the good habit of adding licensing to open source projects. The license allows you to do pretty much anything with CTR. If you want to do things "the right way", just copy the license among the files of the project where you use CTR, rename the file to CTR-LICENSE and mention CTR and me as its creator in your game's credits. But I'm not going to sue anyone for not doing so, rest assured. :lol:

Lastly

If anyone wants to use this, I'm more than willing to help, should you run into any problems. Just comment on this thread, PM me or @lcl me on the GE Discord, whichever you fancy. :D

Re: CTR - Custom Text Renderer

PostPosted: Fri Oct 22, 2021 4:46 am
by bat78
Very nice job, LcL!
It looks like you've done this exactly as you should, but I am not surprised. After all, you're so experienced already.
It's easy to use, fast, easy to set up and feature-rich.
In fact, I'd myself use this text framework for my projects.
-
I was wondering why don't you just listen to a key down event of ESC
where your exit handler will be to do all the cleanup. Why F12?
Before exiting, I think the system will execute all the code on the current frame and exit in the following frame.
-
You know if beneficial criticizing is allowed, I'll probably dig deep into coding practices and things like that :lol:

For example:
Code: Select all
int compareColors(Color *color1, Color *color2)
{
    if (!color1 || !color2) return -1;
    return (color1->r == color2->r && color1->g == color2->g && color1->b == color2->b);
}

Ignoring code formatting and readability, why not just `memcmp` those pointers?
It's not only much more readable and short, but also probably faster.
This is a good habit, not that it is guaranteed to be faster in an interpreted environment.

And so on, but I'm not doing this anymore, unless explicitly asked for :D
All in all, we all know that the code can be further improved in terms of performance, readability, safety and not any less important - size (especially important in gE),
but it's already easily in top 5 most useful APIs in gE :D