From d00de3fd5b69d5b849a0b1ba651276262541c43f Mon Sep 17 00:00:00 2001 From: Piotr Kubaj Date: Fri, 13 Nov 2026 01:11:02 +0100 Subject: [PATCH 1/2] Fix SDL2 build on big-endian hosts The big-endian branch in graphics.cpp dates from SDL1, where the screen surface could have a display format other than RGB565 and the double buffer had to go through SDL_DisplayFormat(). Its SDL2 side was never written: it refers to sdlTexture and myPixels, which do not exist anywhere, so the build fails on big-endian machines: FeLib/Source/graphics.cpp:288:21: error: use of undeclared identifier 'sdlTexture' The same #if also hides the whole stretch-region implementation and the real BlitDBToScreen() behind its #else, so fixing only that line would move the failure to link time. With SDL2 the texture is created as SDL_PIXELFORMAT_RGB565, a packed format that SDL reads in native byte order, which is exactly what the packcol16 double buffer holds. The regular code path is therefore correct on big-endian too. Limit the special case to SDL1 and drop the dead SDL2 placeholder. Tested on FreeBSD/powerpc64 (big-endian POWER9). --- FeLib/Source/graphics.cpp | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/FeLib/Source/graphics.cpp b/FeLib/Source/graphics.cpp index e11a63ed2..e18c1f6c2 100644 --- a/FeLib/Source/graphics.cpp +++ b/FeLib/Source/graphics.cpp @@ -265,11 +265,10 @@ void graphics::SetMode(cchar* Title, cchar* IconName, #endif } -#if SDL_BYTEORDER == SDL_BIG_ENDIAN +#if SDL_MAJOR_VERSION == 1 && SDL_BYTEORDER == SDL_BIG_ENDIAN void graphics::BlitDBToScreen() { -#if SDL_MAJOR_VERSION == 1 SDL_LockSurface(TempSurface); packcol16* SrcPtr = DoubleBuffer->GetImage()[0]; packcol16* DestPtr = static_cast(TempSurface->pixels); @@ -284,9 +283,6 @@ void graphics::BlitDBToScreen() SDL_BlitSurface(S, NULL, Screen, NULL); SDL_FreeSurface(S); SDL_UpdateRect(Screen, 0, 0, Res.X, Res.Y); -#else - SDL_UpdateTexture(sdlTexture, NULL, myPixels, 640 * sizeof (Uint32)); -#endif } #else From 34fb70ec0d05613f4b465bfd475846a0ad884bee Mon Sep 17 00:00:00 2001 From: Piotr Kubaj Date: Fri, 13 Nov 2026 01:11:02 +0100 Subject: [PATCH 2/2] Fix MIDI parsing on big-endian hosts The SMF parser converts the big-endian header and track fields by reversing the bytes in place and then reading them back as native integers, which only works on little-endian machines. On big-endian ones the track count of Cathedral.mid comes out as 4608 instead of 18, MPB_ResetMIDITracks() writes past Track[MAX_MIDI_TRACKS] and the audio thread crashes as soon as the first song is loaded. The tempo meta event has the same problem and additionally memcpy()s its three bytes into the first bytes of a uint32_t, which are the high bytes there. Assemble the values from the individual bytes instead. That is correct on either byte order and also avoids the unaligned 16- and 32-bit accesses into the read buffer. Tested on FreeBSD/powerpc64 (big-endian POWER9): the parsed header of Music/Cathedral.mid now matches the file and the game reaches the main menu. --- audio/midiparser.cpp | 21 +++++---------------- audio/midiplayback.cpp | 5 +++-- 2 files changed, 8 insertions(+), 18 deletions(-) diff --git a/audio/midiparser.cpp b/audio/midiparser.cpp index 5e0d8df7c..178c1dd6c 100644 --- a/audio/midiparser.cpp +++ b/audio/midiparser.cpp @@ -143,18 +143,9 @@ size_t MIDIParse_Header(MIDI_HEADER_CHUNK_t* header, void* data, uint32_t size) if (ptr) { - uint16_t* tmp; - tmp = (uint16_t*)&ptr[MIDI_HEADER_FMT]; - reverseOrder( (char*)tmp, 2); - header->format = *tmp; - - tmp = (uint16_t*)&ptr[MIDI_HEADER_TRACK_COUNT]; - reverseOrder((char*)tmp, 2); - header->trackCount = *tmp; - - tmp = (uint16_t*)&ptr[MIDI_HEADER_PPQ]; - reverseOrder((char*)tmp, 2); - header->PPQ = *tmp; + header->format = (ptr[MIDI_HEADER_FMT] << 8) | ptr[MIDI_HEADER_FMT + 1]; + header->trackCount = (ptr[MIDI_HEADER_TRACK_COUNT] << 8) | ptr[MIDI_HEADER_TRACK_COUNT + 1]; + header->PPQ = (ptr[MIDI_HEADER_PPQ] << 8) | ptr[MIDI_HEADER_PPQ + 1]; uint8_t* diff = ptr; diff = (uint8_t*)((uint8_t*)diff - (uint8_t*)(data)); @@ -188,10 +179,8 @@ void* MIDIParse_Track(MIDI_TRACK_CHUNK_t* track, void* data, uint32_t size) char* ptr = (char*)findSubString( (char*)data, (char*)MIDI_TRACK_STRING, size); if (ptr) { - uint32_t* tmp; - tmp = (uint32_t*)&ptr[MIDI_TRACK_LENGTH]; - reverseOrder( (char*)tmp, 4); - track->length = *tmp; + uint8_t* tmp = (uint8_t*)&ptr[MIDI_TRACK_LENGTH]; + track->length = ((uint32_t)tmp[0] << 24) | ((uint32_t)tmp[1] << 16) | (tmp[2] << 8) | tmp[3]; //MIDI_Printf("TRACKLENG: ", track->length); return &ptr[MIDI_TRACK_LENGTH+4]; } diff --git a/audio/midiplayback.cpp b/audio/midiplayback.cpp index b0c51accf..142729908 100644 --- a/audio/midiplayback.cpp +++ b/audio/midiplayback.cpp @@ -672,8 +672,9 @@ void MPB_ProcessMetaEvent(MIDI_HEADER_CHUNK_t* MIDIHdr, MIDI_TRACK_CHUNK_t* trac case MIDI_META_TEMPO: existingBPM = MIDIHdr->currentState.BPM; - reverseOrder((char*) event->event.metaEvent.data, event->event.metaEvent.length); - memcpy(&MIDIHdr->currentState.BPM, event->event.metaEvent.data, event->event.metaEvent.length); //*bpmptr; + MIDIHdr->currentState.BPM = ((uint32_t)event->event.metaEvent.data[0] << 16) + | (event->event.metaEvent.data[1] << 8) + | event->event.metaEvent.data[2]; MIDIHdr->currentState.BPM = BPM(MIDIHdr->currentState.BPM); MPB_SetTickRate(MIDIHdr->currentState.BPM, MIDIHdr->PPQ);