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 &&
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)
{
switch (event->type)
@ -436,6 +404,16 @@ LRESULT CALLBACK WindowProc2 (HWND hWnd, UINT message,
g_pPixmap->Display ();
}
break;
case WM_MUSIC_STOP:
if (g_pSound->IsStoppedOnDemand ())
break;
if (g_pEvent->IsMovie ())
g_pEvent->StopMovie ();
else
g_pSound->RestartMusic ();
break;
}
break;
}

2
def.h
View File

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

View File

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

View File

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

View File

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

2
menu.h
View File

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

138
sound.cpp
View File

@ -5,6 +5,7 @@
#include <dsound.h>
#include <stdio.h>
#include "sound.h"
#include "event.h"
#include "misc.h"
#include "def.h"
#include "resource.h"
@ -26,71 +27,6 @@ bool CSound::StopAllSounds()
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()
{
int i;
@ -103,6 +39,8 @@ CSound::CSound()
m_midiVolume = 15;
m_lastMidiVolume = 0;
m_nbSuspendSkip = 0;
m_pMusic = nullptr;
m_bStopped = false;
for ( i=0 ; i<MAXBLUPI ; i++ )
{
@ -116,11 +54,6 @@ CSound::~CSound()
{
int i;
if ( m_bEnable )
{
InitMidiVolume(15); // remet un volume moyen !
}
for ( i=0 ; i<MAXSOUND ; i++ )
{
if (!m_lpSDL[i])
@ -142,6 +75,12 @@ bool CSound::Create()
return false;
Mix_AllocateChannels (MAXSOUND);
Mix_HookMusicFinished ([] ()
{
CEvent::PushUserEvent (WM_MUSIC_STOP);
});
return true;
}
@ -331,14 +270,12 @@ bool CSound::PlayImage(int channel, POINT pos, int rank)
bool CSound::PlayMusic(HWND hWnd, LPSTR lpszMIDIFilename)
{
MCI_OPEN_PARMS mciOpenParms;
MCI_PLAY_PARMS mciPlayParms;
DWORD dwReturn;
char string[MAX_PATH];
char string[MAX_PATH];
if ( !m_bEnable ) 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;
if ( lpszMIDIFilename[1] == ':' ) // nom complet "D:\REP..." ?
@ -351,43 +288,21 @@ bool CSound::PlayMusic(HWND hWnd, LPSTR lpszMIDIFilename)
strcat(string, lpszMIDIFilename);
}
// Open the device by specifying the device and filename.
// MCI will attempt to choose the MIDI mapper as the output port.
mciOpenParms.lpstrDeviceType = "sequencer";
mciOpenParms.lpstrElementName = string;
dwReturn = mciSendCommand(NULL,
MCI_OPEN,
MCI_OPEN_TYPE|MCI_OPEN_ELEMENT,
(DWORD_PTR)(LPVOID)&mciOpenParms);
if ( dwReturn != 0 )
m_pMusic = Mix_LoadMUS (string);
if (!m_pMusic)
{
OutputDebug("PlayMusic-1\n");
mciGetErrorString(dwReturn, string, 128);
OutputDebug(string);
// Failed to open device. Don't close it; just return error.
printf ("%s\n", Mix_GetError ());
return false;
}
// The device opened successfully; get the device ID.
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 )
if (Mix_PlayMusic (m_pMusic, 0) == -1)
{
OutputDebug("PlayMusic-2\n");
mciGetErrorString(dwReturn, string, 128);
OutputDebug(string);
StopMusic();
printf ("%s\n", Mix_GetError ());
return false;
}
m_bStopped = false;
strcpy(m_MIDIFilename, lpszMIDIFilename);
return true;
}
@ -415,11 +330,8 @@ void CSound::SuspendMusic()
return;
}
if ( m_MidiDeviceID && m_midiVolume != 0 )
{
mciSendCommand(m_MidiDeviceID, MCI_CLOSE, 0, NULL);
}
m_MidiDeviceID = 0;
m_bStopped = true;
Mix_HaltMusic ();
}
// Shuts down the MIDI player.
@ -437,15 +349,19 @@ bool CSound::IsPlayingMusic()
return (m_MIDIFilename[0] != 0);
}
bool CSound::IsStoppedOnDemand ()
{
return m_bStopped;
}
// Adapte le volume de la musique en cours, si nécessaire.
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;
RestartMusic();
}
}

View File

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