Esempio n. 1
0
static PyObject*
_gfx_rotozoomsize (PyObject *self, PyObject *args)
{
    PyObject *obj = NULL;
    int w, h, dstw, dsth;
    double angle, zoom;

    if (!PyArg_ParseTuple (args, "iidd:rotozoom_size", &w, &h, &angle, &zoom))
    {
        PyErr_Clear ();
        if (PyArg_ParseTuple (args, "Odd:rotozoom_size", &obj, &angle, &zoom))
        {
            if (PySDLSurface_Check (obj))
            {
                w = ((PySDLSurface *)obj)->surface->w;
                h = ((PySDLSurface *)obj)->surface->w;
            }
            else
            {
                if (!SizeFromObj (obj, (pgint32*)&w, (pgint32*)&h))
                    return NULL;
            }
        }
        else
            return NULL;
    }
    rotozoomSurfaceSize (w, h, angle, zoom, &dstw, &dsth);
    return Py_BuildValue ("(ii)", dstw, dsth);
}
Esempio n. 2
0
void sprite_gen_rotation(Sprite *sprite)
{
    rotozoomSurfaceSize(
            sprite->frame_size.x,
            sprite->frame_size.y,
            45, // to maximize size
            ZOOM,  // no zoom
            &sprite->rotated_frame_size.x,
            &sprite->rotated_frame_size.y
            );

    if(sprite->rotated)
        SDL_FreeSurface(sprite->rotated);

    sprite->rotated = SDL_CreateRGBSurface(SDL_HWSURFACE, 
            sprite->rotated_frame_size.x * sprite->frame_count,
            sprite->rotated_frame_size.y * ACTION_COUNT * 360/ANGLE_STEP,
            RGBA_FORMAT);
    if(sprite->rotated == NULL) {
        fprintf(stderr, "CreateRGBSurface failed: %s\n", SDL_GetError());
        exit(1);
    }
    //printf("cache size %dx%d for %d angles\n", sprite->rotated->w, sprite->rotated->h, 360/ANGLE_STEP);

    SDL_Surface *element = SDL_CreateRGBSurface(SDL_HWSURFACE, 
            sprite->frame_size.x, 
            sprite->frame_size.y,
            RGBA_FORMAT);

    SDL_SetAlpha(sprite->source,0,0xff);
    SDL_SetAlpha(element,0,0xff);
    SDL_SetAlpha(sprite->rotated,SDL_SRCALPHA,0xff);

    int frame, action, angle;
    for(action=0; action<ACTION_COUNT; action++) {
        for(frame=0; frame<sprite->frame_count; frame++) {
            SDL_Rect src;
            sprite_origin_rect(sprite, action, frame, &src);
            for(angle=0; angle<360; angle+=ANGLE_STEP) {
                SDL_Rect dst;
                sprite_rotated_rect(sprite, action, frame, angle, &dst);
                SDL_FillRect(element, NULL, 0x00000000);
                SDL_BlitSurface( sprite->source, &src, element, NULL );
                SDL_Surface *rotozoom = rotozoomSurface(element, angle+90, ZOOM, SMOOTHING_ON); // XXX 90 offset, because the sprite images are looking downwards
                SDL_SetAlpha(rotozoom,0,0);
                SDL_SetColorKey(rotozoom,0,0);
                dst.x += dst.w/2 - rotozoom->w/2;
                dst.y += dst.h/2 - rotozoom->h/2; // center
                SDL_BlitSurface(rotozoom, NULL, sprite->rotated, &dst );
                SDL_FreeSurface(rotozoom);
            }
        }
    }

    SDL_FreeSurface(element);
}