Animated Menu Background: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
m ("SetZPos( -1 );" has been set in the menu_background.cpp file instead)
m (Code readability, better VMT section formatting, other minor edits)
Line 20: Line 20:


Once that's done, open up '''cdll_client_int.cpp''' and add this:
Once that's done, open up '''cdll_client_int.cpp''' and add this:
<pre>
<source lang="cpp">
void SwapDisconnectCommand();
void SwapDisconnectCommand();
</pre>
</source>


Above
Above


<pre>
<source lang="cpp">
void CHLClient::PostInit()
void CHLClient::PostInit()
</pre>
</source>


Then at the bottom of the '''PostInit()''' function, add this:
Then at the bottom of the <code>PostInit()</code> function, add this:
<pre>
<source lang="cpp">
SwapDisconnectCommand();
SwapDisconnectCommand();
</pre>
</source>


Now head over to '''baseviewport.cpp''', and add this at the top:
Now head over to '''baseviewport.cpp''', and add this at the top:
<pre>
<source lang="cpp">
#include "../menu_background.h"
#include "../menu_background.h"
</pre>
</source>


In the same file, add this in the constructor ('''CBaseViewport::CBaseViewport()'''):
In the same file, add this in the constructor (<code>CBaseViewport::CBaseViewport()</code>):
<pre>
<source lang="cpp">
m_pMainMenuPanel = NULL;
m_pMainMenuPanel = NULL;
</pre>
</source>


Add this in the destructor ('''CBaseViewport::~CBaseViewport()'''):
Add this in the destructor (<code>CBaseViewport::~CBaseViewport()</code>):
<pre>
<source lang="cpp">
if ( !m_bHasParent && m_pMainMenuPanel )
if ( !m_bHasParent && m_pMainMenuPanel )
{
{
Line 52: Line 52:
}
}
m_pMainMenuPanel = NULL;
m_pMainMenuPanel = NULL;
</pre>
</source>


Add this in the '''Start''' function ('''CBaseViewport::Start(...)'''):
Add this in the <code>CBaseViewport::Start(...)</code> function:
<pre>
<source lang="cpp">
m_pMainMenuPanel = new CMainMenu( NULL, NULL );
m_pMainMenuPanel = new CMainMenu( NULL, NULL );
m_pMainMenuPanel->SetZPos( 500 );
m_pMainMenuPanel->SetZPos( 500 );
m_pMainMenuPanel->SetVisible( false );
m_pMainMenuPanel->SetVisible( false );
m_pMainMenuPanel->StartVideo();
m_pMainMenuPanel->StartVideo();
</pre>
</source>


Add this in the '''OnScreenSizeChanged''' function ('''CBaseViewport::OnScreenSizeChanged()'''):
Add this in the <code>CBaseViewport::OnScreenSizeChanged()</code> function:
<pre>
<source lang="cpp">
bool bRestartMainMenuVideo = false;
bool bRestartMainMenuVideo = false;
if (m_pMainMenuPanel)
if (m_pMainMenuPanel)
Line 72: Line 72:
if (bRestartMainMenuVideo)
if (bRestartMainMenuVideo)
m_pMainMenuPanel->StartVideo();
m_pMainMenuPanel->StartVideo();
</pre>
</source>


Add this in the '''RemoveAllPanels''' function ('''CBaseViewport::RemoveAllPanels( void )'''):
Add this in the <code>CBaseViewport::RemoveAllPanels( void )</code>:
<pre>
<source lang="cpp">
if (m_pMainMenuPanel)
if (m_pMainMenuPanel)
{
{
Line 81: Line 81:
m_pMainMenuPanel = NULL;
m_pMainMenuPanel = NULL;
}
}
</pre>
</source>


Below the '''GetDeathMessageStartHeight( void )''' function add these:
Below the <code>GetDeathMessageStartHeight( void )</code> function add these:
<pre>
<source lang="cpp">
void CBaseViewport::StartMainMenuVideo()
void CBaseViewport::StartMainMenuVideo()
{
{
Line 96: Line 96:
m_pMainMenuPanel->StopVideo();
m_pMainMenuPanel->StopVideo();
}
}
</pre>
</source>


Now head over to '''baseviewport.h''', add these:
Now head over to '''baseviewport.h''', add these:
<pre>
<source lang="cpp">
void StartMainMenuVideo();
void StartMainMenuVideo();
void StopMainMenuVideo();
void StopMainMenuVideo();
</pre>
</source>


Below
Below
<pre>
<source lang="cpp">
virtual int GetDeathMessageStartHeight( void );
virtual int GetDeathMessageStartHeight( void );
</pre>
</source>


After the last function under '''public:''', add:
After the last function under <code>public:</code>, add:
<pre>
<source lang="cpp">
private:
private:
class CMainMenu* m_pMainMenuPanel;
class CMainMenu* m_pMainMenuPanel;
</pre>
</source>


Now head over to '''convar.h''', and make this public (protected -> public):
Now head over to '''convar.h''', and make this public (protected -> public):
<pre>
<source lang="cpp">
protected:
protected:
virtual void Create( const char *pName, const char *pHelpString = 0, int flags = 0 );
virtual void Create( const char *pName, const char *pHelpString = 0, int flags = 0 );
</pre>
</source>


== What Next? ==
== What Next? ==
Line 133: Line 133:


=== The Chapter Images ===
=== The Chapter Images ===
You might notice that your chapter images are all white now, this can be fixed by adding this to your to your chapter's VMT file:
You might notice that your chapter images are all white now, this can be fixed by adding this to your chapter's VMT file:
<pre>
<pre>
"$ignorez" "1"
"$ignorez" "1"
Line 142: Line 142:
"UnlitGeneric"
"UnlitGeneric"
{
{
"$baseTexture" "VGUI/chapters/chapterX"
        "$baseTexture"   "VGUI/chapters/chapterX"
"$vertexalpha" 1
        "$vertexalpha"    "1"
"$gammaColorRead" "1"
        "$gammaColorRead" "1"
"$linearWrite" "1"
        "$linearWrite"   "1"
"$ignorez" "1"
        "$ignorez"       "1"
}
}
</pre>
</pre>
Line 162: Line 162:


== Special Thanks ==
== Special Thanks ==
Special thanks to Taz, OzxyBox and Gocnak from the Source Modding Community for solving the problems this implementation was originally facing!
Special thanks to Taz, OzxyBox, and Gocnak from the Source Modding Community for solving the problems this implementation was originally facing!


[[Category:Programming]]
[[Category:Programming]]
[[Category:Free source code]]
[[Category:Free source code]]

Revision as of 11:51, 11 February 2020

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:

SwapDisconnectCommand();

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

#include "../menu_background.h"

In the same file, add this in the constructor (CBaseViewport::CBaseViewport()):

m_pMainMenuPanel = NULL;

Add this in the destructor (CBaseViewport::~CBaseViewport()):

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

Add this in the CBaseViewport::Start(...) function:

m_pMainMenuPanel = new CMainMenu( NULL, NULL );
m_pMainMenuPanel->SetZPos( 500 );
m_pMainMenuPanel->SetVisible( false );
m_pMainMenuPanel->StartVideo();

Add this in the CBaseViewport::OnScreenSizeChanged() function:

bool bRestartMainMenuVideo = false;
if (m_pMainMenuPanel)
	bRestartMainMenuVideo = m_pMainMenuPanel->IsVideoPlaying();
m_pMainMenuPanel = new CMainMenu( NULL, NULL );
m_pMainMenuPanel->SetZPos( 500 );
m_pMainMenuPanel->SetVisible( false );
if (bRestartMainMenuVideo)
	m_pMainMenuPanel->StartVideo();

Add this in the CBaseViewport::RemoveAllPanels( void ):

if (m_pMainMenuPanel)
{
	m_pMainMenuPanel->MarkForDeletion();
	m_pMainMenuPanel = NULL;
}

Below the GetDeathMessageStartHeight( void ) function add these:

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

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

Now head over to baseviewport.h, add these:

void StartMainMenuVideo();
void StopMainMenuVideo();

Below

virtual int GetDeathMessageStartHeight( void );

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?

The Bink File

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 Chapter Images

You might notice that your chapter images are all white now, this can be fixed by adding this to your chapter's VMT file:

"$ignorez" "1"

Example of working VMT chapter file:

"UnlitGeneric"
{
        "$baseTexture"    "VGUI/chapters/chapterX"
        "$vertexalpha"    "1"
        "$gammaColorRead" "1"
        "$linearWrite"    "1"
        "$ignorez"        "1"
}

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.

Special Thanks

Special thanks to Taz, OzxyBox, and Gocnak from the Source Modding Community for solving the problems this implementation was originally facing!