Welcome, Guest! Login | Register

VGUI Scope [Print this Article]
Posted by: Pongles
Date posted: Jun 06 2003
User Rating: 3 out of 5.0
Number of views: 4430
Number of comments: 5
Description:
This tutorial is a step-by-step guide to creating a ‘VGUI Sniper Scope’. The maximum size of a sprite in Half-Life is 256x256 and to cover a screen you would need to tile it because you cannot stretch sprites. Combine the fact that you need to accommodate for each resolution the maths involved would be impossible to maintain. VGUI uses TGA images, then can be almost any size and you can change the opacity easily by using Photoshop. Ergo, a simple scope system that can look transparent, without looking odd and stretched. Open up your client workspace and create a new file called ‘vgui_scope.cpp’. This file will contain the initialising constructor and the update function. Add the following code:
 CODE  

#include "hud.h"
#include "cl_util.h"
#include "vgui_TeamFortressViewport.h"

CScopePanel::CScopePanel(int x,int y,int wide,int tall) : Label("",0,0,ScreenWidth,ScreenHeight)
{
   char sz[64];

   sprintf( sz, "%i_scope", ScreenWidth );

   Scope = LoadTGANoRes( sz );
}

void CScopePanel::Update()
{
   setImage( Scope );
}

Before I move on, you must define the function ‘LoadTGANoRes’ under ‘vgui_CustomObjects.cpp’. Under ‘LoadTGAForRes’ add the following:
 CODE  

BitmapTGA *LoadTGANoRes( const char* pImageName )
{
   BitmapTGA   *pTGA;

   char sz[256];

   sprintf(sz, "gfx/vgui/%s.tga", pImageName);

   pTGA = vgui_LoadTGA(sz);

   return pTGA;
}

The standard TGA loading code looks for two different images, either ‘640_X.tga’ or ‘320_X.tga’, depending on whether your resolution is greater than 640. The sniper scope needs to fill the entire screen so a custom image function needs to be coded allowing a certain image for a certain resolution. Therefore we need a separate TGA image for each resolution (Half-Life ranges from 640x480 to 1600x1200). I have uploaded an example TGA file (640x480) to my website, you can it here: Pongles.com The TGA file needs to be placed in your mod's gfx\vgui directory. Now close ‘vgui_CusomObjects.cpp’ and load up ‘vgui_TeamFortressViewport.h’, this where we are going to define our sniper class. Right at the top of the file you should see a lot of class definitions, add our scope class to them:
 CODE  

class CScopePanel;

BitmapTGA *LoadTGANoRes(const char* pImageName);

The former line declares the scope class the latter declares the TGA loading image function we have used. Now add the following after the ‘CTFScrollPanel’ class definition:
 CODE  

class CScopePanel : public Label
{
private:
   BitmapTGA   *Scope;
public:
   CScopePanel(int x,int y,int wide,int tall);
   void Update();

   virtual void paintBackground()
   {
      // Do nothing, so the background's left transparent.
   }
};

Still in the same file, find the class – ‘TeamFortressViewport’ (Line 412). Under ‘private’ definitions add the following:
 CODE  

CScopePanel      *m_pScopePanel;

Now under ‘public’ add the following:
 CODE  

   void CreateScope( void );
   void ShowScope( void );
   void HideScope( void );

We have now finished with ‘vgui_TeamFortressViewport.h’ so close it. You can now open up ‘vgui_TeamFortressViewport.cpp’ and find the following : ‘TeamFortressViewport::TeamFortressViewport(int x,int y,int wide,int tall) : Panel(x,y,wide,tall), m_SchemeManager(wide,tall)’ (Line 514)
After ‘m_pClassMenu = NULL;’ add this:
 CODE  

m_pScopePanel = NULL;

After ‘CreateClassMenu();’ add this:
 CODE  

CreateScope();

When ‘TeamFortressViewport’ is initialised the scope needs to be invisible so under the ‘Initialize’ function add the following (Line 595):
 CODE  

   if (m_pScopePanel)
   {
      m_pScopePanel->setVisible( false );
   }

After the ‘TeamFortressViewport::CreateClassMenu()’ function (Line 1849) add the following:
 CODE  

void TeamFortressViewport::ShowScope()
{
   if (m_pScopePanel)
   {
      //m_pScopePanel->Open();
      m_pScopePanel->setVisible( true );
   }
}

void TeamFortressViewport::HideScope()
{
   if (m_pScopePanel)
   {
      m_pScopePanel->setVisible( false );
   }
}

void TeamFortressViewport::CreateScope()
{
   // Create the panel
   m_pScopePanel = new CScopePanel(0, 0, ScreenWidth, ScreenHeight);
   m_pScopePanel->setParent(this);
   m_pScopePanel->setVisible( false );
   m_pScopePanel->Update();
}

