From 7671b21682a92a2af1037260d2a0afca1b5ae4ab Mon Sep 17 00:00:00 2001 From: Ted John Date: Sat, 11 Feb 2017 12:45:53 +0000 Subject: [PATCH] Convert audio.c to C++ so RWops can be removed --- src/openrct2/audio/{audio.c => audio.cpp} | 55 +++++++++++++---------- src/openrct2/libopenrct2.vcxproj | 2 +- 2 files changed, 33 insertions(+), 24 deletions(-) rename src/openrct2/audio/{audio.c => audio.cpp} (90%) diff --git a/src/openrct2/audio/audio.c b/src/openrct2/audio/audio.cpp similarity index 90% rename from src/openrct2/audio/audio.c rename to src/openrct2/audio/audio.cpp index 675662b7c8..82cf8c2122 100644 --- a/src/openrct2/audio/audio.c +++ b/src/openrct2/audio/audio.cpp @@ -14,16 +14,23 @@ *****************************************************************************/ #pragma endregion -#include "../config.h" -#include "../interface/viewport.h" -#include "../intro.h" -#include "../localisation/language.h" +#include "../core/FileStream.hpp" +#include "../core/Memory.hpp" +#include "../core/Util.hpp" #include "../localisation/string_ids.h" #include "../OpenRCT2.h" -#include "../util/util.h" -#include "audio.h" #include "AudioMixer.h" +extern "C" +{ + #include "../config.h" + #include "../interface/viewport.h" + #include "../intro.h" + #include "../localisation/language.h" + #include "../util/util.h" + #include "audio.h" +} + typedef struct rct_audio_params { bool in_range; sint32 volume; @@ -149,7 +156,7 @@ void audio_populate_devices() if (gAudioDeviceCount <= 0) return; - audio_device *systemAudioDevices = malloc(gAudioDeviceCount * sizeof(audio_device)); + audio_device * systemAudioDevices = Memory::AllocateArray(gAudioDeviceCount); for (sint32 i = 0; i < gAudioDeviceCount; i++) { const char *utf8Name = SDL_GetAudioDeviceName(i, SDL_FALSE); if (utf8Name == NULL) @@ -159,12 +166,12 @@ void audio_populate_devices() } #ifndef __LINUX__ gAudioDeviceCount++; - gAudioDevices = malloc(gAudioDeviceCount * sizeof(audio_device)); + gAudioDevices = Memory::AllocateArray(gAudioDeviceCount); safe_strcpy(gAudioDevices[0].name, language_get_string(STR_OPTIONS_SOUND_VALUE_DEFAULT), AUDIO_DEVICE_NAME_SIZE); - memcpy(&gAudioDevices[1], systemAudioDevices, (gAudioDeviceCount - 1) * sizeof(audio_device)); + Memory::CopyArray(&gAudioDevices[1], systemAudioDevices, gAudioDeviceCount - 1); #else - gAudioDevices = malloc(gAudioDeviceCount * sizeof(audio_device)); - memcpy(gAudioDevices, systemAudioDevices, gAudioDeviceCount * sizeof(audio_device)); + gAudioDevices = Memory::AllocateArray(gAudioDeviceCount); + Memory::CopyArray(gAudioDevices, systemAudioDevices, gAudioDeviceCount); #endif // __LINUX__ free(systemAudioDevices); @@ -247,7 +254,7 @@ sint32 audio_play_sound(sint32 soundId, sint32 volume, sint32 pan) sint32 mixerPan = 0; if (pan != AUDIO_PLAY_AT_CENTRE) { sint32 x2 = pan << 16; - uint16 screenWidth = max(64, gScreenWidth); + uint16 screenWidth = Math::Max(64, gScreenWidth); mixerPan = ((x2 / screenWidth) - 0x8000) >> 4; } @@ -345,18 +352,20 @@ void audio_init_ride_sounds_and_info() sint32 deviceNum = 0; audio_init_ride_sounds(deviceNum); - for (sint32 m = 0; m < countof(gRideMusicInfoList); m++) { + for (size_t m = 0; m < Util::CountOf(gRideMusicInfoList); m++) { rct_ride_music_info *rideMusicInfo = gRideMusicInfoList[m]; const utf8 *path = get_file_path(rideMusicInfo->path_id); - SDL_RWops *file = SDL_RWFromFile(path, "rb"); - if (file == NULL) - continue; - - uint32 head; - SDL_RWread(file, &head, sizeof(head), 1); - SDL_RWclose(file); - if (head == 0x78787878) - rideMusicInfo->length = 0; + try + { + auto fs = FileStream(path, FILE_MODE_OPEN); + uint32 head = fs.ReadValue(); + if (head == 0x78787878) { + rideMusicInfo->length = 0; + } + } + catch (const Exception &) + { + } } } @@ -414,7 +423,7 @@ void audio_stop_vehicle_sounds() if (gOpenRCT2Headless || gAudioCurrentDevice == -1) return; - for (sint32 i = 0; i < countof(gVehicleSoundList); i++) { + for (size_t i = 0; i < Util::CountOf(gVehicleSoundList); i++) { rct_vehicle_sound *vehicleSound = &gVehicleSoundList[i]; if (vehicleSound->id == 0xFFFF) continue; diff --git a/src/openrct2/libopenrct2.vcxproj b/src/openrct2/libopenrct2.vcxproj index e2c2c12cf2..a97a484aa0 100644 --- a/src/openrct2/libopenrct2.vcxproj +++ b/src/openrct2/libopenrct2.vcxproj @@ -80,7 +80,7 @@ - +