   SDL - help dragging an image
  I am trying to write a program in SDL in which I can click and drag an image. My eventual goal is to make a checkers game. I've looked all over google and gamedev to try to find an answer but I've come up with nothing. All I can manage to do is get the image to move to the mouse when I click it, but I cannot drag it.Here is all of my code outside of the initialize/load image/clean up stuff.Sprite::Sprite(){x = 0;y = 0;h = 20;w = 20;}void Sprite::handle_events(){dragMode = false;if( event.type == SDL_MOUSEBUTTONDOWN ){if( event.button.button == SDL_BUTTON_LEFT ){mouseX = event.button.x;mouseY = event.button.y;if( ( mouseX &gt; x ) && ( mouseX &lt; x + w ) && ( mouseY &gt; y ) && ( mouseY &lt; y + h ) ){//if mouse is over button + being clicked --- add motiondragMode = true;}}}if( event.button.button == SDL_MOUSEBUTTONUP ){if( event.button.button == SDL_BUTTON_LEFT ){//button released, stop motiondragMode = false;}}}void Sprite::drag(){if(dragMode == true){x = event.motion.x;y = event.motion.y;}}void Sprite::show(){apply_surface(x, y, image, screen);}int main( int argc, char* args[] ){bool quit = false;Sprite mySprite;if( init() == false ){return 1;}if( load_files() == false ){return 1;}    while( quit == false ){while(SDL_PollEvent( &event ) ){mySprite.handle_events();mySprite.drag();SDL_FillRect( screen, &screen-&gt;clip_rect, SDL_MapRGB( screen-&gt;format, 0xFF, 0xFF, 0xFF ) );mySprite.show();if( SDL_Flip( screen ) == -1 ){return 1;}if( event.type == SDL_QUIT ){quit = true;}}}clean_up();return 0;}[Edited by - cbone77 on April 5, 2010 4:02:16 AM]
Quote:Original post by cbone77I don't know how to include the code in one of those scroll boxes.You need to surround the code with [ source ] and [ /source ] (without the spaces).Also, this:if( event.button.button == SDL_MOUSEBUTTONUP )should be:if( event.type == SDL_MOUSEBUTTONUP );  Found the error of my ways! I didn't realize that dragMode being set to false at the beginning of my handle_events function was ruining it. After moving that and setting the image x&y values to += event.motion.xrel/yrel it works perfectly!edit: i also made the event.type change as well, thanks!   