Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[rshapes] DrawRectangleRounded draws a regular rectangle if width or height < 1 #4673

Open
4 tasks done
teatwig opened this issue Jan 9, 2025 · 4 comments
Open
4 tasks done

Comments

@teatwig
Copy link

teatwig commented Jan 9, 2025

  • I tested it on latest raylib version from master branch
  • I checked there is no similar issue already reported
  • I checked the documentation on the wiki
  • My code has no errors or misuse of raylib

Issue description

Since Rectangle accepts floats for width and height it was a bit surprising to me that you can't draw rounded rectangles that have either be smaller than 1.
See: https://github.com/raysan5/raylib/blob/master/src/rshapes.c#L887

My rectangle is drawn with world coordinates between 0.0 and 1.0, which are then scaled to the full size with Camera2D.

If I remove the check it works without issues, so I assume this is a leftover from some earlier behavior?

Environment

Linux, raylib 5.5

Code Example

I'm using the Elixir bindings, but it's really just any rectangle with width or height < 1:

        draw_rectangle_rounded(
          %S.Rectangle{
            x: 0.0,
            y: 0.0
            width: 0.5,
            height: 0.5
          },
          0.5,
          3,
          %S.Color{r: 0, g: 0, b: 0, a: 0}
        )
@raysan5
Copy link
Owner

raysan5 commented Jan 10, 2025

@teatwig Please, could you post some image of the resulting drawn rectangle? Afaik, it's not possible to really draw a sub-pixel element... but I could be wrong...

Also note that DrawRectangleRounded() was reviewed recently, you should test it with latest raylib version.

@teatwig
Copy link
Author

teatwig commented Jan 10, 2025

I tested it again with the latest commit from master (08b089f).

This is my program:

int main(void)
{
    const int width = 800;
    const int height = 600;

    InitWindow(width, height, "rounded rect test");

    Camera2D camera = { 0 };
    camera.zoom = height;

    Rectangle rect = { 0.1f, 0.1f, 0.8f, 0.8f };

    while (!WindowShouldClose())
    {
        BeginDrawing();
            ClearBackground(RAYWHITE);
            BeginMode2D(camera);

                DrawRectangleRounded(rect, 0.4f, 3, BLACK);

            EndMode2D();
        EndDrawing();
    }

    CloseWindow();
    return 0;
}

Unchanged raylib:

With the following patch that disables the check:

--- a/src/rshapes.c
+++ b/src/rshapes.c
@@ -884,7 +884,7 @@ void DrawRectangleLinesEx(Rectangle rec, float lineThick, Color color)
 void DrawRectangleRounded(Rectangle rec, float roundness, int segments, Color color)
 {
     // Not a rounded rectangle
-    if ((roundness <= 0.0f) || (rec.width < 1) || (rec.height < 1 ))
+    if (roundness <= 0.0f)
     {
         DrawRectangleRec(rec, color);
         return;

@raysan5
Copy link
Owner

raysan5 commented Jan 10, 2025

@teatwig should a pixel have rounded corners? please, could you explain some use-case?

@teatwig
Copy link
Author

teatwig commented Jan 10, 2025

I think we shouldn't think of the dimensions of Rectangle as pixels since you can use the camera to zoom in on it, thus making a rectangle that has a width of 0.1 show up as 100 pixels on screen.
For my project I decided to have world coordinates between 0 and 1 to make it easier to think of positioning as a percentage of the screen, so all shapes have a size of ~0.01.

I was mostly wondering why this check exists, and it's nice that I can adjust raylib so it works slightly better for my use case 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants