source: trunk/src/sample.cpp @ 2370

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

system: try to autodetect the data directory from the executable path;
currently works for images (SDL and GDI+ loaders) and sound samples.

  • Property svn:keywords set to Id
File size: 1.8 KB
Line 
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
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    String fullpath = String(System::GetDataDir()) + String(path);
67    data->chunk = Mix_LoadWAV(&fullpath[0]);
68    if (!data->chunk)
69    {
70#if !LOL_RELEASE
71        Log::Error("could not load %s\n", &fullpath[0]);
72#endif
73        SDL_Quit();
74        exit(1);
75    }
76#endif
77}
78
79Sample::~Sample()
80{
81#if defined USE_SDL_MIXER
82    Mix_FreeChunk(data->chunk);
83#endif
84    free(data->name);
85    delete data;
86}
87
88void Sample::TickGame(float seconds)
89{
90    Entity::TickGame(seconds);
91}
92
93char const *Sample::GetName()
94{
95    return data->name;
96}
97
98void Sample::Play()
99{
100#if defined USE_SDL_MIXER
101    Mix_PlayChannel(-1, data->chunk, 0);
102#endif
103}
104
105} /* namespace lol */
106
Note: See TracBrowser for help on using the repository browser.