OpenTTD Source: src/newgrf/newgrf_act0_sounds.cpp Source File

OpenTTD Source 15.0-RC2

newgrf_act0_sounds.cpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
10#include "../stdafx.h"
11#include "../debug.h"
12#include "../newgrf_sound.h"
13#include "newgrf_bytereader.h"
14#include "newgrf_internal.h"
15
16#include "../safeguards.h"
17
26 static ChangeInfoResult SoundEffectChangeInfo(uint first, uint last, int prop, ByteReader &buf)
27{
28 ChangeInfoResult ret = CIR_SUCCESS;
29 if (first == last) return ret;
30
31 if (_cur_gps.grffile->sound_offset == 0) {
32 GrfMsg(1, "SoundEffectChangeInfo: No effects defined, skipping");
33 return CIR_INVALID_ID;
34 }
35
36 if (last - ORIGINAL_SAMPLE_COUNT > _cur_gps.grffile->num_sounds) {
37 GrfMsg(1, "SoundEffectChangeInfo: Attempting to change undefined sound effect ({}), max ({}). Ignoring.", last, ORIGINAL_SAMPLE_COUNT + _cur_gps.grffile->num_sounds);
38 return CIR_INVALID_ID;
39 }
40
41 for (uint id = first; id < last; ++id) {
42 SoundEntry *sound = GetSound(first + _cur_gps.grffile->sound_offset - ORIGINAL_SAMPLE_COUNT);
43
44 switch (prop) {
45 case 0x08: // Relative volume
46 sound->volume = Clamp(buf.ReadByte(), 0, SOUND_EFFECT_MAX_VOLUME);
47 break;
48
49 case 0x09: // Priority
50 sound->priority = buf.ReadByte();
51 break;
52
53 case 0x0A: { // Override old sound
54 SoundID orig_sound = buf.ReadByte();
55
56 if (orig_sound >= ORIGINAL_SAMPLE_COUNT) {
57 GrfMsg(1, "SoundEffectChangeInfo: Original sound {} not defined (max {})", orig_sound, ORIGINAL_SAMPLE_COUNT);
58 } else {
59 SoundEntry *old_sound = GetSound(orig_sound);
60
61 /* Literally copy the data of the new sound over the original */
62 *old_sound = *sound;
63 }
64 break;
65 }
66
67 default:
68 ret = CIR_UNKNOWN;
69 break;
70 }
71 }
72
73 return ret;
74}
75
76template <> ChangeInfoResult GrfChangeInfoHandler<GSF_SOUNDFX>::Reserve(uint, uint, int, ByteReader &) { return CIR_UNHANDLED; }
77template <> ChangeInfoResult GrfChangeInfoHandler<GSF_SOUNDFX>::Activation(uint first, uint last, int prop, ByteReader &buf) { return SoundEffectChangeInfo(first, last, prop, buf); }
ByteReader
Class to read from a NewGRF file.
Definition newgrf_bytereader.h:18
ByteReader::ReadByte
uint8_t ReadByte()
Read a single byte (8 bits).
Definition newgrf_bytereader.h:34
Clamp
constexpr T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition math_func.hpp:79
SoundEffectChangeInfo
static ChangeInfoResult SoundEffectChangeInfo(uint first, uint last, int prop, ByteReader &buf)
Define properties for sound effects.
Definition newgrf_act0_sounds.cpp:26
newgrf_bytereader.h
NewGRF buffer reader definition.
newgrf_internal.h
NewGRF internal processing state.
ChangeInfoResult
ChangeInfoResult
Possible return values for the GrfChangeInfoHandler functions.
Definition newgrf_internal.h:20
CIR_INVALID_ID
@ CIR_INVALID_ID
Attempt to modify an invalid ID.
Definition newgrf_internal.h:25
CIR_UNKNOWN
@ CIR_UNKNOWN
Variable is unknown.
Definition newgrf_internal.h:24
CIR_UNHANDLED
@ CIR_UNHANDLED
Variable was parsed but unread.
Definition newgrf_internal.h:23
CIR_SUCCESS
@ CIR_SUCCESS
Variable was parsed and read.
Definition newgrf_internal.h:21
ORIGINAL_SAMPLE_COUNT
static const uint ORIGINAL_SAMPLE_COUNT
The number of sounds in the original sample.cat.
Definition sound_type.h:124
GrfChangeInfoHandler
GRF feature handler.
Definition newgrf_internal.h:30
GrfProcessingState::grffile
GRFFile * grffile
Currently processed GRF file.
Definition newgrf_internal.h:67
SoundEntry
Definition sound_type.h:19