Fix various compiler warnings in MSVC (non graphics edition)

- Put in casts/checks in audio code when assigning size_t to 32 bit
- () is different from (void)
- Turned off warnings for anonymous unions and negating unsigned integers which were technically accurate but unhelpful (and interfered with bit conversion and a weird bit math construct in audio.c) (CMakeLists only)
This commit is contained in:
mcc 2022-07-01 19:02:34 -04:00 committed by Bjorn
parent cdf6b2017e
commit 28d64b6ced
5 changed files with 21 additions and 11 deletions

View File

@ -597,7 +597,7 @@ if(WIN32)
target_sources(lovr PRIVATE src/core/os_win32.c)
target_sources(lovr PRIVATE etc/lovr.rc)
if (MSVC)
set_target_properties(lovr PROPERTIES COMPILE_FLAGS "/wd4244 /MP")
set_target_properties(lovr PROPERTIES COMPILE_FLAGS "/wd4244 /wd4146 /wd4116 /MP")
else()
set_target_properties(lovr PROPERTIES COMPILE_FLAGS "-MP")
endif()

View File

@ -51,8 +51,9 @@ static int l_lovrSoundGetSampleRate(lua_State* L) {
static int l_lovrSoundGetByteStride(lua_State* L) {
Sound* sound = luax_checktype(L, 1, Sound);
uint32_t stride = lovrSoundGetStride(sound);
lua_pushinteger(L, stride);
size_t stride = lovrSoundGetStride(sound);
lovrCheck(stride < UINT32_MAX, "Sound contains impossibly many channels");
lua_pushinteger(L, (uint32_t)stride);
return 1;
}
@ -125,7 +126,7 @@ static int l_lovrSoundGetFrames(lua_State* L) {
uint32_t frames = 0;
while (frames < count) {
char buffer[4096];
uint32_t chunk = MIN(sizeof(buffer) / stride, count - frames);
uint32_t chunk = MIN((uint32_t)(sizeof(buffer) / stride), count - frames);
uint32_t read = lovrSoundRead(sound, srcOffset + frames, chunk, buffer);
uint32_t samples = read * channels;
if (read == 0) break;
@ -189,7 +190,9 @@ static int l_lovrSoundSetFrames(lua_State* L) {
if (blob) {
uint32_t srcOffset = luax_optu32(L, 5, 0);
uint32_t dstOffset = luax_optu32(L, 4, 0);
uint32_t count = luax_optu32(L, 3, (blob->size - srcOffset) / stride);
size_t defaultCount = (blob->size - srcOffset) / stride;
lovrCheck(defaultCount <= UINT32_MAX, "Sound is too big to work with (somewhere over 4 GiB)");
uint32_t count = luax_optu32(L, 3, (uint32_t)defaultCount);
uint32_t frames = lovrSoundWrite(sound, dstOffset, count, (char*) blob->data + srcOffset);
lua_pushinteger(L, frames);
return 1;
@ -221,7 +224,7 @@ static int l_lovrSoundSetFrames(lua_State* L) {
uint32_t frames = 0;
while (frames < count) {
char buffer[4096];
uint32_t chunk = MIN(sizeof(buffer) / stride, count - frames);
uint32_t chunk = MIN((uint32_t)(sizeof(buffer) / stride), count - frames);
uint32_t samples = chunk * channels;
if (format == SAMPLE_I16) {

View File

@ -14,6 +14,7 @@
#define CTZL __builtin_ctzl
#endif
// Mysterious "m ^= (m & -m)" construct zeroes the lowest nonzero bit.
#define FOREACH_SOURCE(s) for (uint64_t m = state.sourceMask; s = m ? state.sources[CTZL(m)] : NULL, m; m ^= (m & -m))
#define OUTPUT_FORMAT SAMPLE_F32
#define OUTPUT_CHANNELS 2

View File

@ -68,8 +68,10 @@ static uint32_t lovrSoundReadMp3(Sound* sound, uint32_t offset, uint32_t count,
}
uint32_t channels = lovrSoundGetChannelCount(sound);
size_t samples = mp3dec_ex_read(sound->decoder, data, count * channels);
uint32_t frames = samples / channels;
uint32_t readSamples = (uint32_t)(count * channels);
lovrCheck(readSamples/count == channels, "Cannot request more than 2^32-1 samples of sound data at a time"); // Check for overflow
size_t samples = mp3dec_ex_read(sound->decoder, data, readSamples);
uint32_t frames = ((uint32_t)samples / channels);
sound->cursor += frames;
return frames;
}
@ -135,7 +137,9 @@ static bool loadOgg(Sound* sound, Blob* blob, bool decode) {
void* data = calloc(1, size);
lovrAssert(data, "Out of memory");
sound->blob = lovrBlobCreate(data, size, "Sound");
if (stb_vorbis_get_samples_float_interleaved(sound->decoder, lovrSoundGetChannelCount(sound), data, size / sizeof(float)) < (int) sound->frames) {
size_t samples = size / sizeof(float);
lovrCheck(samples < INT_MAX, "Sound is too big to work with (somewhere over 2 GiB)");
if (stb_vorbis_get_samples_float_interleaved(sound->decoder, lovrSoundGetChannelCount(sound), data, (int)(samples)) < (int) sound->frames) {
lovrThrow("Could not decode vorbis from '%s'", blob->name);
}
stb_vorbis_close(sound->decoder);
@ -285,7 +289,9 @@ static bool loadMP3(Sound* sound, Blob* blob, bool decode) {
sound->format = SAMPLE_F32;
sound->sampleRate = info.hz;
sound->layout = info.channels == 2 ? CHANNEL_STEREO : CHANNEL_MONO;
sound->frames = info.samples / info.channels;
size_t frames = info.samples / info.channels;
lovrCheck(frames < UINT32_MAX, "Sound is too long (2^32 or more samples)")
sound->frames = (uint32_t)frames;
sound->read = lovrSoundReadRaw;
return true;
} else {

View File

@ -36,7 +36,7 @@ static void onPermission(os_permission permission, bool granted) {
});
}
static void onQuit() {
static void onQuit(void) {
lovrEventPush((Event) {
.type = EVENT_QUIT,
.data.quit.exitCode = 0