w4tch0ut
w4tch0ut

Reputation: 1

SDL doubts regarding rendering

I was going through this specific lesson.

gSpriteSheetTexture.render
    ( 
    ( SCREEN_WIDTH - currentClip->w ) / 2,
    ( SCREEN_HEIGHT - currentClip->h ) / 2,
    currentClip
    );

the first 2 arguments set the x position and y position of the destination rectangle(sprite clip). I did not understand why did he do (SCREEN_WIDTH - currentClip->w) / 2, when he just could've written SCREEN_WIDTH / 2?

Here's the render function:

void LTexture::render(int x, int y, SDL_Rect* clip)
{
    SDL_Rect renderQuad = { x, y, mWidth, mHeight };
    if (clip != NULL)
    {
        renderQuad.w = clip->w;
        renderQuad.h = clip->h;
    }

    SDL_RenderCopy(gRenderer, mTexture, clip, &renderQuad);
}

Upvotes: 0

Views: 55

Answers (1)

Julian Declercq
Julian Declercq

Reputation: 1596

The teacher did that to make sure you the image is nicely centered on the screen.

If you would use SCREEN_WIDTH / 2, the image would start at the middle of the screen and thus not be entirely centered. Therefore, he subtracts half of the width of the image, which will make sure that the left half of the image is drawn just left of the middle of the screen, and the right half is drawn just right of the middle of the screen.

Here is simple illustration. Let me know if my answer helped you!

enter image description here

Upvotes: 1

Related Questions