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.
 
 
 

96 line
1.5 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2011 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://sam.zoy.org/projects/COPYING.WTFPL for more details.
  9. //
  10. #if defined HAVE_CONFIG_H
  11. # include "config.h"
  12. #endif
  13. #include <cstdlib>
  14. #include <cstdio>
  15. #include <cmath>
  16. #if defined USE_SDL
  17. # include <SDL.h>
  18. # include <SDL_mixer.h>
  19. #endif
  20. #include "core.h"
  21. namespace lol
  22. {
  23. /*
  24. * Sample implementation class
  25. */
  26. class SampleData
  27. {
  28. friend class Sample;
  29. private:
  30. char *name, *path;
  31. #if defined USE_SDL
  32. Mix_Chunk *chunk;
  33. #endif
  34. };
  35. /*
  36. * Public Sample class
  37. */
  38. Sample::Sample(char const *path)
  39. : data(new SampleData())
  40. {
  41. data->name = (char *)malloc(9 + strlen(path) + 1);
  42. data->path = data->name + 9;
  43. sprintf(data->name, "<sample> %s", path);
  44. #if defined USE_SDL
  45. data->chunk = Mix_LoadWAV(path);
  46. if (!data->chunk)
  47. {
  48. #if !LOL_RELEASE
  49. fprintf(stderr, "ERROR: could not load %s\n", path);
  50. #endif
  51. SDL_Quit();
  52. exit(1);
  53. }
  54. #endif
  55. }
  56. Sample::~Sample()
  57. {
  58. #if defined USE_SDL
  59. Mix_FreeChunk(data->chunk);
  60. #endif
  61. free(data->name);
  62. delete data;
  63. }
  64. void Sample::TickGame(float deltams)
  65. {
  66. Entity::TickGame(deltams);
  67. }
  68. char const *Sample::GetName()
  69. {
  70. return data->name;
  71. }
  72. void Sample::Play()
  73. {
  74. #if defined USE_SDL
  75. Mix_PlayChannel(-1, data->chunk, 0);
  76. #endif
  77. }
  78. } /* namespace lol */