source: trunk/src/sample.cpp @ 2183

Last change on this file since 2183 was 2183, checked in by sam, 7 years ago

build: fix the WTFPL site URL in all code comments.

  • Property svn:keywords set to Id
File size: 1.7 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2012 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
34using namespace std;
35
36namespace lol
37{
38
39/*
40 * Sample implementation class
41 */
42
43class SampleData
44{
45    friend class Sample;
46
47private:
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
58Sample::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    data->chunk = Mix_LoadWAV(path);
67    if (!data->chunk)
68    {
69#if !LOL_RELEASE
70        Log::Error("could not load %s\n", path);
71#endif
72        SDL_Quit();
73        exit(1);
74    }
75#endif
76}
77
78Sample::~Sample()
79{
80#if defined USE_SDL_MIXER
81    Mix_FreeChunk(data->chunk);
82#endif
83    free(data->name);
84    delete data;
85}
86
87void Sample::TickGame(float seconds)
88{
89    Entity::TickGame(seconds);
90}
91
92char const *Sample::GetName()
93{
94    return data->name;
95}
96
97void Sample::Play()
98{
99#if defined USE_SDL_MIXER
100    Mix_PlayChannel(-1, data->chunk, 0);
101#endif
102}
103
104} /* namespace lol */
105
Note: See TracBrowser for help on using the repository browser.