Exemplo n.º 1
0
HRESULT KRLTrackCamera::FrameMove(KRLCameraParam* pResultParam, float fDeltaTime)
{
    HRESULT hrResult = E_FAIL;
    int nRetCode = false;
    float fDeltaDistance = 0.0f;
    KRLSpringParam RLSpringParam;
    KRLSpringResult RLSpringResult;

    StartProfile(&g_pRL->m_Profile, KRLPROFILE_TRACK_CAMERA);

    KGLOG_PROCESS_ERROR(pResultParam);
    KG_PROCESS_SUCCESS(!m_nUpdateFlag);

    // 利用弹簧系统移动Camera到期待的位置
    if (m_nUseFlexFlag)
    {
        fDeltaDistance = m_fExpectDistance - m_fDistance - m_fWheelOffset;     // 要参与弹簧系统的距离
        ASSERT(fDeltaDistance >= 0.0f);
        KGLOG_CHECK_ERROR(fDeltaDistance >= 0.0f);

        fDeltaTime *= 0.001f;
 
        RLSpringParam.fDistance = fDeltaDistance;
        RLSpringParam.fTime = fDeltaTime;
        RLSpringParam.fVelocity = m_fCurrentVelocity;
        RLSpringParam.fEpsilon = RL_SPRING_EPSILON;

        nRetCode = RunSpringSystem(RLSpringParam, &RLSpringResult);
        KGLOG_PROCESS_ERROR(nRetCode);

        if (!RLSpringResult.nFinished)
        {
            m_fDistance += RLSpringResult.fDistance;
            m_fCurrentVelocity = RLSpringResult.fVelocity;
        }
        else
        {
            StopSpringSystem((m_fExpectDistance + m_fDistance) * 0.5f);
        }
    }
    else
    {
        StopSpringSystem((m_fExpectDistance + m_fDistance) * 0.5f);
    }

    // 遮挡检测
    DetectObstruct(m_fExpectDistance);

    m_vPosition = m_vExpectTarget - m_vExpectFront * m_fDistance;

    m_fWheelOffset = 0.0f;

Exit1:
    hrResult = S_OK;
Exit0:
    pResultParam->vPosition = m_vPosition;
    pResultParam->vLookAt = m_vLookAt;
    pResultParam->vUp = D3DXVECTOR3(0.0f, 1.0f, 0.0f);
    pResultParam->vTarget = m_vTarget;

    StopProfile(&g_pRL->m_Profile, KRLPROFILE_TRACK_CAMERA);
    return hrResult;
}
Exemplo n.º 2
0
globle int EvaluateExpression(
  void *theEnv,
  struct expr *problem,
  DATA_OBJECT_PTR returnValue)
  {
   struct expr *oldArgument;
   void *oldContext;
   struct FunctionDefinition *fptr;
#if PROFILING_FUNCTIONS
   struct profileFrameInfo profileFrame;
#endif

   if (problem == NULL)
     {
      returnValue->type = SYMBOL;
      returnValue->value = EnvFalseSymbol(theEnv);
      return(EvaluationData(theEnv)->EvaluationError);
     }

   switch (problem->type)
     {
      case STRING:
      case SYMBOL:
      case FLOAT:
      case INTEGER:
#if OBJECT_SYSTEM
      case INSTANCE_NAME:
      case INSTANCE_ADDRESS:
#endif
      case EXTERNAL_ADDRESS:
        returnValue->type = problem->type;
        returnValue->value = problem->value;
        break;

      case DATA_OBJECT_ARRAY: /* TBD Remove with AddPrimitive */
        returnValue->type = problem->type;
        returnValue->value = problem->value;
        break;

      case FCALL:
        {
         fptr = (struct FunctionDefinition *) problem->value;
         oldContext = SetEnvironmentFunctionContext(theEnv,fptr->context);

#if PROFILING_FUNCTIONS   
         StartProfile(theEnv,&profileFrame,
                      &fptr->usrData,
                      ProfileFunctionData(theEnv)->ProfileUserFunctions);
#endif

         oldArgument = EvaluationData(theEnv)->CurrentExpression;
         EvaluationData(theEnv)->CurrentExpression = problem;

         switch(fptr->returnValueType)
           {
            case 'v' :
              if (fptr->environmentAware)
                { (* (void (*)(void *)) fptr->functionPointer)(theEnv); }
              else
                { (* (void (*)(void)) fptr->functionPointer)(); }
              returnValue->type = RVOID;
              returnValue->value = EnvFalseSymbol(theEnv);
              break;
            case 'b' :
              returnValue->type = SYMBOL;
              if (fptr->environmentAware)
                {
                 if ((* (int (*)(void *)) fptr->functionPointer)(theEnv))
                   returnValue->value = EnvTrueSymbol(theEnv);
                 else
                   returnValue->value = EnvFalseSymbol(theEnv);
                }
              else
                {
                 if ((* (int (*)(void)) fptr->functionPointer)())
                   returnValue->value = EnvTrueSymbol(theEnv);
                 else
                   returnValue->value = EnvFalseSymbol(theEnv);
                }
              break;
            case 'a' :
              returnValue->type = EXTERNAL_ADDRESS;
              if (fptr->environmentAware)
                {
                 returnValue->value =
                                (* (void *(*)(void *)) fptr->functionPointer)(theEnv);
                }
              else
                {
                 returnValue->value =
                                (* (void *(*)(void)) fptr->functionPointer)();
                }
              break;
            case 'g' :
              returnValue->type = INTEGER;
              if (fptr->environmentAware)
                {
                 returnValue->value = (void *)
                   EnvAddLong(theEnv,(* (long long (*)(void *)) fptr->functionPointer)(theEnv));
                }
              else
                {
                 returnValue->value = (void *)
                   EnvAddLong(theEnv,(* (long long (*)(void)) fptr->functionPointer)());
                }
              break;
            case 'i' :
              returnValue->type = INTEGER;
              if (fptr->environmentAware)
                {
                 returnValue->value = (void *)
                   EnvAddLong(theEnv,(long long) (* (int (*)(void *)) fptr->functionPointer)(theEnv));
                }
              else
                {
                 returnValue->value = (void *)
                   EnvAddLong(theEnv,(long long) (* (int (*)(void)) fptr->functionPointer)());
                }
              break;
            case 'l' :
              returnValue->type = INTEGER;
              if (fptr->environmentAware)
                {
                 returnValue->value = (void *)
                    EnvAddLong(theEnv,(long long) (* (long int (*)(void *)) fptr->functionPointer)(theEnv));
                }
              else
                {
                 returnValue->value = (void *)
                    EnvAddLong(theEnv,(long long) (* (long int (*)(void)) fptr->functionPointer)());
                }
              break;
            case 'f' :
              returnValue->type = FLOAT;
              if (fptr->environmentAware)
                {
                 returnValue->value = (void *)
                    EnvAddDouble(theEnv,(double) (* (float (*)(void *)) fptr->functionPointer)(theEnv));
                }
              else
                {
                 returnValue->value = (void *)
                    EnvAddDouble(theEnv,(double) (* (float (*)(void)) fptr->functionPointer)());
                }
              break;
            case 'd' :
              returnValue->type = FLOAT;
              if (fptr->environmentAware)
                {
                 returnValue->value = (void *)
                    EnvAddDouble(theEnv,(* (double (*)(void *)) fptr->functionPointer)(theEnv));
                }
              else
                {
                 returnValue->value = (void *)
                    EnvAddDouble(theEnv,(* (double (*)(void)) fptr->functionPointer)());
                }
              break;
            case 's' :
              returnValue->type = STRING;
              if (fptr->environmentAware)
                {
                 returnValue->value = (void *)
                   (* (SYMBOL_HN *(*)(void *)) fptr->functionPointer)(theEnv);
                }
              else
                {
                 returnValue->value = (void *)
                   (* (SYMBOL_HN *(*)(void)) fptr->functionPointer)();
                }
              break;
            case 'w' :
              returnValue->type = SYMBOL;
              if (fptr->environmentAware)
                {
                 returnValue->value = (void *)
                   (* (SYMBOL_HN *(*)(void *)) fptr->functionPointer)(theEnv);
                }
              else
                {
                 returnValue->value = (void *)
                   (* (SYMBOL_HN *(*)(void)) fptr->functionPointer)();
                }
              break;
#if OBJECT_SYSTEM
            case 'x' :
              returnValue->type = INSTANCE_ADDRESS;
              if (fptr->environmentAware)
                {
                 returnValue->value =
                                (* (void *(*)(void *)) fptr->functionPointer)(theEnv);
                }
              else
                {
                 returnValue->value =
                                (* (void *(*)(void)) fptr->functionPointer)();
                }
              break;
            case 'o' :
              returnValue->type = INSTANCE_NAME;
              if (fptr->environmentAware)
                {
                 returnValue->value = (void *)
                   (* (SYMBOL_HN *(*)(void *)) fptr->functionPointer)(theEnv);
                }
              else
                {
                 returnValue->value = (void *)
                   (* (SYMBOL_HN *(*)(void)) fptr->functionPointer)();
                }
              break;
#endif
            case 'c' :
              {
               char cbuff[2];
               if (fptr->environmentAware)
                 {
                  cbuff[0] = (* (char (*)(void *)) fptr->functionPointer)(theEnv);
                 }
               else
                 {
                  cbuff[0] = (* (char (*)(void)) fptr->functionPointer)();
                 }
               cbuff[1] = EOS;
               returnValue->type = SYMBOL;
               returnValue->value = (void *) EnvAddSymbol(theEnv,cbuff);
               break;
              }

            case 'j' :
            case 'k' :
            case 'm' :
            case 'n' :
            case 'u' :
               if (fptr->environmentAware)
                 {
                  (* (void (*)(void *,DATA_OBJECT_PTR)) fptr->functionPointer)(theEnv,returnValue);
                 }
               else
                 {
                  (* (void (*)(DATA_OBJECT_PTR)) fptr->functionPointer)(returnValue);
                 }
              break;

            default :
               SystemError(theEnv,"EVALUATN",2);
               EnvExitRouter(theEnv,EXIT_FAILURE);
               break;
            }

#if PROFILING_FUNCTIONS 
        EndProfile(theEnv,&profileFrame);
#endif

        SetEnvironmentFunctionContext(theEnv,oldContext);
        EvaluationData(theEnv)->CurrentExpression = oldArgument;
        break;
        }

     case MULTIFIELD:
        returnValue->type = MULTIFIELD;
        returnValue->value = ((DATA_OBJECT_PTR) (problem->value))->value;
        returnValue->begin = ((DATA_OBJECT_PTR) (problem->value))->begin;
        returnValue->end = ((DATA_OBJECT_PTR) (problem->value))->end;
        break;

     case MF_VARIABLE:
     case SF_VARIABLE:
        if (GetBoundVariable(theEnv,returnValue,(SYMBOL_HN *) problem->value) == FALSE)
          {
           PrintErrorID(theEnv,"EVALUATN",1,FALSE);
           EnvPrintRouter(theEnv,WERROR,"Variable ");
           EnvPrintRouter(theEnv,WERROR,ValueToString(problem->value));
           EnvPrintRouter(theEnv,WERROR," is unbound\n");
           returnValue->type = SYMBOL;
           returnValue->value = EnvFalseSymbol(theEnv);
           SetEvaluationError(theEnv,TRUE);
          }
        break;

      default:
        if (EvaluationData(theEnv)->PrimitivesArray[problem->type] == NULL)
          {
           SystemError(theEnv,"EVALUATN",3);
           EnvExitRouter(theEnv,EXIT_FAILURE);
          }

        if (EvaluationData(theEnv)->PrimitivesArray[problem->type]->copyToEvaluate)
          {
           returnValue->type = problem->type;
           returnValue->value = problem->value;
           break;
          }

        if (EvaluationData(theEnv)->PrimitivesArray[problem->type]->evaluateFunction == NULL)
          {
           SystemError(theEnv,"EVALUATN",4);
           EnvExitRouter(theEnv,EXIT_FAILURE);
          }

        oldArgument = EvaluationData(theEnv)->CurrentExpression;
        EvaluationData(theEnv)->CurrentExpression = problem;

#if PROFILING_FUNCTIONS 
        StartProfile(theEnv,&profileFrame,
                     &EvaluationData(theEnv)->PrimitivesArray[problem->type]->usrData,
                     ProfileFunctionData(theEnv)->ProfileUserFunctions);
#endif

        (*EvaluationData(theEnv)->PrimitivesArray[problem->type]->evaluateFunction)(theEnv,problem->value,returnValue);

#if PROFILING_FUNCTIONS
        EndProfile(theEnv,&profileFrame);
#endif

        EvaluationData(theEnv)->CurrentExpression = oldArgument;
        break;
     }

   return(EvaluationData(theEnv)->EvaluationError);
  }
