int
SDL_DrawRects(SDL_Surface * dst, const SDL_Rect ** rects, int count,
              Uint32 color)
{
    int i;

    for (i = 0; i < count; ++i) {
        if (SDL_DrawRect(dst, rects[i], color) < 0) {
            return -1;
        }
    }
    return 0;
}
Ejemplo n.º 2
0
/* Calls to commands created via [sdl.surface] are implemented by this
 * C command. */
static int JimSdlHandlerCommand(Jim_Interp *interp, int argc,
        Jim_Obj *const *argv)
{
    JimSdlSurface *jss = Jim_CmdPrivData(interp);
    int option;
    const char *options[] = {
        "free", "flip", "pixel", "rectangle", "box", "line", "aaline",
        "circle", "aacircle", "fcircle", NULL
    };
    enum {OPT_FREE, OPT_FLIP, OPT_PIXEL, OPT_RECTANGLE, OPT_BOX, OPT_LINE,
          OPT_AALINE, OPT_CIRCLE, OPT_AACIRCLE, OPT_FCIRCLE};

    if (argc < 2) {
        Jim_WrongNumArgs(interp, 1, argv, "method ?args ...?");
        return JIM_ERR;
    }
    if (Jim_GetEnum(interp, argv[1], options, &option, "SDL surface method",
                JIM_ERRMSG) != JIM_OK)
        return JIM_ERR;
    if (option == OPT_PIXEL) {
    /* PIXEL */
        long x, y, red, green, blue, alpha = 255;

        if (argc != 7 && argc != 8) {
            Jim_WrongNumArgs(interp, 2, argv, "x y red green blue ?alpha?");
            return JIM_ERR;
        }
        if (Jim_GetLong(interp, argv[2], &x) != JIM_OK ||
            Jim_GetLong(interp, argv[3], &y) != JIM_OK ||
            Jim_GetLong(interp, argv[4], &red) != JIM_OK ||
            Jim_GetLong(interp, argv[5], &green) != JIM_OK ||
            Jim_GetLong(interp, argv[6], &blue) != JIM_OK)
        {
            return JIM_ERR;
        }
        if (argc == 8 && Jim_GetLong(interp, argv[7], &alpha) != JIM_OK)
            return JIM_ERR;
        // pixelRGBA(jss->screen, x, y, red, green, blue, alpha);
        uint32_t color = 0;
        color |= alpha << 24;
        color |= red << 16;
        color |= green << 8;
        color |= blue;
        SDL_DrawPixel(jss->screen, x, y, color);
        return JIM_OK;
    } else if (option == OPT_RECTANGLE || option == OPT_BOX ||
               option == OPT_LINE || option == OPT_AALINE)
    {
    /* RECTANGLE, BOX, LINE, AALINE */
        long x1, y1, x2, y2, red, green, blue, alpha = 255;

        if (argc != 9 && argc != 10) {
            Jim_WrongNumArgs(interp, 2, argv, "x y red green blue ?alpha?");
            return JIM_ERR;
        }
        if (Jim_GetLong(interp, argv[2], &x1) != JIM_OK ||
            Jim_GetLong(interp, argv[3], &y1) != JIM_OK ||
            Jim_GetLong(interp, argv[4], &x2) != JIM_OK ||
            Jim_GetLong(interp, argv[5], &y2) != JIM_OK ||
            Jim_GetLong(interp, argv[6], &red) != JIM_OK ||
            Jim_GetLong(interp, argv[7], &green) != JIM_OK ||
            Jim_GetLong(interp, argv[8], &blue) != JIM_OK)
        {
            return JIM_ERR;
        }
        if (argc == 10 && Jim_GetLong(interp, argv[9], &alpha) != JIM_OK)
            return JIM_ERR;
        uint32_t color = 0;
        color |= alpha << 24;
        color |= red << 16;
        color |= green << 8;
        color |= blue;
        switch(option) {
        case OPT_RECTANGLE: {
          // rectangleRGBA(jss->screen, x1, y1, x2, y2, red, green, blue, alpha);
          SDL_Rect r = {
            .x = x1, .y = y1,
            .w = x2 - x1, .h = y2 - y1
          };
          SDL_DrawRect(jss->screen, &r, color);
          break;
        }
        case OPT_BOX: {
          // boxRGBA(jss->screen, x1, y1, x2, y2, red, green, blue, alpha);
          SDL_Point rect_poly[4] = {
            { .x = x1, .y = y1 },
            { .x = x2, .y = y1 },
            { .x = x2, .y = y2 },
            { .x = x1, .y = y2 }
          };
          SDL_FillPolygon(jss->screen, rect_poly, sizeof(rect_poly)/sizeof(rect_poly[0]), color);
          break;
        }