Fixed container scaling.

This commit is contained in:
Pieter Hulshoff 2016-12-29 17:31:47 +01:00
parent ea9722cd63
commit 22ef2b31fe

View File

@ -273,20 +273,23 @@ bool SDL::renderCopy(SDL_Texture *texture, float alpha, SDL_Rect *src, SDL_Rect
SDL_Rect srcRect; SDL_Rect srcRect;
SDL_Rect dstRect; SDL_Rect dstRect;
SDL_Rect srcRectCopy;
SDL_Rect dstRectCopy;
double scaleX;
double scaleY;
SDL_Rect rotateRect; dstRect.w = dest->w;
rotateRect.w = dest->w; dstRect.h = dest->h;
rotateRect.h = dest->h;
if(fullscreen_) if(fullscreen_)
{ {
rotateRect.x = dest->x + (displayWidth_ - windowWidth_)/2; dstRect.x = dest->x + (displayWidth_ - windowWidth_)/2;
rotateRect.y = dest->y + (displayHeight_ - windowHeight_)/2; dstRect.y = dest->y + (displayHeight_ - windowHeight_)/2;
} }
else else
{ {
rotateRect.x = dest->x; dstRect.x = dest->x;
rotateRect.y = dest->y; dstRect.y = dest->y;
} }
// Create the base fields to check against the container. // Create the base fields to check against the container.
@ -307,44 +310,58 @@ bool SDL::renderCopy(SDL_Texture *texture, float alpha, SDL_Rect *src, SDL_Rect
srcRect.w = w; srcRect.w = w;
srcRect.h = h; srcRect.h = h;
} }
dstRect.x = rotateRect.x;
dstRect.y = rotateRect.y; // Define the scale
dstRect.w = rotateRect.w; scaleX = (dstRect.w > 0) ? static_cast<double>(srcRect.w) / static_cast<double>(dstRect.w) : 0.0;
dstRect.h = rotateRect.h; scaleY = (dstRect.h > 0) ? static_cast<double>(srcRect.h) / static_cast<double>(dstRect.h) : 0.0;
// Make a copy
srcRectCopy.x = srcRect.x;
srcRectCopy.y = srcRect.y;
srcRectCopy.w = srcRect.w;
srcRectCopy.h = srcRect.h;
dstRectCopy.x = dstRect.x;
dstRectCopy.y = dstRect.y;
dstRectCopy.w = dstRect.w;
dstRectCopy.h = dstRect.h;
// If a container has been defined, limit the display to the container boundaries. // If a container has been defined, limit the display to the container boundaries.
if (viewInfo.ContainerWidth > 0 && viewInfo.ContainerHeight > 0 && if (viewInfo.ContainerWidth > 0 && viewInfo.ContainerHeight > 0 &&
rotateRect.w > 0 && rotateRect.h > 0) dstRectCopy.w > 0 && dstRectCopy.h > 0)
{ {
// Correct if the image falls to the left of the container // Correct if the image falls to the left of the container
if (dstRect.x < viewInfo.ContainerX) if (dstRect.x < viewInfo.ContainerX)
{ {
dstRect.x = static_cast<int>(viewInfo.ContainerX); dstRect.x = static_cast<int>(viewInfo.ContainerX);
srcRect.x = srcRect.x + srcRect.w * (dstRect.x - rotateRect.x) / rotateRect.w; dstRect.w = dstRectCopy.w + dstRectCopy.x - dstRect.x;
srcRect.x = srcRectCopy.x + srcRectCopy.w * (dstRect.x - dstRectCopy.x) / dstRectCopy.w;
} }
// Correct if the image falls to the right of the container // Correct if the image falls to the right of the container
if (rotateRect.x + rotateRect.w > viewInfo.ContainerX + viewInfo.ContainerWidth) if ((dstRectCopy.x + dstRectCopy.w) > (viewInfo.ContainerX + viewInfo.ContainerWidth))
{ {
dstRect.w = static_cast<int>(viewInfo.ContainerX + viewInfo.ContainerWidth) - dstRect.x; dstRect.w = static_cast<int>(viewInfo.ContainerX + viewInfo.ContainerWidth) - dstRect.x;
srcRect.w = srcRect.w * dstRect.w / rotateRect.w;
} }
// Correct if the image falls to the top of the container // Correct if the image falls to the top of the container
if (dstRect.y < viewInfo.ContainerY) if (dstRect.y < viewInfo.ContainerY)
{ {
dstRect.y = static_cast<int>(viewInfo.ContainerY); dstRect.y = static_cast<int>(viewInfo.ContainerY);
srcRect.y = srcRect.y + srcRect.h * (dstRect.y - rotateRect.y) / rotateRect.h; dstRect.h = dstRectCopy.h + dstRectCopy.y - dstRect.y;
srcRect.y = srcRectCopy.y + srcRectCopy.h * (dstRect.y - dstRectCopy.y) / dstRectCopy.h;
} }
// Correct if the image falls to the bottom of the container // Correct if the image falls to the bottom of the container
if (rotateRect.y + rotateRect.h > viewInfo.ContainerY + viewInfo.ContainerHeight) if ((dstRectCopy.y + dstRectCopy.h) > (viewInfo.ContainerY + viewInfo.ContainerHeight))
{ {
dstRect.h = static_cast<int>(viewInfo.ContainerY + viewInfo.ContainerHeight) - dstRect.y; dstRect.h = static_cast<int>(viewInfo.ContainerY + viewInfo.ContainerHeight) - dstRect.y;
srcRect.h = srcRect.h * dstRect.h / rotateRect.h;
} }
// Define source width and height
srcRect.w = static_cast<int>(dstRect.w * scaleX);
srcRect.h = static_cast<int>(dstRect.h * scaleY);
} }
SDL_SetTextureAlphaMod(texture, static_cast<char>(alpha * 255)); SDL_SetTextureAlphaMod(texture, static_cast<char>(alpha * 255));
@ -352,34 +369,34 @@ bool SDL::renderCopy(SDL_Texture *texture, float alpha, SDL_Rect *src, SDL_Rect
if (viewInfo.Reflection == "top") if (viewInfo.Reflection == "top")
{ {
rotateRect.h = static_cast<unsigned int>(static_cast<float>(rotateRect.h) * viewInfo.ReflectionScale); dstRect.h = static_cast<unsigned int>(static_cast<float>(dstRect.h) * viewInfo.ReflectionScale);
rotateRect.y = rotateRect.y - rotateRect.h - viewInfo.ReflectionDistance; dstRect.y = dstRect.y - dstRect.h - viewInfo.ReflectionDistance;
SDL_SetTextureAlphaMod(texture, static_cast<char>(viewInfo.ReflectionAlpha * alpha * 255)); SDL_SetTextureAlphaMod(texture, static_cast<char>(viewInfo.ReflectionAlpha * alpha * 255));
SDL_RenderCopyEx(getRenderer(), texture, src, &rotateRect, viewInfo.Angle, NULL, SDL_FLIP_VERTICAL); SDL_RenderCopyEx(getRenderer(), texture, src, &dstRect, viewInfo.Angle, NULL, SDL_FLIP_VERTICAL);
} }
if (viewInfo.Reflection == "bottom") if (viewInfo.Reflection == "bottom")
{ {
rotateRect.y = rotateRect.y + rotateRect.h + viewInfo.ReflectionDistance; dstRect.y = dstRect.y + dstRect.h + viewInfo.ReflectionDistance;
rotateRect.h = static_cast<unsigned int>(static_cast<float>(rotateRect.h) * viewInfo.ReflectionScale); dstRect.h = static_cast<unsigned int>(static_cast<float>(dstRect.h) * viewInfo.ReflectionScale);
SDL_SetTextureAlphaMod(texture, static_cast<char>(viewInfo.ReflectionAlpha * alpha * 255)); SDL_SetTextureAlphaMod(texture, static_cast<char>(viewInfo.ReflectionAlpha * alpha * 255));
SDL_RenderCopyEx(getRenderer(), texture, src, &rotateRect, viewInfo.Angle, NULL, SDL_FLIP_VERTICAL); SDL_RenderCopyEx(getRenderer(), texture, src, &dstRect, viewInfo.Angle, NULL, SDL_FLIP_VERTICAL);
} }
if (viewInfo.Reflection == "left") if (viewInfo.Reflection == "left")
{ {
rotateRect.w = static_cast<unsigned int>(static_cast<float>(rotateRect.w) * viewInfo.ReflectionScale); dstRect.w = static_cast<unsigned int>(static_cast<float>(dstRect.w) * viewInfo.ReflectionScale);
rotateRect.x = rotateRect.x - rotateRect.w - viewInfo.ReflectionDistance; dstRect.x = dstRect.x - dstRect.w - viewInfo.ReflectionDistance;
SDL_SetTextureAlphaMod(texture, static_cast<char>(viewInfo.ReflectionAlpha * alpha * 255)); SDL_SetTextureAlphaMod(texture, static_cast<char>(viewInfo.ReflectionAlpha * alpha * 255));
SDL_RenderCopyEx(getRenderer(), texture, src, &rotateRect, viewInfo.Angle, NULL, SDL_FLIP_HORIZONTAL); SDL_RenderCopyEx(getRenderer(), texture, src, &dstRect, viewInfo.Angle, NULL, SDL_FLIP_HORIZONTAL);
} }
if (viewInfo.Reflection == "right") if (viewInfo.Reflection == "right")
{ {
rotateRect.x = rotateRect.x + rotateRect.w + viewInfo.ReflectionDistance; dstRect.x = dstRect.x + dstRect.w + viewInfo.ReflectionDistance;
rotateRect.w = static_cast<unsigned int>(static_cast<float>(rotateRect.w) * viewInfo.ReflectionScale); dstRect.w = static_cast<unsigned int>(static_cast<float>(dstRect.w) * viewInfo.ReflectionScale);
SDL_SetTextureAlphaMod(texture, static_cast<char>(viewInfo.ReflectionAlpha * alpha * 255)); SDL_SetTextureAlphaMod(texture, static_cast<char>(viewInfo.ReflectionAlpha * alpha * 255));
SDL_RenderCopyEx(getRenderer(), texture, src, &rotateRect, viewInfo.Angle, NULL, SDL_FLIP_HORIZONTAL); SDL_RenderCopyEx(getRenderer(), texture, src, &dstRect, viewInfo.Angle, NULL, SDL_FLIP_HORIZONTAL);
} }
return true; return true;