Skip to content

Commit 59478e4

Browse files
authored
Merge pull request #357 from RobLoach/drawing
Add window::Drawing()
2 parents bdf273a + db8c70b commit 59478e4

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

examples/core/core_basic_window.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,10 @@ int main() {
4141

4242
// Draw
4343
//----------------------------------------------------------------------------------
44-
BeginDrawing();
45-
{
44+
while (window.Drawing()) {
4645
window.ClearBackground(RAYWHITE);
4746
textColor.DrawText("Congrats! You created your first window!", 190, 200, 20);
4847
}
49-
EndDrawing();
5048
//----------------------------------------------------------------------------------
5149
}
5250

include/Window.hpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,38 @@ class Window {
410410
* @see ::SetConfigFlags
411411
*/
412412
static void SetConfigFlags(unsigned int flags) { ::SetConfigFlags(flags); }
413+
414+
/**
415+
* Alternates between calling `BeginDrawing()` and `EndDrawing()`.
416+
*
417+
* @code
418+
* while (window.Drawing()) {
419+
* DrawRectangle();
420+
* }
421+
* @endcode
422+
*
423+
* @return True if we're within the `BeginDrawing()` scope.
424+
*/
425+
bool Drawing() {
426+
if (m_drawing) {
427+
EndDrawing();
428+
m_drawing = false;
429+
}
430+
else {
431+
BeginDrawing();
432+
m_drawing = true;
433+
}
434+
435+
return m_drawing;
436+
}
437+
438+
protected:
439+
/**
440+
* Handles the internal drawing state for calling either `BeginDrawing()` or `EndDrawing()` from the `Drawing()` function.
441+
*
442+
* @see Drawing()
443+
*/
444+
bool m_drawing = false;
413445
};
414446
} // namespace raylib
415447

0 commit comments

Comments
 (0)