Exemplo n.º 3
0
int KRLCamera::UpdateLocal(double fTime, double fTimeLast, DWORD dwGameLoop, BOOL bFrame)
{
    int nRetCode = false;
    int nResult = false;
    HRESULT hr = E_FAIL;
    KRLCameraParam RLCameraParam;
    float fDeltaTime = 0.0f;

    StartProfile(&g_pRL->m_Profile, KRLPROFILE_CAMERA);

    KG_PROCESS_SUCCESS(!m_bFrameMoveEnable);

    fDeltaTime = (float)(g_pRL->m_fTime - g_pRL->m_fTimeLast);

    hr = m_pCamera->Update(fTime, fTimeLast, dwGameLoop, bFrame, &m_CommonInfo);
    KGLOG_COM_PROCESS_ERROR(hr);

    nRetCode = ClearControlsImpluse();
    KGLOG_PROCESS_ERROR(nRetCode);

    hr = m_CommonInfo.RLTrackCamera.FrameMove(&RLCameraParam, fDeltaTime);
    KGLOG_COM_PROCESS_ERROR(hr);

    m_CommonInfo.p3DCamera->SetLookAtPosition(RLCameraParam.vLookAt);
    m_CommonInfo.p3DCamera->SetUpDirection(RLCameraParam.vUp);
    m_CommonInfo.p3DCamera->SetPosition(RLCameraParam.vPosition);
    m_CommonInfo.p3DScene->SetFocus(RLCameraParam.vTarget);

    hr = m_pCamera->PostUpdate(RLCameraParam);
    KGLOG_COM_PROCESS_ERROR(hr);

Exit1:
    nResult = true;
Exit0:

    if (g_pRL->m_CurrentTraceOption.bTraceCamera)
    {
        static D3DXVECTOR3 s_vPosition;

        if (s_vPosition != RLCameraParam.vPosition)
        {
            D3DXVECTOR3 vDelta = RLCameraParam.vPosition - s_vPosition;
            s_vPosition = RLCameraParam.vPosition;

            float fDelta = (float)(fTime - fTimeLast);

            if (fDelta > 0.1f)
            {
                D3DXVECTOR3 vSpeed = vDelta / fDelta;

                float fDeltaLength = D3DXVec3Length(&vDelta);
                float fSpeedLength = D3DXVec3Length(&vSpeed);

                printf("[RL] camera %d, [%.4f,%.4f,%.4f][%.4f,%.4f,%.4f](%.4f)\n",
                    dwGameLoop,
                    vDelta.x, vDelta.y, vDelta.z,
                    vSpeed.x, vSpeed.y, vSpeed.z, fSpeedLength);
            }
            else
            {
                printf("[RL] camera error\n");
            }
        }
    }

    if (g_pRL->m_CurrentTraceOption.bTraceTarget)
    {
        static D3DXVECTOR3 s_vTarget;

        if (s_vTarget != RLCameraParam.vTarget)
        {
            D3DXVECTOR3 vDelta = RLCameraParam.vTarget - s_vTarget;
            s_vTarget = RLCameraParam.vTarget;

            float fDelta = (float)(fTime - fTimeLast);

            D3DXVECTOR3 vSpeed = vDelta / fDelta;

            float fDeltaLength = D3DXVec3Length(&vDelta);
            float fSpeedLength = D3DXVec3Length(&vSpeed);

            printf("[RL] target %d, [%.4f,%.4f,%.4f][%.4f,%.4f,%.4f](%.4f)\n",
                dwGameLoop,
                vDelta.x, vDelta.y, vDelta.z,
                vSpeed.x, vSpeed.y, vSpeed.z, fSpeedLength);
        }
    }

    if (g_pRL->m_TraceOption.bTraceFrameInterval)
    {
        static float s_fDeltaTime = 0.0f;

        if (fDeltaTime > 25.0f)
        {
            s_fDeltaTime = fDeltaTime;

            printf("[RL] %d %d %.4f\n", dwGameLoop, bFrame, fDeltaTime);
        }
    }

    StopProfile(&g_pRL->m_Profile, KRLPROFILE_CAMERA);
    return nResult;
}
Exemplo n.º 4
0
HRESULT Window::MessageLoop()
{
    MSG msg;

    while (true) {
        //
        // Turn on Profiling
        //

        #ifdef ICAP
            if (s_bProfileStarted) {
                if (s_countProfile == 0) {
                    MarkProfile(2); 
                    StopProfile(PROFILE_THREADLEVEL, PROFILE_CURRENTID);
                    s_bProfileStarted = false;
                }
            } else {
                if (s_countProfile != 0) {
                    StartProfile(PROFILE_THREADLEVEL, PROFILE_CURRENTID);
                    MarkProfile(1); 
                    s_bProfileStarted = true;
                }

                if (s_countProfile > 0) {
                    s_countProfile--;
                }
            }
        #endif

        //
        // Call Timers and Idle functions
        //

        CallTimers();
        CallIdleFunctions();

        //
        // Handles Win32 messages
        //

        bool bAnyMessage = true;
        if (g_bContinuousIdle) {
            bAnyMessage = ::PeekMessageA(&msg, NULL, 0, 0, PM_REMOVE) != 0;
        } else {
            ::GetMessageA(&msg, NULL, 0, 0);
        }

        if (bAnyMessage) {
            do {
                if (msg.message == WM_QUIT) {
                    s_mapWindow.SetEmpty();
                    return S_OK;
                }

                switch (msg.message) {
                    case WM_SYSKEYDOWN:
                    case WM_SYSKEYUP:
                    case WM_KEYDOWN:
                    case WM_KEYUP:
                        {
                            KeyState ks;
                            ks.vk = msg.wParam;
                            ks.bAlt = (msg.message == WM_SYSKEYUP || msg.message == WM_SYSKEYDOWN) 
                                && (msg.lParam & (1 << 29)); // See help for WM_SYSKEYDOWN
                            ks.bShift = (GetKeyState(VK_SHIFT) & 0x8000) !=0;
                            ks.bControl = (GetKeyState(VK_CONTROL) & 0x8000) !=0;
                            ks.bDown = (msg.message == WM_KEYDOWN || msg.message == WM_SYSKEYDOWN || ks.vk == 44); // special exception for PrintScreen key
                            ks.countRepeat = LOWORD(msg.lParam);

							bool fHandled = false;
                            bool fForceTranslate = false;

                            TList<TRef<IKeyboardInput> >::Iterator iter(g_listKeyboardInputFilters);
                            while (!iter.End() && !fHandled) {
                                fHandled = iter.Value()->OnKey(NULL, ks, fForceTranslate);
                                iter.Next();
                            }

                            if (!fHandled) {
                                fHandled = (::DispatchMessageA(&msg) == 0);
                            }

                            if (!fHandled || fForceTranslate) {
                                ::TranslateMessage(&msg);
                            }
                        }
                        break;

                    case WM_CHAR:
                        {
                            KeyState ks;
                            ks.vk = msg.wParam;
                            ks.bShift = (GetKeyState(VK_SHIFT) & 0x8000) != 0;
                            ks.bControl = (GetKeyState(VK_CONTROL) & 0x8000) !=0;
                            ks.countRepeat = LOWORD(msg.lParam);

                            bool fHandled = false;

                            TList<TRef<IKeyboardInput> >::Iterator iter(g_listKeyboardInputFilters);
                            while (!iter.End() && !fHandled) {
                                fHandled = iter.Value()->OnChar(NULL, ks);
                                iter.Next();
                            }

                            if (fHandled) {
                                break;
                            }
                        }

                        //
                        // intentional fallthrough
                        //

                    default:
                        ::DispatchMessageA(&msg);
                        break;
                } 
            } while (::PeekMessageA(&msg, NULL, 0, 0, PM_REMOVE));
        }
    }
}
Exemplo n.º 5
0
void Profiler::EndFrame(SpriteRenderer& spriteRenderer, SpriteFont& spriteFont)
{
    // If any profile was previously active but wasn't used this frame, it could still
    // have outstanding queries that we need to keep running
    for(auto iter = profiles.begin(); iter != profiles.end(); iter++)
    {
        const std::wstring& name = (*iter).first;
        ProfileData& profile = (*iter).second;

        if(!profile.CPUProfile && !profile.Active && profile.DisjointQuery[0] != nullptr)
        {
            StartProfile(name);
            EndProfile(name);
            profile.Active = false;
        }
    }

    currFrame = (currFrame + 1) % QueryLatency;

    Float4x4 transform;
    transform.SetTranslation(Float3(25.0f, 100.0f, 0.0f));

    // Iterate over all of the profiles
    for(auto iter = profiles.begin(); iter != profiles.end(); iter++)
    {
        ProfileData& profile = (*iter).second;
        profile.QueryFinished = false;

        float time = 0.0f;
        if(profile.CPUProfile)
        {
            time = (profile.EndTime - profile.StartTime) / 1000.0f;
        }
        else
        {
            if(profile.DisjointQuery[currFrame] == NULL)
                continue;

            // Get the query data
            uint64 startTime = 0;
            while(context->GetData(profile.TimestampStartQuery[currFrame], &startTime, sizeof(startTime), 0) != S_OK);

            uint64 endTime = 0;
            while(context->GetData(profile.TimestampEndQuery[currFrame], &endTime, sizeof(endTime), 0) != S_OK);

            D3D11_QUERY_DATA_TIMESTAMP_DISJOINT disjointData;
            while(context->GetData(profile.DisjointQuery[currFrame], &disjointData, sizeof(disjointData), 0) != S_OK);

            if(disjointData.Disjoint == false)
            {
                uint64 delta = endTime - startTime;
                float frequency = static_cast<float>(disjointData.Frequency);
                time = (delta / frequency) * 1000.0f;
            }
        }

        profile.TimeSamples[profile.CurrSample] = time;
        profile.CurrSample = (profile.CurrSample + 1) % ProfileData::FilterSize;

        float sum = 0.0f;
        for(UINT i = 0; i < ProfileData::FilterSize; ++i)
            sum += profile.TimeSamples[i];
        time = sum / ProfileData::FilterSize;

        if(profile.Active)
        {
            wstring output = (*iter).first + L": " + ToString(time) + L"ms";
            spriteRenderer.RenderText(spriteFont, output.c_str(), transform, Float4(1.0f, 1.0f, 0.0f, 1.0f));
            transform._42 += 25.0f;
        }

        profile.Active = false;
    }
}