Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "SDL.h"
  4. #include "SDL_mixer.h"
  5. #include <dirent.h>
  6.  
  7.  
  8. Mix_Chunk *phaser = NULL;
  9. Mix_Music *music = NULL;
  10.  
  11. char name[240];
  12.  
  13. DIR *directory;
  14.  
  15. int phaserChannel = -1;
  16.  
  17. void handleKey(SDL_KeyboardEvent key);
  18. void musicDone();
  19.  
  20.  
  21.  
  22. int main(int argc, char* args[]) {
  23.  
  24. SDL_Surface *screen;
  25. SDL_Event event;
  26. int done = 0;
  27.  
  28. /* Same setup as before */
  29. int audio_rate = 22050;
  30. Uint16 audio_format = AUDIO_S16;
  31. int audio_channels = 2;
  32. int audio_buffers = 4096;
  33.  
  34. SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO);
  35.  
  36. if(Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers)) {
  37. printf("Unable to open audio!\n");
  38. exit(1);
  39. }
  40.  
  41. /* Pre-load sound effects */
  42.  
  43. directory=opendir("music/");
  44.  
  45. phaser = Mix_LoadWAV("phaser.wav");
  46.  
  47. screen = SDL_SetVideoMode(320, 240, 0, 0);
  48.  
  49.  
  50. while(!done) {
  51.  
  52. if(music == NULL) {
  53. music = Mix_LoadMUS(browser(directory));
  54. Mix_PlayMusic(music, 0);
  55. Mix_HookMusicFinished(musicDone);
  56.  
  57. } /*else {
  58. Mix_HaltMusic();
  59. Mix_FreeMusic(music);
  60. music = NULL;
  61. }*/
  62.  
  63. while(SDL_PollEvent(&event)) {
  64. switch(event.type) {
  65. case SDL_QUIT:
  66. done = 1;
  67. break;
  68. case SDL_KEYDOWN:
  69. case SDL_KEYUP:
  70. handleKey(event.key);
  71. break;
  72. default:break;
  73. }
  74. }
  75. SDL_Delay(50);
  76.  
  77. }
  78.  
  79. Mix_CloseAudio();
  80. SDL_Quit();
  81.  
  82. }
  83.  
  84. void handleKey(SDL_KeyboardEvent key) {
  85. switch(key.keysym.sym) {
  86. case SDLK_p:
  87.  
  88. if(key.type == SDL_KEYDOWN) {
  89.  
  90. if(phaserChannel < 0) {
  91. phaserChannel = Mix_PlayChannel(-1, phaser, -1);
  92. }
  93. } else {
  94. Mix_HaltChannel(phaserChannel);
  95.  
  96. phaserChannel = -1;
  97. }
  98. break;
  99. default:break;
  100. }
  101. }
  102.  
  103. void musicDone() {
  104. Mix_HaltMusic();
  105. Mix_FreeMusic(music);
  106. music = NULL;
  107. }
  108.  
  109. int browser(DIR *dir) {
  110.  
  111. char path[128];
  112. strcpy(path,"music/");
  113. struct dirent *dp;
  114. dp=readdir(dir);
  115. if((!strcmp(".",dp->d_name))||(!strcmp("..",dp->d_name))){
  116. dp=readdir(dir);
  117. };
  118. if((!strcmp(".",dp->d_name))||(!strcmp("..",dp->d_name))){
  119. dp=readdir(dir);
  120. strcpy(name,dp->d_name);
  121. } else{
  122. strcpy(name,dp->d_name);
  123. }
  124. strcat(path,name);
  125.  
  126. return path;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement