#Translatec: C union of structs stored as zig variables having garbage values at runtime.(non web)

1 messages · Page 1 of 1 (latest)

steady crag
#

storing the return value of the nvgRGBA in a zig variable or constant leads to it being garbage. Even when correct value is passed see 3rd case the values at runtime from zig side seem fine but not rendering which could mean it passing garbage to c functions.
nanovg.c:

{
    NVGcolor color;
    // Use longer initialization to suppress warning.
    color.r = r / 255.0f;
    color.g = g / 255.0f;
    color.b = b / 255.0f;
    color.a = a / 255.0f;
    return color;
}```
and nvgColor struct is:
```// nanovg.h
struct NVGcolor {
    union {
        float rgba[4];
        struct {
            float r,g,b,a;
        };
    };
};
typedef struct NVGcolor NVGcolor;
// c.zig
pub const struct_NVGcolor = extern struct {
    unnamed_0: union_unnamed_1 = @import("std").mem.zeroes(union_unnamed_1),
    pub const nvgLerpRGBA = __root.nvgLerpRGBA;
    pub const nvgTransRGBA = __root.nvgTransRGBA;
    pub const nvgTransRGBAf = __root.nvgTransRGBAf;
};
pub const NVGcolor = struct_NVGcolor;

this works:

    c.nvgBeginPath(vg);
    c.nvgRect(vg, 0, kb_y, screen_w, kb_h);
    c.nvgFillColor(vg, c.nvgRGBA(0, 255, 0, 180));
    c.nvgFill(vg);

this dosent:(when i check the value its garbage on desktop and mobile)

    //...
    c.nvgFillColor(vg, col);
    c.nvgFill(vg);

this also doesn't work:

    const col: c.NVGcolor = .{
        .unnamed_0 = .{ .unnamed_0 = .{
            .r = @as(f32, @floatFromInt(r_)) / 255.0,
            .g = @as(f32, @floatFromInt(g_)) / 255.0,
            .b = @as(f32, @floatFromInt(b_)) / 255.0,
            .a = @as(f32, @floatFromInt(a_)) / 255.0,
        } },
    };
    return col;
}
//and calling it here:
    //...
    c.nvgFillColor(vg, rgba(0, 255, 0, 180)); 

Please if this is a zig issue anyone please help me fix it

#

3rd case values are not garbage. but still does not render anything which i could assume c side being garbage. for which i could not debug.

warm wind
#

Could be a translate-c issue with the C unions involved here.

lusty hedge
#

You can attach the code files for people to look at. I think it would help people make traction on figuring out what's wrong.

I know you did that in your previous version of the question too, which I told you wasn't a good question. But this question is much better. In this case it's clear you actually have the problem narrowed down so people know what code they should be looking at.

steady crag
lusty hedge
#

I think we're in very different time zones. I'll check that out tomorrow afternoon (3am for me right now)

steady crag
#

update tried it like this:

const std = @import("std");

pub fn draw(vg: *c.NVGcontext, screen_w: f32, screen_h: f32, dpi: f32) void {
    _ = dpi;
    const kb_h = if (screen_w > screen_h) screen_h * 0.45 else screen_h * 0.35;
    const kb_y = screen_h - kb_h;

    c.nvgSave(vg);
    defer c.nvgRestore(vg);

    c.nvgResetTransform(vg);
    c.nvgGlobalAlpha(vg, 1.0);
    c.nvgScissor(vg, 0, 0, screen_w, screen_h);

    const col: Color = rgba(255, 0, 0, 180);

    std.log.info("Color zig: {}", .{col});
    std.log.info("Color c direct def: {}", .{col.toNVG()});
    const colc = col.toNVG();
    std.log.info("Color c zig var: {}", .{colc});
    std.log.info("col c direct call {}", .{c.nvgRGBAf(col.r, col.g, col.b, col.a)});

    // const std = @import("std");
    // Panel background
    c.nvgBeginPath(vg);
    c.nvgRect(vg, 0, kb_y, screen_w, kb_h);
    c.nvgFillColor(vg, c.nvgRGBAf(col.r, col.g, col.b, col.a)); // only this worked remaining all combos failed.

    c.nvgFillColor(vg, colc);
    c.nvgFill(vg);
}

const Color = struct {
    r: f32,
    g: f32,
    b: f32,
    a: f32,

    fn init(r: u8, g: u8, b: u8, a: u8) Color {
        return .{
            .r = @as(f32, @floatFromInt(r)) / 255.0,
            .g = @as(f32, @floatFromInt(g)) / 255.0,
            .b = @as(f32, @floatFromInt(b)) / 255.0,
            .a = @as(f32, @floatFromInt(a)) / 255.0,
        };
    }

    fn toNVG(self: Color) c.NVGcolor {
        return c.nvgRGBAf(self.r, self.g, self.b, self.a);
    }
};

fn rgba(r: u8, g: u8, b: u8, a: u8) Color {
    return .{
        .r = @as(f32, @floatFromInt(r)) / 255.0,
        .g = @as(f32, @floatFromInt(g)) / 255.0,
        .b = @as(f32, @floatFromInt(b)) / 255.0,
        .a = @as(f32, @floatFromInt(a)) / 255.0,
    };
}
fn rgbaf(r: f32, g: f32, b: f32, a: f32) Color {
    return .{
        .r = r,
        .g = g,
        .b = b,
        .a = a,
    };
}