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

📄 cgame.cpp

📁 一个基于symbian s60 3rd 的3D汽车游戏演示程序,模拟器上编译通过。
💻 CPP
📖 第 1 页 / 共 3 页
字号:
    CPolygonObject* o = CPolygonObject::NewL( i3DBase, 100,100 );
    TInt16* cos = i3DBase->CosTable();
    TInt16* sin = i3DBase->SinTable();

    TInt i;

    TInt z1 = -aWidth/2;
    TInt z2 = aWidth/2;

    o->AddVertex( TVertex( 0,0,0 ) );
    
    const TInt KTexturePixels = 128;

    for( i=0; i<aNumPoints; i++ )
        {
        TInt a1 = KSinTableSize * i / aNumPoints;
        TInt a2 = ( a1 + KSinTableSize / aNumPoints ) & (KSinTableSize-1);
        
        TInt x1 = ( aRadius * cos[ a1 ] ) >> KShift;
        TInt y1 = ( aRadius * sin[ a1 ] ) >> KShift;
        TInt x2 = ( aRadius * cos[ a2 ] ) >> KShift;
        TInt y2 = ( aRadius * sin[ a2 ] ) >> KShift;

        o->AddVertex( TVertex( x1,y1,z1 ) );
        o->AddVertex( TVertex( x1,y1,z2 ) );

        TInt n1 = i*2;
        TInt n2 = n1 + 1;
        TInt n3 = n1 + 2;
        TInt n4 = n1 + 3;
        if( n3 >= aNumPoints*2 ) n3 -= aNumPoints*2;
        if( n4 >= aNumPoints*2 ) n4 -= aNumPoints*2;

        n1 += 1;
        n2 += 1;
        n3 += 1;
        n4 += 1;

        o->AddFace( TFace( n1,n2,n3, 128,0, 255,0, 128,128 ) );
        o->AddFace( TFace( n3,n2,n4, 128,128, 255,0, 255,128 ) );
        
        // calculate texture pixel positions
        // for wheel rim
        TInt tx1 = KTexturePixels/2 + x1 * ( KTexturePixels/2-1 ) / aRadius;
        TInt ty1 = KTexturePixels/2 + y1 * ( KTexturePixels/2-1 ) / aRadius;
        TInt tx2 = KTexturePixels/2 + x2 * ( KTexturePixels/2-1 ) / aRadius;
        TInt ty2 = KTexturePixels/2 + y2 * ( KTexturePixels/2-1 ) / aRadius;

        o->AddFace( TFace( n1,n3,0, tx1,ty1, tx2,ty2, 64,64 ) );
        
        }

    return o;
    }

CPolygonObject* CGame::CreateObject( const TInt* aVertexData, const TInt* aFaceData,
                                     TInt aNumVertices, TInt aNumFaces,
                                     const TPoint& aTexOffset, TInt aTexMul )
    {
    // leave some extra room for vertices and polygons
    // otherwise renderer might not have enough space
    // after clipping
    CPolygonObject* o = CPolygonObject::NewL( i3DBase, aNumVertices*2+10, aNumFaces*2+10 );
    
    TInt n=0;
    TInt i;
    for( i=0; i<aNumVertices; i++ )
        {
        o->AddVertex( TVertex(  aVertexData[ n + 0 ],
                                aVertexData[ n + 1 ],
                                aVertexData[ n + 2 ]
                                ) );

        n += 3;
        }

    n=0;
    for( i=0; i<aNumFaces; i++ )
        {

        o->AddFace( TFace(  aFaceData[ n + 0 ],
                            aFaceData[ n + 1 ],
                            aFaceData[ n + 2 ],
                            aFaceData[ n + 3 ]*aTexMul+aTexOffset.iX,
                            aFaceData[ n + 4 ]*aTexMul+aTexOffset.iY,
                            aFaceData[ n + 5 ]*aTexMul+aTexOffset.iX,
                            aFaceData[ n + 6 ]*aTexMul+aTexOffset.iY,
                            aFaceData[ n + 7 ]*aTexMul+aTexOffset.iX,
                            aFaceData[ n + 8 ]*aTexMul+aTexOffset.iY
                            ) );
        n += 9;
        }

    return o;
    }

