Allegro.cc - Online Community

Allegro.cc Forums » Game Design & Concepts » Format of Text/String

This thread is locked; no one can reply to it. rss feed Print
Format of Text/String
RmBeer2
Member #16,660
April 2017
avatar

This's a design problem that I can't solve.

Normally a normal text string is made up of a succession of bytes (characters) until ending with byte 0.

What I wanted to do here is add some function bytes that would format the text, which is quite useful for formatting text from a simple string of characters.
Also like typography, size, color, style, and others.

Then i build this list of bytes that can change the format of the text, this bytes only work from 1 to 8 and 11 to 31:

General Format: (byte)
0 - End Text
9 - Tabs
10 - Jump of Line

Format: (byte)
1: Letter Color
[1b] - color
2: Background Color
[1b] - color
3: Complete Color
[1b] - color (low nibble:Letter Color/high nibble:Background Color)
4: Delete the current line with the format
5: Active Bold
6: Deactivate Bold
7: Active Underline
8: Deactivate Underline
11: Active Italic
12: Deactivate Italic
13: Typography (in conjunction with size)
[1b] - Number of Typography in the list

The font with the size, since in Allegro both are defined during the loading of the font, and said loaded font can be assigned in the list as an indexed number.

The problem of design here is that byte 1,2,3, and 13, can have byte 0 as the function parameter, and continue reading text, which is no longer compatible with the normal text format, which always ends with 0.
You could strictly define end it with byte 0, or create a new type of format for the text, which would cause problems trying to continue or end the byte 0.

How would you design the format in a text? My goal is to maintain a function for each control byte, although Im open to suggestions.

EDIT ================

The idea of this is mostly to speed up the process of formatting text during rendering and not render as much from the commands or from the text. So you wouldn't have to process so many fragmented texts either.

🌈🌈🌈 🌟 BlackRook WebSite (Only valid from my installer) 🌟 C/C++ 🌟 GNU/Linux 🌟 IceCream/Cornet 🌟 🌈🌈🌈

Rm Beer for Emperor 2021! Rm Beer for Ruinous Slave Drained 2022! Rm Beer for Traveler From The Future Warning Not To Enter In 2023! Rm Beer are building a travel machine for Go Back from 2023! Rm Beer in an apocalyptic world burning hordes of Zombies in 2024!

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

If I was doing it, I would use two strings, one for text and the other for format. Since format is variable length and needs to sometime operate on the same index, I would encode everything in a format string or a special format data type.

amarillion
Member #940
January 2001
avatar

Could you take some inspiration from UTF-8 to encode the function parameters?

Basically you'd spread out the bits of the function parameters. For example, you could encode 12 bits as: 10xxxxxx 10xxxxxx or 18 bits as 10xxxxxx 10xxxxxx 10xxxxxx.

It takes a bit of shuffling, but you'd never end up with a zero in the middle of your string.

torhu
Member #2,727
September 2002
avatar

What is this going to be used for? If fast rendering is the point, you should probably prerender, or adapt to whatever the rendering needs. Which is probably not going to be about parsing anything, but about commands sent to the graphics API.

RmBeer2
Member #16,660
April 2017
avatar

@amarillion :

I had already thought about it. I have no problem with UTF-8, it is what I intend to use. Nor would I have problems with byte 1 and 2, only if the high nibble is filled with garbage, but this is not the case for byte 3 (Could it be the case of using byte 0 as a letter and a background in black at some point?). For byte 13, bit 7 could be kept active constantly and use the rest of the bits as index , which would only leave 128 fonts available instead of 256.

@torhu :

Processing will be noticeably easier when rendering occurs as short as possible while reading the text string. Especially for example if you need to read the text over and over again, for example, render the text in multiple lines in case said box (or window) changes size in real time.

EDIT ==================

This is probably nonsense, and it is better to use chunked texts in memory to speed up the process, but it may be convenient to have the original string in mixed text format.
Let's say that a paragraph or line of chat text would be much more uncomfortable if it was also fragmented between text and format.

🌈🌈🌈 🌟 BlackRook WebSite (Only valid from my installer) 🌟 C/C++ 🌟 GNU/Linux 🌟 IceCream/Cornet 🌟 🌈🌈🌈

Rm Beer for Emperor 2021! Rm Beer for Ruinous Slave Drained 2022! Rm Beer for Traveler From The Future Warning Not To Enter In 2023! Rm Beer are building a travel machine for Go Back from 2023! Rm Beer in an apocalyptic world burning hordes of Zombies in 2024!

Peter Hull
Member #1,136
March 2001

If it were me I would take inspiration from ANSI escape codes
https://en.wikipedia.org/wiki/ANSI_escape_code
The full spec is very complicated but basically use a control code to introduce the sequence, followed by actual data which is normal text. For example for letter color 0, instead of [0x01, 0x00] have the color in 2 hex digits [0x01, '0', '0']. Parsing 2 hex digits would contribute a negligible time to your program, I bet. And as Torhu says, you could pre-render it, if it became a significant overhead.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

bamccaig
Member #7,536
July 2006
avatar

A more precise solution is to bind the buffer length and string length into a struct or object. This requires every write to the buffer to update the string length. And then you can store anything without needing to treat anything magical. I think that bstrlib is considered good? No need to reinvent the wheel.

RmBeer2
Member #16,660
April 2017
avatar

@bamccaig :

I already thought to use ALLEGRO_USTR.
You already have the size in the structure and a set of functions to work with strings.

I've been planning on doing the final render for formatted multiline text for a long time, but laziness always wins.

🌈🌈🌈 🌟 BlackRook WebSite (Only valid from my installer) 🌟 C/C++ 🌟 GNU/Linux 🌟 IceCream/Cornet 🌟 🌈🌈🌈

Rm Beer for Emperor 2021! Rm Beer for Ruinous Slave Drained 2022! Rm Beer for Traveler From The Future Warning Not To Enter In 2023! Rm Beer are building a travel machine for Go Back from 2023! Rm Beer in an apocalyptic world burning hordes of Zombies in 2024!

Go to: