c - SDL - Why moving the mouse changes button state? -
i'm having problem simple directmedia layer library. following code draws block on screen when mouse button pressed:
sdl_event event; while(running){ while(sdl_pollevent(&event)){ while(event.button.state == sdl_pressed){ sdl_pollevent(&event); //where draw boxrect.x = event.motion.x; boxrect.y = event.motion.y; //draw screen sdl_fillrect(display,&boxrect,boxcolor); sdl_flip(display); } // ... } // ... } it works fine until move mouse, why moving mouse makes event.button.state untrue?
how can work both simultaneously (i.e. keep drawing while button pressed)?
the problem code you're calling sdl_pollevent (documented here) twice. said in documentation:
if event not null, next event removed queue , stored in sdl_event structure pointed event.
rearranging code bit, getting rid of second sdl_pollevent, creating proper flow clicking, moving, releasing , extracting rendering input pumping should give this:
sdl_event event; while(running) { while(sdl_pollevent(&event)) { switch(event.type) { // handle drawing simple state machine: case sdl_mousebuttondown: { if(event.button.button == sdl_button_left) { if(statemachine == released) { // ... begin drawing statemachine = dragging } } break; } case sdl_mousemotion: { if(statemachine == dragging) { // ... update extends of rect } } case sdl_mousebuttonup: { if(event.button.button == sdl_button_left) { if(statemachine != released) { // ... finalize drawing... add rect list? flush it? statemachine = released; } } } case sdl_quit: { running = false; break; } } } // outside of event pumping, update graphics sdl_fillrect(display,&boxrect,boxcolor); sdl_flip(display); }
Comments
Post a Comment