Sha256: 5e9c6a34af26ae6b41890f87df9a8d8c6e813e47f4e407f00940e3341fcfa884

Contents?: true

Size: 977 Bytes

Versions: 3

Compression:

Stored size: 977 Bytes

Contents

// sound.c

#include "../include/simple2d.h"


/*
 * Create a sound, given an audio file path
 */
S2D_Sound *S2D_CreateSound(const char *path) {
  S2D_Init();

  // Check if sound file exists
  if (!S2D_FileExists(path)) {
    S2D_Error("S2D_CreateSound", "Sound file `%s` not found", path);
    return NULL;
  }

  // Allocate the sound structure
  S2D_Sound *snd = (S2D_Sound *) malloc(sizeof(S2D_Sound));
  if (!snd) {
    S2D_Error("S2D_CreateSound", "Out of memory!");
    return NULL;
  }

  // Load the sound data from file
  snd->data = Mix_LoadWAV(path);
  if (!snd->data) {
    S2D_Error("Mix_LoadWAV", Mix_GetError());
    free(snd);
    return NULL;
  }

  // Initialize values
  snd->path = path;

  return snd;
}


/*
 * Play the sound
 */
void S2D_PlaySound(S2D_Sound *snd) {
  if (!snd) return;
  Mix_PlayChannel(-1, snd->data, 0);
}


/*
 * Free the sound
 */
void S2D_FreeSound(S2D_Sound *snd) {
  if (!snd) return;
  Mix_FreeChunk(snd->data);
  free(snd);
}

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ruby2d-0.9.3 assets/linux/simple2d/src/sound.c
ruby2d-0.9.2 assets/linux/simple2d/src/sound.c
ruby2d-0.9.1 assets/linux/simple2d/src/sound.c