Skip to content

Commit 009912c

Browse files
committed
Implement y flip in DrawTexturePro
For some reason, only the X flip was implemented. This implements the Y flip and adds tests
1 parent 343b1ae commit 009912c

3 files changed

Lines changed: 93 additions & 12 deletions

File tree

include/raygpu.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,6 +1351,9 @@ typedef struct ProgramInfo{
13511351
EXTERN_C_BEGIN
13521352

13531353
RGAPI void InitWindow(int width, int height, const char* title);
1354+
RGAPI SubWindow InitWindow_SDL3 (int width, int height, const char* title);
1355+
RGAPI SubWindow InitWindow_GLFW(int width, int height, const char* title);
1356+
RGAPI SubWindow InitWindow_RGFW(int width, int height, const char* title);
13541357
RGAPI void InitProgram(ProgramInfo program);
13551358
RGAPI void requestAnimationFrameLoopWithJSPI(void (*callback)(void), int /* unused */, int/* unused */);
13561359
RGAPI void requestAnimationFrameLoopWithJSPIArg(void (*callback)(void*), void* userData, int/* unused */, int/* unused */);
@@ -1361,7 +1364,6 @@ RGAPI SubWindow OpenSubWindow (int width, int height, const char* title);
13611364
RGAPI SubWindow OpenSubWindow_SDL3(int width, int height, const char* title);
13621365
RGAPI SubWindow OpenSubWindow_GLFW(int width, int height, const char* title);
13631366
RGAPI SubWindow OpenSubWindow_RGFW(int width, int height, const char* title);
1364-
RGAPI SubWindow InitWindow_SDL3 (int width, int height, const char* title);
13651367
RGAPI void CloseSubWindow(SubWindow subWindow);
13661368
RGAPI void CloseSubWindow_SDL3(SubWindow subWindow);
13671369
RGAPI void CloseSubWindow_GLFW(SubWindow subWindow);
@@ -1434,8 +1436,6 @@ RGAPI int GetMonitorHeight_GLFW(cwoid);
14341436
RGAPI void SetWindowShouldClose_GLFW(GLFWwindow* win);
14351437
RGAPI void Initialize_SDL3(cwoid);
14361438
RGAPI bool WindowShouldClose_GLFW(GLFWwindow* win);
1437-
RGAPI SubWindow InitWindow_GLFW(int width, int height, const char* title);
1438-
RGAPI SubWindow InitWindow_RGFW(int width, int height, const char* title);
14391439
RGAPI void ToggleFullscreen_GLFW(cwoid);
14401440
RGAPI void ToggleFullscreen_SDL3(cwoid);
14411441

src/rshapes.c

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2485,9 +2485,10 @@ void DrawTexturePro(Texture texture, Rectangle source, Rectangle dest, Vector2 o
24852485
float height = (float)texture.height;
24862486

24872487
bool flipX = false;
2488+
bool flipY = false;
24882489

24892490
if (source.width < 0) { flipX = true; source.width *= -1; }
2490-
if (source.height < 0) source.y -= source.height;
2491+
if (source.height < 0) { flipY = true; source.height *= -1; }
24912492

24922493
Vector2 topLeft = {0};
24932494
Vector2 topRight = {0};
@@ -2539,23 +2540,31 @@ void DrawTexturePro(Texture texture, Rectangle source, Rectangle dest, Vector2 o
25392540
//rlNormal3f(0.0f, 0.0f, 1.0f); // Normal vector pointing towards viewer
25402541

25412542
// Top-left corner for texture and quad
2542-
if (flipX) rlTexCoord2f((source.x + source.width)/width, source.y/height);
2543-
else rlTexCoord2f(source.x/width, source.y/height);
2543+
rlTexCoord2f(
2544+
flipX ? (source.x + source.width)/width : source.x/width,
2545+
flipY ? (source.y + source.height)/height : source.y/height
2546+
);
25442547
rlVertex2f(topLeft.x, topLeft.y);
25452548

25462549
// Bottom-left corner for texture and quad
2547-
if (flipX) rlTexCoord2f((source.x + source.width)/width, (source.y + source.height)/height);
2548-
else rlTexCoord2f(source.x/width, (source.y + source.height)/height);
2550+
rlTexCoord2f(
2551+
flipX ? (source.x + source.width)/width : source.x/width,
2552+
flipY ? source.y/height : (source.y + source.height)/height
2553+
);
25492554
rlVertex2f(bottomLeft.x, bottomLeft.y);
25502555

25512556
// Bottom-right corner for texture and quad
2552-
if (flipX) rlTexCoord2f(source.x/width, (source.y + source.height)/height);
2553-
else rlTexCoord2f((source.x + source.width)/width, (source.y + source.height)/height);
2557+
rlTexCoord2f(
2558+
flipX ? source.x/width : (source.x + source.width)/width,
2559+
flipY ? source.y/height : (source.y + source.height)/height
2560+
);
25542561
rlVertex2f(bottomRight.x, bottomRight.y);
25552562

25562563
// Top-right corner for texture and quad
2557-
if (flipX) rlTexCoord2f(source.x/width, source.y/height);
2558-
else rlTexCoord2f((source.x + source.width)/width, source.y/height);
2564+
rlTexCoord2f(
2565+
flipX ? source.x/width : (source.x + source.width)/width,
2566+
flipY ? (source.y + source.height)/height : source.y/height
2567+
);
25592568
rlVertex2f(topRight.x, topRight.y);
25602569

25612570
rlEnd();

tests/rg_tests.cpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,75 @@ TEST_F(VisualTest, DrawGradientRec) {
4444

4545
UnloadRenderTexture(target);
4646
}
47+
48+
TEST_F(VisualTest, DrawTextureProFlipY) {
49+
// Create a checkerboard texture to test flipping
50+
Image img = GenImageChecker(RED, BLUE, 64, 64, 8);
51+
Texture tex = LoadTextureFromImage(img);
52+
UnloadImage(img);
53+
54+
RenderTexture target = LoadRenderTexture(128, 128);
55+
56+
CommandBuffer cb = {0};
57+
BeginCommandBuffer(&cb);
58+
59+
BeginTextureMode(target);
60+
ClearBackground(BLACK);
61+
// Draw normal texture on left
62+
DrawTexturePro(tex,
63+
CLITERAL(Rectangle){0, 0, 64, 64}, // source
64+
CLITERAL(Rectangle){0, 0, 64, 64}, // dest
65+
CLITERAL(Vector2){0, 0}, // origin
66+
0.0f, WHITE); // rotation, tint
67+
// Draw vertically flipped texture on right
68+
DrawTexturePro(tex,
69+
CLITERAL(Rectangle){0, 0, 64, -64}, // negative height for vertical flip
70+
CLITERAL(Rectangle){64, 0, 64, 64}, // dest
71+
CLITERAL(Vector2){0, 0}, // origin
72+
0.0f, WHITE); // rotation, tint
73+
EndTextureMode();
74+
75+
EndCommandBuffer(&cb);
76+
SubmitCommandBuffer(&cb);
77+
78+
VerifyBuffer(target, "draw_texture_pro_flip_y");
79+
80+
UnloadTexture(tex);
81+
UnloadRenderTexture(target);
82+
}
83+
84+
TEST_F(VisualTest, DrawTextureProFlipX) {
85+
// Create a checkerboard texture to test flipping
86+
Image img = GenImageChecker(RED, BLUE, 64, 64, 8);
87+
Texture tex = LoadTextureFromImage(img);
88+
UnloadImage(img);
89+
90+
RenderTexture target = LoadRenderTexture(128, 128);
91+
92+
CommandBuffer cb = {0};
93+
BeginCommandBuffer(&cb);
94+
95+
BeginTextureMode(target);
96+
ClearBackground(BLACK);
97+
// Draw normal texture on top
98+
DrawTexturePro(tex,
99+
CLITERAL(Rectangle){0, 0, 64, 64}, // source
100+
CLITERAL(Rectangle){0, 0, 64, 64}, // dest
101+
CLITERAL(Vector2){0, 0}, // origin
102+
0.0f, WHITE); // rotation, tint
103+
// Draw horizontally flipped texture on bottom
104+
DrawTexturePro(tex,
105+
CLITERAL(Rectangle){0, 0, -64, 64}, // negative width for horizontal flip
106+
CLITERAL(Rectangle){0, 64, 64, 64}, // dest
107+
CLITERAL(Vector2){0, 0}, // origin
108+
0.0f, WHITE); // rotation, tint
109+
EndTextureMode();
110+
111+
EndCommandBuffer(&cb);
112+
SubmitCommandBuffer(&cb);
113+
114+
VerifyBuffer(target, "draw_texture_pro_flip_x");
115+
116+
UnloadTexture(tex);
117+
UnloadRenderTexture(target);
118+
}

0 commit comments

Comments
 (0)