What is damage() all about?

55 views
Skip to first unread message

Heart Bleed

unread,
Apr 25, 2025, 4:04:13 PM4/25/25
to fltk.general
Can anyone please explain what this function all about :

int MyClass::handle(int event) {
// ...
if (change_to_part1) damage(1);
if (change_to_part2) damage(2);
if (change_to_part3) damage(4);
}
void MyClass::draw() {
if (damage() & FL_DAMAGE_ALL) {
// ... draw frame/box and other static stuff ...
}
if (damage() & (FL_DAMAGE_ALL | 1)) draw_part1();
if (damage() & (FL_DAMAGE_ALL | 2)) draw_part2();
if (damage() & (FL_DAMAGE_ALL | 4)) draw_part3();
}

Also how does one detect change_to_part1?

Regards and Thanks a lot

Albrecht Schlosser

unread,
Apr 26, 2025, 7:49:56 AM4/26/25
to fltkg...@googlegroups.com
On 4/25/25 22:04 Heart Bleed wrote:
Can anyone please explain what this function all about :

Yes. ;-)

Sometimes it's useful to draw only a part of a widget, particularly if the widget is big and drawing it is "expensive" (CPU intense). The damage() bits can help to draw only those parts that have changed between the last draw() and the next draw() call.



int MyClass::handle(int event) {
// ...
if (change_to_part1) damage(1);
if (change_to_part2) damage(2);
if (change_to_part3) damage(4);
}

As you can see, the above part of the example code is within the handle() method. The "change_to_partx" variables are pseudo code (examples). Your widget has to interpret user actions and decide if a specific part has changed, and then set the bit related to that part. For instance, if the user clicks a button that changes a display (for instance a counter), then you would set the bit that this display (counter) has changed.

You can set damage() bits at any time, not only in the handle() method, for instance in a timer callback to notify the user that "something" has changed.


void MyClass::draw() {
if (damage() & FL_DAMAGE_ALL) {
// ... draw frame/box and other static stuff ...
}
if (damage() & (FL_DAMAGE_ALL | 1)) draw_part1();
if (damage() & (FL_DAMAGE_ALL | 2)) draw_part2();
if (damage() & (FL_DAMAGE_ALL | 4)) draw_part3();
}


The code above is in the draw() method and checks what parts have been changed, according to the bits that have been set earlier, i.e. between draw() calls. As written above, this can be anything and depends on the widget you are implementing.


Also how does one detect change_to_part1?

See above, it's up to you to determine which parts of the widget have been changed since the last draw() call.

If your widget is small you can probably ignore all this damage() stuff.

Heart Bleed

unread,
Apr 26, 2025, 2:11:48 PM4/26/25
to fltk.general
Thanks a lot once again Albrecht sir.
Message has been deleted

Heart Bleed

unread,
Apr 28, 2025, 4:49:33 AM4/28/25
to fltk.general
One little thing to ask Albrecht sir . Overall can I say damage() is kind of way to pass information to draw() from event()? I ask this cuz I am using some convex drawing and I want to update their position and shape change according to events.

I could achieve same thing using some global integer/vector though ? i.e setting bits/insert in handle() and consuming them in draw() ?

Matthias Melcher

unread,
Apr 28, 2025, 4:53:57 AM4/28/25
to fltk.general
Yes, you can use damage() to set flags that tell the YourWidget::draw() method what was damaged and needs to be redrawn.


You can use the predefined flags mentioned above or use the USER flags to indicate your own damage options.

Yes, you can do this from a thread. Everything but opening and closing windows can be done from a thread. There is an entire chapter in the documentation:
A typical call would look like this:

Fl::lock();  // Use this if we call from a thread
myWidget->damage(FL_DAMAGE_USER1); // Tell myWidget->draw() method its job
 - or -
myWIdget->redraw(); // Basically calls damage(FL_DAMAGE_ALL);
Fl::awake();  // Tell the main FLTK thread to wake up and handle pending redraws
Fl::unlock(); // Use this if we call from a thread

Heart Bleed

unread,
Apr 28, 2025, 6:04:30 AM4/28/25
to fltk.general
Thank you so much for acknowledging :) 
Reply all
Reply to author
Forward
0 new messages