void CGame::DrawText( CPolygonObject* aObject, const TDesC8& aStr )
    {
    TInt n = aStr.Length();
    
    // reserve space for polygons in 3D-objects
    aObject->Reset( n*4, n*3 );
    
    TInt x = 0;
    TInt y = 0;
    
    // default font size in 3D-space
    const TInt size = 500;

    // go trough whole string
    for( TInt i=0; i<n; i++ )
        {
        TInt c = aStr[ i ];
        // check for line feed character:
        if( c == '\n' )
            {
            x = 0;
            y++;
            }
        else
            {
            TInt xx = x * size;
            TInt yy = y * size;

            TInt v1 = aObject->AddVertex( TVertex( xx, yy, 0 ) );
            TInt v2 = aObject->AddVertex( TVertex( xx + size, yy, 0 ) );
            TInt v3 = aObject->AddVertex( TVertex( xx + size, yy + size, 0 ) );
            TInt v4 = aObject->AddVertex( TVertex( xx, yy + size, 0 ) );
            
            // fonts are in 16x16 array in bitmap
            // first 16 characters are on first line
            // next 16 characters on second line and so on...
            TInt tx = c & 15;
            TInt ty = c >> 4;
            
            // fonts are 16 pixels high and wide
            // calculate top left ( x1, y1 ) and
            // bottom right( x2, y2 ) pixel positions
            // for current font
            TInt x1 = tx * 16;
            TInt y1 = ty * 16;
            TInt x2 = x1 + 15;
            TInt y2 = y1 + 15;
            
            // each visible font consists of two triangles
            // which make a textured rectangle with font picture
            aObject->AddFace( TFace( v1,v2,v3, x1,y1, x2,y1, x2,y2 ) );
            aObject->AddFace( TFace( v1,v3,v4, x1,y1, x2,y2, x1,y2 ) );

            x++;
            }
        }
    aObject->Init();
    }
    
TInt CGame::PolygonCount()
    {
    return i3DBase->PolygonCount();
    }

void CGame::Command( TInt aCommand )
    {
    switch( aCommand )
        {
        case EExample3DCamera1:
            iCameraMode = ECameraBehind;
            break;
        case EExample3DCamera2:
            iCameraMode = ECameraInCar;
            break;
        case EExample3DCamera3:
            iCameraMode = ECameraSouth;
            break;
        case EExample3DCamera4:
            iCameraMode = ECameraTV1;
            break;
        case EExample3DCamera5:
            iCameraMode = ECameraTV2;
            break;
        case EExample3DCamera6:
            iCameraMode = ECameraTV3;
            break;
        case EExample3DCamera7:
            iCameraMode = ECameraTV4;
            break;
        case EExample3DCameraToggle:
            iCameraMode++;
            if( iCameraMode == ECameraNone )
                {
                iCameraMode = 0;
                }
            break;
#ifdef SENSOR_API_SUPPORT_ENABLED
        case EExample3DSensorsOn:
            if ( iSensorsSupported )
                {
                iSensorThrottleMode = ETrue;
                iSensorSteeringMode = ETrue;
                if( !iSensor )
                    {
                    if ( !RegisterSensor() )
                        {
                        iSensorThrottleMode = EFalse;
                        iSensorSteeringMode = EFalse;
                        }
                    }
                }
            break;
        case EExample3DSensorsSteering:
            if ( iSensorsSupported )
                {
                iSensorThrottleMode = EFalse;
                iSensorSteeringMode = ETrue;
                if( !iSensor )
                    {
                    if ( !RegisterSensor() )
                        {
                        iSensorThrottleMode = EFalse;
                        iSensorSteeringMode = EFalse;
                        }
                    }
                }
            break;
        case EExample3DSensorsOff:
            if ( iSensorsSupported )
                {
                iSensorThrottleMode = EFalse;
                iSensorSteeringMode = EFalse;
                if( iSensor )
                    {
                    UnregisterSensor();
                    }
                }
            break;
#endif //SENSOR_API_SUPPORT_ENABLED
        default:
            break;
        }
    }

