1
0
mirror of https://github.com/blupi-games/planetblupi synced 2024-12-30 10:15:36 +01:00

Add musib handling with SDL

Note that the music volume affects the chunk volumes
too. Maybe a bug...
This commit is contained in:
Mathieu Schroeter 2017-02-04 17:59:07 +01:00
parent 1007905fc9
commit 1b5727c790
8 changed files with 52 additions and 145 deletions

View File

@ -329,38 +329,6 @@ LRESULT CALLBACK WindowProc2 (HWND hWnd, UINT message,
if ( g_pEvent != NULL && if ( g_pEvent != NULL &&
g_pEvent->TreatEvent(event) ) return 0; g_pEvent->TreatEvent(event) ) return 0;
switch( message )
{
case MM_MCINOTIFY:
OutputDebug("Event MM_MCINOTIFY\n");
if ( g_pEvent->IsMovie() ) // film en cours ?
{
if ( wParam == MCI_NOTIFY_SUCCESSFUL )
{
g_pEvent->StopMovie();
}
}
else
{
// music over, play it again
g_pSound->SuspendMusic();
// if music finished, play it again. Otherwise assume that
// it was aborted by the user or otherwise
if ( wParam == MCI_NOTIFY_SUCCESSFUL )
{
OutputDebug("Event MCI_NOTIFY_SUCCESSFUL\n");
g_pSound->RestartMusic();
}
else
{
char s[50];
sprintf(s, "wParam=%d\n", static_cast<int> (wParam));
OutputDebug(s);
}
}
break;
}
if (event) if (event)
{ {
switch (event->type) switch (event->type)
@ -436,6 +404,16 @@ LRESULT CALLBACK WindowProc2 (HWND hWnd, UINT message,
g_pPixmap->Display (); g_pPixmap->Display ();
} }
break; break;
case WM_MUSIC_STOP:
if (g_pSound->IsStoppedOnDemand ())
break;
if (g_pEvent->IsMovie ())
g_pEvent->StopMovie ();
else
g_pSound->RestartMusic ();
break;
} }
break; break;
} }

2
def.h
View File

@ -588,6 +588,8 @@ enum MouseSprites
#define WM_PHASE_INTRO1 (WM_USER+536) #define WM_PHASE_INTRO1 (WM_USER+536)
#define WM_PHASE_INTRO2 (WM_USER+537) #define WM_PHASE_INTRO2 (WM_USER+537)
#define WM_MUSIC_STOP (WM_USER+550)
#define WM_PREV (WM_USER+600) #define WM_PREV (WM_USER+600)
#define WM_NEXT (WM_USER+601) #define WM_NEXT (WM_USER+601)
#define WM_MOVIE (WM_USER+602) #define WM_MOVIE (WM_USER+602)

View File

@ -3127,6 +3127,7 @@ bool CEvent::ChangePhase(UINT phase)
if ( music > 0 ) if ( music > 0 )
{ {
sprintf(filename, "sound\\music%.3d.blp", music-1); sprintf(filename, "sound\\music%.3d.blp", music-1);
m_pSound->StopMusic ();
m_pSound->PlayMusic(m_hWnd, filename); m_pSound->PlayMusic(m_hWnd, filename);
} }
} }
@ -3651,7 +3652,7 @@ void CEvent::ChangeButtons(int message)
if ( volume > 0 ) if ( volume > 0 )
{ {
m_pSound->SetMidiVolume(volume-1); m_pSound->SetMidiVolume(volume-1);
m_pSound->SuspendMusic(); m_pSound->AdaptVolumeMusic ();
} }
} }
if ( message == WM_BUTTON6 ) if ( message == WM_BUTTON6 )
@ -3660,7 +3661,7 @@ void CEvent::ChangeButtons(int message)
if ( volume < MAXVOLUME ) if ( volume < MAXVOLUME )
{ {
m_pSound->SetMidiVolume(volume+1); m_pSound->SetMidiVolume(volume+1);
m_pSound->SuspendMusic(); m_pSound->AdaptVolumeMusic ();
} }
} }

View File

@ -4,6 +4,7 @@
#include "jauge.h" #include "jauge.h"
#include "menu.h" #include "menu.h"
#include "button.h"
class CMovie; class CMovie;

View File

@ -2,6 +2,10 @@
#pragma once #pragma once
class CPixmap;
class CDecor;
class CSound;
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
class CJauge class CJauge

