SDL and events

Fillbert
Unregistered
 
Post: #1
For some reason I can't get my mouse events to work correctly using SDL. How should I go about getting the mouse cord. of an mouse down event. I use SDL_PollEvent : while(SDL_PollEvent(&myEvent) != 0 ), after that I check to see where the mouse cords are using: if(mouseD.type == SDL_MOUSEBUTTONUP)
{
mouseD.button.x = Xpos;
mouseD.button.y = Ypos;
}

but this code doesn't work for the cords, they arer the same no matter what. The other way I check and get the cord is SDL_GetMouseState(X,Y) bu then in the program if I click on my object it shows it being clicked but then if I go outside the object it will still show it being clicked for a little while then it will stop. can someone please help me out here.
Quote this message in a reply
Jeff Binder
Unregistered
 
Post: #2
I'm not sure exactly what you're trying to do here... If we could see some more of your code we may be able to help some more.

Assuming mouseD is the SDL_Event, and Xpos and Ypos are where you want the location to go, you should be going

Xpos = mouseD.button.x;
Ypos = mouseD.button.y;

instead of

mouseD.button.x = Xpos;
mouseD.button.y = Ypos;

. As for the SDL_GetMouseState() problem, I'd need to see some more code to help.
Quote this message in a reply
Mars_999
Unregistered
 
Post: #3
Hello, I use SDL a lot. I don't have my Mac yet still waiting for it to show up
!!!! Starting to Pi** me off!!! Anyway, if you can wait till I get it I can show you my code for dealing with events in SDL. It is not to hard but the manual is quite confusing. Let me know if you want some help. Later
Quote this message in a reply
kainsin
Unregistered
 
Post: #4
I don't know if this is the case, but as far as I know, threads are broken with SDL for Mac OS X ( and maybe Mac OS 9 as well? ). If you use threads _at all_ you will not be able to get cursor information. If this is the case then you may want to try disabling threads to see if it works.
Quote this message in a reply
Fillbert
Unregistered
 
Post: #5
ok... I'll explain this a little more. I'm making a set of classes that are used to make GUI's, just for fun. So I have a loop that runs intill you click QUIT. Inside that I have a while loop runnig thru the event queue. Then I have a function upDate, which checks to see if there was a mouse click and if there was one then it checks all of the buttons to see if that click was inside of it. If it is it returns a "action" and main deals with it. My prroblum is that it seems like the cords that I get are not really where the mouse it. I can click inside of the button have it click but then bring the mouse out of the button click again and have it show the button being clicked. and if I move it anywhere on teh screen it wil do that. Also sometimes the button won't even click. I'm sure that the way I check to see if the mouse it in is correct so it has to be the events.Code below


my main loop:
Code:
while ( !done )
        {
            while(SDL_PollEvent(&event) != 0 )
                {
                
                    temp = myGUI.upDate(screen, event);
                          
                  //  fprintf(stderr, "Here is an event\n");
                        if(temp != NOTHING)
                            {
                            if(temp == ENDGAME)
                                done = 1;
                            SDL_UpdateRect(screen , 0 , 0 , 0 , 0 );
                            SDL_Delay(100);  
                            }
                  
                      
                }
              
                myGUI.drawSplash(screen);
                SDL_UpdateRect(screen , 0 , 0 , 0 , 0 );
            }


update:
Code:
Uint16 Xpos , Ypos ;
    
    drawSplash(myWindow);
    
if(mouseD.type == SDL_MOUSEBUTTONDOWN)
            {
                   Xpos = mouseD.button.x;
                    Ypos = mouseD.button.y;
    
    for(int loop = 0; loop < myButtons.length(); loop++)
            {
                            if(myButtons[loop].ISclicked(Xpos,Ypos) == true)
                    {
                        myButtons[loop].drawButton(myWindow, myRect);
                        return myButtons[loop].getAction();
                    }
                    
            }
        
                }
    
    
    
    return NOTHING;


and my inbounds check X and Y are mouse cords:
Code:
if((X >=  myPos.x && Y >= myPos.y )&&(X <= (myPos.x + myPos.w ) && Y <=(myPos.y + myPos.h )))
        {
        clicked = true;
        return true;
        }    
        
    return false;


-thank you very mcuh already

-Fillbert
Quote this message in a reply
kainsin
Unregistered
 
Post: #6
Have you tried printing out the coordinates that the mouse was at when the click event occured? I'm not sure how SDL does it exactly, but the button may be using one set of coordinates ( the window's ) and the mouse may be using another ( the screen's ). Also, I think that SDL windows have their window's origin set at the bottom-left corner, but if care was not taken then the mouse may be returning a point where the screen origin is at the top-left corner.
Quote this message in a reply
wally
Unregistered
 
Post: #7
As far as I can tell, SDL is not at fault here. Remember that the origin of the SDL window (coordinate 0,0) is the top left corner, just below the window's title bar.


Try the following sample code which demonstrates all the ways to get the position of the mouse. Note that SDL_GetRelativeMouseState() gets the relative position of the mouse since you created the window or called SDL_GetRelativeMouseState().

Code:
    while ( !done ) {

        while ( SDL_PollEvent(&event) ) {
            
    switch (event.type) {

                case SDL_MOUSEBUTTONDOWN:
                    {
                          int x, y;
                          printf("Event says: %d %d\n", event.button.x, event.button.y);

                          SDL_GetMouseState(&x, &y);
                          printf("SDL_GetMouseState says: %d %d\n", x, y);

                          SDL_GetRelativeMouseState(&x, &y);
                          printf("SDL_GetRelativeMouseState says: %d %d\n", x, y);
                    }
                    break;
                default:
                    /* do nothing */
        }
    }
Quote this message in a reply
Mars_999
Unregistered
 
Post: #8
Ok, here is the code I use in SDL to get mouse coordinates.
Code:
void Events::GetEvents(void)
{
    while(SDL_PollEvent(&event))
    {
        switch(event.type)
        {
            case SDL_KEYDOWN:
                switch(event.key.keysym.sym)
                {
                    case SDLK_SPACE:
                        break;
                    case SDLK_ESCAPE:
                        done = 1;
                        break;
                    default:
                        break;
                }
                break;

            case SDL_KEYUP:
                switch(event.key.keysym.sym)
                {
                    case SDLK_SPACE:
                        break;
                    default:
                        break;
                }
                break;

            case SDL_MOUSEBUTTONDOWN:
                mouse_down_x = event.button.x;
                mouse_down_y = (768 - event.button.y);
                break;

            case SDL_MOUSEMOTION:
                mouse_x = event.motion.x;
                mouse_y = event.motion.y;
                break;
            default:
                break;
        }
    }
}

HTH let me know if your still having problems. =)
Quote this message in a reply
Post Reply