ObjectivelyMVC 0.1.0
Object oriented MVC framework for OpenGL, SDL2 and GNU C
Sound.c
Go to the documentation of this file.
1/*
2 * ObjectivelyMVC: Object oriented MVC framework for OpenGL, SDL2 and GNU C.
3 * Copyright (C) 2014 Jay Dolan <jay@jaydolan.com>
4 *
5 * This software is provided 'as-is', without any express or implied
6 * warranty. In no event will the authors be held liable for any damages
7 * arising from the use of this software.
8 *
9 * Permission is granted to anyone to use this software for any purpose,
10 * including commercial applications, and to alter it and redistribute it
11 * freely, subject to the following restrictions:
12 *
13 * 1. The origin of this software must not be misrepresented; you must not
14 * claim that you wrote the original software. If you use this software
15 * in a product, an acknowledgment in the product documentation would be
16 * appreciated but is not required.
17 *
18 * 2. Altered source versions must be plainly marked as such, and must not be
19 * misrepresented as being the original software.
20 *
21 * 3. This notice may not be removed or altered from any source distribution.
22 */
23
24#include <assert.h>
25
26#include <SDL_rwops.h>
27
28#include "Sound.h"
29
30#define _Class _Sound
31
32#pragma mark - Object
33
37static void dealloc(Object *self) {
38
39 Sound *this = (Sound *) self;
40
41 Mix_FreeChunk(this->chunk);
42
43 super(Object, self, dealloc);
44}
45
46#pragma mark - Sound
47
52static Sound *initWithBytes(Sound *self, const uint8_t *bytes, size_t length) {
53
54 SDL_RWops *src = SDL_RWFromConstMem(bytes, (int) length);
55 if (src) {
56 self = $(self, initWithChunk, Mix_LoadWAV_RW(src, 1));
57 } else {
58 self = release(self);
59 }
60
61 return self;
62}
63
68static Sound *initWithData(Sound *self, const Data *data) {
69
70 if (data) {
71 self = $(self, initWithBytes, data->bytes, data->length);
72 } else {
73 self = release(self);
74 }
75
76 return self;
77}
78
83static Sound *initWithChunk(Sound *self, Mix_Chunk *chunk) {
84
85 self = (Sound *) super(Object, self, init);
86 if (self) {
87 self->chunk = chunk;
88 assert(self->chunk);
89 }
90
91 return self;
92}
93
98static Sound *initWithResource(Sound *self, const Resource *resource) {
99
100 if (resource) {
101 self = $(self, initWithData, resource->data);
102 } else {
103 self = release(self);
104 }
105
106 return self;
107}
108
113static Sound *initWithResourceName(Sound *self, const char *name) {
114
115 Resource *resource = $$(Resource, resourceWithName, name);
116
117 self = $(self, initWithResource, resource);
118
119 release(resource);
120
121 return self;
122}
123
128static Sound *soundWithBytes(const uint8_t *bytes, size_t length) {
129 return $(alloc(Sound), initWithBytes, bytes, length);
130}
131
136static Sound *soundWithChunk(Mix_Chunk *chunk) {
137 return $(alloc(Sound), initWithChunk, chunk);
138}
139
144static Sound *soundWithData(const Data *data) {
145 return $(alloc(Sound), initWithData, data);
146}
147
152static Sound *soundWithResource(const Resource *resource) {
153 return $(alloc(Sound), initWithResource, resource);
154}
155
160static Sound *soundWithResourceName(const char *name) {
161 return $(alloc(Sound), initWithResourceName, name);
162}
163
164#pragma mark - Class lifecycle
165
169static void initialize(Class *clazz) {
170
171 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
172
173 ((SoundInterface *) clazz->interface)->initWithBytes = initWithBytes;
174 ((SoundInterface *) clazz->interface)->initWithChunk = initWithChunk;
175 ((SoundInterface *) clazz->interface)->initWithData = initWithData;
176 ((SoundInterface *) clazz->interface)->initWithResource = initWithResource;
177 ((SoundInterface *) clazz->interface)->initWithResourceName = initWithResourceName;
178
179 ((SoundInterface *) clazz->interface)->soundWithBytes = soundWithBytes;
180 ((SoundInterface *) clazz->interface)->soundWithChunk = soundWithChunk;
181 ((SoundInterface *) clazz->interface)->soundWithData = soundWithData;
182 ((SoundInterface *) clazz->interface)->soundWithResource = soundWithResource;
183 ((SoundInterface *) clazz->interface)->soundWithResourceName = soundWithResourceName;
184}
185
190Class *_Sound(void) {
191 static Class *clazz;
192 static Once once;
193
194 do_once(&once, {
195 clazz = _initialize(&(const ClassDef) {
196 .name = "Sound",
197 .superclass = _Object(),
198 .instanceSize = sizeof(Sound),
199 .interfaceOffset = offsetof(Sound, interface),
200 .interfaceSize = sizeof(SoundInterface),
202 });
203 });
204
205 return clazz;
206}
207
208#undef _Class
static void dealloc(Object *self)
Definition: Sound.c:37
static void initialize(Class *clazz)
Definition: Sound.c:169
Sound loading and playback.
CollectionView * init(CollectionView *self, const SDL_Rect *frame)
Initializes this CollectionView with the specified frame and style.
Font * initWithData(Font *self, Data *data, int size, int index)
Data * data
The raw font data.
Definition: Font.h:79
Image * initWithResourceName(Image *self, const char *name)
Initializes this Image, loading the Resource by the given name.
Definition: Image.c:142
Image * initWithBytes(Image *self, const uint8_t *bytes, size_t length)
Initializes this Image with the specified bytes.
Definition: Image.c:88
Image * initWithResource(Image *self, const Resource *resource)
Initializes this Image with the specified Resource.
Definition: Image.c:126
Sound loading and playback.
Definition: Sound.h:45
Sound * soundWithData(const Data *data)
Instantiates an Sound with the specified Data.
Definition: Sound.c:144
Class * _Sound(void)
The Sound archetype.
Definition: Sound.c:190
Sound * soundWithResourceName(const char *name)
Instantiates an Sound with the specified Resource name.
Definition: Sound.c:160
Mix_Chunk * chunk
The backing audio chunk.
Definition: Sound.h:61
Sound * soundWithBytes(const uint8_t *bytes, size_t length)
Instantiates an Sound with the specified bytes.
Definition: Sound.c:128
Sound * initWithChunk(Sound *, const Chunk *)
Initializes this Sound with the given audio chunk.
Sound * initWithData(Sound *, Data *)
Sound * soundWithResource(const Resource *resource)
Instantiates an Sound with the specified Resource.
Definition: Sound.c:152
Sound * soundWithChunk(Mix_Chunk *chunk)
Instantiates an Sound with the specified chunk.
Definition: Sound.c:136