| 1 |
// |
| 2 |
// Copyright(C) 2021 by Ryan Krafnick |
| 3 |
// |
| 4 |
// This program is free software; you can redistribute it and/or |
| 5 |
// modify it under the terms of the GNU General Public License |
| 6 |
// as published by the Free Software Foundation; either version 2 |
| 7 |
// of the License, or (at your option) any later version. |
| 8 |
// |
| 9 |
// This program is distributed in the hope that it will be useful, |
| 10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
// GNU General Public License for more details. |
| 13 |
// |
| 14 |
// DESCRIPTION: |
| 15 |
// DSDA Music |
| 16 |
// |
| 17 |
|
| 18 |
#include <stdlib.h> |
| 19 |
#include <string.h> |
| 20 |
#include <ctype.h> |
| 21 |
|
| 22 |
#include "doomtype.h" |
| 23 |
#include "p_saveg.h" |
| 24 |
#include "s_advsound.h" |
| 25 |
#include "s_sound.h" |
| 26 |
|
| 27 |
#include "music.h" |
| 28 |
|
| 29 |
musicinfo_t* S_music; |
| 30 |
int num_music; |
| 31 |
int mus_musinfo; |
| 32 |
static int deh_musicnames_size; |
| 33 |
static char** deh_musicnames; |
| 34 |
static byte* music_state; |
| 35 |
|
| 36 |
static void dsda_EnsureCapacity(int limit) { |
| 37 |
while (limit >= num_music) { |
| 38 |
int old_num_music = num_music; |
| 39 |
|
| 40 |
num_music *= 2; |
| 41 |
|
| 42 |
S_music = realloc(S_music, num_music * sizeof(*S_music)); |
| 43 |
memset(S_music + old_num_music, 0, (num_music - old_num_music) * sizeof(*S_music)); |
| 44 |
|
| 45 |
music_state = realloc(music_state, num_music * sizeof(*music_state)); |
| 46 |
memset(music_state + old_num_music, 0, |
| 47 |
(num_music - old_num_music) * sizeof(*music_state)); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
int dsda_GetDehMusicIndex(const char* key, size_t length) { |
| 52 |
int i; |
| 53 |
|
| 54 |
for (i = 1; i < num_music; ++i) |
| 55 |
if ( |
| 56 |
S_music[i].name && |
| 57 |
strlen(S_music[i].name) == length && |
| 58 |
!strnicmp(S_music[i].name, key, length) && |
| 59 |
!music_state[i] |
| 60 |
) { |
| 61 |
music_state[i] = true; // music has been edited |
| 62 |
return i; |
| 63 |
} |
| 64 |
|
| 65 |
return -1; |
| 66 |
} |
| 67 |
|
| 68 |
int dsda_GetOriginalMusicIndex(const char* key) { |
| 69 |
int i; |
| 70 |
// const char* c; |
| 71 |
|
| 72 |
for (i = 1; deh_musicnames[i]; ++i) |
| 73 |
if (!strncasecmp(deh_musicnames[i], key, 6)) |
| 74 |
return i; |
| 75 |
|
| 76 |
return -1; |
| 77 |
|
| 78 |
// is it a number? |
| 79 |
// for (c = key; *c; c++) |
| 80 |
// if (!isdigit(*c)) |
| 81 |
// return -1; |
| 82 |
// |
| 83 |
// i = atoi(key); |
| 84 |
// dsda_EnsureCapacity(i); |
| 85 |
// |
| 86 |
// return i; |
| 87 |
} |
| 88 |
|
| 89 |
void dsda_InitializeMusic(musicinfo_t* source, int count) { |
| 90 |
int i; |
| 91 |
extern int raven; |
| 92 |
|
| 93 |
num_music = count; |
| 94 |
deh_musicnames_size = num_music + 1; |
| 95 |
mus_musinfo = num_music - 1; |
| 96 |
|
| 97 |
S_music = source; |
| 98 |
|
| 99 |
// S_music = malloc(num_music * sizeof(*S_music)); |
| 100 |
// memcpy(S_music, source, num_music * sizeof(*S_music)); |
| 101 |
|
| 102 |
if (raven) return; |
| 103 |
|
| 104 |
deh_musicnames = malloc(deh_musicnames_size * sizeof(*deh_musicnames)); |
| 105 |
for (i = 1; i < num_music; i++) |
| 106 |
if (S_music[i].name != NULL) |
| 107 |
deh_musicnames[i] = strdup(S_music[i].name); |
| 108 |
else |
| 109 |
deh_musicnames[i] = NULL; |
| 110 |
deh_musicnames[0] = NULL; |
| 111 |
deh_musicnames[num_music] = NULL; |
| 112 |
|
| 113 |
music_state = calloc(num_music, sizeof(*music_state)); |
| 114 |
} |
| 115 |
|
| 116 |
void dsda_FreeDehMusic(void) { |
| 117 |
int i; |
| 118 |
|
| 119 |
if (deh_musicnames) |
| 120 |
for (i = 0; i < deh_musicnames_size; i++) |
| 121 |
if (deh_musicnames[i]) |
| 122 |
free(deh_musicnames[i]); |
| 123 |
|
| 124 |
free(deh_musicnames); |
| 125 |
free(music_state); |
| 126 |
} |
| 127 |
|
| 128 |
static int music_queue = -1; |
| 129 |
|
| 130 |
void dsda_ArchiveMusic(void) { |
| 131 |
P_SAVE_X(musinfo.current_item); |
| 132 |
} |
| 133 |
|
| 134 |
void dsda_UnArchiveMusic(void) { |
| 135 |
P_LOAD_X(music_queue); |
| 136 |
} |
| 137 |
|
| 138 |
dboolean dsda_StartQueuedMusic(void) { |
| 139 |
if (music_queue == -1) |
| 140 |
return false; |
| 141 |
|
| 142 |
S_ChangeMusInfoMusic(music_queue, true); |
| 143 |
music_queue = -1; |
| 144 |
|
| 145 |
return true; |
| 146 |
} |