Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Joyal

Member Since 15 Mar 2013
Offline Last Active Jan 16 2014 06:20 AM

Topics I've Started

PeekMessage high cpu usage

17 December 2013 - 12:58 PM

Hello,

 

Im working making a 2D game from scracth using Directx 11 , Windows 8.1 , and Visual Studios 2012

 

I create a simple window and show some sprites, but i realized that my "Game" is using a lot of CPU Usage.

After debuggin i spotted that PeekMessage was the one causing this, i even try to remove all the code and just leave a Blank Window and still using a lot of CPU without anything, it uses between 15-30 % of CPU , this is my game loop

        MSG msg;
        ZeroMemory( &msg, sizeof(msg) );

        long lastTime = GetTickCount();
        while( msg.message!=WM_QUIT)
        {

            if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
            {
                TranslateMessage( &msg );
                DispatchMessage( &msg );
            }else{

                //Gametime Calc
                long deltaTime = GetTickCount() - lastTime;
                lastTime = GetTickCount();
            
                float gameTime = (float) deltaTime / 1000;

                Update(gameTime);
                Render(gameTime);
            }  
            //Sleep(1);
        }

you will notice that i have a commeted line "Sleep", if i add that line , the CPU usage go down to 0 - 0.7%  , but it drops the FPS count like 70%

 

i also tried remove everything

        MSG msg;
        ZeroMemory( &msg, sizeof(msg) );

        long lastTime = GetTickCount();
        while( msg.message!=WM_QUIT)
        {

            if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
            {
                TranslateMessage( &msg );
                DispatchMessage( &msg );
            }else{


            }  
        }

 and even without the IF statment , or any other code, just the blank window, and still using cpu about 15-30 %


[Directx 11] [2D] problem positioning a 2D quad , center starting at bottom left and n...

03 December 2013 - 06:50 AM

I'm following some tutorials of directx 11,and i was able to draw a quad, but my problem began when i was applying transforms , for some reason, my quad at position 0,0, start  at bottom left of my screen, but i want to it starts at Top Left , i don't figure out why
 
this happend when i used XMMatrixOrthographicOffCenterLH , but if i use XMMatrixOrthographicLH , it start at the center of the screen, im doing something wrong? 
 
here is some screenshots:
 
using XMMatrixOrthographicOffCenterLH : http://troll.ws/image/27213361
 
using XMMatrixOrthographicLH : http://troll.ws/image/ebf0dbc8
 
 
SpriteTest.h 

    #pragma once
    #ifndef JDX_SPRITE_TEST_H
    #define JDX_SPRITE_TEST_H


    //DX Includes
    #include <DirectXMath.h>
    using namespace DirectX;
   
    class SpriteTest{
    public:
     void Draw();
    private:
     struct ShaderParameters { XMMATRIX worldMatrix; };
     struct VERTEX { XMFLOAT3 Position; XMFLOAT4 Color; };  
    ...
    ...
    };
    #endif
