Method DrawRectanglePro() not rendering rectangle

Viewed 72

I have been trying to test out raylib and I am trying to render rectangles at an angle, but I am unsure as to why they will not render. The method DrawRectangle() does work, despite DrawRectanglePro() not working.

#include <iostream>
#include "raylib.h"

int main() {
    InitWindow(800, 800, "More Raylib Practice");
    SetWindowState(FLAG_VSYNC_HINT);
    
    while (!WindowShouldClose()) {
        BeginDrawing();
        ClearBackground(BLACK);
        
        DrawRectangle(10, 10, 10, 10, RED); //normal rectangle DOES render

        Rectangle rec = { 10, 10, 10, 10 };
        DrawRectanglePro(rec, { 400, 400 }, (float)45, WHITE); //angled rectangle doesn't?
        
        EndDrawing();
    }

    CloseWindow();
    return 0;
}
1 Answers

It looks like you're misusing the origin parameter, in your case { 400, 400 }. The origin parameter is used to set the origin of the draw point from the referenced rectangle.

Your code is saying, on a 10 x 10 square, move right 400, and down 400, then draw that at 10, 10. If you setup a 2d camera you would find your white triangle 400 ish pixels up and to the left, off screen.

Most of the time you want to change an origin is to the center of the rectangle or texture. This lets you rotate something on the center as opposed to the top left corner of the texture.

In summary, here's the corrected code:

#include <iostream>
#include "raylib.h"

int main() {
    InitWindow(800, 800, "More Raylib Practice");
    SetWindowState(FLAG_VSYNC_HINT);
    
    while (!WindowShouldClose()) {
        BeginDrawing();
        ClearBackground(BLACK);
        
        DrawRectangle(10, 10, 10, 10, RED);

        Rectangle rec = { 10, 10, 10, 10 };
        DrawRectanglePro(rec, { 0, 0 }, (float)45, WHITE); //Set origin to 0,0        
        EndDrawing();
    }

    CloseWindow();
    return 0;
}
Related