Skip to content

Commit 328e4c3

Browse files
committed
fix scaled scenelayer drawing
1 parent b244da8 commit 328e4c3

File tree

2 files changed

+43
-35
lines changed

2 files changed

+43
-35
lines changed

Source/Entities/SceneLayer.cpp

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "GLResourceMan.h"
99
#include "BigTexture.h"
1010

11+
#include "Draw.h"
1112
#include "tracy/Tracy.hpp"
1213
#include "tracy/TracyOpenGL.hpp"
1314

@@ -418,7 +419,7 @@ void SceneLayerImpl<TRACK_DRAWINGS, STATIC_TEXTURE>::UpdateTargetRegion(const Bo
418419
std::vector<Box> updateRegions{};
419420
m_MainTexture->m_Bitmap = m_MainBitmap;
420421

421-
if (m_MainBitmap->w < targetBox.m_Width && m_MainBitmap->h < targetBox.m_Height) {
422+
if (m_ScaledDimensions.m_X < targetBox.m_Width && m_ScaledDimensions.m_Y < targetBox.m_Height) {
422423
// Bitmap will be in frame entirely, upload all.
423424
updateRegions.emplace_back(
424425
Vector(),
@@ -429,32 +430,32 @@ void SceneLayerImpl<TRACK_DRAWINGS, STATIC_TEXTURE>::UpdateTargetRegion(const Bo
429430
// Upload wrapped region
430431

431432
Box cornerWrapEither(
432-
m_Offset,
433-
std::min(m_MainBitmap->w - m_Offset.m_X,targetBox.m_Width),
434-
std::min(m_MainBitmap->h - m_Offset.m_Y, targetBox.m_Height)
433+
m_Offset / m_ScaleFactor,
434+
std::min((m_ScaledDimensions.m_X - m_Offset.m_X) / m_ScaleFactor.m_X,targetBox.m_Width / m_ScaleFactor.m_X),
435+
std::min((m_ScaledDimensions.m_Y - m_Offset.m_Y) / m_ScaleFactor.m_Y, targetBox.m_Height / m_ScaleFactor.m_Y)
435436
);
436437
updateRegions.push_back(cornerWrapEither);
437438

438439
if (m_WrapX && cornerWrapEither.m_Width < targetBox.m_Width) {
439440
updateRegions.emplace_back(
440-
Vector(0, m_Offset.m_Y),
441-
targetBox.m_Width + m_Offset.m_X - m_MainBitmap->w,
442-
std::min(m_MainBitmap->h - m_Offset.m_Y, targetBox.m_Height)
441+
Vector(0, m_Offset.m_Y) / m_ScaleFactor,
442+
(targetBox.m_Width + m_Offset.m_X - m_ScaledDimensions.m_X) / m_ScaleFactor.m_X,
443+
std::min(m_ScaledDimensions.m_Y - m_Offset.m_Y, targetBox.m_Height) / m_ScaleFactor.m_Y
443444
);
444445
}
445446
if (m_WrapY && cornerWrapEither.m_Height < targetBox.m_Width) {
446447
updateRegions.emplace_back(
447-
Vector(m_Offset.m_X, 0),
448-
std::min(m_MainBitmap->w - m_Offset.m_X, targetBox.m_Width),
449-
targetBox.m_Height + m_Offset.m_Y - m_MainBitmap->h
448+
Vector(m_Offset.m_X, 0) / m_ScaleFactor,
449+
std::min(m_ScaledDimensions.m_X - m_Offset.m_X, targetBox.m_Width) / m_ScaleFactor.m_X,
450+
(targetBox.m_Height + m_Offset.m_Y - m_ScaledDimensions.m_Y) / m_ScaleFactor.m_Y
450451
);
451452
}
452453

453454
if (m_WrapX && m_WrapY && cornerWrapEither.m_Height < targetBox.m_Height && cornerWrapEither.m_Width < targetBox.m_Width) {
454455
updateRegions.emplace_back(
455456
Vector(),
456-
targetBox.m_Width + m_Offset.m_X - m_MainBitmap->w,
457-
targetBox.m_Height + m_Offset.m_Y - m_MainBitmap->h
457+
(targetBox.m_Width + m_Offset.m_X - m_ScaledDimensions.m_X) / m_ScaleFactor.m_X,
458+
(targetBox.m_Height + m_Offset.m_Y - m_ScaledDimensions.m_Y) / m_ScaleFactor.m_Y
458459
);
459460
}
460461
}
@@ -511,34 +512,28 @@ void SceneLayerImpl<TRACK_DRAWINGS, STATIC_TEXTURE>::DrawTiled(const Box& target
511512
int areaToCoverX = m_Offset.GetFloorIntX() + targetBox.GetCorner().GetFloorIntX() + std::min(targetDimensions.GetWidth(), targetBox.GetWidth());
512513
int areaToCoverY = m_Offset.GetFloorIntY() + targetBox.GetCorner().GetFloorIntY() + std::min(targetDimensions.GetHeight(), targetBox.GetHeight());
513514

515+
if (!m_DrawMasked) {
516+
rlDrawRenderBatchActive();
517+
int maskedUniformLocation = rlGetLocationUniform(rlGetShaderCurrent(), "drawMasked");
518+
rlEnableShader(rlGetShaderCurrent());
519+
glUniform1i(maskedUniformLocation, 0);
520+
}
521+
514522
for (int tiledOffsetX = 0; tiledOffsetX < areaToCoverX;) {
515523
float destX = targetBox.GetCorner().GetFloorIntX() + tiledOffsetX - m_Offset.GetFloorIntX();
516524

517525
for (int tiledOffsetY = 0; tiledOffsetY < areaToCoverY;) {
518526
float destY = targetBox.GetCorner().GetFloorIntY() + tiledOffsetY - m_Offset.GetFloorIntY();
519-
520-
if (!drawScaled) {
521-
if constexpr (STATIC_TEXTURE) {
522-
DrawTexture(g_GLResourceMan.GetStaticTextureFromBitmap(m_MainBitmap), destX, destY, {255, 255, 255, 255});
523-
} else {
524-
m_MainTexture->Draw(
525-
{0.f, 0.f, static_cast<float>(m_MainBitmap->w), static_cast<float>(m_MainBitmap->h)},
526-
{destX, destY, static_cast<float>(m_MainBitmap->w), static_cast<float>(m_MainBitmap->h)}
527-
);
528-
}
527+
if constexpr (STATIC_TEXTURE) {
528+
DrawTexturePro(
529+
g_GLResourceMan.GetStaticTextureFromBitmap(m_MainBitmap),
530+
{0.0f, 0.0f, static_cast<float>(m_MainBitmap->w), static_cast<float>(m_MainBitmap->h)},
531+
{destX, destY, bitmapWidth, bitmapHeight},
532+
{0.0f, 0.0f}, 0.0f, {255, 255, 255, 255});
529533
} else {
530-
if constexpr (STATIC_TEXTURE) {
531-
DrawTexturePro(
532-
g_GLResourceMan.GetStaticTextureFromBitmap(m_MainBitmap),
533-
{0.0f, 0.0f, static_cast<float>(m_MainBitmap->w), static_cast<float>(m_MainBitmap->h)},
534-
{destX, destY, bitmapWidth, bitmapHeight},
535-
{0.0f, 0.0f}, 0.0f, {255, 255, 255, 255});
536-
} else {
537-
m_MainTexture->Draw(
538-
{0.0f, 0.0f, static_cast<float>(m_MainBitmap->w), static_cast<float>(m_MainBitmap->h)},
539-
{destX, destY, bitmapWidth, bitmapHeight}
540-
);
541-
}
534+
m_MainTexture->Draw(
535+
{0.0f, 0.0f, static_cast<float>(m_MainBitmap->w), static_cast<float>(m_MainBitmap->h)},
536+
{destX, destY, bitmapWidth, bitmapHeight});
542537
}
543538
if (!m_WrapY) {
544539
break;
@@ -550,6 +545,13 @@ void SceneLayerImpl<TRACK_DRAWINGS, STATIC_TEXTURE>::DrawTiled(const Box& target
550545
}
551546
tiledOffsetX += bitmapWidth;
552547
}
548+
549+
if (!m_DrawMasked) {
550+
rlDrawRenderBatchActive();
551+
int drawMaskedUniform = rlGetLocationUniform(rlGetShaderCurrent(), "drawMasked");
552+
rlEnableShader(rlGetShaderCurrent());
553+
glUniform1i(drawMaskedUniform, 1);
554+
}
553555
}
554556

555557
template <bool TRACK_DRAWINGS, bool STATIC_TEXTURE>

Source/Renderer/raylib/rlgl.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,7 @@ typedef struct rlDrawCall {
407407
int vertexAlignment; // Number of vertex required for index alignment (LINES, TRIANGLES)
408408
//unsigned int vaoId; // Vertex array id to be used on the draw -> Using RLGL.currentBatch->vertexBuffer.vaoId
409409
//unsigned int shaderId; // Shader id to be used on the draw -> Using RLGL.currentShaderId
410+
//rlUniformValue* shaderUniforms;
410411
unsigned int textureId; // Texture id to be used on the draw -> Use to create new draw call if changes
411412

412413
//Matrix projection; // Projection matrix for this draw -> Using RLGL.projection by default
@@ -779,6 +780,7 @@ RLAPI void rlSetUniformMatrix(int locIndex, RLMatrix mat);
779780
RLAPI void rlSetUniformMatrices(int locIndex, const RLMatrix *mat, int count); // Set shader value matrices
780781
RLAPI void rlSetUniformSampler(int locIndex, unsigned int textureId); // Set shader value sampler
781782
RLAPI void rlSetShader(unsigned int id, int *locs); // Set shader currently active (id and locations)
783+
RLAPI unsigned int rlGetShaderCurrent();
782784

783785
// Compute shader management
784786
RLAPI unsigned int rlLoadComputeShaderProgram(unsigned int shaderId); // Load compute shader program
@@ -3224,7 +3226,7 @@ unsigned int rlLoadTexture(const void *data, int width, int height, int format,
32243226
#endif
32253227
#endif // GRAPHICS_API_OPENGL_11
32263228

3227-
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
3229+
//glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
32283230

32293231
glGenTextures(1, &id); // Generate texture id
32303232

@@ -4412,6 +4414,10 @@ void rlSetShader(unsigned int id, int *locs)
44124414
#endif
44134415
}
44144416

4417+
unsigned int rlGetShaderCurrent() {
4418+
return RLGL.State.currentShaderId;
4419+
}
4420+
44154421
// Load compute shader program
44164422
unsigned int rlLoadComputeShaderProgram(unsigned int shaderId)
44174423
{

0 commit comments

Comments
 (0)