Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

121 рядки
2.1 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright © 2010—2015 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 defined USE_SDL_MIXER
  17. # if defined HAVE_SDL_SDL_H
  18. # include <SDL/SDL.h>
  19. # else
  20. # include <SDL.h>
  21. # endif
  22. # if defined HAVE_SDL_SDL_MIXER_H
  23. # include <SDL/SDL_mixer.h>
  24. # else
  25. # include <SDL_mixer.h>
  26. # endif
  27. #endif
  28. namespace lol
  29. {
  30. /*
  31. * Sample implementation class
  32. */
  33. class SampleData
  34. {
  35. friend class Sample;
  36. private:
  37. String m_name;
  38. #if defined 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 SampleData())
  48. {
  49. data->m_name = String("<sample> ") + path;
  50. #if defined USE_SDL_MIXER
  51. for (auto candidate : System::GetPathList(path))
  52. {
  53. data->m_chunk = Mix_LoadWAV(candidate.C());
  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 USE_SDL_MIXER
  67. if (data->m_chunk)
  68. Mix_FreeChunk(data->m_chunk);
  69. #endif
  70. delete data;
  71. }
  72. void Sample::TickGame(float seconds)
  73. {
  74. Entity::TickGame(seconds);
  75. }
  76. char const *Sample::GetName()
  77. {
  78. return data->m_name.C();
  79. }
  80. void Sample::Play()
  81. {
  82. #if defined 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 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 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 */