Rewrite of the stage module.
[cantaveria.git] / music.c
blob8e3c78b5a2bc6296a06e0ab2ed8dc61354bd8586
1 /*
2 Cantaveria - action adventure platform game
3 Copyright (C) 2009 2010 Evan Rinehart
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to
18 The Free Software Foundation, Inc.
19 51 Franklin Street, Fifth Floor
20 Boston, MA 02110-1301, USA
22 evanrinehart@gmail.com
25 #include <stdio.h>
26 #include <stdlib.h>
28 #include <list.h>
29 #include <util.h>
30 #include <midi.h>
31 #include <seq.h>
32 #include <music.h>
34 mus_id current_id = MUS_NOTHING;
36 list* songs[32] = {
37 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
38 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
39 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
40 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
45 list* positions[32];
46 int playing = 0;
48 static char* mus_name(mus_id id){
49 switch(id){
50 case MUS_NOTHING: return "MUS_NOTHING";
51 case MUS_COOL: return "MUS_COOL";
52 case MUS_TEST1: return "MUS_TEST1";
53 default: return "(?)";
57 static int is_id_invalid(mus_id id){
58 if(id >= 32) return 1;
59 else return 0;
63 int music_load(char* filename, mus_id id){
64 list* events;
66 if(id == MUS_NOTHING){
67 error_msg("music_load: you can't load a song into MUS_NOTHING\n");
68 return -1;
71 if(is_id_invalid(id)){
72 error_msg("music_load: music id out of range (%d)\n", id);
73 return -1;
76 if(songs[id] != NULL){
77 error_msg("music_load: slot %s not empty\n", mus_name(id));
78 return -1;
81 events = midi_load(filename);
82 if(events == NULL){
83 error_msg("music_load: unable to load \"%s\"\n", filename);
84 return -1;
87 songs[id] = events;
88 positions[id] = events->next;
89 return 0;
92 void music_unload(mus_id id){
93 if(is_id_invalid(id)) return;
94 if(songs[id] == NULL) return;
96 if(music_current() == id) music_stop(id);
98 list* ptr = songs[id]->next;
99 while(ptr){
100 free(ptr->item);
101 ptr = ptr->next;
104 recycle(songs[id]->next);
105 songs[id] = NULL;
108 mus_id music_current(){
109 return current_id;
112 void music_play(mus_id id){
113 if(is_id_invalid(id)) return;
114 if(songs[id] == NULL) return;
115 if(music_current() == id && playing) return;
117 if(playing) music_pause();
118 seq_load(songs[id]);
119 seq_seek(positions[id]);
120 seq_enable();
122 current_id = id;
123 playing = 1;
127 void music_stop(mus_id id){
128 if(is_id_invalid(id)) return;
129 if(songs[id] == NULL) return;
130 if(music_current() == id){
131 seq_disable();
132 playing = 0;
135 positions[id] = 0;
138 void music_pause(){
139 playing = 0;
140 seq_disable();
141 positions[music_current()] = seq_tell();
144 void music_volume(int percent){
145 seq_instant(EVX_MUSICVOLUME, 0, percent, 0);
148 void music_fadeout(int seconds){
149 seq_instant(EVX_FADEOUT, 0, seconds, 0);
152 void music_print(mus_id id){
155 void music_debug(){