⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mousezoneclass.cpp

📁 <B>很多DirectX 9.0游戏编程源码例子</B>
💻 CPP
字号:
//-----------------------------------------------------------------------------
// Name: MouseZoneClass.cpp
//
// Description: Class to simplify game menu navigation with the mouse
//
// File Function: Function file for the class
//
// Code: 
//		Copyright (c) 2002 LostLogic Corporation. All rights reserved.
//
// Local Files Required:
//		MouseZoneClass.h
//		MouseZoneClass.cpp
//-----------------------------------------------------------------------------
#include "MouseZoneClass.h"

// Constructor
MouseZoneClass::MouseZoneClass( void )
{
	// Default the maximum to zero
	m_iMaxZones = 0;
}

// Destructor
MouseZoneClass::~MouseZoneClass( void )
{
	// Clean up allocated zones
	vFreeZones();
}

void MouseZoneClass::vInitialize( int iMaxZones )
{
	int i;
	
	// Clear out existing zones
	vFreeZones();

	// Store the maximum number of zones
	m_iMaxZones = iMaxZones;

	//
	// Allocate memory for the maximum number of zones
	//
	m_HotSpots = new stHotSpot[ m_iMaxZones ];

	//
	// Clear out the zone information
	//
	for( i = 0; i < m_iMaxZones; i++ ) {
		m_HotSpots[ i ].m_shZoneXPos = 0;
		m_HotSpots[ i ].m_shZoneYPos = 0;
		m_HotSpots[ i ].m_shZoneWidth = 0;
		m_HotSpots[ i ].m_shZoneHeight = 0;
		m_HotSpots[ i ].m_shClickType = 0;
		m_HotSpots[ i ].m_bActive = 0;
		m_HotSpots[ i ].m_szZoneName = new char[ 64 ];
		memset( m_HotSpots[ i ].m_szZoneName, 0x00, 64 );
	}
}

void MouseZoneClass::vFreeZones( void )
{
	int i;

	if( m_iMaxZones ) {
		
		// Free up the name
		for( i = 0; i < m_iMaxZones; i++ ) {
			delete [] m_HotSpots[ i ].m_szZoneName;
		}
		// Free up the hotspots
		delete [] m_HotSpots;

		m_iMaxZones = 0;
	}
}

int MouseZoneClass::iAddZone( char *szZoneName, short shX, short shY, short shWidth, short shHeight, short shClickType )
{
	int i;
	
	for( i = 0; i < m_iMaxZones; i++ ) {
		// Check if it is inactive
		if( m_HotSpots[ i ].m_bActive == 0 ) {
			m_HotSpots[ i ].m_shZoneXPos = shX;
			m_HotSpots[ i ].m_shZoneYPos = shY;
			m_HotSpots[ i ].m_shZoneWidth = shWidth;
			m_HotSpots[ i ].m_shZoneHeight = shHeight;
			m_HotSpots[ i ].m_shClickType = shClickType;
			// Activate the hotspot
			m_HotSpots[ i ].m_bActive = 1;
			// Store the name
			strcpy( m_HotSpots[ i ].m_szZoneName, szZoneName );
			return( i );
		}
	}
	// No free zones, return -1 (error)
	return( -1 );
}

int MouseZoneClass::iRemoveZone( char *szZoneName )
{
	int i;

	for( i = 0; i < m_iMaxZones; i++ ) {
		// Check if it is active
		if( m_HotSpots[ i ].m_bActive == 1 ) {
			// Check for zone name match
			if( !stricmp( m_HotSpots[ i ].m_szZoneName, szZoneName ) ) {
				m_HotSpots[ i ].m_bActive = 0;
				return( 1 );
			}
		}
	}
	return( 0 );
}

bool MouseZoneClass::bCheckZones( short shX, short shY, char *szZoneHit, bool bLeftDown, bool bRightDown )
{
	int i;
	
	for( i = (m_iMaxZones-1); i >= 0; i-- ) {
		// Check if it is active
		if( m_HotSpots[ i ].m_bActive == 1 ) {
			// Check if right button type
			if( (bLeftDown && m_HotSpots[ i ].m_shClickType == 0) || 
				(bRightDown && m_HotSpots[ i ].m_shClickType == 1) || 
				((bRightDown || bLeftDown) && m_HotSpots[ i ].m_shClickType == 2) || 
				((!bRightDown && !bLeftDown) && m_HotSpots[ i ].m_shClickType == 3) ) {
				// Check if along horizontal
				if( m_HotSpots[ i ].m_shZoneXPos <= shX ) {
					// Check if along vertical
					if( m_HotSpots[ i ].m_shZoneYPos <= shY ) {
						// Check within width
						if( ( m_HotSpots[ i ].m_shZoneXPos + m_HotSpots[ i ].m_shZoneWidth ) >= shX ) {
							// Check within height
							if( ( m_HotSpots[ i ].m_shZoneYPos + m_HotSpots[ i ].m_shZoneHeight ) >= shY ) {
								// Set the pointer to the zone name
								strcpy( szZoneHit, m_HotSpots[ i ].m_szZoneName );
								// Return a 1 (hit)
								return( 1 );
							}
						}
					}	
				}
			}
		}
	}
	// Return a 0 (no hit)
	return( 0 );
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -