iDevGames Forums
MouseMotion and MouseDown/Up Events - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Graphics & Audio Programming (/forum-9.html)
+--- Thread: MouseMotion and MouseDown/Up Events (/thread-6068.html)



MouseMotion and MouseDown/Up Events - Nick - Nov 27, 2004 01:30 AM

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;



MouseMotion and MouseDown/Up Events - OneSadCookie - Nov 27, 2004 02:05 AM

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.


MouseMotion and MouseDown/Up Events - Nick - Nov 27, 2004 10:57 AM

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.


MouseMotion and MouseDown/Up Events - Nick - Nov 27, 2004 02:39 PM

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.


MouseMotion and MouseDown/Up Events - Nick - Nov 27, 2004 04:09 PM

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.


MouseMotion and MouseDown/Up Events - aaronsullivan - Nov 28, 2004 07:08 PM

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.


MouseMotion and MouseDown/Up Events - Nick - Nov 28, 2004 07:21 PM

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.


MouseMotion and MouseDown/Up Events - aaronsullivan - Nov 29, 2004 07:27 AM

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


MouseMotion and MouseDown/Up Events - Nick - Nov 29, 2004 10:53 AM

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.


MouseMotion and MouseDown/Up Events - PowerMacX - Dec 2, 2004 01:12 AM

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.


MouseMotion and MouseDown/Up Events - Nick - Dec 2, 2004 09:53 AM

I'll give that a look. Thanks for the pointer.