Skip to content

Optimize Lua graphics #13

@toncho11

Description

@toncho11
  1. We can remove luaL_checknumber() for example and replace it like that:

// only Lua 5.1
#define fast_arg_number(L, n)  (nvalue((L)->base + ((n) - 1)))
#define fast_arg_int(L, n)     ((int)fast_arg_number((L), (n)))

static int luaB_plot_line(lua_State *L)
{
    int x1 = fast_arg_int(L, 1);
    int y1 = fast_arg_int(L, 2);
    int x2 = fast_arg_int(L, 3);
    int y2 = fast_arg_int(L, 4);
    int color = fast_arg_int(L, 5);

    /* draw line here */

    return 0;
}
  1. Add fill_rect(x, y, w, h, color), currently only plot_line() is available. Both the movement up/down is 2 lines as it is the ball, so we can save 2 calls in both.

We can also have a special rectangle_delta() function that both deletes and draws a rectangle. This will furhter reduce the calls, but in the best case scenario we need such function in Nano-x as well.

static void gfx_fill_rect(int x, int y, int w, int h, int color)
{
    int yy;

    if (w <= 0 || h <= 0)
        return;

    for (yy = 0; yy < h; yy++) {
        gfx_line(x, y + yy, x + w - 1, y + yy, color);
    }
}

static int luaB_rectangle_delta(lua_State *L)
{
    int rx = (int)lua_tointeger(L, 1);
    int ry = (int)lua_tointeger(L, 2);
    int rw = (int)lua_tointeger(L, 3);
    int rh = (int)lua_tointeger(L, 4);

    int ax = (int)lua_tointeger(L, 5);
    int ay = (int)lua_tointeger(L, 6);
    int aw = (int)lua_tointeger(L, 7);
    int ah = (int)lua_tointeger(L, 8);

    int fg = (int)lua_tointeger(L, 9);
    int bg = (int)lua_tointeger(L, 10);

    /* Remove old exposed part. */
    gfx_fill_rect(rx, ry, rw, rh, bg);

    /* Draw new exposed part. */
    gfx_fill_rect(ax, ay, aw, ah, fg);

    return 0;
}
  1. Optimise the sleep() and event handling. This is more about design.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions