Error with "SDL_MouseMotionEvent *motion = event->motion;"
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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;
}
}
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:
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.