106 line
1.7 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. data->chunk = Mix_LoadWAV(path);
  55. if (!data->chunk)
  56. {
  57. #if !LOL_RELEASE
  58. Log::Error("could not load %s\n", path);
  59. #endif
  60. SDL_Quit();
  61. exit(1);
  62. }
  63. #endif
  64. }
  65. Sample::~Sample()
  66. {
  67. #if defined USE_SDL_MIXER
  68. Mix_FreeChunk(data->chunk);
  69. #endif
  70. free(data->name);
  71. delete data;
  72. }
  73. void Sample::TickGame(float seconds)
  74. {
  75. Entity::TickGame(seconds);
  76. }
  77. char const *Sample::GetName()
  78. {
  79. return data->name;
  80. }
  81. void Sample::Play()
  82. {
  83. #if defined USE_SDL_MIXER
  84. Mix_PlayChannel(-1, data->chunk, 0);
  85. #endif
  86. }
  87. } /* namespace lol */