SpriteTest.cpp
 
    #include "SpriteTest.h"
    using namespace DirectX;
    
    void SpriteTest::Initialize(){
    
     // load and compile the two shaders
     ID3D10Blob *VS = NULL, *PS = NULL;
    
     m_pRenderManager->CompileShader("Shaders/SpriteBatchVS.hlsl","main","vs_5_0",&VS);
     m_pRenderManager->CompileShader("Shaders/SpriteBatchPS.hlsl","main","ps_5_0",&PS);
    
    
     // encapsulate both shaders into shader objects
     m_pDevice->CreateVertexShader(VS->GetBufferPointer(), VS->GetBufferSize(), NULL, &m_pVertexShader);
     m_pDevice->CreatePixelShader(PS->GetBufferPointer(), PS->GetBufferSize(), NULL, &m_pPixelShader);
    
     // set the shader objects
     m_pDeviceContext->VSSetShader(m_pVertexShader, 0, 0);
     m_pDeviceContext->PSSetShader(m_pPixelShader, 0, 0);
    
     // create the input layout object
     D3D11_INPUT_ELEMENT_DESC ied[] =
     {
     {"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0},
     {"COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0},
     };
    
     m_pDevice->CreateInputLayout(ied, 2, VS->GetBufferPointer(), VS->GetBufferSize(), &m_pInputLayout);
     m_pDeviceContext->IASetInputLayout(m_pInputLayout);
    
    
     // __________ Init Vertex Buffer
    
     // create a triangle using the VERTEX struct
     /*
     1__3
     |\ |
     | \|
     0--2
    
     struct VERTEX { XMFLOAT3 Position; XMFLOAT4 Color; };  
     */
     VERTEX QuadVertices[] =
     {
     { XMFLOAT3(-1.0f, -1.0f, 0.0f), XMFLOAT4(1.0f, 0.0f, 0.0f, 1.0f)},
     { XMFLOAT3(-1.0f,  1.0f, 0.0f), XMFLOAT4(0.0f, 1.0f, 0.0f, 1.0f)},
     { XMFLOAT3( 1.0f, -1.0f, 0.0f), XMFLOAT4(0.0f, 0.0f, 1.0f, 1.0f)},
     { XMFLOAT3 (1.0f,  1.0f, 0.0f), XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f)}, 
    
     };
    
     // create the vertex buffer
     D3D11_BUFFER_DESC bd;
     ZeroMemory(&bd, sizeof(bd));
    
     bd.Usage = D3D11_USAGE_DYNAMIC;                // write access access by CPU and GPU
     bd.ByteWidth = sizeof(VERTEX) * 4;             // size is the VERTEX struct * 4
     bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;       // use as a vertex buffer
     bd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;    // allow CPU to write in buffer
    
     m_pDevice->CreateBuffer(&bd, NULL, &m_pVertexBuffer);       // create the buffer
    
     // copy the vertices into the buffer
     D3D11_MAPPED_SUBRESOURCE ms;
     m_pDeviceContext->Map(m_pVertexBuffer, NULL, D3D11_MAP_WRITE_DISCARD, NULL, &ms);    // map the buffer
     memcpy(ms.pData, QuadVertices, sizeof(QuadVertices));  // copy the data
     m_pDeviceContext->Unmap(m_pVertexBuffer, NULL);                                      // unmap the buffer
     
    
     // __________ Init Index Buffer
    
     WORD indices[] =
     {
     0,1,2,
     2,1,3,
     };
    
    
     D3D11_BUFFER_DESC indexDesc; 
     ZeroMemory( &indexDesc, sizeof( indexDesc ) );
     
     indexDesc.Usage = D3D11_USAGE_DEFAULT;
     indexDesc.ByteWidth = sizeof( WORD ) * 6;
     indexDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
     indexDesc.CPUAccessFlags = 0;
    
     D3D11_SUBRESOURCE_DATA indexData; 
     ZeroMemory( &indexData, sizeof( indexData ) ); 
     indexData.pSysMem = indices;
    
     m_pDevice->CreateBuffer( &indexDesc, &indexData, &m_pIndexBuffer );
    
    
     // __________ Init Constant Buffer
     D3D11_BUFFER_DESC constDesc; 
     ZeroMemory( &constDesc, sizeof( constDesc ) );
     
     constDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER; 
     constDesc.ByteWidth = sizeof( XMMATRIX ); 
     constDesc.Usage = D3D11_USAGE_DEFAULT;
     
     m_pDevice->CreateBuffer( &constDesc, 0, &m_pCostantBuffer );
    
    }
    
    void SpriteTest::Draw(){
     // select which vertex buffer to display
     
     UINT stride = sizeof(VERTEX);
     UINT offset = 0;
     m_pDeviceContext->IASetVertexBuffers(0, 1, &m_pVertexBuffer, &stride, &offset);
     
     // select which primtive type we are using
     m_pDeviceContext->IASetIndexBuffer( m_pIndexBuffer, DXGI_FORMAT_R16_UINT, 0 );
     m_pDeviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
    
     XMMATRIX view = XMMatrixIdentity(); 
     //XMMATRIX projection = XMMatrixOrthographicLH( 1024, 768, 0.0f, 100.0f );
     XMMATRIX projection  = XMMatrixOrthographicOffCenterLH( 0.0f,(float)m_pRenderManager->GetWidth(),0.0f,(float) m_pRenderManager->GetHeight(), 0.0f, 100.0f );
     XMMATRIX worldMatrix = XMMatrixMultiply( view, projection );
    
     ShaderParameters shaderParameters;
    
     // __________ Sprite World Coordinates
     float spriteWidth  = 32.0f,
       spriteHeight = 32.0f,
       spritePosX = 0.0f,
       spritePosY = 0.0f;
    
     XMMATRIX l_translation = XMMatrixTranslation(spritePosX, spritePosY, 0.0f ); 
     XMMATRIX l_rotationZ = XMMatrixRotationZ( 0.0f ); 
     XMMATRIX l_scale = XMMatrixScaling( 1.0f * spriteWidth,1.0f * spriteHeight, 1.0f );
     XMMATRIX l_spriteWVP  = l_translation * l_rotationZ * l_scale;
    
     // __________ Prepare World Coordinates to send to the shader
     XMMATRIX l_worldMatrix = XMMatrixMultiply( l_spriteWVP, worldMatrix ); 
     l_worldMatrix = XMMatrixTranspose( l_worldMatrix );
    
     shaderParameters.worldMatrix = l_worldMatrix;
     
     m_pDeviceContext->UpdateSubresource( m_pCostantBuffer, 0, NULL, &shaderParameters, 0, 0 );  
     m_pDeviceContext->VSSetConstantBuffers( 0, 1, &m_pCostantBuffer );
     
     m_pDeviceContext->DrawIndexed( 6, 0, 0 );
     
    }
vertex shader:
 
    cbuffer cbParameters : register( b0 ) { 
     float4x4 worldMatrix; 
    };
    
    struct VOut
    {
     float4 position : SV_POSITION;
     float4 color : COLOR;
    };
    
    VOut main(float4 position : POSITION, float4 color : COLOR)
    {
     VOut output;
    
     output.position = mul(position,worldMatrix);
     output.color = color;
    
     return output;
    }
 
pixel shader:
 
    float4 main(float4 position : SV_POSITION, float4 color : COLOR) : SV_TARGET
    {
     return color;
    }

 


Creating a simple 2d engine (C++), best way to create an Sprite class?

15 March 2013 - 07:28 AM

Since MS announced that they are not give more support to XNA , for me was an opportunity to start to learn DirectX , so i wanted to learn DX11,

i was following this tutorials 

 

http://www.rastertek.com/tutdx11.html

 

i want to build a simple 2D engine while im learning with basic thing,like render an sprite based on a spritesheet, ,animate it, scale, rotation, flip image

 

I started creating some simple basic structure, 

- Engine Class, this hold the initialization of the Window basically

- RenderManager class, this hold all the DX stuff, initialize it, create the swapchain, store the device , etc

 

So now my next step is create the Sprite class , i wanted to create an method called "Quad2D" inside my RenderManager, so i can use it on my render method in my sprite class

 

//This is not actual code, is just oh i want to approach this
class Sprite{

public void Render(RenderManager *rm){
  
    rm->Quad2D(Texture,Position,Width,Height);

}
}

i have some experience with OpenGL and Directx9 , it was easy to approach this, i just need to create an Array with the vertex, send to the VertexBuffer, bind the Texture, and done, 

 

but because now DX10 and DX11 not used fixed pipeline, and you need create this with more advanced stuff (Shaders),

i stopped in here http://www.rastertek.com/dx11tut11.html , i can see how to render the image and everything, but i was thinking, do i need to create an separate Class just to render a Quad? if so, this is going to compile the shader everytime is used?, well one solution maybe is just declare an static atribute for the ps and vs to share the same compile shader

 

so i want to hear your suggestion, how do you manage your Sprite class? 

 

if it necessary, i can attach my source code, but it doesnt has too much, just open a window

 

Note: i want to know how to do it in DX11, please dont reply, (used DX9,OpenGL,DX10, etc..)


PARTNERS