mirror of
https://github.com/OpenTTD/OpenTTD.git
synced 2025-02-01 11:59:26 +00:00
(svn r2897) Check the return values of [cm]alloc and the length of an array, plus some smaller changes
This commit is contained in:
parent
175b2cd12a
commit
abb034c2cd
58
sound.c
58
sound.c
@ -27,13 +27,21 @@ static FileEntry* _files;
|
|||||||
|
|
||||||
static void OpenBankFile(const char *filename)
|
static void OpenBankFile(const char *filename)
|
||||||
{
|
{
|
||||||
uint count, i;
|
|
||||||
uint32 size, tag;
|
|
||||||
FileEntry* fe;
|
FileEntry* fe;
|
||||||
|
uint count;
|
||||||
|
uint i;
|
||||||
|
|
||||||
FioOpenFile(SOUND_SLOT, filename);
|
FioOpenFile(SOUND_SLOT, filename);
|
||||||
_file_count = count = FioReadDword() >> 3;
|
count = FioReadDword() / 8;
|
||||||
fe = _files = calloc(sizeof(FileEntry), count);
|
fe = calloc(sizeof(*fe), count);
|
||||||
|
|
||||||
|
if (fe == NULL) {
|
||||||
|
_file_count = 0;
|
||||||
|
_files = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
_file_count = count;
|
||||||
|
_files = fe;
|
||||||
|
|
||||||
FioSeekTo(0, SEEK_SET);
|
FioSeekTo(0, SEEK_SET);
|
||||||
|
|
||||||
@ -54,8 +62,8 @@ static void OpenBankFile(const char *filename)
|
|||||||
|
|
||||||
// Read riff tags
|
// Read riff tags
|
||||||
for (;;) {
|
for (;;) {
|
||||||
tag = FioReadDword();
|
uint32 tag = FioReadDword();
|
||||||
size = FioReadDword();
|
uint32 size = FioReadDword();
|
||||||
|
|
||||||
if (tag == ' tmf') {
|
if (tag == ' tmf') {
|
||||||
FioReadWord(); // wFormatTag
|
FioReadWord(); // wFormatTag
|
||||||
@ -91,14 +99,18 @@ static void OpenBankFile(const char *filename)
|
|||||||
|
|
||||||
static bool SetBankSource(MixerChannel *mc, uint bank)
|
static bool SetBankSource(MixerChannel *mc, uint bank)
|
||||||
{
|
{
|
||||||
FileEntry *fe = &_files[bank];
|
FileEntry* fe;
|
||||||
int8* mem;
|
int8* mem;
|
||||||
uint i;
|
uint i;
|
||||||
|
|
||||||
if (fe->file_size == 0)
|
if (bank >= _file_count) return false;
|
||||||
return false;
|
fe = &_files[bank];
|
||||||
|
|
||||||
|
if (fe->file_size == 0) return false;
|
||||||
|
|
||||||
|
mem = malloc(fe->file_size);
|
||||||
|
if (mem == NULL) return false;
|
||||||
|
|
||||||
mem = malloc(fe->file_size); /* XXX unchecked malloc */
|
|
||||||
FioSeekToFile(fe->file_offset);
|
FioSeekToFile(fe->file_offset);
|
||||||
FioReadBlock(mem, fe->file_size);
|
FioReadBlock(mem, fe->file_size);
|
||||||
|
|
||||||
@ -121,16 +133,15 @@ bool SoundInitialize(const char *filename)
|
|||||||
// Low level sound player
|
// Low level sound player
|
||||||
static void StartSound(uint sound, uint panning, uint volume)
|
static void StartSound(uint sound, uint panning, uint volume)
|
||||||
{
|
{
|
||||||
if (volume != 0) {
|
MixerChannel* mc;
|
||||||
MixerChannel *mc = MxAllocateChannel(_mixer);
|
|
||||||
if (mc == NULL)
|
if (volume == 0) return;
|
||||||
return;
|
mc = MxAllocateChannel(_mixer);
|
||||||
if (SetBankSource(mc, sound)) {
|
if (mc == NULL) return;
|
||||||
|
if (!SetBankSource(mc, sound)) return;
|
||||||
MxSetChannelVolume(mc, volume << 8, volume << 8);
|
MxSetChannelVolume(mc, volume << 8, volume << 8);
|
||||||
MxActivateChannel(mc);
|
MxActivateChannel(mc);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static const byte _vol_factor_by_zoom[] = {255, 190, 134};
|
static const byte _vol_factor_by_zoom[] = {255, 190, 134};
|
||||||
@ -163,19 +174,18 @@ static const byte _sound_idx[] = {
|
|||||||
|
|
||||||
static void SndPlayScreenCoordFx(SoundFx sound, int x, int y)
|
static void SndPlayScreenCoordFx(SoundFx sound, int x, int y)
|
||||||
{
|
{
|
||||||
Window *w;
|
const Window* w;
|
||||||
ViewPort *vp;
|
|
||||||
int left;
|
|
||||||
|
|
||||||
if (msf.effect_vol == 0)
|
if (msf.effect_vol == 0) return;
|
||||||
return;
|
|
||||||
|
|
||||||
for (w = _windows; w != _last_window; w++) {
|
for (w = _windows; w != _last_window; w++) {
|
||||||
if ((vp = w->viewport) != NULL &&
|
const ViewPort* vp = w->viewport;
|
||||||
|
|
||||||
|
if (vp != NULL &&
|
||||||
IS_INSIDE_1D(x, vp->virtual_left, vp->virtual_width) &&
|
IS_INSIDE_1D(x, vp->virtual_left, vp->virtual_width) &&
|
||||||
IS_INSIDE_1D(y, vp->virtual_top, vp->virtual_height)) {
|
IS_INSIDE_1D(y, vp->virtual_top, vp->virtual_height)) {
|
||||||
|
int left = ((x - vp->virtual_left) >> vp->zoom) + vp->left;
|
||||||
|
|
||||||
left = ((x - vp->virtual_left) >> vp->zoom) + vp->left;
|
|
||||||
StartSound(
|
StartSound(
|
||||||
_sound_idx[sound],
|
_sound_idx[sound],
|
||||||
clamp(left / 71, 0, 8),
|
clamp(left / 71, 0, 8),
|
||||||
|
Loading…
Reference in New Issue
Block a user