All the VGUI code is completed now. To make the scope appear and disappear you use these two functions:
Use ‘gViewPort->HideScope();’ to hide the scope. Use ‘gViewPort->ShowScope();’ to show the scope. You could leave the tutorial now and use ‘HideScope’ and ‘ShowScope’ on your own. I have created an example on how to use them though. Close all your open windows and open up ‘ammo.cpp’. Find this line – ‘if ( gHUD.m_iFOV >= 90 )’ [ edit: This can be found in CHudAmmo::MsgFunc_CurWeapon -- Entropy ] and replace that entire if...else statement with the following:
 CODE  

   if ( gHUD.m_iFOV >= 90 )
   { // normal crosshairs
      if (fOnTarget && m_pWeapon->hAutoaim)
      {
         SetCrosshair(m_pWeapon->hAutoaim, m_pWeapon->rcAutoaim, 255, 255, 255);
      }
      else
      {
         SetCrosshair(m_pWeapon->hCrosshair, m_pWeapon->rcCrosshair, 255, 255, 255);
      }

      gViewPort->HideScope();
   }
   else
   { // zoomed crosshairs
      if (fOnTarget && m_pWeapon->hZoomedAutoaim)
      {
         SetCrosshair(m_pWeapon->hZoomedAutoaim, m_pWeapon->rcZoomedAutoaim,
               255, 255, 255);
      }
      else
      {
         SetCrosshair(m_pWeapon->hZoomedCrosshair, m_pWeapon->rcZoomedCrosshair,
               255, 255, 255);
      }

      gViewPort->ShowScope();
   }

Good Luck!

Final note - in order to use the supplied tga file, you'll need to be running Half-Life in 640x480 resolution.

Rate This Article
This article is currently rated: 3 out of 5.0 (2 Votes)

You have to register to rate this article.
User Comments Showing comments 1-5

Posted By: Put Put Cars on Feb 23 2004 at 18:34:24
Nice!

Posted By: whitedragon on Apr 16 2004 at 22:26:21
Problem in coding has 30000000 Errors :( I know i followed it correctly i just know i did Toto.

Posted By: Pongles on Apr 17 2004 at 16:11:19
maybe your just not reading it?

Posted By: Unbreakable on Oct 08 2006 at 16:30:56
I got 3 errors:
'ShowScope' : local function definitions are illegal
'HideScope' : local function definitions are illegal
'CreateScope' : local function definitions are illegal

What could it be. How do I fix that?

Posted By: Pongles on Jan 19 2007 at 07:48:36
you're probarly missing a }


You must register to post a comment. If you have already registered, you must login.

Latest Articles
3rd person View in Multiplayer
Half-Life 2 | Coding | Client Side Tutorials
How to enable it in HL2DM

By: cct | Nov 13 2006

Making a Camera
Half-Life 2 | Level Design
This camera is good for when you join a map, it gives you a view of the map before you join a team

By: slackiller | Mar 04 2006

Making a camera , Part 2
Half-Life 2 | Level Design
these cameras are working monitors that turn on when a button is pushed.

By: slackiller | Mar 04 2006

Storing weapons on ladder
Half-Life 2 | Coding | Snippets
like Raven Sheild or BF2

By: British_Bomber | Dec 24 2005

Implementation of a string lookup table
Half-Life 2 | Coding | Snippets
A string lookup table is a set of functions that is used to convert strings to pre-defined values

By: deathz0rz | Nov 13 2005


Latest Comments
Spinning Corpses Simple Fix
Half-Life | Coding | Snippets
By: darkPhoenix | Sep 05 2008
 
Where do we go from here
General | News
By: MIFUNE | Jun 09 2008
 
The Input/Output system
Half-Life 2 | Level Design
By: nazitaco | Dec 23 2007
 
Where do we go from here
General | News
By: Rob_F | Nov 22 2007
 
Rescaling Half-Life
Half-Life | Coding | Shared Tutorials
By: christoph | Nov 12 2007
 
GameUI
Half-Life 2 | Coding | Client Side Tutorials
By: Evil_j | Oct 29 2007
 
3 State Zoom For Any Weapon
Half-Life 2 | Coding | Server Side Tutorials
By: Ennuified | Oct 18 2007
 
Storing weapons on ladder
Half-Life 2 | Coding | Snippets
By: cct | Sep 07 2007
 
CTF Gameplay Part 1
Half-Life | Coding | Shared Tutorials
By: DarkNight | Aug 28 2007
 
CTF Gameplay Part 1
Half-Life | Coding | Shared Tutorials
By: deedok | Aug 20 2007
 

Site Info
296 Approved Articless
5 Pending Articles
3940 Registered Members
0 People Online (26 guests)
About - Credits - Contact Us

Wavelength version: 3.0.0.9
Valid XHTML 1.0! Valid CSS!