Skip to content

Commit 6d4ba38

Browse files
committed
fix scaled scenelayer upload
1 parent 790fb3b commit 6d4ba38

File tree

1 file changed

+29
-39
lines changed

1 file changed

+29
-39
lines changed

Source/Entities/SceneLayer.cpp

Lines changed: 29 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "tracy/TracyOpenGL.hpp"
1414

1515
#include <array>
16+
#include <cmath>
1617

1718
using namespace RTE;
1819

@@ -415,50 +416,39 @@ void SceneLayerImpl<TRACK_DRAWINGS, STATIC_TEXTURE>::RegisterDrawing(const Vecto
415416

416417
template <bool TRACK_DRAWINGS, bool STATIC_TEXTURE>
417418
void SceneLayerImpl<TRACK_DRAWINGS, STATIC_TEXTURE>::UpdateTargetRegion(const Box& targetBox) {
419+
if constexpr (TRACK_DRAWINGS) {
420+
m_MainTexture->m_Bitmap = m_MainBitmap;
421+
}
418422
if constexpr (!STATIC_TEXTURE) {
419423
RTEAssert(bitmap_color_depth(m_MainBitmap) == 8, "Truecolor scenelayer used for non gpu drawing!");
420424
std::vector<Box> updateRegions{};
421-
m_MainTexture->m_Bitmap = m_MainBitmap;
422-
423-
if (m_ScaledDimensions.m_X < targetBox.m_Width && m_ScaledDimensions.m_Y < targetBox.m_Height) {
424-
// Bitmap will be in frame entirely, upload all.
425-
updateRegions.emplace_back(
426-
Vector(),
427-
m_MainBitmap->w,
428-
m_MainBitmap->h
429-
);
430-
} else {
431-
// Upload wrapped region
432-
433-
Box cornerWrapEither(
434-
m_Offset / m_ScaleFactor,
435-
std::min((m_ScaledDimensions.m_X - m_Offset.m_X) / m_ScaleFactor.m_X,targetBox.m_Width / m_ScaleFactor.m_X),
436-
std::min((m_ScaledDimensions.m_Y - m_Offset.m_Y) / m_ScaleFactor.m_Y, targetBox.m_Height / m_ScaleFactor.m_Y)
437-
);
438-
updateRegions.push_back(cornerWrapEither);
439-
440-
if (m_WrapX && cornerWrapEither.m_Width < targetBox.m_Width) {
441-
updateRegions.emplace_back(
442-
Vector(0, m_Offset.m_Y) / m_ScaleFactor,
443-
(targetBox.m_Width + m_Offset.m_X - m_ScaledDimensions.m_X) / m_ScaleFactor.m_X,
444-
std::min(m_ScaledDimensions.m_Y - m_Offset.m_Y, targetBox.m_Height) / m_ScaleFactor.m_Y
445-
);
446-
}
447-
if (m_WrapY && cornerWrapEither.m_Height < targetBox.m_Width) {
448-
updateRegions.emplace_back(
449-
Vector(m_Offset.m_X, 0) / m_ScaleFactor,
450-
std::min(m_ScaledDimensions.m_X - m_Offset.m_X, targetBox.m_Width) / m_ScaleFactor.m_X,
451-
(targetBox.m_Height + m_Offset.m_Y - m_ScaledDimensions.m_Y) / m_ScaleFactor.m_Y
452-
);
425+
float bitmapWidth = m_MainBitmap->w;
426+
float bitmapHeight = m_MainBitmap->h;
427+
int areaToCoverX = (m_Offset.GetFloorIntX() + targetBox.GetCorner().GetFloorIntX() + targetBox.GetWidth()) / m_ScaleFactor.m_X;
428+
int areaToCoverY = (m_Offset.GetFloorIntY() + targetBox.GetCorner().GetFloorIntY() + targetBox.GetHeight()) / m_ScaleFactor.m_Y;
429+
Box scaledTarget(targetBox.m_Corner / m_ScaleFactor, targetBox.m_Width / m_ScaleFactor.m_X, targetBox.m_Height / m_ScaleFactor.m_Y);
430+
Vector scaledOffset(m_Offset/m_ScaleFactor);
431+
Box bitmapDimensions(Vector(), bitmapWidth, bitmapHeight);
432+
433+
for (int tiledOffsetX = 0; tiledOffsetX < areaToCoverX;) {
434+
float destX = scaledTarget.GetCorner().GetFloorIntX() + tiledOffsetX - scaledOffset.GetFloorIntX();
435+
436+
for (int tiledOffsetY = 0; tiledOffsetY < areaToCoverY;) {
437+
float destY = scaledTarget.GetCorner().GetFloorIntY() + tiledOffsetY - scaledOffset.GetFloorIntY();
438+
Box update = bitmapDimensions.GetIntersection({-Vector(destX, destY), scaledTarget.m_Width, scaledTarget.m_Height});
439+
update.m_Corner = update.m_Corner.GetFloored();
440+
update.m_Width = std::ceilf(update.m_Width) + 1;
441+
update.m_Height = std::ceilf(update.m_Height) + 1;
442+
updateRegions.emplace_back(update);
443+
if (!m_WrapY) {
444+
break;
445+
}
446+
tiledOffsetY += bitmapHeight;
453447
}
454-
455-
if (m_WrapX && m_WrapY && cornerWrapEither.m_Height < targetBox.m_Height && cornerWrapEither.m_Width < targetBox.m_Width) {
456-
updateRegions.emplace_back(
457-
Vector(),
458-
(targetBox.m_Width + m_Offset.m_X - m_ScaledDimensions.m_X) / m_ScaleFactor.m_X,
459-
(targetBox.m_Height + m_Offset.m_Y - m_ScaledDimensions.m_Y) / m_ScaleFactor.m_Y
460-
);
448+
if (!m_WrapX) {
449+
break;
461450
}
451+
tiledOffsetX += bitmapWidth;
462452
}
463453

464454
for (auto& region: updateRegions) {

0 commit comments

Comments
 (0)