ObjectivelyMVC 0.1.0
Object oriented MVC framework for OpenGL, SDL2 and GNU C
Macros | Functions | Variables
SoundStage.c File Reference
#include <assert.h>
#include <SDL2/SDL_mixer.h>
#include "clack.wav.h"
#include "click.wav.h"
#include "Log.h"
#include "SoundStage.h"
#include "WindowController.h"

Go to the source code of this file.

Macros

#define _Class   _SoundStage
 

Functions

Class * _SoundStage (void)
 
static void dealloc (Object *self)
 
static void destroy (Class *clazz)
 
static SoundStageinit (SoundStage *self)
 
static void initialize (Class *clazz)
 
OBJECTIVELYMVC_EXPORT void MVC_PlaySound (const Sound *sound)
 Plays the specified Sound through the current SoundStage (if any). More...
 
static void play (const SoundStage *self, const Sound *sound)
 

Variables

Sound_clack
 
Sound_click
 

Macro Definition Documentation

◆ _Class

#define _Class   _SoundStage

Definition at line 35 of file SoundStage.c.

Function Documentation

◆ _SoundStage()

Class * _SoundStage ( void  )

Definition at line 120 of file SoundStage.c.

120 {
121 static Class *clazz;
122 static Once once;
123
124 do_once(&once, {
125 clazz = _initialize(&(const ClassDef) {
126 .name = "SoundStage",
127 .superclass = _Object(),
128 .instanceSize = sizeof(SoundStage),
129 .interfaceOffset = offsetof(SoundStage, interface),
130 .interfaceSize = sizeof(SoundStageInterface),
132 .destroy = destroy,
133 });
134 });
135
136 return clazz;
137}
static void destroy(Class *clazz)
Definition: SoundStage.c:108
static void initialize(Class *clazz)
Definition: SoundStage.c:91
The SoundStage type.
Definition: SoundStage.h:40

◆ dealloc()

static void dealloc ( Object *  self)
static
See also
Object::dealloc(Object *)

Definition at line 42 of file SoundStage.c.

42 {
43
44 Mix_CloseAudio();
45
46 super(Object, self, dealloc);
47}
static void dealloc(Object *self)
Definition: SoundStage.c:42

◆ destroy()

static void destroy ( Class *  clazz)
static
See also
Class::destroy(Class *)

Definition at line 108 of file SoundStage.c.

108 {
109
110 release(_click);
111 release(_clack);
112
113 Mix_Quit();
114}
Sound * _clack
Definition: SoundStage.c:86
Sound * _click
Definition: SoundStage.c:85

◆ init()

static SoundStage * init ( SoundStage self)
static

Definition at line 55 of file SoundStage.c.

55 {
56
57 self = (SoundStage *) super(Object, self, init);
58 if (self) {
59
60 if (Mix_OpenAudioDevice(48000, AUDIO_S16SYS, 2, 2048, NULL, SDL_AUDIO_ALLOW_ANY_CHANGE) == 0) {
61 MVC_LogInfo("Opened audio device for playback\n");
62 } else {
63 MVC_LogError("Failed to open audio device: %s\n", SDL_GetError());
64 self = release(self);
65 }
66 }
67
68 return self;
69}
#define MVC_LogInfo(fmt,...)
Definition: Log.h:49
#define MVC_LogError(fmt,...)
Definition: Log.h:55
CollectionView * init(CollectionView *self, const SDL_Rect *frame)
Initializes this CollectionView with the specified frame and style.

◆ initialize()

static void initialize ( Class *  clazz)
static
See also
Class::initialize(Class *)

Definition at line 91 of file SoundStage.c.

91 {
92
93 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
94
95 ((SoundStageInterface *) clazz->interface)->init = init;
96 ((SoundStageInterface *) clazz->interface)->play = play;
97
98 const int init = Mix_Init(0xff);
99 assert(init);
100
101 _click = $$(Sound, soundWithBytes, click_wav, click_wav_len);
102 _clack = $$(Sound, soundWithBytes, clack_wav, clack_wav_len);
103}
Sound loading and playback.
Definition: Sound.h:45
Sound * soundWithBytes(const uint8_t *bytes, size_t length)
Instantiates an Sound with the specified bytes.
Definition: Sound.c:128
void play(const SoundStage *self, const char *sound)
Enqueues the given Sound for playback.

◆ MVC_PlaySound()

OBJECTIVELYMVC_EXPORT void MVC_PlaySound ( const Sound sound)

Plays the specified Sound through the current SoundStage (if any).

Parameters
soundThe Sound to play.
Remarks
This is a convenience function, allowing Views to emit sounds in an ad-hoc way, without obtaining a reference to the SoundStage.

Definition at line 141 of file SoundStage.c.

141 {
142
143 SDL_Window *window = SDL_GL_GetCurrentWindow();
144 assert(window);
145
147 assert(windowController);
149
150 $(windowController->soundStage, play, sound);
151}
A WindowController manages a ViewController and its descendants within an SDL_Window.
WindowController * windowController(SDL_Window *window)
SoundStage * soundStage
The SoundStage.

◆ play()

static void play ( const SoundStage self,
const Sound sound 
)
static

Definition at line 75 of file SoundStage.c.

75 {
76 static unsigned int ch;
77
78 assert(sound);
79
80 Mix_PlayChannel(ch++ % MIX_CHANNELS, sound->chunk, 0);
81}
Mix_Chunk * chunk
The backing audio chunk.
Definition: Sound.h:61

Variable Documentation

◆ _clack

Sound* _clack

Definition at line 86 of file SoundStage.c.

◆ _click

Sound* _click

Definition at line 85 of file SoundStage.c.