107 lines
1.8 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2013 Sam Hocevar <sam@hocevar.net>
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the Do What The Fuck You Want To
  7. // Public License, Version 2, as published by Sam Hocevar. See
  8. // http://www.wtfpl.net/ for more details.
  9. //
  10. #if defined HAVE_CONFIG_H
  11. # include "config.h"
  12. #endif
  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. #include "core.h"
  29. using namespace std;
  30. namespace lol
  31. {
  32. /*
  33. * Sample implementation class
  34. */
  35. class SampleData
  36. {
  37. friend class Sample;
  38. private:
  39. char *name, *path;
  40. #if defined USE_SDL_MIXER
  41. Mix_Chunk *chunk;
  42. #endif
  43. };
  44. /*
  45. * Public Sample class
  46. */
  47. Sample::Sample(char const *path)
  48. : data(new SampleData())
  49. {
  50. data->name = (char *)malloc(9 + strlen(path) + 1);
  51. data->path = data->name + 9;
  52. sprintf(data->name, "<sample> %s", path);
  53. #if defined USE_SDL_MIXER
  54. String fullpath = String(System::GetDataDir()) + String(path);
  55. data->chunk = Mix_LoadWAV(&fullpath[0]);
  56. if (!data->chunk)
  57. {
  58. #if !LOL_RELEASE
  59. Log::Error("could not load %s\n", &fullpath[0]);
  60. #endif
  61. SDL_Quit();
  62. exit(1);
  63. }
  64. #endif
  65. }
  66. Sample::~Sample()
  67. {
  68. #if defined USE_SDL_MIXER
  69. Mix_FreeChunk(data->chunk);
  70. #endif
  71. free(data->name);
  72. delete data;
  73. }
  74. void Sample::TickGame(float seconds)
  75. {
  76. Entity::TickGame(seconds);
  77. }
  78. char const *Sample::GetName()
  79. {
  80. return data->name;
  81. }
  82. void Sample::Play()
  83. {
  84. #if defined USE_SDL_MIXER
  85. Mix_PlayChannel(-1, data->chunk, 0);
  86. #endif
  87. }
  88. } /* namespace lol */