Пример #1
0
void render()
{
   static RECT rc = {0, 0, 320, 100};   // rectangular region.. used for text drawing
   static DWORD frameCount = 0;
   static DWORD startTime = clock();
   char str[16];

   doMath();   // do the math.. :-P   
   
   frameCount++;   //  increment frame count
    
   // Clear the back buffer to a black... values r g b are 0-256
    lpD3DDevice8->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER ,D3DCOLOR_XRGB(256, 256, 256), 1.0f, 0);

   // render the cubes
   myRect1->render(clock());
   myRect2->render(clock());

   // this function writes a formatted string to a character string
   // in this case.. it will write "Avg fps" followed by the 
   // frames per second.. with 2 decimal places
   sprintf(str, "Avg fps %.2f", (float) frameCount / ((clock() - startTime) / 1000.0f));
      
   // draw the text string..
   lpD3DXFont->Begin();
   lpD3DXFont->DrawText(str, -1, &rc, DT_LEFT, 0xFFFFFFFF);
   lpD3DXFont->End();
   
    // present the back buffer.. or "flip" the page
    lpD3DDevice8->Present( NULL, NULL, NULL, NULL );   // these options are for using rectangular
    // regions for rendering/drawing...
    // 3rd is which target window.. NULL makes it use the currently set one (default)
    // last one is NEVER used.. that happens with DirectX often
}
Пример #2
0
void cGraphicsLayer::DrawTextString( int x, int y,
                                     DWORD color, const char * str )
{

    HRESULT r = 0;

    if( !m_pBackSurf )
        return;

    // Get a handle for the font to use
    HFONT hFont = (HFONT)GetStockObject( SYSTEM_FONT );

    LPD3DXFONT pFont = 0;
    // Create the D3DX Font
    r = D3DXCreateFont( m_pDevice, hFont, &pFont );
    if( FAILED( r ) )
        return;

    // Rectangle where the text will be located
    RECT TextRect = { x, y, 0, 0 };

    // Inform font it is about to be used
    pFont->Begin();

    // Calculate the rectangle the text will occupy
    pFont->DrawText( str, -1, &TextRect, DT_CALCRECT, 0 );

    // Output the text, left aligned
    pFont->DrawText( str, -1, &TextRect, DT_LEFT, color );

    // Finish up drawing
    pFont->End();

    // Release the font
    pFont->Release();

}
Пример #3
0
void render()
{
   static RECT rc = {0, 0, 320, 100};   // rectangular region.. used for text drawing
   static DWORD frameCount = 0;
   static DWORD startTime = clock();
   char str[16];
   DWORD val;

   doMath();   // do the math.. :-P   
   
   frameCount++;   //  increment frame count
    
   // Clear the back buffer to a black... values r g b are 0-256
   lpD3DDevice8->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(256, 256, 256), 1.0f, 0);
   
   lpD3DDevice8->GetRenderState(D3DRS_LIGHTING, &val);
   if (val)
   {
      D3DLIGHT8 light;                           // structure for light data
      ZeroMemory( &light, sizeof(D3DLIGHT8) );   // zero the mem
      light.Type = D3DLIGHT_POINT;               // set type, either SPOT, DIRECTIONAL, or POINT

      // light being put onto objects..
      light.Diffuse.r = 1.0f;
      light.Diffuse.g = 1.0f;
      light.Diffuse.b = 1.0f;
      // ambient light of the light source.. only on when light is on
      light.Ambient.r = 0.25f;
      light.Ambient.g = 0.25f;
      light.Ambient.b = 0.25f;

      // attenuation = Atten0 + Atten1 * d + Atten2 * d * d;
      // where d is distance
      // attenuation is decreasing brightness as 
      // a vertex is further away from the light source
      light.Attenuation0   = .5f;
      light.Attenuation1   = 0.10f;
      light.Attenuation2   = 0.01f;

      // spot lights and directional lights also have a direction
      // but a POINT light shines in all directions.. kinda like a light bulb
      
      light.Position = D3DXVECTOR3(0, 0, -2);

      light.Range = 15;   // after this range the light doesn't affect vertices
      lpD3DDevice8->SetLight( 0, &light );    // set the light as index 0
      lpD3DDevice8->LightEnable( 0, true );   // enable light at index 0
      
      // ambient light.. for everything.. not attached to 
      // this light, just lighting in general
      lpD3DDevice8->SetRenderState( D3DRS_AMBIENT, 0x00202020 );
   }

   // render the cubes
   myRect1->render(clock());
   myRect2->render(clock());
   myRect3->render(clock());
   myRect4->render(clock());
   myRect5->render(clock());

   // this function writes a formatted string to a character string
   // in this case.. it will write "Avg fps"  followed by the 
   // frames per second.. with 2 decimal places
   sprintf(str, "Avg fps %.2f", (float) frameCount / ((clock() - startTime) / 1000.0f));
      
   // draw the text string..
   lpD3DXFont->Begin();
   lpD3DXFont->DrawText(str, -1, &rc, DT_LEFT, 0xFFFFFFFF);
   lpD3DXFont->End();
   
   // present the back buffer.. or "flip" the page
   lpD3DDevice8->Present( NULL, NULL, NULL, NULL );   // these options are for using rectangular
      // regions for rendering/drawing...
      // 3rd is which target window.. NULL makes it use the currently set one (default)
      // last one is NEVER used.. that happens with DirectX often
}