LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Error with "SDL_MouseMotionEvent *motion = event->motion;" (http://www.linuxquestions.org/questions/programming-9/error-with-sdl_mousemotionevent-%2Amotion-%3D-event-motion%3B-603360/)

aatwell 11-29-2007 06:31 PM

Error with "SDL_MouseMotionEvent *motion = event->motion;"
 
Greetings,
I have issue trying to capture mouse events. I get the error below when I try to compile using gcc on my Fedora Core system.

error: incompatible types in initialization

It's in the function below. Any help would be great. If rest of code is needed, I'll post it. Thanks. - Andrew

Code:

void HandleMouseEvents(SDL_Event*event){
 SDL_MouseMotionEvent*motion=event->motion; // THIS LINE GETS ERROR //
 SDL_MouseButtonEvent*button=event->button; // THIS LINE GETS ERROR //
 switch(event -> type) {
  case SDL_MOUSEMOTION:
    printf("Mouse moved to: (%d, %d)\n",
            motion->x, motion->y);
    printf("Relative motion: (%d, %d)\n",
            motion->xrel, motion->yrel);
    break;
  case SDL_MOUSEBUTTONDOWN:
    printf("Mouse button %d pressed at (%d, %d)\n",
            button->button,
            button->x, button->y);
    break;

  case SDL_MOUSEBUTTONUP:
    printf("Mouse button %d released at (%d, %d)\n",
    button->button, button->x, button->y);
    break;

  default:
    break;
 }
}


aatwell 11-30-2007 12:23 PM

Ok folks,
Here's the code I'm using.... Can anyone please help resolve this issue? I get the following errors:

mouse.c: In function 'HandleMouseEvents':
mouse.c:79: error: incompatible types in initialization
mouse.c:80: error: incompatible types in initialization
mouse.c: In function 'HandleKeyboardEvents':
mouse.c:109: error: invalid type argument of 'unary'


Code:

/*
 **  mouse.c - Code is from book        **
 **  Linux Game Programming by Mark    **
 **  "Nurgle" Collins, et al.          **
 **  Prima Publishing, 2001. Page 55    **
 **  Modified by Andrew Atwell because  **
 **  original code fails to compile.    **
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <X11/Xlib.h>
#include <SDL/SDL.h>

void HandleMouseEvents(SDL_Event*event);
void HandleKeyboardEvents(SDL_Event*event);

/* ************************************
typedef union
{
        Uint8 type;
        SDL_ActiveEvent active;
        SDL_KeyboardEvent key;
        SDL_MouseMotionEvent motion;
        SDL_MouseButtonEvent button;
        SDL_JoyAxisEvent jaxis;
        SDL_JoyBallEvent jball;
        SDL_JoyHatEvent jhat;
        SDL_JoyButtonEvent jbutton;
        SDL_ResizeEvent resize;
        SDL_QuitEvent quit;
        SDL_UserEvent user;
        SDL_SysWMEvent syswm;
} SDL_Event;
  ************************************ */

int main()
{
        SDL_Event event;
        int SDL_PollEvent(SDL_Event *event);
        int SDL_WaitEvent(SDL_Event *event);
        int quit=0;
        SDL_Init(SDL_INIT_VIDEO);
        SDL_SetVideoMode(320,200,0,0);

        while(SDL_Pollevent(&event))
        {
                switch(event.type)
                {
                        case SDL_KEYDOWN:
                        case SDL_KEYUP:
                                HandleKeyboardEvent(&event);
                                break;
                        case SDL_MOUSEMOTION:
                        case SDL_MOUSEBUTTONDOWN:
                        case SDL_MOUSEBUTTONUP:
                                HandleMouseEvent(&event);
                                break;
                        case SDL_JOYAXISMOTION:
                        case SDL_JOYHATMOTION:
                        case SDL_JOYBALLMOTION:
                        case SDL_JOYBUTTONDOWN:
                        case SDL_JOYBUTTONUP:
                                break;
                        default:
                                break;
                }
        }

        SDL_Quit();
        return(0);
}



void HandleMouseEvents(SDL_Event*event)
{
        SDL_MouseMotionEvent *motion = event -> motion; // THIS LINE HAS ISSUE //
        SDL_MouseButtonEvent *button = event -> button; // THIS LINE HAS ISSUE //
        switch(event -> type)
        {
                case SDL_MOUSEMOTION:
                        printf("Mouse moved to: (%d, %d)\n",
                                motion->x, motion->y);
                        printf("Relative motion: (%d, %d)\n",
                                motion->xrel, motion->yrel);
                                break;

                case SDL_MOUSEBUTTONDOWN:
                        printf("Mouse button %d pressed at (%d, %d)\n",
                                button->button, button->x, button->y);
                                break;

                case SDL_MOUSEBUTTONUP:
                        printf("Mouse button %d released at (%d, %d)\n",
                                button->button, button->x, button->y);
                                break;

                default:
                        break;
        }

}


void HandleKeyboardEvents(SDL_Event*event)
{
        SDL_KeyboardEvent*key = *event -> key; // THIS LINE HAS ISSUE //
        switch (key -> keysym.sym)
        {
                case SDLK_LEFT:
                        printf("Left key\n");
                        break;
                case SDLK_RIGHT:
                        printf("Right key\n");
                        break;
                case SDLK_UP:
                        printf("Up key\n");
                        break;
                case SDLK_DOWN:
                        printf("Down key\n");
                        break;
                default:
                        printf("Other key\n");
                        break;
        }

        switch (key -> type)
        {
                case SDL_KEYUP:
                        printf("released.\n");
                        break;
                case SDL_KEYDOWN:
                        printf("pressed.\n");
                        break;
                default:
                        break;
        }
}


aatwell 11-30-2007 01:16 PM

Well. I did notice that the first two errors in the function HandleMouseEvents(SDL_Event*event) go away when I make the assignment a separate statement from the declaration like this:

Code:

SDL_MouseMotionEvent *motion;
*motion = event -> motion;
SDL_MouseButtonEvent *button;
*button = event -> button;

But the third error: "invalid type argument of 'unary'" still remains.

aatwell 11-30-2007 01:46 PM

Forget it folks. This book is useless. once I comment out the line "SDL_KeyboardEvent*key = *event -> key;" in the HandleKeyboardEvents(SDL_Event*event) function the error goes away. But I get a lot of unrelated errors like this.

mouse.c: In function 'HandleKeyboardEvents':
mouse.c:115: error: case label not within a switch statement
mouse.c:117: error: break statement not within loop or switch
mouse.c:118: error: case label not within a switch statement
mouse.c:120: error: break statement not within loop or switch
mouse.c:121: error: case label not within a switch statement
mouse.c:123: error: break statement not within loop or switch
mouse.c:124: error: case label not within a switch statement
mouse.c:126: error: break statement not within loop or switch
mouse.c:127: error: 'default' label not within a switch statement
mouse.c:129: error: break statement not within loop or switch


This book is absolutely ridiculous. It promises so much in the beginning, but doesn't deliver much. I did get some use from it, but only after doing Internet searches and discovering from other sources how it's really done. Every code given in the book fails to compile from syntax or other errors. Don't waste your money or time. I'm going to the Bookstore to find a REAL book on C coding.


All times are GMT -5. The time now is 11:56 PM.