Skip to content

Fix build and startup crash on big-endian hosts - #698

Open
pkubaj wants to merge 2 commits into
Attnam:masterfrom
pkubaj:be-fix
Open

pkubaj wants to merge 2 commits into
Attnam:masterfrom
pkubaj:be-fix

Conversation

@pkubaj

@pkubaj pkubaj commented Nov 13, 2026

Copy link
Copy Markdown

Two small fixes found while packaging IVAN 0.59 for FreeBSD/powerpc64 (big-endian POWER9). Both apply to current master.

0001 Fix SDL2 build on big-endian hosts

FeLib/Source/graphics.cpp guards an SDL1-era BlitDBToScreen() with #if SDL_BYTEORDER == SDL_BIG_ENDIAN. Its SDL2 side refers to sdlTexture and myPixels, which do not exist, so the build fails:

FeLib/Source/graphics.cpp:288:21: error: use of undeclared identifier 'sdlTexture'

The #else of the same #if also hides the whole stretch-region implementation and the real BlitDBToScreen(). With SDL2 the texture is SDL_PIXELFORMAT_RGB565, a packed format SDL reads in native byte order, which is what the packcol16 double buffer holds, so the regular path is already correct on big-endian. The patch limits the special case to SDL1 and drops the dead placeholder.

0002 Fix MIDI parsing on big-endian hosts

audio/midiparser.cpp converts the big-endian SMF header and track fields by reversing bytes in place and reading them as native integers, which only works on little-endian. On powerpc64 Cathedral.mid reports 4608 tracks instead of 18, MPB_ResetMIDITracks() runs past Track[MAX_MIDI_TRACKS] and the audio thread segfaults while loading the first song. The tempo meta event in audio/midiplayback.cpp has the same problem plus a memcpy of 3 bytes into a uint32_t. The patch assembles the values from bytes, which is byte-order independent and also avoids the unaligned accesses.

Testing

  • FreeBSD 15.2 powerpc64 (big-endian), clang 19, SDL2 2.32: builds, and the game reaches the main menu (previously crashed on startup).
  • A small harness calling MIDIParse_Header() / MIDIParse_Track() on Music/Cathedral.mid returns format 1, 18 tracks, PPQ 96, first track length 12, matching the file bytes.
  • No behaviour change on little-endian: the preprocessor picks the same code as before in 0001, and the byte arithmetic in 0002 produces the same values the swap did.

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).
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant