source: trunk/src/sample.cpp @ 680

Last change on this file since 680 was 680, checked in by sam, 10 years ago

Create the SdlApp class to avoid code duplication. Ensure USE_SDL is
tested wherever relevant.

  • Property svn:keywords set to Id
File size: 1.5 KB
Line 
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
11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#include <cstdlib>
16#include <cstdio>
17#include <cmath>
18
19#if defined USE_SDL
20#   include <SDL.h>
21#   include <SDL_mixer.h>
22#endif
23
24#include "core.h"
25
26/*
27 * Sample implementation class
28 */
29
30class SampleData
31{
32    friend class Sample;
33
34private:
35    char *name, *path;
36#if defined USE_SDL
37    Mix_Chunk *chunk;
38#endif
39};
40
41/*
42 * Public Sample class
43 */
44
45Sample::Sample(char const *path)
46  : data(new SampleData())
47{
48    data->name = (char *)malloc(9 + strlen(path) + 1);
49    data->path = data->name + 9;
50    sprintf(data->name, "<sample> %s", path);
51
52#if defined USE_SDL
53    data->chunk = Mix_LoadWAV(path);
54    if (!data->chunk)
55    {
56#if !LOL_RELEASE
57        fprintf(stderr, "ERROR: could not load %s\n", path);
58#endif
59        SDL_Quit();
60        exit(1);
61    }
62#endif
63}
64
65Sample::~Sample()
66{
67#if defined USE_SDL
68    Mix_FreeChunk(data->chunk);
69#endif
70    free(data->name);
71    delete data;
72}
73
74void Sample::TickGame(float deltams)
75{
76    Entity::TickGame(deltams);
77}
78
79char const *Sample::GetName()
80{
81    return data->name;
82}
83
84void Sample::Play()
85{
86#if defined USE_SDL
87    Mix_PlayChannel(-1, data->chunk, 0);
88#endif
89}
90
Note: See TracBrowser for help on using the repository browser.