MouseMotion and MouseDown/Up Events

Sage
Posts: 1,066
Joined: 2004.07
Post: #1
I'm working on mouse input for my game right now and need to use the SDL mouse events to detect where on the screen my mouse is (so to display a "cursor") and to detect mouse clicks and releases for various selecting of things. I know the events are SDL_MOUSEMOTION, SDL_MOUSEBUTTONDOWN, and SDL_MOUSEBUTTONUP but I don't know how to extract the coordinates for each. I've been trying things for an hour or so but to no avail. Here's what my current code for keypresses looks like. What is a comparable method that would be used simply to extract the x,y, and relative of each from the mouse moved and the x and y from button up and button down?

Code:
while( SDL_PollEvent(& event) ) // look for events (like keystrokes, resizing etc.)
        {
            switch ( event.type ) // what kind of event have we got ?
            {
                case SDL_QUIT : // if user wishes to quit
                    done = true; // this implies our job is done
                    break;
                    
                case SDL_KEYDOWN : // if the user has pressed a key
                    HandleKeyPressEvent( & event. key.keysym ); // callback for handling keystrokes, arg is key pressed
                    break;
                    
                case SDL_KEYUP :
                    HandleKeyReleaseEvent(& event.key.keysym) ; // callback for handling keystrokes, arg is key released
                    break;


.........

void HandleKeyPressEvent(SDL_keysym * keysym)
{
    switch (keysym -> sym) //which key has been pressed
    {
        case SDLK_ESCAPE : //the escape key
            escPressed = true;
            break;
            
        case SDLK_TAB : //the tab key
            tabPressed = true;
            break;
            
        case SDLK_UP : //the up arrow
            upPressed = true;
            break;
            
        case SDLK_DOWN : //the down arrow
            downPressed = true;
            break;
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
http://sdldoc.csn.ul.ie/sdlevent.php

looks like in your case SDL_MOUSEMOTION you should refer to event.motion.whatever, and in your case SDL_MOUSEBUTTONDOWN and up, you should refer to event.button.whatever.
Quote this message in a reply
Sage
Posts: 1,066
Joined: 2004.07
Post: #3
Oh geeze. That does make perfect sense. I guess trying to learn new code at 2am is not such a good idea. Wow I didn't even think about it like that. Thanks.
Quote this message in a reply
Sage
Posts: 1,066
Joined: 2004.07
Post: #4
I'm having some trouble right now. Here's my handling of the Mousemotion event. Earlier this code was working fine. Now there is the code in there for converting the points to object coordinates for figuring out objects in OpenGL. Everytime I run this program, I get a fatal signal. One time it pointed to my currentScreenMouseX = mme->x line but now I get ??. I don't know if it's a good thing that the debugger can't help me. Perhaps something I'm doing just slightly wrong?

Just as a note: I only need (at this time) the X and Z object coordinates which is why there is a variable created and not used just to house the Y object coordinate.
Code:
float currentScreenMouseX, currentScreenMouseY;
GLdouble currentObjectMouseX, currentObjectMouseZ;
...
case SDL_MOUSEMOTION : //if the mouse has moved
                    HandleMouseMotionEvent(&event.motion);
                    break;
...
void HandleMouseMotionEvent(SDL_MouseMotionEvent *mme)
{
    currentScreenMouseX = mme->x;
    currentScreenMouseY = mme->y;
    GLdouble y;
    GLdouble model, proj;
    GLint view;
    glGetDoublev(GL_MODELVIEW_MATRIX,&model);
    glGetDoublev(GL_PROJECTION_MATRIX,&proj);
    glGetIntegerv(GL_VIEWPORT,&view);
    gluUnProject(currentScreenMouseX,currentScreenMouseY,0,
                 &model,&proj,&view,
                 &currentObjectMouseX,&y,&currentObjectMouseZ);
}
Any help is appreciated.
Quote this message in a reply
Sage
Posts: 1,066
Joined: 2004.07
Post: #5
I've found that the problem is that within my HandleMouseMotionEvent() function, mme is not actually represented by any data. That is why I get a bad access call each time. I added the calling of the function to the above code. I don't know why mme is invalid. The problem is that it worked before I added the OpenGL stuff to it so It's no that my SDL code is off. I don't know what caused the error.
Quote this message in a reply
Member
Posts: 233
Joined: 2003.05
Post: #6
I think event.motion is a coordinate struct of some kind, not a MouseMotionEvent.

In other words... send a pointer to the whole EVENT, not just the motion part.

HandleMouseMotionEvent(&event);
...
currentScreenMouseX = mme->motion.x;
etc.

"Pay no attention to that man behind the curtain." - Wizard of Oz
Quote this message in a reply
Sage
Posts: 1,066
Joined: 2004.07
Post: #7
No event.motion is a MouseMotionEvent (I looked it up). I just rewrote the way it handled mouse input using deltas and such rather than absolute coordinates and now it works just fine. Not sure why the massive errors though...
Thanks for all the help.
Quote this message in a reply
Member
Posts: 233
Joined: 2003.05
Post: #8
Yep, you're right. Should have looked it up myself, haven't looked at that code in awhile. Not sure how openGL would be effecting the mme pointer... it really shouldn't make a difference with that at all. Using deltas for the mouse to get absolute coordinates can get some unpredictable results... but if it works, it works. Grin

"Pay no attention to that man behind the curtain." - Wizard of Oz
Quote this message in a reply
Sage
Posts: 1,066
Joined: 2004.07
Post: #9
Well I'm not needing the absolute coordinates. I'm just moving an icon around and just need response with the mouse. It's working.... for now anyway.
Quote this message in a reply
Moderator
Posts: 770
Joined: 2003.04
Post: #10
Nick Wrote:Well I'm not needing the absolute coordinates. I'm just moving an icon around and just need response with the mouse. It's working.... for now anyway.

Look at my entry's source code (link in Okugai's thread), specifically, the GraphicMouse class.
Quote this message in a reply
Sage
Posts: 1,066
Joined: 2004.07
Post: #11
I'll give that a look. Thanks for the pointer.
Quote this message in a reply
Post Reply