#ifdef SENSOR_API_SUPPORT_ENABLED
void CGame::SetupSensors()
    {
    // Sensor control object
    iSensor = NULL;
    
    TBool sensorDllLoaded = EFalse;

#ifdef SENSOR_API_LOAD_DYNAMICALLY
    // Try to load Sensor API library dynamically
    _LIT( KSensorApiDll, "RRSensorApi" );
    TUidType dllUid( KDynamicLibraryUid );
    if (iSensorApi.Load( KSensorApiDll, dllUid ) == KErrNone)
        {
        sensorDllLoaded = ETrue;
        }
#else
    // If sensor DLL is loaded statically, there's nothing to be done here
    sensorDllLoaded = ETrue;

#endif //SENSOR_API_LOAD_DYNAMICALLY

    // If sensor DLL was loaded successfully, try to register the sensors
    if (sensorDllLoaded && RegisterSensor())
        {
        // Success: Use accelerometer controls
        iSensorsSupported = ETrue;
        iSensorSteeringMode = ETrue;
        iSensorThrottleMode = ETrue;
        }
    else 
        {
        // Fail: Use keypad controls
        iSensorsSupported = EFalse;
        iSensorSteeringMode = EFalse;
        iSensorThrottleMode = EFalse;
        }
    
    // Reset sensor data
    iSensorDataX = 0;
    iSensorDataY = 0;
    iSensorDataZ = 0;
    
    // Create sensor data filters
    iSensorDataFilterX = CSensorDataFilter::NewL( KSensorBufferSize );
    iSensorDataFilterY = CSensorDataFilter::NewL( KSensorBufferSize );
    iSensorDataFilterZ = CSensorDataFilter::NewL( KSensorBufferSize );
    }

TBool CGame::RegisterSensor()
    {
    // Get list of available sensors
    RArray<TRRSensorInfo> sensorList;
    CleanupClosePushL( sensorList );
    
    // Call Sensor API function CRRSensorApi::FindSensorsL()
#ifdef SENSOR_API_LOAD_DYNAMICALLY
    // If Sensor API library is dynamically linked
    typedef void ( *TFindSensorsLFunction )( RArray<TRRSensorInfo>& ); 
    TFindSensorsLFunction findSensorsLFunction = ( TFindSensorsLFunction )iSensorApi.Lookup( 1 );
    findSensorsLFunction( sensorList );
#else
    // If Sensor API library is statically linked
    CRRSensorApi::FindSensorsL( sensorList );
#endif //SENSOR_API_LOAD_DYNAMICALLY
    
    // Get number of sensors available
    TInt sensorCount = sensorList.Count();

    for( TInt i = 0 ; i != sensorCount ; i++ )
        {
        if( sensorList[i].iSensorId == KAccSensorUID )
            {
            
            // Call Sensor API function CRRSensorApi::NewL()
#ifdef SENSOR_API_LOAD_DYNAMICALLY
            // If Sensor API library is dynamically linked
            typedef CRRSensorApi* ( *TNewLFunction )( TRRSensorInfo ); 
            TNewLFunction newLFunction = ( TNewLFunction )iSensorApi.Lookup( 2 );
            iSensor = newLFunction( sensorList[i] );
#else
            // If Sensor API library is statically linked
            iSensor = CRRSensorApi::NewL( sensorList[i] );
#endif //SENSOR_API_LOAD_DYNAMICALLY
            
            // Register accelerometer data listener
            iSensor->AddDataListener( this );
            }
        }
    
    CleanupStack::PopAndDestroy();
    
    // Did accelerometer sensor found?
    if ( iSensor != NULL )
        {
        return ETrue;
        }
    else
        {
        return EFalse;
        }
    }

void CGame::HandleDataEventL( TRRSensorInfo aSensor, TRRSensorEvent aEvent )
    {
    switch ( aSensor.iSensorId )
        {
        case KAccSensorUID:
            {
            // Smooth sensor data values with some filtering to prevent random noise
            iSensorDataX = iSensorDataFilterX->FilterSensorData( aEvent.iSensorData1 );
            iSensorDataY = iSensorDataFilterY->FilterSensorData( aEvent.iSensorData2 );
            iSensorDataZ = iSensorDataFilterZ->FilterSensorData( aEvent.iSensorData3 );
            }
            break;
        default:
            break;
        }
    }

void CGame::UnregisterSensor()
    {
    // Unregister accelerometer data listener
    iSensor->RemoveDataListener();
    delete iSensor;
    iSensor = NULL;
    iSensorDataX = 0;
    iSensorDataY = 0;
    iSensorDataZ = 0;
    }
    
void CGame::CleanupSensors()
    {
    if ( iSensorsSupported && iSensor ) 
        {
        UnregisterSensor();
        }
    
#ifdef SENSOR_API_LOAD_DYNAMICALLY
    // Close dynamically loaded library
    iSensorApi.Close();
#endif //SENSOR_API_LOAD_DYNAMICALLY
    
    delete iSensorDataFilterX;
    delete iSensorDataFilterY;
    delete iSensorDataFilterZ;
    }
#endif //SENSOR_API_SUPPORT_ENABLED

⌨️ 快捷键说明

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