2
menu.h
View File

@ -2,6 +2,8 @@
#pragma once #pragma once
#include "def.h"
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
class CMenu class CMenu

138
sound.cpp
View File

@ -5,6 +5,7 @@
#include <dsound.h> #include <dsound.h>
#include <stdio.h> #include <stdio.h>
#include "sound.h" #include "sound.h"
#include "event.h"
#include "misc.h" #include "misc.h"
#include "def.h" #include "def.h"
#include "resource.h" #include "resource.h"
@ -26,71 +27,6 @@ bool CSound::StopAllSounds()
return true; return true;
} }
/////////////////////////////////////////////////////////////////////////////
// Modifie le volume midi.
// Le volume est compris entre 0 et 20 !
void InitMidiVolume(int volume)
{
int nb, i, n;
MMRESULT result;
HMIDIOUT hmo = 0;
static unsigned int table[21] =
{
0x00000000,
0x11111111,
0x22222222,
0x33333333,
0x44444444,
0x55555555,
0x66666666,
0x77777777,
0x88888888,
0x99999999,
0xAAAAAAAA,
0xBBBBBBBB,
0xCCCCCCCC,
0xDDDDDDDD,
0xEEEEEEEE,
0xF222F222,
0xF555F555,
0xF777F777,
0xFAAAFAAA,
0xFDDDFDDD,
0xFFFFFFFF,
};
if ( volume < 0 ) volume = 0;
if ( volume > MAXVOLUME ) volume = MAXVOLUME;
nb = midiOutGetNumDevs();
for ( i=0 ; i<nb ; i++ )
{
result = midiOutOpen((LPHMIDIOUT)&hmo, i, 0L, 0L, 0L);
if ( result != MMSYSERR_NOERROR )
{
continue;
}
result = midiOutSetVolume(hmo, table[volume]);
if ( result != MMSYSERR_NOERROR )
{
n = 1;
}
midiOutClose(hmo);
hmo = 0;
}
}
/////////////////////////////////////////////////////////////////////////////
// Constructeur.
CSound::CSound() CSound::CSound()
{ {
int i; int i;
@ -103,6 +39,8 @@ CSound::CSound()
m_midiVolume = 15; m_midiVolume = 15;
m_lastMidiVolume = 0; m_lastMidiVolume = 0;
m_nbSuspendSkip = 0; m_nbSuspendSkip = 0;
m_pMusic = nullptr;
m_bStopped = false;
for ( i=0 ; i<MAXBLUPI ; i++ ) for ( i=0 ; i<MAXBLUPI ; i++ )
{ {
@ -116,11 +54,6 @@ CSound::~CSound()
{ {
int i; int i;
if ( m_bEnable )
{
InitMidiVolume(15); // remet un volume moyen !
}
for ( i=0 ; i<MAXSOUND ; i++ ) for ( i=0 ; i<MAXSOUND ; i++ )
{ {
if (!m_lpSDL[i]) if (!m_lpSDL[i])
@ -142,6 +75,12 @@ bool CSound::Create()
return false; return false;
Mix_AllocateChannels (MAXSOUND); Mix_AllocateChannels (MAXSOUND);
Mix_HookMusicFinished ([] ()
{
CEvent::PushUserEvent (WM_MUSIC_STOP);
});
return true; return true;
} }
@ -331,14 +270,12 @@ bool CSound::PlayImage(int channel, POINT pos, int rank)
bool CSound::PlayMusic(HWND hWnd, LPSTR lpszMIDIFilename) bool CSound::PlayMusic(HWND hWnd, LPSTR lpszMIDIFilename)
{ {
MCI_OPEN_PARMS mciOpenParms; char string[MAX_PATH];
MCI_PLAY_PARMS mciPlayParms;
DWORD dwReturn;
char string[MAX_PATH];
if ( !m_bEnable ) return true; if ( !m_bEnable ) return true;
if ( m_midiVolume == 0 ) return true; if ( m_midiVolume == 0 ) return true;
InitMidiVolume(m_midiVolume);
Mix_VolumeMusic (MIX_MAX_VOLUME * 100 * m_midiVolume / 20 / 100);
m_lastMidiVolume = m_midiVolume; m_lastMidiVolume = m_midiVolume;
if ( lpszMIDIFilename[1] == ':' ) // nom complet "D:\REP..." ? if ( lpszMIDIFilename[1] == ':' ) // nom complet "D:\REP..." ?
@ -351,43 +288,21 @@ bool CSound::PlayMusic(HWND hWnd, LPSTR lpszMIDIFilename)
strcat(string, lpszMIDIFilename); strcat(string, lpszMIDIFilename);
} }
// Open the device by specifying the device and filename. m_pMusic = Mix_LoadMUS (string);
// MCI will attempt to choose the MIDI mapper as the output port. if (!m_pMusic)
mciOpenParms.lpstrDeviceType = "sequencer";
mciOpenParms.lpstrElementName = string;
dwReturn = mciSendCommand(NULL,
MCI_OPEN,
MCI_OPEN_TYPE|MCI_OPEN_ELEMENT,
(DWORD_PTR)(LPVOID)&mciOpenParms);
if ( dwReturn != 0 )
{ {
OutputDebug("PlayMusic-1\n"); printf ("%s\n", Mix_GetError ());
mciGetErrorString(dwReturn, string, 128);
OutputDebug(string);
// Failed to open device. Don't close it; just return error.
return false; return false;
} }
// The device opened successfully; get the device ID. if (Mix_PlayMusic (m_pMusic, 0) == -1)
m_MidiDeviceID = mciOpenParms.wDeviceID;
// Begin playback.
mciPlayParms.dwCallback = (DWORD_PTR)hWnd;
dwReturn = mciSendCommand(m_MidiDeviceID,
MCI_PLAY,
MCI_NOTIFY,
(DWORD_PTR)(LPVOID)&mciPlayParms);
if ( dwReturn != 0 )
{ {
OutputDebug("PlayMusic-2\n"); printf ("%s\n", Mix_GetError ());
mciGetErrorString(dwReturn, string, 128);
OutputDebug(string);
StopMusic();
return false; return false;
} }
m_bStopped = false;
strcpy(m_MIDIFilename, lpszMIDIFilename); strcpy(m_MIDIFilename, lpszMIDIFilename);
return true; return true;
} }
@ -415,11 +330,8 @@ void CSound::SuspendMusic()
return; return;
} }
if ( m_MidiDeviceID && m_midiVolume != 0 ) m_bStopped = true;
{ Mix_HaltMusic ();
mciSendCommand(m_MidiDeviceID, MCI_CLOSE, 0, NULL);
}
m_MidiDeviceID = 0;
} }
// Shuts down the MIDI player. // Shuts down the MIDI player.
@ -437,15 +349,19 @@ bool CSound::IsPlayingMusic()
return (m_MIDIFilename[0] != 0); return (m_MIDIFilename[0] != 0);
} }
bool CSound::IsStoppedOnDemand ()
{
return m_bStopped;
}
// Adapte le volume de la musique en cours, si nécessaire. // Adapte le volume de la musique en cours, si nécessaire.
void CSound::AdaptVolumeMusic() void CSound::AdaptVolumeMusic()
{ {
if ( m_midiVolume != m_lastMidiVolume ) if (m_midiVolume != m_lastMidiVolume)
{ {
InitMidiVolume(m_midiVolume); Mix_VolumeMusic (MIX_MAX_VOLUME * 100 * m_midiVolume / 20 / 100);
m_lastMidiVolume = m_midiVolume; m_lastMidiVolume = m_midiVolume;
RestartMusic();
} }
} }

View File

@ -39,6 +39,7 @@ public:
void SuspendMusic(); void SuspendMusic();
void StopMusic(); void StopMusic();
bool IsPlayingMusic(); bool IsPlayingMusic();
bool IsStoppedOnDemand ();
void AdaptVolumeMusic(); void AdaptVolumeMusic();
void SetSuspendSkip(int nb); void SetSuspendSkip(int nb);
bool StopAllSounds(); bool StopAllSounds();
@ -47,6 +48,8 @@ protected:
HWND m_hWnd; HWND m_hWnd;
bool m_bEnable; bool m_bEnable;
bool m_bState; bool m_bState;
bool m_bStopped;
Mix_Music *m_pMusic;
Mix_Chunk *m_lpSDL[MAXSOUND]; Mix_Chunk *m_lpSDL[MAXSOUND];
short m_channelBlupi[MAXBLUPI]; short m_channelBlupi[MAXBLUPI];
UINT m_MidiDeviceID; UINT m_MidiDeviceID;