Skip to content

Instantly share code, notes, and snippets.

@Nbc66
Last active April 15, 2021 12:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nbc66/a3fdd971a89f72294ad89e40acab6083 to your computer and use it in GitHub Desktop.
Save Nbc66/a3fdd971a89f72294ad89e40acab6083 to your computer and use it in GitHub Desktop.
Source Code for the music system for the main menu to be used in your source engine mod
#include "cbase.h"
#include "vgui_controls/SectionedListPanel.h"
#include "vgui_controls/ImagePanel.h"
#include "engine/IEngineSound.h"
#include "filesystem.h"
#include "icommandline.h"
#include "music_system.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
int m_nSongGuid;
ConVar mainmenu_music("mainmenu_music", "1", FCVAR_ARCHIVE, "Toggle music in the main menu");
CPFMainMenuMusic music;
void CPFMainMenuMusic::GetRandomMusic(char* pszBuf, int iBufLength)
{
Assert(iBufLength);
char szPath[MAX_PATH];
// Check that there's music available
if (!g_pFullFileSystem->FileExists("sound/ui/music/gamestartup1.mp3"))
{
Assert(false);
*pszBuf = '\0';
}
// Discover tracks, 1 through n
int iLastTrack = 0;
do
{
Q_snprintf(szPath, sizeof(szPath), "sound/ui/music/gamestartup%d.mp3", ++iLastTrack);
} while (g_pFullFileSystem->FileExists(szPath));
// Pick a random one
Q_snprintf(szPath, sizeof(szPath), "*#ui/music/gamestartup%d.mp3", RandomInt(1, iLastTrack - 1));
Q_strncpy(pszBuf, szPath, iBufLength);
}
void CPFMainMenuMusic::Init()
{
m_psMusicStatus = MUSIC_FIND;
m_pzMusicLink[0] = '\0';
m_nSongGuid = 0;
}
void CPFMainMenuMusic::OnTick()
{
if (mainmenu_music.GetBool() && !engine->IsConnected() || mainmenu_music.GetBool() && engine->IsLevelMainMenuBackground())
{
if ((m_psMusicStatus == MUSIC_FIND || m_psMusicStatus == MUSIC_STOP_FIND) && !enginesound->IsSoundStillPlaying(m_nSongGuid))
{
GetRandomMusic(m_pzMusicLink, sizeof(m_pzMusicLink));
m_psMusicStatus = MUSIC_PLAY;
}
else if ((m_psMusicStatus == MUSIC_PLAY || m_psMusicStatus == MUSIC_STOP_PLAY) && m_pzMusicLink[0] != '\0')
{
enginesound->StopSoundByGuid(m_nSongGuid);
ConVar* snd_musicvolume = cvar->FindVar("snd_musicvolume");
float fVolume = (snd_musicvolume ? snd_musicvolume->GetFloat() : 1.0f);
enginesound->EmitAmbientSound(m_pzMusicLink, fVolume, PITCH_NORM, 0);
m_nSongGuid = enginesound->GetGuidForLastSoundEmitted();
m_psMusicStatus = MUSIC_FIND;
}
}
else if (m_psMusicStatus == MUSIC_FIND)
{
enginesound->StopSoundByGuid(m_nSongGuid);
m_psMusicStatus = (m_nSongGuid == 0 ? MUSIC_STOP_FIND : MUSIC_STOP_PLAY);
}
}
void RandomMusicCommand()
{
enginesound->StopSoundByGuid(m_nSongGuid);
}
ConCommand randommusic("randommusic", RandomMusicCommand , "play random music");
#pragma once
#include "cbase.h"
#include "vgui_basepanel.h"
enum MusicStatus
{
MUSIC_STOP,
MUSIC_FIND,
MUSIC_PLAY,
MUSIC_STOP_FIND,
MUSIC_STOP_PLAY,
};
class CPFMainMenuMusic
{
public:
void Init();
void OnTick();
void RandomMusicCommand();
//int m_nSongGuid;
private:
void GetRandomMusic(char* pszBuf, int iBufLength);
char m_pzMusicLink[64];
MusicStatus m_psMusicStatus;
int m_iPlayGameStartupSound = 0;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment