Mercurial > sdl-ios-xcode
annotate XCodeiPhoneOS/Demos/src/mixer.c @ 2461:f8cd2eecde64 gsoc2008_iphone
grammar, typos
author | Holmes Futrell <hfutrell@umail.ucsb.edu> |
---|---|
date | Mon, 18 Aug 2008 18:02:43 +0000 |
parents | 6a946f3155d8 |
children |
rev | line source |
---|---|
2384
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
1 /* |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
2 * mixer.c |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
3 * written by Holmes Futrell |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
4 * use however you want |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
5 */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
6 |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
7 #import "SDL.h" |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
8 #import "common.h" |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
9 |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
10 #define NUM_CHANNELS 8 /* max number of sounds we can play at once */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
11 #define NUM_DRUMS 4 /* number of drums in our set */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
12 #define MILLESECONDS_PER_FRAME 16 /* about 60 frames per second */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
13 |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
14 static struct { |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
15 SDL_Rect rect; /* where the button is drawn */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
16 SDL_Color upColor; /* color when button is not active */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
17 SDL_Color downColor; /* color when button is active */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
18 int isPressed; /* is the button being pressed ? */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
19 int touchIndex; /* what mouse (touch) index pressed the button ? */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
20 } buttons[NUM_DRUMS]; |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
21 |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
22 struct sound { |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
23 Uint8 *buffer; /* audio buffer for sound file */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
24 Uint32 length; /* length of the buffer (in bytes) */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
25 }; |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
26 |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
27 /* this array holds the audio for the drum noises */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
28 static struct sound drums[NUM_DRUMS]; |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
29 |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
30 /* function declarations */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
31 void handleMouseButtonDown(SDL_Event *event); |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
32 void handleMouseButtonUp(SDL_Event *event); |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
33 int playSound(struct sound *); |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
34 void render(void); |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
35 void initializeButtons(); |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
36 void audioCallback(void *userdata, Uint8 *stream, int len); |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
37 void loadSound(const char *file, struct sound *s); |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
38 |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
39 struct { |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
40 /* channel array holds information about currently playing sounds */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
41 struct { |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
42 Uint8 *position; /* what is the current position in the buffer of this sound ? */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
43 Uint32 remaining; /* how many bytes remaining before we're done playing the sound ? */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
44 Uint32 timestamp; /* when did this sound start playing ? */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
45 } channels[NUM_CHANNELS]; |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
46 SDL_AudioSpec outputSpec; /* what audio format are we using for output? */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
47 int numSoundsPlaying; /* how many sounds are currently playing */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
48 } mixer; |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
49 |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
50 /* sets up the buttons (color, position, state) */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
51 void initializeButtons() { |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
52 |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
53 int i; |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
54 int spacing = 10; /* gap between drum buttons */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
55 SDL_Rect buttonRect; /* keeps track of where to position drum */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
56 SDL_Color upColor = { 86, 86, 140, 255 }; /* color of drum when not pressed */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
57 SDL_Color downColor = { 191, 191, 221, 255 }; /* color of drum when pressed */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
58 |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
59 buttonRect.x = spacing; |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
60 buttonRect.y = spacing; |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
61 buttonRect.w = SCREEN_WIDTH - 2 * spacing; |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
62 buttonRect.h = ( SCREEN_HEIGHT - (NUM_DRUMS + 1) * spacing ) / NUM_DRUMS; |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
63 |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
64 /* setup each button */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
65 for (i=0; i<NUM_DRUMS; i++) { |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
66 |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
67 buttons[i].rect = buttonRect; |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
68 buttons[i].isPressed = 0; |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
69 buttons[i].upColor = upColor; |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
70 buttons[i].downColor = downColor; |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
71 |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
72 buttonRect.y += spacing + buttonRect.h; /* setup y coordinate for next drum */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
73 |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
74 } |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
75 } |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
76 /* |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
77 loads a wav file (stored in 'file'), converts it to the mixer's output format, |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
78 and stores the resulting buffer and length in the sound structure |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
79 */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
80 void loadSound(const char *file, struct sound *s) { |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
81 SDL_AudioSpec spec; /* the audio format of the .wav file */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
82 SDL_AudioCVT cvt; /* used to convert .wav to output format when formats differ */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
83 int result; |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
84 if (SDL_LoadWAV(file, &spec, &s->buffer, &s->length) == NULL) { |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
85 fatalError("could not load .wav"); |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
86 } |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
87 /* build the audio converter */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
88 result = SDL_BuildAudioCVT(&cvt, spec.format, spec.channels, spec.freq, |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
89 mixer.outputSpec.format, mixer.outputSpec.channels, mixer.outputSpec.freq); |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
90 if (result == -1) { |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
91 fatalError("could not build audio CVT"); |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
92 } |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
93 else if (result != 0) { |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
94 /* |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
95 this happens when the .wav format differs from the output format. |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
96 we convert the .wav buffer here |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
97 */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
98 cvt.buf = (Uint8 *)SDL_malloc(s->length * cvt.len_mult); /* allocate conversion buffer */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
99 cvt.len = s->length; /* set conversion buffer length */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
100 SDL_memcpy(cvt.buf, s->buffer, s->length); /* copy sound to conversion buffer */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
101 if (SDL_ConvertAudio(&cvt) == -1) { /* convert the sound */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
102 fatalError("could not convert .wav"); |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
103 } |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
104 SDL_free(s->buffer); /* free the original (unconverted) buffer */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
105 s->buffer = cvt.buf; /* point sound buffer to converted buffer */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
106 s->length = cvt.len_cvt; /* set sound buffer's new length */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
107 } |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
108 } |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
109 |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
110 /* called from main event loop */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
111 void handleMouseButtonDown(SDL_Event *event) { |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
112 |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
113 int x, y, mouseIndex, i, drumIndex; |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
114 |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
115 mouseIndex = event->button.which; |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
116 drumIndex = -1; |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
117 |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
118 SDL_SelectMouse(mouseIndex); |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
119 SDL_GetMouseState(&x, &y); |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
120 /* check if we hit any of the drum buttons */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
121 for (i=0; i<NUM_DRUMS; i++) { |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
122 if (x >= buttons[i].rect.x && x < buttons[i].rect.x + buttons[i].rect.w \ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
123 && y >= buttons[i].rect.y && y < buttons[i].rect.y + buttons[i].rect.h) { |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
124 drumIndex = i; |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
125 break; |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
126 } |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
127 } |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
128 if (drumIndex != -1) { |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
129 /* if we hit a button */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
130 buttons[drumIndex].touchIndex = mouseIndex; |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
131 buttons[drumIndex].isPressed = 1; |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
132 playSound(&drums[drumIndex]); |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
133 } |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
134 |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
135 } |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
136 |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
137 /* called from main event loop */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
138 void handleMouseButtonUp(SDL_Event *event) { |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
139 int i; |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
140 int mouseIndex = event->button.which; |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
141 /* check if this should cause any of the buttons to become unpressed */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
142 for (i=0; i<NUM_DRUMS; i++) { |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
143 if (buttons[i].touchIndex == mouseIndex) { |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
144 buttons[i].isPressed = 0; |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
145 } |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
146 } |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
147 } |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
148 |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
149 /* draws buttons to screen */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
150 void render(void) { |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
151 int i; |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
152 SDL_RenderFill(50, 50, 50, 255, NULL); /* draw background (gray) */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
153 /* draw the drum buttons */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
154 for (i=0; i<NUM_DRUMS; i++) { |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
155 SDL_Color color = buttons[i].isPressed ? buttons[i].downColor : buttons[i].upColor; |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
156 SDL_RenderFill(color.r, color.g, color.b, color.unused, &buttons[i].rect); |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
157 } |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
158 /* update the screen */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
159 SDL_RenderPresent(); |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
160 } |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
161 |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
162 /* |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
163 finds a sound channel in the mixer for a sound |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
164 and sets it up to start playing |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
165 */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
166 int playSound(struct sound *s) { |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
167 /* |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
168 find an empty channel to play on. |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
169 if no channel is available, use oldest channel |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
170 */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
171 int i; |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
172 int selected_channel = -1; |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
173 int oldest_channel = 0; |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
174 |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
175 if (mixer.numSoundsPlaying == 0) { |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
176 /* we're playing a sound now, so start audio callback back up */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
177 SDL_PauseAudio(0); |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
178 } |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
179 |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
180 /* find a sound channel to play the sound on */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
181 for (i=0; i<NUM_CHANNELS; i++) { |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
182 if (mixer.channels[i].position == NULL) { |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
183 /* if no sound on this channel, select it */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
184 selected_channel = i; |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
185 break; |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
186 } |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
187 /* if this channel's sound is older than the oldest so far, set it to oldest */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
188 if (mixer.channels[i].timestamp < mixer.channels[oldest_channel].timestamp) |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
189 oldest_channel = i; |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
190 } |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
191 |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
192 /* no empty channels, take the oldest one */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
193 if (selected_channel == -1) |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
194 selected_channel = oldest_channel; |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
195 else |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
196 mixer.numSoundsPlaying++; |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
197 |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
198 /* point channel data to wav data */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
199 mixer.channels[selected_channel].position = s->buffer; |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
200 mixer.channels[selected_channel].remaining = s->length; |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
201 mixer.channels[selected_channel].timestamp = SDL_GetTicks(); |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
202 |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
203 return selected_channel; |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
204 } |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
205 |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
206 /* |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
207 Called from SDL's audio system. Supplies sound input with data by mixing together all |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
208 currently playing sound effects. |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
209 */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
210 void audioCallback(void *userdata, Uint8 *stream, int len) { |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
211 int i; |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
212 int copy_amt; |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
213 SDL_memset(stream, mixer.outputSpec.silence, len); /* initialize buffer to silence */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
214 /* for each channel, mix in whatever is playing on that channel */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
215 for (i=0; i<NUM_CHANNELS; i++) { |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
216 if (mixer.channels[i].position == NULL) { |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
217 /* if no sound is playing on this channel */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
218 continue; /* nothing to do for this channel */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
219 } |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
220 |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
221 /* copy len bytes to the buffer, unless we have fewer than len bytes remaining */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
222 copy_amt = mixer.channels[i].remaining < len ? mixer.channels[i].remaining : len; |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
223 |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
224 /* mix this sound effect with the output */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
225 SDL_MixAudioFormat(stream, mixer.channels[i].position, mixer.outputSpec.format, copy_amt, 150); |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
226 |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
227 /* update buffer position in sound effect and the number of bytes left */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
228 mixer.channels[i].position += copy_amt; |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
229 mixer.channels[i].remaining -= copy_amt; |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
230 |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
231 /* did we finish playing the sound effect ? */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
232 if (mixer.channels[i].remaining == 0) { |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
233 mixer.channels[i].position = NULL; /* indicates no sound playing on channel anymore */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
234 mixer.numSoundsPlaying--; |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
235 if (mixer.numSoundsPlaying == 0) { |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
236 /* if no sounds left playing, pause audio callback */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
237 SDL_PauseAudio(1); |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
238 } |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
239 } |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
240 } |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
241 } |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
242 |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
243 int main(int argc, char *argv[]) { |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
244 |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
245 int done; /* has user tried to quit ? */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
246 SDL_WindowID windowID; /* our main window */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
247 SDL_Event event; |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
248 Uint32 startFrame; /* holds when frame started processing */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
249 Uint32 endFrame; /* holds when frame ended processing */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
250 Uint32 delay; /* calculated delay, how long should we wait before next frame? */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
251 |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
252 if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO) < 0) { |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
253 fatalError("could not initialize SDL"); |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
254 } |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
255 windowID = SDL_CreateWindow(NULL, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_OPENGL | SDL_WINDOW_BORDERLESS); |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
256 SDL_CreateRenderer(windowID, 0, 0); |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
257 |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
258 /* initialize the mixer */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
259 SDL_memset(&mixer, 0, sizeof(mixer)); |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
260 /* setup output format */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
261 mixer.outputSpec.freq = 44100; |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
262 mixer.outputSpec.format = AUDIO_S16LSB; |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
263 mixer.outputSpec.channels = 2; |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
264 mixer.outputSpec.samples = 256; |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
265 mixer.outputSpec.callback = audioCallback; |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
266 mixer.outputSpec.userdata = NULL; |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
267 |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
268 /* open audio for output */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
269 if (SDL_OpenAudio(&mixer.outputSpec, NULL) != 0) { |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
270 fatalError("Opening audio failed"); |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
271 } |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
272 |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
273 /* load our drum noises */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
274 loadSound("ds_kick_big_amb.wav", &drums[3]); |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
275 loadSound("ds_brush_snare.wav", &drums[2]); |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
276 loadSound("ds_loose_skin_mute.wav", &drums[1]); |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
277 loadSound("ds_china.wav", &drums[0]); |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
278 |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
279 /* setup positions, colors, and state of buttons */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
280 initializeButtons(); |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
281 |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
282 /* enter main loop */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
283 done = 0; |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
284 while(!done) { |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
285 startFrame = SDL_GetTicks(); |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
286 while (SDL_PollEvent(&event)) { |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
287 switch(event.type) { |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
288 case SDL_MOUSEBUTTONDOWN: |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
289 handleMouseButtonDown(&event); |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
290 break; |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
291 case SDL_MOUSEBUTTONUP: |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
292 handleMouseButtonUp(&event); |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
293 break; |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
294 case SDL_QUIT: |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
295 done = 1; |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
296 break; |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
297 } |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
298 } |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
299 render(); /* draw buttons */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
300 endFrame = SDL_GetTicks(); |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
301 |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
302 /* figure out how much time we have left, and then sleep */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
303 delay = MILLESECONDS_PER_FRAME - (endFrame - startFrame); |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
304 if (delay < 0) { |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
305 delay = 0; |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
306 } else if (delay > MILLESECONDS_PER_FRAME) { |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
307 delay = MILLESECONDS_PER_FRAME; |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
308 } |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
309 SDL_Delay(delay); |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
310 } |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
311 |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
312 /* cleanup code, let's free up those sound buffers */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
313 int i; |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
314 for (i=0; i<NUM_DRUMS; i++) { |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
315 SDL_free(drums[i].buffer); |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
316 } |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
317 /* let SDL do its exit code */ |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
318 SDL_Quit(); |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
319 |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
320 return 0; |
6a946f3155d8
Drum kit demo (SDL_mixer)
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff
changeset
|
321 } |