/
mmpong.c
315 lines (268 loc) · 6.97 KB
1
2
3
4
5
/* MMPong - a Pong example using ManyMouse
Damon Ragno - with parts copied from the SDL manymouse example by Ryan Gordon
6
7
version 0.0.1
8
9
10
11
12
13
14
15
16
17
18
19
20
TODO:
-clean up the code, so it actually looks like something that would be an example
-add increasing speed
-add an actual scoreboard and not stdout
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "SDL.h"
#include "manymouse.h"
21
#define MAX_MICE 32
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
#define LEFT 0
#define RIGHT 1
#define UP 2
#define DOWN 3
int available_mice = 0;
typedef struct
{
int connected;
int x;
int y;
SDL_Color color;
char name[64];
} Mouse;
typedef struct
{
int x;
int y;
int speed;
int dir_h;
int dir_w;
} Ball;
static Mouse mice[MAX_MICE];
static Ball puck;
static int score1 = 0 , score2=0;
void score(int who)
{
53
54
55
const char *fmt = "MMPong, an example of ManyMouse usage: Score: %d - %d";
size_t len = sizeof (fmt) + 64;
char *buf = (char *) alloca(len);
56
Ball *ball = &puck;
57
58
59
if(who == 1){
score1++;
60
printf("player 1 has %d goals\n", score1);
61
62
} else{
score2++;
63
printf("player 2 has %d goals\n", score2);
64
}
65
66
67
snprintf(buf, len, fmt, score1, score2);
SDL_WM_SetCaption(buf, "MMPong");
68
69
70
ball->x = 320 ;
ball->y = rand() % 480 ;
ball->speed = 1;
71
72
if(rand() % 2 == 0) ball->dir_h = UP; else ball->dir_h = DOWN;
if(rand() % 2 == 0) ball->dir_w = LEFT; else ball->dir_w = RIGHT;
73
74
75
76
77
78
79
80
81
82
83
84
}
static void initial_setup(int screen_w, int screen_h)
{
int i;
memset(mice, '\0', sizeof (mice));
Ball *ball = &puck;
/* pick some random colors for each mouse. Sets position of both paddles */
for (i = 0; i < MAX_MICE; i++)
{
Mouse *mouse = &mice[i];
85
if((i % 2) == 0){
86
87
88
89
90
91
92
93
94
95
96
mouse->x = 2;
mouse->y = screen_h / 2;
} else {
mouse->x = screen_w -17 ;
mouse->y = screen_h / 2;
}
mouse->color.r = (int) (255.0*rand()/(RAND_MAX+1.0));
mouse->color.g = (int) (255.0*rand()/(RAND_MAX+1.0));
mouse->color.b = (int) (255.0*rand()/(RAND_MAX+1.0));
}
97
98
ball->x = screen_w / 2;
ball->y = rand() % screen_h;
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
ball->speed = 1;
ball->dir_h = UP;
ball->dir_w = LEFT;
}
static void init_mice(void)
{
available_mice = ManyMouse_Init();
if (available_mice > MAX_MICE)
available_mice = MAX_MICE;
if (available_mice == 0)
printf("No mice detected!\n");
else
{
int i;
117
printf("ManyMouse driver: %s\n", ManyMouse_DriverName());
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
for (i = 0; i < available_mice; i++)
{
const char *name = ManyMouse_DeviceName(i);
strncpy(mice[i].name, name, sizeof (mice[i].name));
mice[i].name[sizeof (mice[i].name) - 1] = '\0';
mice[i].connected = 1;
printf("#%d: %s\n", i, mice[i].name);
}
}
}
static void update_mice(int screen_w, int screen_h)
{
ManyMouseEvent event;
while (ManyMouse_PollEvent(&event))
{
Mouse *mouse;
if (event.device >= (unsigned int) available_mice)
continue;
mouse = &mice[event.device];
if (event.type == MANYMOUSE_EVENT_RELMOTION)
{
/* ignore side to side mouse movement */
if (event.item == 1) mouse->y += event.value;
if (mouse->y < 0) mouse->y = 0;
if (mouse->y > 410) mouse->y = 410;
}
else if (event.type == MANYMOUSE_EVENT_ABSMOTION)
; /* !!! FIXME: do something with this. */
else if (event.type == MANYMOUSE_EVENT_BUTTON)
; /* !!! FIXME: do something with this. */
else if (event.type == MANYMOUSE_EVENT_SCROLL)
; /* !!! FIXME: do something with this. */
else if (event.type == MANYMOUSE_EVENT_DISCONNECT)
mice[event.device].connected = 0;
}
}
static void update_ball(int screen_w, int screen_h)
{
165
166
static Uint32 lastupdate = 0;
Uint32 now = SDL_GetTicks();
167
Ball *ball = &puck;
168
int i = 0;
169
170
171
172
173
174
if (now - lastupdate < 10)
return;
lastupdate += 10;
175
if(ball->y < 0) ball->dir_h = DOWN;
176
177
else if(ball->y > screen_h - 10) ball->dir_h = UP;
178
if(ball->x < 15){
179
180
181
182
183
184
185
186
187
188
189
int scored = 1;
for (i = 0; i < available_mice; i += 2) {
if((ball->y >= mice[i].y) && (ball->y < mice[i].y + 70)) {
scored = 0;
break;
}
}
if (scored)
score(2);
else
ball->dir_w = RIGHT;
190
}
191
192
193
194
195
196
197
198
199
200
201
202
else if(ball->x > (screen_w - 25)) {
int scored = 1;
for (i = 1; i < available_mice; i += 2) {
if((ball->y >= mice[i].y) && (ball->y < mice[i].y + 70)) {
scored = 0;
break;
}
}
if (scored)
score(1);
else
ball->dir_w = LEFT;
203
}
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
if(ball->dir_w == LEFT && ball->dir_h == UP){
ball->x -= 3 ;
ball->y -= 3 ;
}
if(ball->dir_w == RIGHT && ball->dir_h == UP){
ball->x += 3;
ball->y -= 3 ;
}
if(ball->dir_w == LEFT && ball->dir_h == DOWN){
ball->x -= 3;
ball->y += 3;
}
if(ball->dir_w == RIGHT && ball->dir_h == DOWN){
ball->x +=3;
ball->y += 3;
}
}
static void draw_mouse(SDL_Surface *screen, int idx)
{
SDL_Rect r = { mice[idx].x, mice[idx].y, 15, 70 };
Uint32 color;
if (!mice[idx].connected)
return;
if (mice[idx].x < 0) mice[idx].x = 0;
if (mice[idx].x >= screen->w) mice[idx].x = screen->w-1;
if (mice[idx].y < 0) mice[idx].y = 0;
if (mice[idx].y >= screen->h) mice[idx].y = screen->h-1;
color = SDL_MapRGB(screen->format, mice[idx].color.r,
mice[idx].color.g, mice[idx].color.b);
SDL_FillRect(screen, &r, color);
}
static void draw_screen(SDL_Surface *screen)
{
int i;
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 50, 0, 0));
for (i = 0; i < available_mice; i++)
draw_mouse(screen, i);
Ball *ball = &puck;
SDL_Rect b = {ball->x, ball->y, 10, 10};
Uint32 color = SDL_MapRGB(screen->format, 255, 255, 43);
SDL_FillRect(screen, &b, color);
SDL_Flip(screen);
}
int main(int argc, char *argv[])
{
Uint32 initflags = SDL_INIT_VIDEO; /* See documentation for details */
SDL_Surface *screen;
Uint8 video_bpp = 0;
Uint32 videoflags = (SDL_HWSURFACE || SDL_DOUBLEBUF);
int done;
int cursor = 0;
SDL_Event event;
/* Initialize the SDL library */
if ( SDL_Init(initflags) < 0 ) {
fprintf(stderr, "Couldn't initialize SDL: %s\n",
SDL_GetError());
exit(1);
}
/* Set 640x480 video mode */
screen=SDL_SetVideoMode(640,480, video_bpp, videoflags);
if (screen == NULL) {
fprintf(stderr, "Couldn't set 640x480x%d video mode: %s\n",
video_bpp, SDL_GetError());
SDL_Quit();
exit(2);
}
280
SDL_WM_SetCaption("MMPong, an example of ManyMouse usage: Score: 0 - 0", "MMPong");
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
SDL_ShowCursor(cursor);
SDL_WM_GrabInput(SDL_GRAB_ON);
initial_setup(screen->w, screen->h);
init_mice();
done = 0;
while ( !done ) {
/* Check for events */
while ( SDL_PollEvent(&event) ) {
switch (event.type) {
case SDL_MOUSEMOTION:
break;
case SDL_MOUSEBUTTONDOWN:
break;
case SDL_KEYDOWN:
/* Any keypress quits the app... */
case SDL_QUIT:
done = 1;
break;
default:
break;
}
}
update_mice(screen->w, screen->h);
update_ball(screen->w, screen->h);
draw_screen(screen);
}
/* Clean up the SDL library */
ManyMouse_Quit();
SDL_Quit();
return(0);
}