-
Notifications
You must be signed in to change notification settings - Fork 107
Add window::Drawing() #357
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
@leyloe Mind testing this out? Running the basic window example may be enough to see things running. |
sure, im getting home |
it's flipping a variable at the end of the scope. hopefully some compilers can optimize this to keep up with the "0 cost abstraction" since this "loop" shouldn't iterate a second time |
|
Shouldn't that be an && instead of a |? |
nah you gotta pipe it, && crashes the program i don't really recommend this approach as it generates unoptimal assembly in release compared to the nested loops. however, if you can figure out how to remove the extra nesting without the compiler generating extra code maybe via extra library functionality that would help |
disregard, i forgot that stuff exists outside of the draw calls, but still exist in the should close loop |
|
I do not think this is a good approach in general. What could be good idea is to set a state in the begin and end drawing mode functions, checking the state and log an error, when its attempted to draw without the correct state set. |
|
but yeah, i haven’t started using raylib in a advanced way yet. but im putting a game together and im liking this scoped draw feature so far. if anyone objects/wants to challenge this opinion, please provide a clear code example of it ruining the functionality of any common case—something the legacy draw scope didn’t have problems with, unlike this. if anyone wants to try it just pull a zip from the ref head set(RAYLIB_VERSION 5.5)
set(RAYLIB_CPP_VERSION drawing)
FetchContent_Declare(
raylib
URL https://github.com/raysan5/raylib/archive/refs/tags/${RAYLIB_VERSION}.tar.gz
)
FetchContent_Declare(
raylib_cpp
URL https://github.com/RobLoach/raylib-cpp/archive/refs/heads/${RAYLIB_CPP_VERSION}.zip
)
FetchContent_MakeAvailable(
raylib
raylib_cpp
) |
|
Sorry for the comment yesterday, I was tired, and misread the file changes. I like the idea of the approach, as a defined scope in a loop is more readable. But I personally still dont see a benefit, as my code looks more like this: |
i meann it's just c++ style syntactic sugar and abstraction. this library has lots of it. with your point. whats the point of using this library? and i guess this just makes the draw more raii. that's about it |
no, its while (!window.shouldclose())
{
// calculate cube stuff in the backend
while(window.Drawing)
{
// actual frontend
}
// stuff to do after
}also, that can be an option, sure. but it depends if you're passing external objects into the loop. your function would have a million arguments |
|
Correct, you usually want to separate your logic from your rendering. This |
like this? while(window.Drawing) doRender(); |
|
@RobLoach @kyomawolf i guess this expands on and addresses your concerns relating to logic modularization #include <raylib-cpp.hpp>
#define SCREEN_WIDTH 1280
#define SCREEN_HEIGHT 800
#define WINDOW_TITLE "Pong"
class Ball
{
public:
Ball(raylib::Vector2 position, float radius, float speed) : m_Position{position}, m_Radius{radius}, m_Speed{speed, speed}
{
}
void Draw()
{
m_Position.DrawCircle(m_Radius, raylib::Color::White());
}
void Update()
{
m_Position += m_Speed;
if (m_Position.y + m_Radius >= SCREEN_HEIGHT || m_Position.y - m_Radius <= 0)
m_Speed.y *= -1;
if (m_Position.x + m_Radius >= SCREEN_WIDTH || m_Position.x - m_Radius <= 0)
m_Speed.x *= -1;
}
private:
raylib::Vector2 m_Position;
raylib::Vector2 m_Speed;
float m_Radius;
};
raylib::Window window(SCREEN_WIDTH, SCREEN_HEIGHT, WINDOW_TITLE);
raylib::Vector2 center{SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2};
Ball ball{center, 20, 7};
void Setup()
{
window.SetTargetFPS(60);
}
void Update()
{
ball.Update();
}
void Render()
{
window.ClearBackground(raylib::Color::Black());
DrawLine(center.x, 0, center.x, SCREEN_HEIGHT, raylib::Color::White());
ball.Draw();
}
int main()
{
Setup();
while (!window.ShouldClose())
{
Update();
while (window.Drawing())
Render();
}
return 0;
} |
|
Since this is a small change, I think it's worth bringing in. It doesn't enforce its use, just saves you a line of code in your rendering method 😉 |






Fixes #356