Now Reading
Easy Characteristic Gross Code | Angus Cheng

Easy Characteristic Gross Code | Angus Cheng

2023-12-07 01:29:21

A number of days in the past I talked about supporting textual content occasions with a number of colors and a number of textual content speeds. I spent about eight hours writing the code. It took me loads longer than I anticipated so I assumed I’d share the main points right here.

Instance Information

set_title(information, PHOENIX);
set_text_color(information, SKYBLUE);
add_show_text_V2(information, "They're saying the jay walker was... ");
set_text_color(information, RED);
add_show_text_V2(information, "you.");
set_text_color(information, SKYBLUE);

That is how I used to be anticipating to arrange the occasions. There are 4 distinct occasions there, which usually would require me to make 4 completely different occasion lists and 4 completely different occasion counts. As a substitute I received lazy and merged them into one referred to as SetTextConfig.

typedef struct SetTextConfig {
    const char *title;
    const char *physique;
    Coloration coloration;
    bool setColor;
    float secondsPerChar;
    enum Justification justification;
    bool clearText;
} SetTextConfig;

Dealing with Occasions

After we encounter a SET_TEXT_CONFIG occasion, we add it to an inventory of energetic SetTextConfig structs. If the occasion has a real clearText property, we reset the some textual content variables and ‘clear the record’.

else if (eventType == SET_TEXT_CONFIG) {
    SetTextConfig occasion = eventData->setTextConfigs[eventIndex];

    if (occasion.clearText) {
        information->textEventCount = 0;
        information->textTimer = 0.0;
        information->previousCharacterCount = 0;
    }
    else if (occasion.secondsPerChar > 0) {
        information->secondsPerChar = occasion.secondsPerChar;
    }
    else {
        int i = information->textEventCount++;
        information->textEvents[i] = occasion;
    }
}

The second if department is definitely incorrect. I haven’t applied variable textual content velocity but and rapidly wished these occasions to have an affect. Later I’ll wipe out that department and cope with secondsPerChar occasions someplace else.

Positioning Textual content

At this level we have now an inventory of energetic textual content occasions. All we have now to do is write some code that can format the textual content primarily based on these occasions. One thing that can return us an inventory structs like this.

typedef struct TextPiece { 
    char textual content[300];
    Vector2 place;
    Coloration coloration;
} TextPiece;

Final likelihood to click on away as a result of the next code is gross.

See Also

void position_text_pieces(
    GamePlayData *information, 
    Font font,
    float fontSize,
    float maxWidth,
    int *pieceCount,
    TextPiece items[10]
) {
    int pieceIndex = 0;
    Coloration coloration = WHITE;
    float x = 0;
    float y = 0;

    char line[300];
    int lineIndex = 0;
    char phrase[300];
    int wordIndex = 0;
    memset(line, 0, sizeof(line));
    memset(phrase, 0, sizeof(line));

    for (int i = 0; i < information->textEventCount; i++) {
        SetTextConfig occasion = information->textEvents[i];

        if (occasion.setColor) {
            coloration = occasion.coloration;
        }

        if (occasion.physique != NULL) {
            int bodyLength = TextLength(occasion.physique);

            for (int j = 0; j < bodyLength; j++) {
                char c = occasion.physique[j];

                if (c == 'n') {
                    
                }
                else if (c == ' ') {
                    phrase[wordIndex++] = c;
                    Vector2 wordDims = MeasureTextEx(font, phrase, fontSize, 0);
                    Vector2 lineDims = MeasureTextEx(font, line, fontSize, 0);

                    // Exceeded the maxWidth, create a TextPiece
                    if (x + wordDims.x + lineDims.x > maxWidth) {
                        add_text_piece(items, &pieceIndex, line, (Vector2){x, y}, coloration);

                        // Filter out the road
                        lineIndex = 0;
                        memset(line, 0, sizeof(line));

                        x = 0;
                        y += lineDims.y;
                    }

                    // Write the phrase to the road
                    for (int okay = 0; okay < wordIndex; okay++) {
                        line[lineIndex++] = phrase[k];
                    }
                    
                    memset(phrase, 0, sizeof(phrase));
                    wordIndex = 0;
                }
                else {
                    phrase[wordIndex++] = c;
                }
            }

            if (TextLength(phrase) > 0) {
                // Write the phrase to the road
                for (int okay = 0; okay < wordIndex; okay++) {
                    line[lineIndex++] = phrase[k];
                }

                memset(phrase, 0, sizeof(phrase));
                wordIndex = 0;
            }

            if (TextLength(line) > 0) {
                add_text_piece(items, &pieceIndex, line, (Vector2){x, y}, coloration);
                x += MeasureTextEx(font, line, fontSize, 0).x;
                
                // Filter out the road
                lineIndex = 0;
                memset(line, 0, sizeof(line));
            }
        }
    }

    *pieceCount = pieceIndex;
}

The concept is we need to maintain observe of the present textual content color and textual content place. It additionally wraps textual content phrase by phrase. Some concepts to scrub it up:

  1. String copying code that get replaced with strcpy calls.
  2. Final two if statements are duplicated code, strive get them into the internal loop
  3. Substitute float x and float y with. Vector2 place.

The code works although! I most likely wont change it, however I’d write some exams for it as a result of it’s difficult.

Source Link

What's Your Reaction?
Excited
0
Happy
0
In Love
0
Not Sure
0
Silly
0
View Comments (0)

Leave a Reply

Your email address will not be published.

2022 Blinking Robots.
WordPress by Doejo

Scroll To Top