Snippets

Peter Finlayson cut-down Pygame surface blit code

Created by Peter Finlayson
/*this internal blit function is accessable through the C api*/
int
PySurface_Blit (PyObject * dstobj, PyObject * srcobj, SDL_Rect * dstrect,
                SDL_Rect * srcrect, int the_args)
{
    SDL_Surface *src = PySurface_AsSurface (srcobj);
    SDL_Surface *dst = PySurface_AsSurface (dstobj);
    int result;

    result = SDL_BlitSurface (src, srcrect, dst, dstrect);

    if (result == -1)
        RAISE (PyExc_SDLError, SDL_GetError ());
    if (result == -2)
        RAISE (PyExc_SDLError, "Surface was lost");

    return result != 0;
}


static PyObject*
surf_blit (PyObject *self, PyObject *args, PyObject *keywds)
{
    SDL_Surface *src, *dest = PySurface_AsSurface (self);
    GAME_Rect *src_rect, temp;
    PyObject *srcobject, *argpos, *argrect = NULL;
    int dx, dy, result;
    SDL_Rect dest_rect, sdlsrc_rect;
    int sx, sy;
    int the_args = 0;

    static char *kwids[] = {"source", "dest", "area", "special_flags", NULL};
    if (!PyArg_ParseTupleAndKeywords (args, keywds, "O!O|Oi", kwids,
                                      &PySurface_Type, &srcobject, &argpos,
                                      &argrect, &the_args))
        return NULL;

    src = PySurface_AsSurface (srcobject);

    if ((src_rect = GameRect_FromObject (argpos, &temp))) {
        dx = src_rect->x;
        dy = src_rect->y;
    }
    else if (TwoIntsFromObj (argpos, &sx, &sy)) {
        dx = sx;
        dy = sy;
    }
    else
        return RAISE (PyExc_TypeError, "invalid destination position for blit");

    temp.x = temp.y = 0;
    temp.w = src->w;
    temp.h = src->h;
    src_rect = &temp;

    dest_rect.x = (short) dx;
    dest_rect.y = (short) dy;
    dest_rect.w = (unsigned short) src_rect->w;
    dest_rect.h = (unsigned short) src_rect->h;
    sdlsrc_rect.x = (short) src_rect->x;
    sdlsrc_rect.y = (short) src_rect->y;
    sdlsrc_rect.w = (unsigned short) src_rect->w;
    sdlsrc_rect.h = (unsigned short) src_rect->h;

    if (!the_args)
        the_args = 0;

    result = PySurface_Blit (self, srcobject, &dest_rect, &sdlsrc_rect,
                             the_args);
    if (result != 0)
        return NULL;

    //return PyRect_New (&dest_rect);
    Py_RETURN_NONE;
}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.