You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

121 lines
2.2 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright © 2010—2018 Sam Hocevar <sam@hocevar.net>
  5. //
  6. // Lol Engine is free software. It comes without any warranty, to
  7. // the extent permitted by applicable law. You can redistribute it
  8. // and/or modify it under the terms of the Do What the Fuck You Want
  9. // to Public License, Version 2, as published by the WTFPL Task Force.
  10. // See http://www.wtfpl.net/ for more details.
  11. //
  12. #include <lol/engine-internal.h>
  13. #include <cstdlib>
  14. #include <cstdio>
  15. #include <cstring>
  16. #if LOL_USE_SDL_MIXER
  17. # if HAVE_SDL_SDL_H
  18. # include <SDL/SDL.h>
  19. # include <SDL/SDL_mixer.h>
  20. # elif HAVE_SDL2_SDL_H
  21. # include <SDL2/SDL.h>
  22. # include <SDL2/SDL_mixer.h>
  23. # else
  24. # include <SDL.h>
  25. # include <SDL_mixer.h>
  26. # endif
  27. #endif
  28. namespace lol
  29. {
  30. /*
  31. * sample implementation class
  32. */
  33. class sample_data
  34. {
  35. friend class sample;
  36. private:
  37. std::string m_name;
  38. #if defined LOL_USE_SDL_MIXER
  39. Mix_Chunk *m_chunk;
  40. int m_channel;
  41. #endif
  42. };
  43. /*
  44. * Public sample class
  45. */
  46. sample::sample(char const *path)
  47. : data(new sample_data())
  48. {
  49. data->m_name = std::string("<sample> ") + path;
  50. #if defined LOL_USE_SDL_MIXER
  51. for (auto candidate : sys::get_path_list(path))
  52. {
  53. data->m_chunk = Mix_LoadWAV(candidate.c_str());
  54. if (data->m_chunk)
  55. break;
  56. }
  57. if (!data->m_chunk)
  58. {
  59. msg::error("could not load sample %s: %s\n", path, Mix_GetError());
  60. }
  61. data->m_channel = -1;
  62. #endif
  63. }
  64. sample::~sample()
  65. {
  66. #if defined LOL_USE_SDL_MIXER
  67. if (data->m_chunk)
  68. Mix_FreeChunk(data->m_chunk);
  69. #endif
  70. delete data;
  71. }
  72. void sample::tick_game(float seconds)
  73. {
  74. Entity::tick_game(seconds);
  75. }
  76. std::string sample::GetName() const
  77. {
  78. return data->m_name;
  79. }
  80. void sample::play()
  81. {
  82. #if defined LOL_USE_SDL_MIXER
  83. if (data->m_chunk)
  84. data->m_channel = Mix_PlayChannel(-1, data->m_chunk, 0);
  85. #endif
  86. }
  87. void sample::loop()
  88. {
  89. #if defined LOL_USE_SDL_MIXER
  90. if (data->m_chunk)
  91. data->m_channel = Mix_PlayChannel(-1, data->m_chunk, -1);
  92. #endif
  93. }
  94. void sample::stop()
  95. {
  96. #if defined LOL_USE_SDL_MIXER
  97. if (data->m_channel >= 0)
  98. Mix_HaltChannel(data->m_channel);
  99. data->m_channel = -1;
  100. #endif
  101. }
  102. } /* namespace lol */