I drew the circle!

My code:

#include "raylib.h"

int main()
{

    int width = 350;
    int height = 200;

    InitWindow(width, height, "My Window");

    while (WindowShouldClose() == false)
    {
        BeginDrawing();
        ClearBackground(WHITE);

        DrawCircle(width/2, height/2, 25, BLUE);

        EndDrawing();
    }

}

However, this visually simple looking program does take 30% of my CPU power. I suppose it has something to do with the while loop. I assume it’s the program that keeps on re-rendering the canvas and the circle, even though it doesn’t change. And there probably isn’t a framerate limit. :sweat_smile:

I did a similar thing to you by creating variables of all my function arguments. A good practice I think which allows you to make a single change in one place.

I don’t know if the while loop is just how things are done in Raylib to render windows, or if we will be learning a better way later in the course, but it does seem to be a very inefficient use of system resources.

The main reason why this may take such a massive toll on Your CPU is due to the fact that You are making it work harder. The way how You define the centerx and centery are fine, they are giving the same output, but it makes Your CPU counting it all the time.

So You’re having a nice readable code, but it’s not optimized for the CPU. Optimized code is designed and written in a way to decrease the load on the CPU. Refactoring on the other hand makes the code easier for a human to read, but it’s getting the PC to work harder to achieve the same goal.

If loop will be refactoring because a person writes like 5 lines of code and creates a hundred lines in the console.
Optimized code will be writing those 100 lines by hand because CPU will display them quicker as they are.

1 Like

There’s also the fact the frame rate isn’t capped.

1 Like

Privacy & Terms