LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-29-2007, 06:31 PM   #1
aatwell
Member
 
Registered: Apr 2007
Location: San Jose, CA
Posts: 31

Rep: Reputation: 15
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;
 }
}
 
Old 11-30-2007, 12:23 PM   #2
aatwell
Member
 
Registered: Apr 2007
Location: San Jose, CA
Posts: 31

Original Poster
Rep: Reputation: 15
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;
        }
}
 
Old 11-30-2007, 01:16 PM   #3
aatwell
Member
 
Registered: Apr 2007
Location: San Jose, CA
Posts: 31

Original Poster
Rep: Reputation: 15
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.
 
Old 11-30-2007, 01:46 PM   #4
aatwell
Member
 
Registered: Apr 2007
Location: San Jose, CA
Posts: 31

Original Poster
Rep: Reputation: 15
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
error: Error for wireless request "Set Mode" (8B06) : invalid argument "roaming" penguin chick Linux - Wireless Networking 5 08-22-2008 01:16 PM
LXer: "a Watershed Event for Open Source Office Suites": LinuxWorld ... LXer Syndicated Linux News 0 01-20-2006 07:01 PM
LXer: "a Watershed Event for Open Source Office Suites": LinuxWorld ... LXer Syndicated Linux News 0 01-20-2006 06:47 PM
Illegal Seek error from Motion larry.barnes Linux - Software 0 05-01-2004 08:58 PM
music/sound in general plays in "slow motion" tvstonydanza Linux - General 0 01-29-2004 07:29 PM


All times are GMT -5. The time now is 10:39 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration