main.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #include <iostream>
  2. #include <SDL/SDL.h>
  3. #include "Sounderer.h"
  4. int main ( int argc, char** argv )
  5. {
  6. /// [1] Démarrage
  7. // [1.1] Démarrages SDL
  8. if ( SDL_Init( SDL_INIT_VIDEO ) < 0)
  9. {
  10. std::cout << "Impossible d'initialiser la SDL: " << SDL_GetError() << std::endl;
  11. return 1;
  12. }
  13. // [1.2] Préparation de fermeture
  14. atexit(SDL_Quit);
  15. bool done = false;
  16. // [1.3] Para-fenêtre
  17. SDL_WM_SetCaption("Tests du son", 0);
  18. /// [2] Préparation des composants
  19. // [2.1] Préparation de la fenêtre
  20. SDL_Surface* screen = SDL_SetVideoMode(640, 480, 32,
  21. SDL_HWSURFACE|SDL_DOUBLEBUF);
  22. if ( !screen )
  23. {
  24. std::cout << "Impossible de crée la fenêtre : " << SDL_GetError() << std::endl;
  25. return 1;
  26. }
  27. // [2.2] Préparation du son
  28. Sounderer soundPlayer("Sons/");
  29. if (soundPlayer.init() == false) done = true;
  30. else
  31. {
  32. soundPlayer.preLoad("growl1", EXT_OGG);
  33. soundPlayer.preLoad("breathe1", EXT_OGG);
  34. soundPlayer.preLoad("fire", EXT_OGG);
  35. soundPlayer.preLoad("metal1", EXT_OGG);
  36. }
  37. // [2.3] Mesures de performance
  38. /*Uint32 tempsDepart, difference, moyenne, nbTests(100000);
  39. tempsDepart = SDL_GetTicks();
  40. for (unsigned int i(0); i<nbTests; i++)
  41. {
  42. //quelque chose à mesurer
  43. }
  44. difference = SDL_GetTicks() - tempsDepart;
  45. moyenne = difference / nbTests;
  46. std::cout << "L'appel de play() a duré " <<difference<<" / "<<nbTests<<" soit "<<moyenne<<"ms en moyenne."<<std::endl;*/
  47. /// [3] Boucle principale
  48. while (!done)
  49. {
  50. // [3.1] Gestion évènements
  51. SDL_Event event;
  52. while (SDL_PollEvent(&event))
  53. {
  54. switch (event.type)
  55. {
  56. case SDL_QUIT:
  57. done = true;
  58. break;
  59. case SDL_KEYDOWN:
  60. switch (event.key.keysym.sym)
  61. {
  62. case SDLK_ESCAPE:
  63. done = true;
  64. break;
  65. case SDLK_b:
  66. soundPlayer.play("breathe1");
  67. break;
  68. case SDLK_e:
  69. soundPlayer.fadePlay("effect", 3692, INFINITY_LOOP);
  70. break;
  71. case SDLK_f:
  72. soundPlayer.play("fire");
  73. break;
  74. case SDLK_g:
  75. soundPlayer.play("growl1");
  76. break;
  77. case SDLK_h:
  78. soundPlayer.play("hou", 2);
  79. break;
  80. default:
  81. soundPlayer.play("metal1", INFINITY_LOOP);
  82. break;
  83. }
  84. break;
  85. case SDL_KEYUP:
  86. switch (event.key.keysym.sym)
  87. {
  88. case SDLK_ESCAPE:
  89. done = true;
  90. break;
  91. case SDLK_b:
  92. soundPlayer.stop("breathe1");
  93. break;
  94. case SDLK_e:
  95. soundPlayer.stop("effect");
  96. break;
  97. case SDLK_f:
  98. soundPlayer.stop("fire");
  99. break;
  100. case SDLK_g:
  101. soundPlayer.stop("growl1");
  102. break;
  103. case SDLK_h:
  104. soundPlayer.stop("hou");
  105. break;
  106. default:
  107. soundPlayer.stop("metal1");
  108. break;
  109. }
  110. break;
  111. } // end switch event type
  112. } // end of message processing
  113. // [3.2] Calculs
  114. // [3.3] Dessin des composants
  115. SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 150, 55, 12));
  116. //
  117. SDL_Flip(screen);
  118. } //fin bcl principale
  119. ///[4] Destruction des composants
  120. SDL_FreeSurface(screen);
  121. std::cout << "Aucune erreur détectée." << std::endl;
  122. return 0;
  123. }