IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

DirectX Discussion :

Problème d'affichage d'un sprite en C++. [DirectX 10]


Sujet :

DirectX

  1. #1
    Membre à l'essai
    Inscrit en
    Avril 2007
    Messages
    17
    Détails du profil
    Informations forums :
    Inscription : Avril 2007
    Messages : 17
    Points : 21
    Points
    21
    Par défaut Problème d'affichage d'un sprite en C++.
    Bonjour à tous,

    Voila plusieurs semaines que j'essaye d'afficher 2 sprites avec des textures différentes.
    J'ai commencé par directX11 et en navigant sur le web et les forum, j'ai cru comprendre que directX10 avait des fonctionnalités très intéressantes quant à la création de sprites 2D.
    Je lis actuellement en ligne le livre Begining DIRECTX10 game programming et au chapitre 3, il y a un exemple que je m'évertue à reproduire

    Je pense ne pas avoir saisi certains éléments clefs car pas moyen d'obtenir le résultat escompté, à savoir, afficher au moins 2 sprites de textures différentes...

    Ma question porte donc bien sur DirectX 10.
    J'ai placé le code de l'exemple dans Visual studio express 10, je compile, pas de prolème de link, tout semble donc bien configurer.
    Je debug et je constate que mes objets textures, etc, sont bien rempli, le code ne part pas en erreur mais rien ne s'affiche si ce n'est qu'un écran noir.
    Je serais très très heureux d'avoir un peu d'aide afin de me mettre le pied à l'étrier

    Voici le code, ma foi assez simple, init de windows, init de directX, créations des éléments clefs : device, textures, projection, view port et pour finir le render.
    Aurais je omis quelques chose? (la texture est bien présente dans le répertoire et le chargement s'effectue bien(texture != NULL est ok)

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
     
     
    // Include the Windows header file, needed for all Windows applications
    #include <windows.h>
    #include <tchar.h>
    #include <d3d10.h>
    #include <d3dx10.h>
     
    //Attention pour le linker il faut rajouter dans projet/propriétés/editeur de lien/entrée  lib supplémentaire D3D10.lib
    // Direct3D global vars
    ID3D10Device* pD3DDevice = NULL;
    IDXGISwapChain* pSwapChain = NULL;
    ID3D10RenderTargetView* pRenderTargetView = NULL;
    ID3DX10Sprite *spriteObject = NULL;
    // Create a new sprite variable
    D3DX10_SPRITE testSprite;
    D3D10_VIEWPORT viewPort;
    HINSTANCE hInst; // global handle to hold the application instance
    HWND wndHandle; // global variable to hold the window handle
    int width = 640;
    int height = 480;
    // forward declarations
    bool InitWindow( HINSTANCE hInstance, int width, int height );
    LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM );
     
     
    /*******************************************************************
    * InitDirect3D
    * Initializes Direct3D
    * Inputs - Parent window handle - HWND,
    Window width - int
    Window height - int
    Updating the Code 31
    * Outputs - true if successful, false if failed - bool
    *******************************************************************/
    bool InitDirect3D(HWND hWnd, int width, int height)
    {
    		// Create the clear the DXGI_SWAP_CHAIN_DESC structure
    		DXGI_SWAP_CHAIN_DESC swapChainDesc;
    		ZeroMemory(&swapChainDesc, sizeof(swapChainDesc));
    		// Fill in the needed values
    		swapChainDesc.BufferCount = 1;
    		swapChainDesc.BufferDesc.Width = width;
    		swapChainDesc.BufferDesc.Height = height;
    		swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
    		swapChainDesc.BufferDesc.RefreshRate.Numerator = 60;
    		swapChainDesc.BufferDesc.RefreshRate.Denominator = 1;
    		swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
    		swapChainDesc.OutputWindow = hWnd;
    		swapChainDesc.SampleDesc.Count = 1;
    		swapChainDesc.SampleDesc.Quality = 0;
    		swapChainDesc.Windowed = TRUE;
    		// Create the D3D device and the swap chain
    		HRESULT hr = D3D10CreateDeviceAndSwapChain(NULL,
    		D3D10_DRIVER_TYPE_REFERENCE,
    		NULL,
    		0,
    		D3D10_SDK_VERSION,
    		&swapChainDesc,
    		&pSwapChain,
    		&pD3DDevice);
    		// Error checking. Make sure the device was created
    		if (hr != S_OK)
    		{
    		return false;
    		}
    		// Get the back buffer from the swapchain
    		ID3D10Texture2D *pBackBuffer;
    		hr = pSwapChain->GetBuffer(0, __uuidof(ID3D10Texture2D), (LPVOID*)
    		&pBackBuffer);
    		if (hr != S_OK)
    		{
    			return false;
     
    		}
    		// create the render target view
    		hr = pD3DDevice->CreateRenderTargetView(pBackBuffer, NULL,
    		&pRenderTargetView);
    		// release the back buffer
    		pBackBuffer->Release();
    		// Make sure the render target view was created successfully
    		if (hr != S_OK)
    		{
    		return false;
    		}
    		// set the render target
    		pD3DDevice->OMSetRenderTargets(1, &pRenderTargetView, NULL);
    		// create and set the viewport
     
    		viewPort.Width = width;
    		viewPort.Height = height;
    		viewPort.MinDepth = 0.0f;
    		viewPort.MaxDepth = 1.0f;
    		viewPort.TopLeftX = 0;
    		viewPort.TopLeftY = 0;
    		pD3DDevice->RSSetViewports(1, &viewPort);
    		return true;
    }
     
    /*******************************************************************
    * Render
    * All drawing happens in the Render function
    * Inputs - void
    * Outputs - void
    *******************************************************************/
    void Render()
    {
     
    	if (pD3DDevice != NULL)
    	{
    		// clear the target buffer
    		pD3DDevice->ClearRenderTargetView(pRenderTargetView, D3DXCOLOR (0.0f,
    		0.0f, 0.0f, 0.0f));
    		// start drawing the sprites
    		spriteObject->Begin(D3DX10_SPRITE_SORT_TEXTURE);
    		// Draw all the sprites
    		if(spriteObject->DrawSpritesImmediate(&testSprite, 1, 0, 0)!=S_OK)
    		{
     
    			int flag=0;
    		}
    		// Finish up and send the sprites to the hardware
    		spriteObject->End();
     
    		// display the next item in the swap chain
    		pSwapChain->Present(0, 0);
    	}
     
    }
     
    /*******************************************************************
    * ShutdownDirect3D
    * Closes down and releases the resources for Direct3D
     
    * Inputs - void
    * Outputs - void
    *******************************************************************/
    void ShutdownDirect3D()
    {
    	// release the rendertarget
    	if (pRenderTargetView)
    	{
    		pRenderTargetView->Release();
    	}
    	// release the swapchain
    	if (pSwapChain)
    	{
    		pSwapChain->Release();
    	}
    	// release the D3D Device
    	if (pD3DDevice)
    	{
    		pD3DDevice->Release();
    	}
    }
     
    /*******************************************************************
    * GetTexture2DFromFile
    * Loads a texture from a file into a ID3D10Texture2D object
    * Inputs - LPCSTR the path and filename of the texture
    * Outputs - pointer to an ID3D10Texture2D object
    *******************************************************************/
    ID3D10Texture2D* GetTexture2DFromFile(LPCWSTR filename)
    {
    		ID3D10Texture2D* texture2D = NULL;
    		ID3D10Resource* pD3D10Resource = NULL;
    		// Loads the texture into a temporary ID3D10Resource object
    		HRESULT hr = D3DX10CreateTextureFromFile(pD3DDevice,filename,NULL,NULL,&pD3D10Resource,NULL);
    		// Make sure the texture was loaded in successfully
    		if (FAILED(hr))
    		{
    			return NULL;
    		}
     
    		// Translates the ID3D10Resource object into a ID3D10Texture2D object
    		pD3D10Resource ->QueryInterface(__uuidof( ID3D10Texture2D),
    		(LPVOID*)&texture2D);
    		pD3D10Resource ->Release();
    		// returns the ID3D10Texture2D object
    		return texture2D;
    }
     
    // This is winmain, the main entry point for Windows applications
    int APIENTRY _tWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,LPTSTR lpCmdLine, int nCmdShow )
    {
    	// Initialize the window
    	if ( !InitWindow( hInstance, width, height ) )
    	{
    	return false;
    	}
     
    	// called after creating the window
    	if ( !InitDirect3D( wndHandle, width, height) )
    	{
    		return 0;
    	}
     
     
    	LPCWSTR filename=L"Textures/Alpha.png";
     
    	ID3D10Texture2D*  texture=GetTexture2DFromFile(filename);
    	D3D10_SHADER_RESOURCE_VIEW_DESC SRVDesc;
    	if (texture != NULL)
    	{
    		// Get the texture details
    		D3D10_TEXTURE2D_DESC desc;
    		texture->GetDesc( &desc );
    		D3D10_RESOURCE_DIMENSION rr;
     
    		texture->GetType(&rr);
     
    		// Create a shader resource view of the texture
     
    		// Clear out the shader resource view description structure
    		ZeroMemory( &SRVDesc, sizeof(SRVDesc) );
     
    		// Set the texture format
    		SRVDesc.Format = desc.Format;
    		// Set the type of resource
    		SRVDesc.ViewDimension = D3D10_SRV_DIMENSION_TEXTURE2D;
    		SRVDesc.Texture2D.MipLevels = desc.MipLevels;
     
     
    	}
     
    	ID3D10ShaderResourceView *gSpriteTextureRV = NULL;
        pD3DDevice->CreateShaderResourceView(texture, &SRVDesc, &gSpriteTextureRV);
     
     
     
    	// Set the sprite’s shader resource view
    	testSprite.pTexture = gSpriteTextureRV;
    	// top-left location in U,V coords
    	testSprite.TexCoord.x = 0;
    	testSprite.TexCoord.y = 0;
    	// Determine the texture size in U,V coords
    	testSprite.TexSize.x = 1.0f;
    	testSprite.TexSize.y = 1.0f;
    	// Set the texture index. Single textures will use 0
    	testSprite.TextureIndex = 0;
    	// The color to apply to this sprite, full color applies white.
    	testSprite.ColorModulate = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
     
     
    	// create the sprite object
    	HRESULT hr = D3DX10CreateSprite(pD3DDevice, 0, &spriteObject);
    	// Make sure the sprite creation was successful
    	if (hr != S_OK)
    	{
    	// Handle failure
    	}
     
    	// spriteObject contains a valid ID3DX10Sprite object
    	if (!spriteObject)
    	{
    	spriteObject->Release();
    	spriteObject = NULL;
    	}
     
     
    	//Projection
    	D3DXMATRIX matProjection;
    	// Create the projection matrix using the values in the viewport
    	D3DXMatrixOrthoOffCenterLH(&matProjection,
    	(float)viewPort.TopLeftX,
    	(float)viewPort.Width,
    	(float)viewPort.TopLeftY,
    	(float)viewPort.Height,
    	0.1f,
    	10);
    	// Set the projection matrix
    	hr = spriteObject->SetProjectionTransform(&matProjection);
     
    	// these variables describe the dimensions of the sprite
    	// and where it should be located.
    	float spritePosX = 0;
    	float spritePosY = 0;
    	float spriteWidth = 120;
    	float spriteHeight = 158;
    	// The translation matrix to be created
    	D3DXMATRIX matTranslation;
    	// Create the translation matrix
    	D3DXMatrixTranslation( &matTranslation,spritePosX,spritePosY,0.1f);
     
    	// Scale the sprite to its correct width and height
    	D3DXMATRIX matScaling;
    	D3DXMatrixScaling(&matScaling, spriteWidth, spriteHeight, 1.0f );
    	//testSprite.matWorld = (matScaling * matTranslation);
     
    	// main message loop:
    	MSG msg = {0};
    	while (WM_QUIT != msg.message)
    	{
    		while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) == TRUE)
    		{
    			TranslateMessage(&msg);
    			DispatchMessage(&msg);
    		}
    	// Additional game logic can be called from here
    		Render();
    	}
    	return (int) msg.wParam;
    }
     
     
     
     
    /*******************************************************************
    * InitWindow
    * Inits and creates and main app window
    * Inputs - application instance - HINSTANCE
    Window width - int
    Window height - int
    * Outputs - true if successful, false if failed - bool
    *******************************************************************/
    bool InitWindow(HINSTANCE hInstance, int width, int height)
    {
    	WNDCLASSEX wcex;
    	// Fill in the WNDCLASSEX structure. This describes how the window
    	// will look to the system
    	wcex.cbSize = sizeof(WNDCLASSEX); // the size of the structure
    	wcex.style = CS_HREDRAW | CS_VREDRAW; // the class style
    	wcex.lpfnWndProc = (WNDPROC)WndProc; // the window procedure callback
    	wcex.cbClsExtra = 0; // extra bytes to allocate for this class
    	wcex.cbWndExtra = 0; // extra bytes to allocate for this instance
    	wcex.hInstance = hInstance; // handle to the application instance
    	wcex.hIcon = 0; // icon to associate with the application
    	wcex.hCursor = LoadCursor(NULL, IDC_ARROW); // the default cursor to use
    	wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); // the background color
    	wcex.lpszMenuName = NULL; // the resource name for the menu
    	wcex.lpszClassName = TEXT("DirectXExample"); // the class name being created
    	wcex.hIconSm = 0; // the handle to the small icon
    	RegisterClassEx(&wcex);
     
    	// Resize the window
    	RECT rect = { 0, 0, width, height };
    	AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, FALSE);
     
    	// create the window from the class above
    	wndHandle = CreateWindow(TEXT("DirectXExample"),
    	TEXT("DirectXExample"),
    	WS_OVERLAPPEDWINDOW,
    	CW_USEDEFAULT,
    	CW_USEDEFAULT,
    	rect.right - rect.left,
    	rect.bottom - rect.top,
    	NULL,
    	NULL,
    	hInstance,
    	NULL);
     
    	if (!wndHandle)
    	{
    		return false;
    	}
     
    	// Display the window on the screen
    	ShowWindow(wndHandle, SW_SHOW);
    	UpdateWindow(wndHandle);
    	return true;
    }
     
    /*******************************************************************
    * WndProc
    * The main window procedure for the application
    * Inputs - application window handle - HWND
    message sent to the window - UINT
    wParam of the message being sent - WPARAM
    lParam of the message being sent - LPARAM
    * Outputs - LRESULT
    *******************************************************************/
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    		// Check any available messages from the queue
    		switch (message)
    		{
    			// Allow the user to press the Escape key to end the application
    			case WM_KEYDOWN:
    				switch(wParam)
     
    				{
    				// Check if the user hit the Escape key
    				case VK_ESCAPE:
    					PostQuitMessage(0);
    					break;
    				}
    			break;
    			// The user hit the close button, close the application
    			case WM_DESTROY:
    				PostQuitMessage(0);
    			break;
    		}
    		// Always return the message to the default window procedure for further
     
    		return DefWindowProc(hWnd, message, wParam, lParam);
    }
    Un grand merci à vous.

    Gilles

  2. #2
    Responsable 2D/3D/Jeux


    Avatar de LittleWhite
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Mai 2008
    Messages
    26 858
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Mai 2008
    Messages : 26 858
    Points : 218 577
    Points
    218 577
    Billets dans le blog
    120
    Par défaut
    Bonjour,

    Pouvons nous avoir une capture du rendu ? Pour savoir sur quel type de bogue nous devons nous pencher en lisant le code.
    De plus, je vous conseillerai de passer à DirectX 11, car DirectX 10 est plus ou moins mis à l'écart (c'était une sorte de version de transition).
    Vous souhaitez participer à la rubrique 2D/3D/Jeux ? Contactez-moi

    Ma page sur DVP
    Mon Portfolio

    Qui connaît l'erreur, connaît la solution.

  3. #3
    Inactif  

    Homme Profil pro
    Ingénieur test de performance
    Inscrit en
    Décembre 2003
    Messages
    1 986
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 49
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Ingénieur test de performance
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Décembre 2003
    Messages : 1 986
    Points : 2 605
    Points
    2 605
    Par défaut
    Bonjour.

    Peut-on avoir la texture en lien. Le nom Alpha.png me laisse dubitatif...

    Citation Envoyé par LittleWhite Voir le message
    Bonjour,

    Pouvons nous avoir une capture du rendu ? Pour savoir sur quel type de bogue nous devons nous pencher en lisant le code.
    De plus, je vous conseillerai de passer à DirectX 11, car DirectX 10 est plus ou moins mis à l'écart (c'était une sorte de version de transition).
    Bonjour LittleWhite.

    Il faut quand même signaler que DirectX11 casse pas mal de chose (ce qui est compréhensible), et que du coup, plein de fonctionnalités présentes dans Dx9 et 10 ont disparu, comme la gestion des polices :


    Il y a d'autres choses, et même si Dx11 est plus performant et plus compatible, il nécessite de faire plus de chose à la main.

    Dx9 et 10 ont des fonctionnalités intéressantes pour un débutant et même en production. Surtout pour de la projection ortho avec sprite. Je ne pense pas que Dx11 soit spécialement pertinent dans cette situation. Même si dans l'idéal, il faut promouvoir la dernière API en cours. Mais comme celle-ci n'est pas aussi mature que les autres, le choix n'est pas aussi évident je pense.

  4. #4
    Responsable 2D/3D/Jeux


    Avatar de LittleWhite
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Mai 2008
    Messages
    26 858
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Mai 2008
    Messages : 26 858
    Points : 218 577
    Points
    218 577
    Billets dans le blog
    120
    Par défaut
    Je suis entièrement d'accord avec vous, moldavi, mais c'est surtout lorsque j'ai lu ça :
    J'ai commencé par directX11 et en navigant sur le web et les forum, j'ai cru comprendre que directX10 avait des fonctionnalités très intéressantes quant à la création de sprites 2D.
    Mais oui, DirectX9/10 était plus simple, autant que OpenGL 1/2 par rapport à OpenGL 3, mais, il y a un moment, il faut faire le pas (et non revenir en arrière).
    Après, il y a un demoscener qui nous donne son avis (par rapport à ses besoins) sur le choix de la bibliothèque : http://jeux.developpez.com/tutoriels...-ergon-4k/#LII . J'avais cru comprendre qu'il y avait Direct2D/DirectWrite avec DirectX 11 ?
    Vous souhaitez participer à la rubrique 2D/3D/Jeux ? Contactez-moi

    Ma page sur DVP
    Mon Portfolio

    Qui connaît l'erreur, connaît la solution.

  5. #5
    Membre à l'essai
    Inscrit en
    Avril 2007
    Messages
    17
    Détails du profil
    Informations forums :
    Inscription : Avril 2007
    Messages : 17
    Points : 21
    Points
    21
    Par défaut Le lien vers ma texture
    Bonjour,

    Comme demandé, voici un lien vers ma texture : http://www.imagine-chr.fr/images/Alpha.png

    De même, je n'obtiens que la fenêtre windows dont j'ai fait la capture d'écran : http://www.imagine-chr.fr/images/resultat_dx10.png

    Sur le fond, j'avais pensé et dailleurs investi dans un livre sur DirectX11(Begining DirectX11 Game programming) et en surfant sur le web, j'avais cru comprendre qu'il était plus approprié d'utiliser DirectX10 en ce sens ou le dev 2D, dont les sprites étaient plus facilement exploitable.
    Je suis sûr que le problème que je rencontre est dû à une mauvaise interprétation de ma part sur ce qu'il faut faire
    J'ai trouvé pas mal d'infos sur la création de Sprites, j'avais réussi à créer une série de 4 sprites(mais tous identiques dans la texture) en faisant une compilation sur un exemple tout fait.
    OR, rien de mieux pour comprendre que de se lancer par bout de code simples certes, mais sans pratiquer du copier collé systématique.
    Je me suis basé sur un exemple de Begining DirectX10 et je compte le modifier afin d'afficher 2 sprites différents. Mais bon, je serais déjà heureux de faire fonctionner le premier code .

    Merci pour votre aide.

    Cordialement,

    Gilles

  6. #6
    Inactif  

    Homme Profil pro
    Ingénieur test de performance
    Inscrit en
    Décembre 2003
    Messages
    1 986
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 49
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Ingénieur test de performance
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Décembre 2003
    Messages : 1 986
    Points : 2 605
    Points
    2 605
    Par défaut
    Bonjour.

    Je pose juste le code, il y a un problème de BlendState, il faut le positionner au niveau du device. Et aussi un problème de transformation :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
     
     
    #pragma comment(lib, "d3d10")
    #pragma comment(lib, "d3dx10")
     
    // Include the Windows header file, needed for all Windows applications
    #include <windows.h>
    #include <tchar.h>
    #include <d3d10.h>
    #include <d3dx10.h>
     
    //Attention pour le linker il faut rajouter dans projet/propriétés/editeur de lien/entrée  lib supplémentaire D3D10.lib
    // Direct3D global vars
    ID3D10Device* pD3DDevice = NULL;
    IDXGISwapChain* pSwapChain = NULL;
    ID3D10RenderTargetView* pRenderTargetView = NULL;
    ID3DX10Sprite *spriteObject = NULL;
    // Create a new sprite variable
    D3DX10_SPRITE testSprite;
    D3D10_VIEWPORT viewPort;
    HINSTANCE hInst; // global handle to hold the application instance
    HWND wndHandle; // global variable to hold the window handle
    int width = 640;
    int height = 480;
    // forward declarations
    bool InitWindow( HINSTANCE hInstance, int width, int height );
    LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM );
     
     
    /*******************************************************************
    * InitDirect3D
    * Initializes Direct3D
    * Inputs - Parent window handle - HWND,
    Window width - int
    Window height - int
    Updating the Code 31
    * Outputs - true if successful, false if failed - bool
    *******************************************************************/
    bool InitDirect3D(HWND hWnd, int width, int height){
     
    		// Create the clear the DXGI_SWAP_CHAIN_DESC structure
    		DXGI_SWAP_CHAIN_DESC swapChainDesc;
    		ZeroMemory(&swapChainDesc, sizeof(swapChainDesc));
    		// Fill in the needed values
    		swapChainDesc.BufferCount = 1;
    		swapChainDesc.BufferDesc.Width = width;
    		swapChainDesc.BufferDesc.Height = height;
    		swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
    		swapChainDesc.BufferDesc.RefreshRate.Numerator = 60;
    		swapChainDesc.BufferDesc.RefreshRate.Denominator = 1;
    		swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
    		swapChainDesc.OutputWindow = hWnd;
    		swapChainDesc.SampleDesc.Count = 1;
    		swapChainDesc.SampleDesc.Quality = 0;
    		swapChainDesc.Windowed = TRUE;
     
    		// Create the D3D device and the swap chain
    		HRESULT hr = D3D10CreateDeviceAndSwapChain(NULL, D3D10_DRIVER_TYPE_REFERENCE, NULL, 0, D3D10_SDK_VERSION, &swapChainDesc, &pSwapChain, &pD3DDevice);
     
    		// Error checking. Make sure the device was created
    		if (hr != S_OK){
    				return false;
    		}
     
    		// Get the back buffer from the swapchain
    		ID3D10Texture2D *pBackBuffer;
     
    		hr = pSwapChain->GetBuffer(0, __uuidof(ID3D10Texture2D), (LPVOID*)&pBackBuffer);
     
    		if(hr != S_OK){
    				return false;
    		}
     
    		// create the render target view
    		hr = pD3DDevice->CreateRenderTargetView(pBackBuffer, NULL, &pRenderTargetView);
     
    		// release the back buffer
    		pBackBuffer->Release();
     
    		// Make sure the render target view was created successfully
    		if(hr != S_OK){
    				return false;
    		}
     
    		// set the render target
    		pD3DDevice->OMSetRenderTargets(1, &pRenderTargetView, NULL);
     
    		// create and set the viewport 
    		viewPort.Width = width;
    		viewPort.Height = height;
    		viewPort.MinDepth = 0.0f;
    		viewPort.MaxDepth = 1.0f;
    		viewPort.TopLeftX = 0;
    		viewPort.TopLeftY = 0;
     
    		pD3DDevice->RSSetViewports(1, &viewPort);
     
    		return true;
    }
     
    /*******************************************************************
    * Render
    * All drawing happens in the Render function
    * Inputs - void
    * Outputs - void
    *******************************************************************/
    void Render(){
     
    		HRESULT hr;
     
    		if(pD3DDevice != NULL){
     
    				// clear the target buffer
    				pD3DDevice->ClearRenderTargetView(pRenderTargetView, D3DXCOLOR (0.0f, 0.0f, 0.0f, 0.0f));
     
    				// start drawing the sprites
    				hr = spriteObject->Begin(D3DX10_SPRITE_SORT_TEXTURE);
     
    				// Draw all the sprites
    				if((hr = spriteObject->DrawSpritesImmediate(&testSprite, 1, 0, 0)) !=S_OK){
     
    						int flag=0;
    				}
     
    				// Finish up and send the sprites to the hardware
    				hr = spriteObject->End();
     
    				// display the next item in the swap chain
    				hr = pSwapChain->Present(0, 0);
    		}
    }
     
    /*******************************************************************
    * ShutdownDirect3D
    * Closes down and releases the resources for Direct3D
     
    * Inputs - void
    * Outputs - void
    *******************************************************************/
    void ShutdownDirect3D()
    {
    	// release the rendertarget
    	if (pRenderTargetView)
    	{
    		pRenderTargetView->Release();
    	}
    	// release the swapchain
    	if (pSwapChain)
    	{
    		pSwapChain->Release();
    	}
    	// release the D3D Device
    	if (pD3DDevice)
    	{
    		pD3DDevice->Release();
    	}
    }
     
    /*******************************************************************
    * GetTexture2DFromFile
    * Loads a texture from a file into a ID3D10Texture2D object
    * Inputs - LPCSTR the path and filename of the texture
    * Outputs - pointer to an ID3D10Texture2D object
    *******************************************************************/
    ID3D10Texture2D* GetTexture2DFromFile(LPCWSTR filename)
    {
    		ID3D10Texture2D* texture2D = NULL;
    		ID3D10Resource* pD3D10Resource = NULL;
    		// Loads the texture into a temporary ID3D10Resource object
    		HRESULT hr = D3DX10CreateTextureFromFile(pD3DDevice,filename,NULL,NULL,&pD3D10Resource,NULL);
    		// Make sure the texture was loaded in successfully
    		if (FAILED(hr))
    		{
    			return NULL;
    		}
     
    		// Translates the ID3D10Resource object into a ID3D10Texture2D object
    		hr = pD3D10Resource ->QueryInterface(__uuidof( ID3D10Texture2D), (LPVOID*)&texture2D);
    		pD3D10Resource ->Release();
     
    		// returns the ID3D10Texture2D object
    		return texture2D;
    }
     
    // This is winmain, the main entry point for Windows applications
    int APIENTRY _tWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,LPTSTR lpCmdLine, int nCmdShow )
    {
    	// Initialize the window
    	if ( !InitWindow( hInstance, width, height ) )
    	{
    	return false;
    	}
     
    	// called after creating the window
    	if ( !InitDirect3D( wndHandle, width, height) )
    	{
    		return 0;
    	}
     
     
    	LPCWSTR filename=L"Textures/Alpha.png";
     
    	ID3D10Texture2D*  texture=GetTexture2DFromFile(filename);
    	D3D10_SHADER_RESOURCE_VIEW_DESC SRVDesc;
    	if (texture != NULL)
    	{
    		// Get the texture details
    		D3D10_TEXTURE2D_DESC desc;
    		texture->GetDesc( &desc );
    		D3D10_RESOURCE_DIMENSION rr;
     
    		texture->GetType(&rr);
     
    		// Create a shader resource view of the texture
     
    		// Clear out the shader resource view description structure
    		ZeroMemory( &SRVDesc, sizeof(SRVDesc) );
     
    		// Set the texture format
    		SRVDesc.Format = desc.Format;
    		// Set the type of resource
    		SRVDesc.ViewDimension = D3D10_SRV_DIMENSION_TEXTURE2D;
    		SRVDesc.Texture2D.MipLevels = desc.MipLevels;
     
     
    	}
     
    	ID3D10ShaderResourceView *gSpriteTextureRV = NULL;
        pD3DDevice->CreateShaderResourceView(texture, &SRVDesc, &gSpriteTextureRV);
     
     
     
    	// Set the sprite’s shader resource view
    	testSprite.pTexture = gSpriteTextureRV;
    	// top-left location in U,V coords
    	testSprite.TexCoord.x = 0;
    	testSprite.TexCoord.y = 0;
    	// Determine the texture size in U,V coords
    	testSprite.TexSize.x = 1.0f;
    	testSprite.TexSize.y = 1.0f;
    	// Set the texture index. Single textures will use 0
    	testSprite.TextureIndex = 0;
    	// The color to apply to this sprite, full color applies white.
    	testSprite.ColorModulate = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
     
    	// these variables describe the dimensions of the sprite
    	// and where it should be located.
    	float spritePosX = 300;
    	float spritePosY = 300;
    	float spriteWidth = 120;
    	float spriteHeight = 158;
    	// The translation matrix to be created
    	D3DXMATRIX matTranslation;
    	// Create the translation matrix
    	D3DXMatrixTranslation( &matTranslation,spritePosX,spritePosY,0.1f);
     
    	// Scale the sprite to its correct width and height
    	D3DXMATRIX matScaling;
    	D3DXMatrixScaling(&matScaling, spriteWidth, spriteHeight, 1.0f );
    	testSprite.matWorld = (matScaling * matTranslation);
     
     
    	// create the sprite object
    	HRESULT hr = D3DX10CreateSprite(pD3DDevice, 0, &spriteObject);
    	// Make sure the sprite creation was successful
    	if (hr != S_OK)
    	{
    	// Handle failure
    	}
     
    	// spriteObject contains a valid ID3DX10Sprite object
    	if (!spriteObject)
    	{
    	spriteObject->Release();
    	spriteObject = NULL;
    	}
     
     
    	//Projection
    	D3DXMATRIX matProjection;
    	// Create the projection matrix using the values in the viewport
    	D3DXMatrixOrthoOffCenterLH(&matProjection,
    	(float)viewPort.TopLeftX,
    	(float)viewPort.Width,
    	(float)viewPort.TopLeftY,
    	(float)viewPort.Height,
    	0.1f,
    	10);
    	// Set the projection matrix
    	hr = spriteObject->SetProjectionTransform(&matProjection);
     
    	//D3DXMATRIX matView;
    	//hr = spriteObject->SetViewTransform(&matView);
     
    	ID3D10BlendState* m_pFontBlendState10 = NULL;
    	D3D10_BLEND_DESC StateDesc;
     ZeroMemory( &StateDesc, sizeof( D3D10_BLEND_DESC ) );
     StateDesc.AlphaToCoverageEnable = FALSE;
     StateDesc.BlendEnable[0] = TRUE;
     StateDesc.SrcBlend = D3D10_BLEND_SRC_ALPHA;
     StateDesc.DestBlend = D3D10_BLEND_INV_SRC_ALPHA;
     StateDesc.BlendOp = D3D10_BLEND_OP_ADD;
     StateDesc.SrcBlendAlpha = D3D10_BLEND_ZERO;
     StateDesc.DestBlendAlpha = D3D10_BLEND_ZERO;
     StateDesc.BlendOpAlpha = D3D10_BLEND_OP_ADD;
     StateDesc.RenderTargetWriteMask[0] = 0xf;
     hr = pD3DDevice->CreateBlendState( &StateDesc, &m_pFontBlendState10 );
     
    	FLOAT NewBlendFactor[4] = {0,0,0,0};
    	pD3DDevice->OMSetBlendState( m_pFontBlendState10, NewBlendFactor, 0xffffffff );
     
    	// main message loop:
    	MSG msg = {0};
    	while (WM_QUIT != msg.message)
    	{
    		while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) == TRUE)
    		{
    			TranslateMessage(&msg);
    			DispatchMessage(&msg);
    		}
    	// Additional game logic can be called from here
    		Render();
    	}
    	return (int) msg.wParam;
    }
     
     
     
     
    /*******************************************************************
    * InitWindow
    * Inits and creates and main app window
    * Inputs - application instance - HINSTANCE
    Window width - int
    Window height - int
    * Outputs - true if successful, false if failed - bool
    *******************************************************************/
    bool InitWindow(HINSTANCE hInstance, int width, int height)
    {
    	WNDCLASSEX wcex;
    	// Fill in the WNDCLASSEX structure. This describes how the window
    	// will look to the system
    	wcex.cbSize = sizeof(WNDCLASSEX); // the size of the structure
    	wcex.style = CS_HREDRAW | CS_VREDRAW; // the class style
    	wcex.lpfnWndProc = (WNDPROC)WndProc; // the window procedure callback
    	wcex.cbClsExtra = 0; // extra bytes to allocate for this class
    	wcex.cbWndExtra = 0; // extra bytes to allocate for this instance
    	wcex.hInstance = hInstance; // handle to the application instance
    	wcex.hIcon = 0; // icon to associate with the application
    	wcex.hCursor = LoadCursor(NULL, IDC_ARROW); // the default cursor to use
    	wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); // the background color
    	wcex.lpszMenuName = NULL; // the resource name for the menu
    	wcex.lpszClassName = TEXT("DirectXExample"); // the class name being created
    	wcex.hIconSm = 0; // the handle to the small icon
    	RegisterClassEx(&wcex);
     
    	// Resize the window
    	RECT rect = { 0, 0, width, height };
    	AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, FALSE);
     
    	// create the window from the class above
    	wndHandle = CreateWindow(TEXT("DirectXExample"),
    	TEXT("DirectXExample"),
    	WS_OVERLAPPEDWINDOW,
    	CW_USEDEFAULT,
    	CW_USEDEFAULT,
    	rect.right - rect.left,
    	rect.bottom - rect.top,
    	NULL,
    	NULL,
    	hInstance,
    	NULL);
     
    	if (!wndHandle)
    	{
    		return false;
    	}
     
    	// Display the window on the screen
    	ShowWindow(wndHandle, SW_SHOW);
    	UpdateWindow(wndHandle);
    	return true;
    }
     
    /*******************************************************************
    * WndProc
    * The main window procedure for the application
    * Inputs - application window handle - HWND
    message sent to the window - UINT
    wParam of the message being sent - WPARAM
    lParam of the message being sent - LPARAM
    * Outputs - LRESULT
    *******************************************************************/
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    		// Check any available messages from the queue
    		switch (message)
    		{
    			// Allow the user to press the Escape key to end the application
    			case WM_KEYDOWN:
    				switch(wParam)
     
    				{
    				// Check if the user hit the Escape key
    				case VK_ESCAPE:
    					PostQuitMessage(0);
    					break;
    				}
    			break;
    			// The user hit the close button, close the application
    			case WM_DESTROY:
    				PostQuitMessage(0);
    			break;
    		}
    		// Always return the message to the default window procedure for further
     
    		return DefWindowProc(hWnd, message, wParam, lParam);
    }

    EDIT

    Lors de la première initialisation de la carte graphique, celle-ci possède un état par défaut. Pour utiliser l'alpha blending, il faut lui dire. Dans ce cas là c'est CreateBlendState qui s'en charge.

    Ensuite j'ai dé-commenté cette ligne et je l'ai placé en amont :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    testSprite.matWorld = (matScaling * matTranslation);
    Il faut aussi transformer la position du sprite.

    Une dernière remarque, j'ai ajouté :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    ID3D10BlendState* m_pFontBlendState10
    Il faut le Release, d'ailleurs j'ai l'impression que ShutdownDirect3D n'est pas utilisée.

  7. #7
    Membre à l'essai
    Inscrit en
    Avril 2007
    Messages
    17
    Détails du profil
    Informations forums :
    Inscription : Avril 2007
    Messages : 17
    Points : 21
    Points
    21
    Par défaut Merci beaucoup!!
    Bonjour,

    Un grand merci, cela fonctionne beaucoup mieux en effet

    Je vais donc regarder les specs de CreateBlendState afin de comprendre et d'apprendre

    Pour la fonction ShutDown j'ai bien saisi l'importance de vider les objets mais en effet, j'étais surtout tétanisé de ne rien voir à l'écran lors d'un test.

    Merci encore pour votre aide efficace et formatrice.

    Cordialement,

    Gilles

  8. #8
    Inactif  

    Homme Profil pro
    Ingénieur test de performance
    Inscrit en
    Décembre 2003
    Messages
    1 986
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 49
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Ingénieur test de performance
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Décembre 2003
    Messages : 1 986
    Points : 2 605
    Points
    2 605
    Par défaut
    Bonjour.

    Citation Envoyé par LittleWhite Voir le message
    J'avais cru comprendre qu'il y avait Direct2D/DirectWrite avec DirectX 11 ?
    Désolé, je n'avais pas bien vu la question...

    En effet, il y a Direct2D/DirectWrite pour faire du texte sous DX11. Mais tu dois ajouter l'interopérabilité avec DX11 dans le code, ce qui est un travail supplémentaire.

    Avec DX9, la gestion des fontes est intégrée. Quelques lignes de code donc, qui répondent souvent à pas mal de besoin. Et pour quelqu'un qui veut s"amuser à faire de la 2D, DX9 est encore une API de choix.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Problème d'affichage de Sprite
    Par Nizar89 dans le forum SFML
    Réponses: 5
    Dernier message: 17/04/2012, 08h59
  2. Problème d'affichage avec trace
    Par WriteLN dans le forum Flash
    Réponses: 10
    Dernier message: 22/10/2003, 16h59
  3. [Kylix] problème d'affichage
    Par scalvi dans le forum EDI
    Réponses: 1
    Dernier message: 18/06/2003, 10h07
  4. Réponses: 1
    Dernier message: 06/03/2003, 11h57
  5. probléme d'affichage d'une fiche
    Par sb dans le forum Composants VCL
    Réponses: 7
    Dernier message: 29/08/2002, 09h43

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo