diff --git a/examples/shapes/shapes_polygon_lines.c b/examples/shapes/shapes_polygon_lines.c new file mode 100644 index 000000000..fc42688e8 --- /dev/null +++ b/examples/shapes/shapes_polygon_lines.c @@ -0,0 +1,118 @@ +/******************************************************************************************* +* +* raylib [shapes] example - polygon lines +* +* Example complexity rating: [★★☆☆] 2/4 +* +* Example originally created with raylib 6.1, last time updated with raylib 6.1 +* +* Example contributed by Matthew Roush (@MatthewRoush) and reviewed by Ramon Santamaria (@raysan5) +* +* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified, +* BSD-like license that allows static linking with closed source software +* +* Copyright (c) 2026 Matthew Roush (@MatthewRoush) +* +********************************************************************************************/ + +#include "raylib.h" + +//------------------------------------------------------------------------------------ +// Program main entry point +//------------------------------------------------------------------------------------ +int main(void) +{ + // Initialization + //-------------------------------------------------------------------------------------- + const int screenWidth = 800; + const int screenHeight = 450; + + InitWindow(screenWidth, screenHeight, "raylib [shapes] example - polygon lines"); + + int thick = 2; + float rotation = 0.0f; + + SetTargetFPS(60); + //-------------------------------------------------------------------------------------- + + // Main game loop + while (!WindowShouldClose()) // Detect window close button or ESC key + { + // Update + //---------------------------------------------------------------------------------- + if (IsKeyPressed(KEY_UP)) thick = thick + 1; + if (IsKeyPressed(KEY_DOWN)) thick = thick - 1; + + thick = (thick < 1) ? 1 : (thick > 60) ? 60 : thick; + + rotation += (360.0f/10.0f)*GetFrameTime(); + //---------------------------------------------------------------------------------- + + // Draw + //---------------------------------------------------------------------------------- + BeginDrawing(); + + ClearBackground(RAYWHITE); + + DrawText(TextFormat("thick = %d", thick), 10, 280, 20, LIME); + + // The square and circle outlines in the top left should match + // The fist pair of outlines are drawn using `DrawRectangleLinesEx()` and `DrawCircleLinesEx()` + // The second pair are drawn using `DrawPolyLinesEx()` with parameters that should visually match the first pair + + DrawText("These should look identical!", 10, 10, 20, MAROON); + + // Outline pair 1 + DrawRectangle(10, 40, 50, 50, LIGHTGRAY); + DrawRectangleLinesEx((Rectangle){ 10, 40, 50, 50 }, (float)thick, RED); + + DrawCircle(95, 65, 25, LIGHTGRAY); + DrawCircleLinesEx((Vector2){ 95, 65 }, 25, (float)thick, RED); + + DrawText("DrawRectangleLinesEx() and DrawCircleLinesEx()", 130, 60, 10, BLACK); + + // Outline pair 2 + DrawPoly((Vector2){ 35, 125 }, 4, 35.355f, 45, LIGHTGRAY); + DrawPolyLinesEx((Vector2){ 35, 125 }, 4, 35.355f, 45, (float)thick, RED); + + DrawPoly((Vector2){ 95, 125 }, 36, 25, 0, LIGHTGRAY); + DrawPolyLinesEx((Vector2){ 95, 125 }, 36, 25, 0, (float)thick, RED); + + DrawText("DrawPolyLinesEx()", 130, 120, 10, BLACK); + + // Some other shapes, all of these outlines should have the same looking thickness + DrawPoly((Vector2){ 290, 220 }, 3, 60, rotation, LIGHTGRAY); + DrawPolyLinesEx((Vector2){ 290, 220 }, 3, 60, rotation, (float)thick, BLUE); + + DrawPoly((Vector2){ 430, 220 }, 4, 60, rotation, LIGHTGRAY); + DrawPolyLinesEx((Vector2){ 430, 220 }, 4, 60, rotation, (float)thick, BLUE); + + DrawPoly((Vector2){ 570, 220 }, 5, 60, rotation, LIGHTGRAY); + DrawPolyLinesEx((Vector2){ 570, 220 }, 5, 60, rotation, (float)thick, BLUE); + + DrawPoly((Vector2){ 710, 220 }, 6, 60, rotation, LIGHTGRAY); + DrawPolyLinesEx((Vector2){ 710, 220 }, 6, 60, rotation, (float)thick, BLUE); + + DrawPoly((Vector2){ 290, 360 }, 7, 60, rotation, LIGHTGRAY); + DrawPolyLinesEx((Vector2){ 290, 360 }, 7, 60, rotation, (float)thick, BLUE); + + DrawPoly((Vector2){ 430, 360 }, 8, 60, rotation, LIGHTGRAY); + DrawPolyLinesEx((Vector2){ 430, 360 }, 8, 60, rotation, (float)thick, BLUE); + + DrawPoly((Vector2){ 570, 360 }, 9, 60, rotation, LIGHTGRAY); + DrawPolyLinesEx((Vector2){ 570, 360 }, 9, 60, rotation, (float)thick, BLUE); + + DrawPoly((Vector2){ 710, 360 }, 10, 60, rotation, LIGHTGRAY); + DrawPolyLinesEx((Vector2){ 710, 360 }, 10, 60, rotation, (float)thick, BLUE); + + EndDrawing(); + //---------------------------------------------------------------------------------- + } + + // De-Initialization + //-------------------------------------------------------------------------------------- + CloseWindow(); // Close window and OpenGL context + //-------------------------------------------------------------------------------------- + + return 0; +} diff --git a/examples/shapes/shapes_polygon_lines.png b/examples/shapes/shapes_polygon_lines.png new file mode 100644 index 000000000..83ec926ae Binary files /dev/null and b/examples/shapes/shapes_polygon_lines.png differ diff --git a/src/rlgl.h b/src/rlgl.h index be24bbb78..44341137e 100644 --- a/src/rlgl.h +++ b/src/rlgl.h @@ -3755,7 +3755,7 @@ void *rlReadTexturePixels(unsigned int id, int width, int height, int format) glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels); #else // Reading data as original texture format, in some platforms (RPI, Wasm) it works - pixels = (unsigned char *)RL_MALLOC(GetPixelDataSize(width, height, format)); + pixels = (unsigned char *)RL_MALLOC(rlGetPixelDataSize(width, height, format)); unsigned int glInternalFormat = 0, glFormat = 0, glType = 0; rlGetGlTextureFormats(format, &glInternalFormat, &glFormat, &glType); glReadPixels(0, 0, width, height, glFormat, glType, pixels); diff --git a/src/rshapes.c b/src/rshapes.c index 76fb0c3c2..cc994b99e 100644 --- a/src/rshapes.c +++ b/src/rshapes.c @@ -631,7 +631,9 @@ void DrawRectangleLines(int posX, int posY, int width, int height, Color color) // Draw rectangle outline with line thickness void DrawRectangleLinesEx(Rectangle rec, float thick, Color color) { - if ((thick > rec.width) || (thick > rec.height)) + if (thick <= 0.0f) return; + + if ((thick > rec.width/2) || (thick > rec.height/2)) { if (rec.width >= rec.height) thick = rec.height/2; else if (rec.width <= rec.height) thick = rec.width/2; diff --git a/src/rtext.c b/src/rtext.c index d138f1f90..93e34a6f6 100644 --- a/src/rtext.c +++ b/src/rtext.c @@ -1987,8 +1987,8 @@ char *TextInsert(const char *text, const char *insert, int position) // TODO: Allow copying data inserted up to maximum buffer size and stop for (int i = 0; i < position; i++) buffer[i] = text[i]; - for (int i = position; i < insertLen + position; i++) buffer[i] = insert[i - position]; - for (int i = (insertLen + position); i < (textLen + insertLen); i++) buffer[i] = text[i - insertLen]; + for (int i = 0; i < insertLen; i++) buffer[i+position] = insert[i]; + for (int i = position; i < textLen; i++) buffer[i+insertLen] = text[i]; buffer[textLen + insertLen] = '\0'; // Add EOL }