How do SDL_RenderSetScale and SDL_RenderDrawLine i

2019-02-27 10:52发布

问题:

From the short description of SDL_RenderSetScale() and SDL_RenderSetLogicalSize() on the SDL2 wiki I was under the impression that these commands scale the whole frame independent of its content, basically the way you would scale an image in a graphics editor.

And in my previous programs where I was only scaling and rendering SDL_Rects and SDL_Textures, that appeared to be the case. It is however only partially the case for drawing lines with RenderDrawLine().

The Line is scaled properly if it is parallel to either the x or y axis, but in any other case it is not scaled at all.

SDL_RenderSetScale( gRenderer, gScaler, gScaler ); 
SDL_SetRenderDrawColor( gRenderer, 0x00, 0x00, 0x00, 0xFF );
SDL_RenderClear( gRenderer );
SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
SDL_RenderDrawLine( gRenderer, x1, y1, x2, y2 );
SDL_RenderPresent( gRenderer );

Is there a way to scale the line regardless of angle with SDL_RenderSetScale()? How does the function decide what to scale and what to ignore?

回答1:

This is a known bug of SDL renderer, line drawing function doesn't scale then drawn at free angle. It's still present in recently released SDL-2.0.6 (see /* FIXME: We can't use a rect for this line... */ ):

static int
RenderDrawLinesWithRects(SDL_Renderer * renderer,
                     const SDL_Point * points, int count)
{
    SDL_FRect *frect;
    SDL_FRect *frects;
    SDL_FPoint fpoints[2];
    int i, nrects;
    int status;

    frects = SDL_stack_alloc(SDL_FRect, count-1);
    if (!frects) {
        return SDL_OutOfMemory();
    }

    status = 0;
    nrects = 0;
    for (i = 0; i < count-1; ++i) {
        if (points[i].x == points[i+1].x) {
            int minY = SDL_min(points[i].y, points[i+1].y);
            int maxY = SDL_max(points[i].y, points[i+1].y);

            frect = &frects[nrects++];
            frect->x = points[i].x * renderer->scale.x;
            frect->y = minY * renderer->scale.y;
            frect->w = renderer->scale.x;
            frect->h = (maxY - minY + 1) * renderer->scale.y;
        } else if (points[i].y == points[i+1].y) {
            int minX = SDL_min(points[i].x, points[i+1].x);
            int maxX = SDL_max(points[i].x, points[i+1].x);

            frect = &frects[nrects++];
            frect->x = minX * renderer->scale.x;
            frect->y = points[i].y * renderer->scale.y;
            frect->w = (maxX - minX + 1) * renderer->scale.x;
            frect->h = renderer->scale.y;
        } else {
            /* FIXME: We can't use a rect for this line... */
            fpoints[0].x = points[i].x * renderer->scale.x;
            fpoints[0].y = points[i].y * renderer->scale.y;
            fpoints[1].x = points[i+1].x * renderer->scale.x;
            fpoints[1].y = points[i+1].y * renderer->scale.y;
            status += renderer->RenderDrawLines(renderer, fpoints, 2);
        }
    }

    status += renderer->RenderFillRects(renderer, frects, nrects);

    SDL_stack_free(frects);

    if (status < 0) {
        status = -1;
    }
    return status;
}

I assume this won't be fixed anytime soon, since this is a rarely used feature.