#Raylib DrawTextPro problems with formatting

1 messages · Page 1 of 1 (latest)

junior zephyr
#

So I asked a similar question before, and so far I've had no problems. Here's the code.

    pub fn add_combat_log(self : *InfoLog, attacker : *ent.Entity, victim : *ent.Entity, damage : usize) void {
        const count = comptime std.fmt.count("{s} took {} damage from {s}!", 
            .{"A very large name", 255, "A very large name"});
        var buf : [count + 1]u8 = undefined;
        const text = std.fmt.bufPrintZ(&buf, "{s} took {} damage from {s}", 
            .{victim.name, damage, attacker.name}) catch unreachable;
        std.debug.print("{s}", .{text});
        self.add_log(text);
    }

And here is how I draw the log

pub fn render(per_data : *pData.PersistentData) void {
    for(0..hud_cfg.LOG_MAX) |l| {
        const text = per_data.log.log[l];
        const y : f32 = @floatFromInt(hud_cfg.LOG_AREA_Y + hud_cfg.LOG_AREA_HEIGHT - hud_cfg.BORDER_SIZE -
            (l * hud_cfg.LOG_TEXT_SEPARATOR + 10));
        const x : f32 = hud_cfg.LOG_AREA_X + 10;
        if(text.len > 0) {
            rl.DrawTextPro(
                per_data.get_font().*,
                text.ptr, 
                rl.Vector2 {
                    .x = x, 
                    .y = y, 
                },
                rl.Vector2 {
                    .x = 0,
                    .y = 0,
                },
                0,
                hud_cfg.LOG_TEXT_SIZE,
                2, 
                rl.YELLOW
            );
        }
    }
}

For some reason, the drawn text is completely wrong and weird characters. But the debug printed text to the console is correct. I don't understand.

fallen crown
#

Wrong and weird exactly how? What font are you using? Please share a pic.

junior zephyr
#

The yellow text

#

And the regular message logs work fine, using the same font. It's just the combat logs don't work right.

fallen crown
#

You haven't shown the code that calls the rendered output, and where the text comes from. I'm fairly certain the issues is the content of your string, and not how you're rendering.

junior zephyr
#

The text comes from this

//Target is attacked by attacker, return true if target dies
pub fn attack(_ : *states.State, per_data : *pData.PersistentData, target : *ent.Entity, attacker : *ent.Entity) bool {
    //Take damage
    const damage = attacker.combat_data.damage;
    target.vital_data.current_hp -= damage;
    
    //Use the flash shader on the target
    target.combat_data.flashing = true;

    //Add combat log
    per_data.log.add_combat_log(attacker, target, damage);

    //If target is dead, set target marked for death by cleanup, also make invisible until then
    if(target.vital_data.current_hp <= 0) {
        target.active = false;
        target.alive = false;
        target.render_data.visible = false;
        return true;
    }
    else return false;
}
#

And..

#
pub const InfoLog = struct {
    log : [hud_cfg.LOG_MAX][]const u8,

    pub fn add_log(self : *InfoLog, m : []const u8) void {
        self.log[5] = self.log[4];
        self.log[4] = self.log[3];
        self.log[3] = self.log[2];
        self.log[2] = self.log[1];
        self.log[1] = self.log[0];
        self.log[0] = m;
    }

    pub fn add_combat_log(self : *InfoLog, attacker : *ent.Entity, victim : *ent.Entity, damage : i8) void {
        const count = comptime std.fmt.count("{s} took {} damage from {s}!", 
            .{"A very large name", 255, "A very large name"});
        var buf : [count + 1]u8 = undefined;
        const text = std.fmt.bufPrintZ(&buf, "{s} took {} damage from {s}!", 
            .{victim.name, damage, attacker.name}) catch unreachable;
        std.debug.print("{s}", .{text});
        self.add_log(text);
    }

    pub fn add_message_log(self : *InfoLog, m : []const u8) void {
        self.add_log(m);
    }
};

pub fn new_info_log() InfoLog {
    var info_log = InfoLog {
        .log = undefined,
    };

    for(0..hud_cfg.LOG_MAX) |l| {
        info_log.log[l] = "";
    }

    return info_log;
}

The entire struct.

#

The self.log thing is uh..yeah I haven't looked up how to use arrays completely correctly yet. 😅

fallen crown
#

The buffer your text is formatted into is on the stack. When it's logged directly by debug.print it's fine - you're in the stack frame, but then that is added to the log, so the text is referring to data that is no longer valid. The text will need to be allocated, or add_log (which I don't think you've shown) needs to copy it into the log's buffer when it gets rendered.

junior zephyr
#

Oohh~! That makes sense. 😄