Immediate-Mode-UI / Nuklear

A single-header ANSI C immediate mode cross-platform GUI library
https://immediate-mode-ui.github.io/Nuklear/doc/index.html
Other
9.2k stars 557 forks source link

Extra work to render polylines? #352

Open Andersama opened 3 years ago

Andersama commented 3 years ago

Looking at the api, it appears as though when rendering polygons / polylines there's work being done to allocate data for the buffer, then to cast data to shorts, then later cast back to floats. I just made a quick tweak by adding an extra command to pass along a float* since all the draw calls appear to expect floating point data anyway, removed the casts. Appears to be a pretty nice improvement. Unless I'm missing something important about how those functions work.


    struct nk_command_polyline_float {
        struct nk_command header;
        struct nk_color color;
        unsigned short line_thickness;
        unsigned short point_count;
        struct nk_vec2 *points; //struct nk_vec2 points[1];
    };
    //...
    NK_API void
nk_stroke_polyline_float(struct nk_command_buffer* b, float* points, int point_count,
    float line_thickness, struct nk_color col)
{
    int i;
    nk_size size = 0;
    struct nk_command_polyline_float *cmd;

    NK_ASSERT(b);
    if (!b || col.a == 0 || line_thickness <= 0) return;
    size = sizeof(*cmd);
    //+ sizeof(float) * 2 * (nk_size)point_count;
    cmd = (struct nk_command_polyline_float*)nk_command_buffer_push(b, NK_COMMAND_POLYLINE_FLOAT, size);
    if (!cmd) return;
    cmd->color = col;
    cmd->point_count = (unsigned short)point_count;
    cmd->line_thickness = (unsigned short)line_thickness;
    cmd->points = (struct nk_vec2*)points;
}.
//...
        case NK_COMMAND_POLYLINE_FLOAT: {
            const struct nk_command_polyline_float* p = (const struct nk_command_polyline_float*)cmd;
            nk_draw_list_stroke_poly_line(&ctx->draw_list,
                p->points, p->point_count, p->color, NK_STROKE_OPEN, p->line_thickness, (&ctx->draw_list)->config.line_AA);
        }

Also reduces the size of the command struct being allocated as we don't need to allocate any additional data.

Hejsil commented 3 years ago

Hi. This might be something good to get in, but from the code sample it is a little hard to see what is original and what has changed. Can you post a diff or just open a PR? I'll have a look then and see if it makes sense

Andersama commented 3 years ago

Sure, I'll see what I can do, I just made edits to the single header file, so I'm not 100% clear on where the code I edited was, I just added it in. struct nk_command_polyline_float was underneath the definition for struct nk_command_polyline, nk_stroke_polyline_float underneath the definition for nk_stroke_polyline, and I added to the command enum NK_COMMAND_POLYLINE_FLOAT.

Give me couple hours I'll get back with a PR, trying to make the program I put together a bit more error tolerant.

Update: here's the pull request #354 does somewhat violate the rule of passing data by value over reference/pointer, but it's kind of the point.