Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/sound/src/demo4.c @ 2974

Last change on this file since 2974 was 2777, checked in by simon, 20 years ago

/branches/sound: brought up sound_control.cc which doesn't do anything at the moment.

File size: 3.3 KB
Line 
1#include <stdio.h>
2#include <stdlib.h>
3#include "SDL.h"
4#include "SDL_mixer.h"
5#include "SDL_thread.h"
6
7Mix_Chunk *phaser = NULL;
8Mix_Music *music = NULL;
9
10int phaserChannel = -1;
11
12void handleKey(SDL_KeyboardEvent key);
13void musicDone();
14
15/*This function polls every 20 ms for new SDL_Events.
16If it finds such events, it determines type and has a behaviour for different
17SDL_Keys. It can be called by SDL_Quit or other events.*/
18
19int main(void) {
20  SDL_Surface *screen;
21  SDL_Event event;
22  int done = 0, bits = 0, audio_rate = 44100, audio_channels = 2, 
23  audio_buffers = 4096, volume = SDL_MIX_MAXVOLUME;
24  Uint16 audio_format = AUDIO_S16; 
25
26  // Opening the sound and graphic card
27  SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO);
28  if(Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers)) {
29    printf("Unable to open audio!\n");
30    exit(1);
31  }
32
33  // Print out some info on the audio device and stream
34  Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels);
35  bits=audio_format&0xFF;
36  printf("Opened audio at %d Hz %d bit %s, %d bytes audio buffer\n", audio_rate, bits, audio_channels>1?"stereo":"mono", audio_buffers );
37
38  //
39  phaser = Mix_LoadWAV("phaser.wav");
40  screen = SDL_SetVideoMode(1024,768,0,SDL_FULLSCREEN|SDL_ANYFORMAT|SDL_HWSURFACE|SDL_DOUBLEBUF);
41  SDL_WM_SetCaption("mixer4 - SDL_mixer demo","mixer4");
42  SDL_ShowCursor(SDL_DISABLE); 
43 
44  while(!done) {
45    while(SDL_PollEvent(&event)) {
46      switch(event.type) {
47      case SDL_QUIT:
48        done = 1;
49        break;
50      case SDL_KEYDOWN:
51        switch(event.key.keysym.sym){
52
53          // Escape and q exit the loop
54        case SDLK_ESCAPE:
55          done = 1;
56          break;
57        case SDLK_q:
58          done = 1;
59          break;
60
61          // Restart the music
62        case SDLK_r:
63          Mix_RewindMusic();
64          break;
65
66          // For future implementation with a mp3 -> fast forward and backwards
67        case SDLK_LEFT:
68          switch(Mix_GetMusicType(NULL))
69            {
70            case MUS_MP3:
71              Mix_SetMusicPosition(-5);
72              break;
73            default:
74              printf("Cannot turn back this type of music\n");
75              break;
76            }
77          break;
78        case SDLK_RIGHT:
79          switch(Mix_GetMusicType(NULL))
80            {
81            case MUS_MP3:
82              Mix_SetMusicPosition(+5);
83              break;
84              default:
85                printf("cannot forward this type of music\n");
86                break;
87            }
88          break;
89
90          // Up and down serve as volume control
91        case SDLK_UP:
92          volume=(volume+1)<<1;
93          if(volume>SDL_MIX_MAXVOLUME)
94            volume=SDL_MIX_MAXVOLUME;
95            Mix_VolumeMusic(volume);
96            break;
97        case SDLK_DOWN:
98          volume>>=1;
99          Mix_VolumeMusic(volume);
100          break;
101
102          // Stops the music and continues the music
103        case SDLK_SPACE:
104          if(Mix_PausedMusic())
105            Mix_ResumeMusic();
106          else
107            Mix_PauseMusic();
108          break;
109
110          // Phaser
111        case SDLK_p:
112          if(event.key.type == SDL_KEYDOWN) {
113            if(phaserChannel < 0) {
114              phaserChannel = Mix_PlayChannel(-1, phaser, -1);
115            }
116            else {
117              Mix_HaltChannel(phaserChannel);
118              phaserChannel = -1;
119            }
120          }
121          break;
122
123          // Music
124        case SDLK_m:
125          if(event.key.state == SDL_PRESSED) {
126            if(music == NULL) {
127              music = Mix_LoadMUS("music.ogg"); 
128              Mix_PlayMusic(music, 0);
129              Mix_HookMusicFinished(musicDone);
130            } else {
131              Mix_HaltMusic();
132              Mix_FreeMusic(music);
133              music = NULL;
134            }
135          }
136          break;
137        default:
138          break;
139        }
140      }
141      SDL_Delay(30);
142    }
143  }
144  Mix_CloseAudio();
145  SDL_Quit();
146}
147 
148void musicDone() {
149  Mix_HaltMusic();
150  Mix_FreeMusic(music);
151  music = NULL;
152}
Note: See TracBrowser for help on using the repository browser.