raysan5 / raylib

A simple and easy-to-use library to enjoy videogames programming
http://www.raylib.com
zlib License
22.28k stars 2.24k forks source link

[rshapes] `DrawRectangleLines()` misalignment #4386

Open XeroOl opened 6 days ago

XeroOl commented 6 days ago

I expected the DrawRectangleLines function to draw a closed rectangle, but it seems to have coordinates wrong, because it didn't exactly draw a rectangle.

image

Environment

Raylib Desktop. Arch Linux. AMD Radeon RX 6950 XT GPU Says it supports OpenGL 4.6, Not sure how to check which one raylib is using.

Code Example

#include "raylib.h"
int main(void) {
  InitWindow(640, 480, "game");
  SetTargetFPS(60);
  while (!WindowShouldClose()) {
    BeginDrawing();
      Camera2D camera = {};
      camera.zoom = 8.0;
      BeginMode2D(camera);  // large zoom to make the problem clear
        DrawRectangleLines(10, 10, 20, 20, WHITE);  // draw a rectangle outline
      EndMode2D();
    EndDrawing();
  }
  CloseWindow();
  return 0;
}
casperbear commented 6 days ago

There are two ways of drawing rectangle lines in OpenGL and Raysan5 is choosing between them. RayLib 5 implemented one way, RayLib master another. Both ways may produce visual inaccuracies depending on situation and GPU driver, plus the first way is slower. If you are using RayLib master and see visual artifacts, try re-implementing the first way from RayLib 5 and see if it works for you.

void DrawRectangleFrame(int posX, int posY, int width, int height, int thickness, Color color)
{
    // Based on https://github.com/raysan5/raylib/blob/29ce13b77736f4eb2fa6018d9c164d76c4df54bc/src/rshapes.c#L811

    DrawRectangle(posX, posY, width, thickness, color);
    DrawRectangle(posX + width - thickness, posY + thickness, thickness, height - 2 * thickness, color);
    DrawRectangle(posX, posY + height - thickness, width, thickness, color);
    DrawRectangle(posX, posY + thickness, thickness, height - 2 * thickness, color);
}
raysan5 commented 5 days ago

Actually this issue is still being reviewed, there is also another open issue and an open PR...