Skip to main content
gamedev.net
🔒 Locked

fltk handle function

Started by greentea8202 1 reply 1.1k views
Original Post
greentea8202
greentea8202
I made Fl_Gl_Window for openGL figures. Then I am trying to do mouse handling for the figure. In my draw(), I have a simple cube rendered. And I declared handle(), which I did not put any code inside the function. Then in main, I only call draw function.. but there is no gl window at all. Can anyone tell me what is going on? I never called handle function anywhere and did not put any code inside the handle. Is this function called automatically? Also I was wondering if fltk would be able to recognize both mouse button push (left + middle) at the same time. And if that is possible, how should I implement to make them different from left push and middle push. Help me out please! Thx!
JTippetts
JTippetts
handle() and draw() are virtual functions that are overloaded to provide window drawing and input handling capabilities for the window. If you want your window to be able to handle events: mouse clicks, mouse movement, etc...--then you need to provide such handling in handle(), which will be called automatically from FLTK's event loop.

However, in order to do this, you need to execute fltk::run() in your main, rather than manually calling draw(). fltk::run() starts the FLTK event management loop, which gathers events, passes them out to windows, calls draw() functions when needed and in order, etc, etc, etc. Without properly executing the FLTK system, certain things are not properly done, such as (IIRC) creating the OpenGL rendering context. Which may be why you get no GL window at all. You shouldn't ever have to manually call draw() or handle(); these are called automatically by the fltk::run() function. You may need to call the redraw() function during the handling of events (inside handle()) to force a re-draw of the OpenGL scene.

As far as handling 2 mouse buttons at once, no problem. The function fltk::event_state() will return a value which can be logically ANDed with the button identifiers (BUTTON1, BUTTON2, BUTTON3) to see if that button is pressed or not when an event is being processed. Any button press that happens within the boundaries of your window will cause an event to be sent to that window's handle() function. By processing the PUSH event in handle(), you can detect mouse button presses, and by analyzing the return value of event_state() you can determine which buttons were pressed or held when the event was sent. Here's an example handle() function:

int DerivedGLWindowClass::handle(int event){ switch(event) {  case PUSH:   unsigned long state=fltk::event_state();   if(state & BUTTON1)   {    // Button 1 (Left) is pressed   }   if(state & BUTTON2)   {    // Button 2 (Middle) is pressed   }   if(state & BUTTON3)   {    // Button 3 (Right) is pressed   }   if((state & BUTTON1) && (state & BUTTON2) && (state & BUTTON3))   {    // All three buttons are pressed   }   break; }}


Good luck, and I hope that helps.
greentea8202
greentea8202

Thanks for your help!! I see how it works now ^^

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.