Animated Menu Background

From Valve Developer Community
Revision as of 05:26, 5 September 2018 by GamerDude27 (talk | contribs) (Initial write-up for the "Animated Menu Background" page.)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Note:This implementation has some issues, and we need your help! Please check out "Problem 4" at the bottom of the page.


What Is This?

This implementation allows you to use a .bik file as your main menu's background. Similar to what's seen in Left 4 Dead 2 and Portal 2.

The Implementation

First off you'll need these two files:


Which you'll put into src/game/client/


Once that's done, open up cdll_client_int.cpp and add this:

void SwapDisconnectCommand();

Above

void CHLClient::PostInit()

Then at the bottom of the PostInit() function, add this:

#ifdef SDK_DLL
	SwapDisconnectCommand();
#endif

Now head over to clientmode_sdk.cpp, and add this as the last include at the top of the file:

// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"

Now head over to sdkviewport.cpp, and again add this at the top:

#include "menu_background.h"

// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"

In the same file, add this:

SDKViewport::SDKViewport()
{
	m_pMainMenuPanel = NULL;
}

SDKViewport::~SDKViewport()
{
	if ( !m_bHasParent && m_pMainMenuPanel )
	{
		m_pMainMenuPanel->MarkForDeletion();
	}
	m_pMainMenuPanel = NULL;
}

void SDKViewport::Start(IGameUIFuncs *pGameUIFuncs, IGameEventManager2 *pGameEventManager)
{
	m_pMainMenuPanel = new CMainMenu( NULL, NULL );
	m_pMainMenuPanel->SetZPos( 500 );
	m_pMainMenuPanel->SetVisible( false );
	m_pMainMenuPanel->StartVideo();
 	BaseClass::Start(pGameUIFuncs, pGameEventManager);
}

Above

void SDKViewport::ApplySchemeSettings( vgui::IScheme *pScheme )

Then below that function, add:

void SDKViewport::OnScreenSizeChanged(int iOldWide, int iOldTall)
{
	bool bRestartMainMenuVideo = false;
	if (m_pMainMenuPanel)
		bRestartMainMenuVideo = m_pMainMenuPanel->IsVideoPlaying();
 	BaseClass::OnScreenSizeChanged(iOldWide, iOldTall);
 	m_pMainMenuPanel = new CMainMenu( NULL, NULL );
	m_pMainMenuPanel->SetZPos( 500 );
	m_pMainMenuPanel->SetVisible( false );
	if (bRestartMainMenuVideo)
		m_pMainMenuPanel->StartVideo();
}

void SDKViewport::RemoveAllPanels( void)
{
	if (m_pMainMenuPanel)
	{
		m_pMainMenuPanel->MarkForDeletion();
		m_pMainMenuPanel = NULL;
	}
 	BaseClass::RemoveAllPanels();
}

And below the GetDeathMessageStartHeight( void ) function add this:

void SDKViewport::StartMainMenuVideo()
{
	if (m_pMainMenuPanel)
		m_pMainMenuPanel->StartVideo();
}

void SDKViewport::StopMainMenuVideo()
{
	if (m_pMainMenuPanel)
		m_pMainMenuPanel->StopVideo();
}

Now head over to sdkviewport.h, add this:

public:
	SDKViewport();
	~SDKViewport();

Above

public:
	IViewPortPanel* CreatePanelByName(const char *szPanelName);
	...

In the same file, add these:

virtual void Start( IGameUIFuncs *pGameUIFuncs, IGameEventManager2 *pGameEventManager );
virtual void OnScreenSizeChanged(int iOldWide, int iOldTall);
virtual void RemoveAllPanels( void);
void StartMainMenuVideo();
void StopMainMenuVideo();

Below

public:
	IViewPortPanel* CreatePanelByName(const char *szPanelName);
Note:Preferably in the same order as the functions seen in the .cpp file.

After the last function under public:, add:

private:
	class CMainMenu* m_pMainMenuPanel;

Now head over to convar.h, and make this public (protected: -> public:):

protected:
	virtual void Create( const char *pName, const char *pHelpString = 0, int flags = 0 );

What Next?

Now that you're done with the implementation, you'll need a .bik file to play as your background.

I won't show you how to make your own .bik file, but I can recommend following this guide for that:


Once you've got your .bik file ready, rename it to mainmenu.bik, and place it in your mod's media folder. If a media folder doesn't exist, create one.

The Problems

Problem 1:

Solution:

In your Client project, right click "Source Files" -> Add -> Existing Item, then navigate to src/game/client/sdk/ and add clientmode_sdk.cpp and src/game/client/.

Optional Solution:

Open your mod's .vpc file in src/game/client/, and add these two lines:

$File "sdk\clientmode_sdk.cpp"
$File "sdk\vgui\sdkviewport.cpp"

Under

$Folder "Source Files"
Note:If you don't have one for your mod, open client_hl2.vpc or client_episodic.vpc depending on which game your mod is for.
Note:If you've never edited a .vpc file before, close Visual Studio after you've saved the .vpc changes, go to the src folder and run creategameprojects.bat before reopening your solution.

Problem 2:

While compiling, if you run into this error:

Cannot open include file: 'sdk_shareddefs.h': No such file or directory (menu_background.cpp)

Head back into sdkviewport.h, and turn this:

#include "sdk_shareddefs.h"

Into this:

#include "../../shared/sdk/sdk_shareddefs.h"

Similarly, if you're running into this:

Cannot open include file: 'sdkviewport.h': No such file or directory (sdk\clientmode_sdk.cpp)

Go to clientmode_sdk.h, and turn this:

#include "sdkviewport.h"

Into this:

#include "vgui/sdkviewport.h"

Problem 3 (temporary fix):

While compiling, if you run into these errors:

clientmode_hlnormal.obj : error LNK2005: "class IClientMode * __cdecl GetClientModeNormal(void)" (?GetClientModeNormal@@YAPAVIClientMode@@XZ) already defined in clientmode_sdk.obj
hl2_clientmode.obj : error LNK2005: "class IVModeManager * modemanager" (?modemanager@@3PAVIVModeManager@@A) already defined in clientmode_sdk.obj
hl2_clientmode.obj : error LNK2005: "class ConVar default_fov" (?default_fov@@3VConVar@@A) already defined in clientmode_sdk.obj

Go to hl2_clientmode.cpp, and at the top comment out this line:

ConVar default_fov( "default_fov", "75", FCVAR_CHEAT );

Then scroll down to the bottom and comment out these lines:

static CHLModeManager g_HLModeManager;
IVModeManager *modemanager = &g_HLModeManager;

Now go to clientmode_hlnormal.cpp, and comment out this function:

IClientMode *GetClientModeNormal()
{
	static ClientModeHLNormal g_ClientModeNormal;
	return &g_ClientModeNormal;
}

This is a really bad solution to this problem. If you know of a better solution to this problem, please edit the page with the appropriate information on how to do so!

Problem 4:

Once this is implemented, you'll notice that your main menu's title text is gone and that your chapter images are all white.

If anyone knows how to get these issues fixed, please do edit this page with the appropriate information on how to do so!

Conclusion

And that's it, try launching your mod to see if it works!

This is currently only tested on the SP branch of Source SDK 2013. The MP branch and Source SDK 2007 remains untested as of writing this article.