2

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;
}
Ken White
  • 123,280
  • 14
  • 225
  • 444
DAG3223
  • 27
  • 5

1 Answers1

3

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;
}
John Pavek
  • 2,595
  • 3
  • 15
  • 33
  • Is this interpretation of the function parameters documented anywhere? I still don't understand the logic. – aldo Nov 15 '22 at 03:37
  • @aldo https://www.raylib.com/cheatsheet/cheatsheet.html This is the best documenation I know of for Raylib. – John Pavek Nov 15 '22 at 15:33