-
Notifications
You must be signed in to change notification settings - Fork 1
/
trevorSdl.c
220 lines (176 loc) · 5.65 KB
/
trevorSdl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/select.h>
#include <sys/wait.h>
#include <err.h>
#include <fcntl.h>
#include <mqueue.h>
#include <signal.h>
#include <string.h>
#include <SDL/SDL.h>
#include <stdbool.h>
//created by Trevor Chan, and modified by Randy White
//Ripped from man page
#define handle_error_en(en, msg) \
do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
#define handle_error(msg) \
do { perror(msg); exit(EXIT_FAILURE); } while (0)
#define MQNAME "/CREAL_JOY_0"
#define LOOPS 1000
#define PRIO 10
#define C_MAX_MSG 256 //this will be the max messages right here
#define C_DEF_MODE 777 //this is for default shit right here with fds
int pid;
pid_t mainPid;
bool mainGameLoopConditional;
//begin
int main(int argc, char* argv[])
{
//sdl stuff
SDL_Joystick *joystick;
SDL_Event event;
mainGameLoopConditional = true; //this is for the main loop
//message queue stuff
mqd_t mq;
int status;
struct mq_attr attr,attrOld;
int i;
fd_set set;//crap
struct mq_attr mqa;
mode_t defaultMode = C_DEF_MODE; //this is supposed to be the default mode right here for the queue
//Analog joystick dead zone
const int JOYSTICK_DEAD_ZONE = 8000; //taken from lazy foo's website : http://lazyfoo.net/tutorials/SDL/19_gamepads_and_joysticks/index.php
mq_unlink(MQNAME); //remove the original message queue
attr.mq_flags = 0;
attr.mq_curmsgs = 0;
attr.mq_maxmsg = 1024;
attr.mq_msgsize = 2; //just using an integer here
mq = mq_open(MQNAME, O_CREAT|O_RDWR , 0644, &attr); //openning the bullshit
perror("mq_open\n");
if (mq == (mqd_t)-1)
{
//interesting to cast this type over -1
perror("mq_open\n");
return -1;
}
status = mq_getattr(mq, &attr);
if (status)
{
perror("mq_getattr()");
return -1;
}
//~ mq_setattr(mq,&attr,NULL); //SETTING THE ATTRIBUTES OF THE MESSAGE QUEUE
//~ mq_send(mq, "L", 2, 1); //make sure to cast this thing here
perror("set attr\n");
if (SDL_Init( SDL_INIT_JOYSTICK|SDL_INIT_VIDEO ) < 0) //SDL_INIT_VIDEO | //ONLY TRYING TO INITIALIZE THE JOY STICK SYSTEM
{
fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
exit(1);
}
else
{
SDL_JoystickEventState(SDL_ENABLE);
joystick = SDL_JoystickOpen(0);
printf("%i joysticks were found.\n\n", SDL_NumJoysticks() );
printf("The names of the joysticks are:\n");
for( i=0; i < SDL_NumJoysticks(); i++ )
{
printf(" %s\n", SDL_JoystickName(i));
}
while(mainGameLoopConditional)
{
usleep(1); //sleep you bastard
while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_JOYAXISMOTION: /* Handle Joystick Motion */
{
if ( ( event.jaxis.value < -3200 ) || (event.jaxis.value > 3200 ) )
{
if( event.jaxis.axis == 0)
{
//left and right movement code
if( event.jaxis.value < -JOYSTICK_DEAD_ZONE )
{
printf("Joystick LEFT movement\n");
mq_send(mq, "L", 2, 1); //make sure to cast this thing here
perror("send\n");
}
//Right of dead zone
else if( event.jaxis.value > JOYSTICK_DEAD_ZONE )
{
printf("Joystick RIGHT movement\n");
mq_send(mq, "R", 2, 1); //make sure to cast this thing here
perror("send\n");
}
}
if( event.jaxis.axis == 1)
{
//up and down movement code
if( event.jaxis.value < -JOYSTICK_DEAD_ZONE )
{
printf("Joystick UP movement\n");
mq_send(mq, "U", 2, 1); //make sure to cast this thing here
perror("send\n");
}
//Right of dead zone
else if( event.jaxis.value > JOYSTICK_DEAD_ZONE )
{
printf("Joystick DOWN movement\n");
mq_send(mq, "D", 2, 1); //make sure to cast this thing here
perror("send\n");
}
}
}
break;
}
//~ case SDL_JoyButtonEvent:
//~ {
//~
//~ break;
//~ }
case SDL_KEYDOWN:
{
/* handle keyboard stuff here */
break;
}
case SDL_QUIT:
{
/* Set whatever flags are necessary to */
/* end the main game loop here */
mainGameLoopConditional = false; //exit out of the game loop
break;
}
}
}
}//main game loop
SDL_JoystickClose(joystick);
}
mq_close(mq); //close the file descriptor
SDL_Quit();
return 0;
}
//GRAVE YARD BELOW
//~ case SDL_KEYDOWN:
//~ /* keyboard stuff */
//~ while( SDL_PollEvent( &event ) )
//~ {
//~ /* We are only worried about SDL_KEYDOWN and SDL_KEYUP events */
//~ switch( event.type )
//~ {
//~ case SDL_KEYDOWN:
//~ printf( "Key press detected\n" );
//~ break;
//~
//~ case SDL_KEYUP:
//~ printf( "Key release detected\n" );
//~ break;
//~
//~ default:
//~ break;
//~ }
//~ }
//~ break;