summaryrefslogtreecommitdiff
path: root/src/sound.c
blob: 45cb22509afb1e69b2d745cc41db74fa3622838c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

#include "main.h"
#include "interface.h"
#include "list.h"
#include "path.h"
#include "string_length.h"
#include "audio.h"
#include "sound.h"

static list_t *listSound;

static bool_t isSoundInit = FALSE;

bool_t isSoundInicialized()
{
	return isSoundInit;
}

void initSound()
{
	assert( isAudioInicialized() );

	listSound = newList();
	isSoundInit = TRUE;

	printf("init sound..\n");
}

static Mix_Chunk* loadMixSound(char *file)
{
	Mix_Chunk *new;
	char str[STR_PATH_SIZE];

	sprintf(str, PATH_SOUND "%s", file);
	new = Mix_LoadWAV(str);

	if( new == NULL )
	{
		fprintf(stderr, "Nelze nahrat zvuk %s : %s\n", str, Mix_GetError());
		return NULL;
	}

	return new;
}

static sound_t *newSound(char *file, char *name, int group)
{
	sound_t *new;

	new = malloc( sizeof(sound_t) );
	new->name = strdup(name);
	new->group = group;
	new->sound = loadMixSound(file);

	return new;
}

static void playMixSound(sound_t *p)
{
	if( Mix_PlayChannel(-1, p->sound, 0) == -1 )
	{
		fprintf(stderr, "Nelze prehrat zvuk : %s\n", Mix_GetError());
		return;
	}
}

static void destroySound(sound_t *p)
{
	free(p->name);
	Mix_FreeChunk(p->sound);
	free(p);
}

void addSound(char *file, char *name, int group)
{
	addList( listSound, newSound(file, name, group) );
}

void playSound(char *name, int group)
{
	int i;
	sound_t *this;

	for( i = 0 ; i < listSound->count ; i++ )
	{
		this = (sound_t *)listSound->list[i];

		if( this->group == group && strcmp(this->name, name) == 0 )
		{
			playMixSound(this);
			return;
		}
	}
}

void quitSound()
{
	destroyListItem(listSound, destroySound);
	isSoundInit = FALSE;
	printf("quit sound..\n");
}