ihhub / fheroes2

fheroes2 is a recreation of Heroes of Might and Magic II game engine.
https://ihhub.github.io/fheroes2/
GNU General Public License v2.0
2.72k stars 376 forks source link

Combat screen, creature animation freeze during movement at start of combat #8379

Open LeHerosInconnu opened 9 months ago

LeHerosInconnu commented 9 months ago

Preliminary checks

Platform

Windows

Describe the bug

In fheroes2, there is a creature animation freeze during movement at start of combat.

In fheroes2:

The second troop of wolves freezes as it moves.

https://github.com/ihhub/fheroes2/assets/43583994/b73ffd00-d9e3-4f95-9caa-347625438e5d

The troop of phoenixes freezes at the start of its movement.

https://github.com/ihhub/fheroes2/assets/43583994/534f23b7-301e-46af-9802-b5ae9e79c9c9

The troop of gargoyles freezes as it moves.

https://github.com/ihhub/fheroes2/assets/43583994/d5b61a99-b5f2-4a37-9bb2-6a9de211731e

The first troop of vampire lords freezes when attacking.

https://github.com/ihhub/fheroes2/assets/43583994/b5d319e5-2496-47c9-bfb0-12997cb72dc6

With all audio settings to "off": In fheroes2:

All audio off 01

The first troop of vampire lords freezes at the start of its movement.

https://github.com/ihhub/fheroes2/assets/43583994/43bc232f-f05e-4f9c-82c1-e2c290e3225d

Save file

Here are the save files: Creature animation freeze 01.zip

Additional info

Related closed issue: https://github.com/ihhub/fheroes2/issues/7197.

oleg-derevenetz commented 9 months ago

I'd say that the reason is the same as in #8376 and #8375 - WAV sound effect playback is started with a noticeable delay on this particular PC, which shouldn't be, because WAV playback is very cheap.

oleg-derevenetz commented 9 months ago

Marking this as an audio-related issue but it's just a guess because I cannot reproduce this on my devices.

oleg-derevenetz commented 8 months ago

I would say that the problem here and in other similar issues (#8375, #8376) is that WAV sound is played at the same moment that the MIDI music starts (and the MIDI music is a heavy thing), and until the MIDI music starts, it's impossible to play anything else, because SDL_mixer functions themselves are not safe for simultaneous use from multiple threads (at least it seems so after a cursory acquaintance with their insides).

This problem is observed with MIDI music only (not with external music, because external MP3/FLAC/OGG music is a relatively light thing in comparison with MIDI). Playback of horse steps is performed in background thread, therefore, their playback is just delayed a bit - that's why their playback continues a little longer after the hero stops, while playback of musical effects for map objects is performed in the main thread, and this can lead to animation delays when the corresponding object located on the border of two terrains with different music is visited. The same applies to the playback of musical effects for units at the beginning of the battle when the background battle MIDI music starts.

LeHerosInconnu commented 8 months ago

Hello @Branikolog,

Hi, @LeHerosInconnu Are you using MIDI? I believe this issue is valid only for MIDI sound, while for external music I cannot reproduce this problem.

Yes, MIDI is used when the problem is detected. I did a test with .ogg files in the MUSIC folder and "Music Type" set to "External" and the problem is no longer present. There must have been a change in MIDI management at some point.

Originally posted by @LeHerosInconnu in #8375 (comment)