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 | |
---|
11 | #if defined HAVE_CONFIG_H |
---|
12 | # include "config.h" |
---|
13 | #endif |
---|
14 | |
---|
15 | #include <cstdlib> |
---|
16 | #include <cstdio> |
---|
17 | #include <cstring> |
---|
18 | |
---|
19 | #if defined USE_SDL_MIXER |
---|
20 | # if defined HAVE_SDL_SDL_H |
---|
21 | # include <SDL/SDL.h> |
---|
22 | # else |
---|
23 | # include <SDL.h> |
---|
24 | # endif |
---|
25 | # if defined HAVE_SDL_SDL_MIXER_H |
---|
26 | # include <SDL/SDL_mixer.h> |
---|
27 | # else |
---|
28 | # include <SDL_mixer.h> |
---|
29 | # endif |
---|
30 | #endif |
---|
31 | |
---|
32 | #include "core.h" |
---|
33 | |
---|
34 | using namespace std; |
---|
35 | |
---|
36 | namespace lol |
---|
37 | { |
---|
38 | |
---|
39 | /* |
---|
40 | * Sample implementation class |
---|
41 | */ |
---|
42 | |
---|
43 | class SampleData |
---|
44 | { |
---|
45 | friend class Sample; |
---|
46 | |
---|
47 | private: |
---|
48 | char *name, *path; |
---|
49 | #if defined USE_SDL_MIXER |
---|
50 | Mix_Chunk *chunk; |
---|
51 | #endif |
---|
52 | }; |
---|
53 | |
---|
54 | /* |
---|
55 | * Public Sample class |
---|
56 | */ |
---|
57 | |
---|
58 | Sample::Sample(char const *path) |
---|
59 | : data(new SampleData()) |
---|
60 | { |
---|
61 | data->name = (char *)malloc(9 + strlen(path) + 1); |
---|
62 | data->path = data->name + 9; |
---|
63 | sprintf(data->name, "<sample> %s", path); |
---|
64 | |
---|
65 | #if defined USE_SDL_MIXER |
---|
66 | Array<String> pathlist = System::GetPathList(path); |
---|
67 | for (int i = 0; i < pathlist.Count(); ++i) |
---|
68 | { |
---|
69 | data->chunk = Mix_LoadWAV(pathlist[i].C()); |
---|
70 | if (data->chunk) |
---|
71 | break; |
---|
72 | } |
---|
73 | if (!data->chunk) |
---|
74 | { |
---|
75 | #if !LOL_RELEASE |
---|
76 | Log::Error("could not load sample %s\n", path); |
---|
77 | #endif |
---|
78 | SDL_Quit(); |
---|
79 | exit(1); |
---|
80 | } |
---|
81 | #endif |
---|
82 | } |
---|
83 | |
---|
84 | Sample::~Sample() |
---|
85 | { |
---|
86 | #if defined USE_SDL_MIXER |
---|
87 | Mix_FreeChunk(data->chunk); |
---|
88 | #endif |
---|
89 | free(data->name); |
---|
90 | delete data; |
---|
91 | } |
---|
92 | |
---|
93 | void Sample::TickGame(float seconds) |
---|
94 | { |
---|
95 | Entity::TickGame(seconds); |
---|
96 | } |
---|
97 | |
---|
98 | char const *Sample::GetName() |
---|
99 | { |
---|
100 | return data->name; |
---|
101 | } |
---|
102 | |
---|
103 | void Sample::Play() |
---|
104 | { |
---|
105 | #if defined USE_SDL_MIXER |
---|
106 | Mix_PlayChannel(-1, data->chunk, 0); |
---|
107 | #endif |
---|
108 | } |
---|
109 | |
---|
110 | } /* namespace lol */ |
---|
111 | |
---|