summaryrefslogtreecommitdiff
path: root/src/sound.c
blob: 2e7ef4bdda4a1235bc4e469d832645881fafd1dc (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128

#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;
static bool_t isSoundActive = TRUE;

bool_t isSoundInicialized()
{
	return isSoundInit;
}

void initSound()
{
	if( isAudioInicialized() == FALSE )
	{
		isSoundInit = FALSE;
	}

	listSound = newList();
	isSoundInit = TRUE;
	isSoundActive = TRUE;

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

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

	printf("load sound %s..\n", file);
	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;

	if( isSoundInit == FALSE ||
	    isSoundActive == FALSE )
	{
		return;
	}

	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 setSoundActive(bool_t n)
{
	isSoundActive = n;
}

void quitSound()
{
	if( isSoundInit == FALSE )
	{
		return;
	}

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