50cent Posted December 21, 2010 Report Posted December 21, 2010 (edited) Urmatorul program afiseaza o fereastra si initializaeaza directx 11#include <Windows.h>#include <d3d11.h>#include <d3dx11.h>#include <d3dx10.h>// librariile directx#pragma comment (lib, "d3d11.lib")#pragma comment (lib, "d3dx11.lib")#pragma comment (lib, "d3dx10.lib")// declarare functiivoid initializare(HWND);void eliberare(void);void deseneaza(void);LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);//functia de procesare mesaje windows/* variabile globale */IDXGISwapChain *swapchain; // pointer catre interfata swap chainID3D11Device *dev; // pointer catre interfata d3dID3D11DeviceContext *devcon; // pointer catre dispozitivul d3dID3D11RenderTargetView *backbuffer; // pointer catre backbuffer/* creare fereastra folosind functia WinMain */int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){ WNDCLASSEX clasa;//clasa windows folosita pentru afisarea ferestrei HWND hWnd;//fereastra principala DWORD stil=WS_OVERLAPPED|WS_SYSMENU|WS_MINIMIZEBOX;//stil fereastra , folositi WS_OVERLAPPEDWINDOW pentru redimensionare fereastra si buton de maximizare ZeroMemory(&clasa,sizeof(WNDCLASSEX));//seteaza NULL toti membrii WNDCLASSEX clasa.cbSize = sizeof(WNDCLASSEX);// dimensiunea structurii clasa.style = CS_HREDRAW | CS_VREDRAW;// redeseneaza fereastra daca a fost miscata clasa.lpfnWndProc = WindowProc;//functia de procesare mesaje declarata mai sus clasa.hInstance = hInstance;//instanta aplicatie clasa.hCursor = LoadCursor(NULL, IDC_ARROW);//cursor , puteti testa IDC_WAIT clasa.hbrBackground = (HBRUSH)COLOR_WINDOW;//culoare fundal fereastra clasa.lpszClassName = "directx11";//numele classei clasa.hIcon=LoadIcon(NULL,IDI_APPLICATION);//icon aplicatie, puteti testa IDI_INFORMATION RegisterClassEx(&clasa);//inregistrare clasa pt a putea fi folosita hWnd = CreateWindowEx(NULL,"directx11","primul program directx 11",stil,10,10,800,600,NULL,NULL,hInstance,NULL);//creare fereastra ShowWindow(hWnd, nCmdShow);//afisare fereastra MSG msg={0};//variabila care stocheaza mesajele windows initializare(hWnd);//apelam functia de initializare direct 3d cu parametru hWnd(fereastra principala) /* bucla principala a programului */ while( WM_QUIT != msg.message ) { if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )//daca avem mesaje care asteapta sa fie procesate { TranslateMessage( &msg );//traducem mesajul pentru a putea fi procesat de functia noastra DispatchMessage( &msg );// trimitem mesajul tradus functiei WindowProc } else { deseneaza();//altfel desenam un cadru pe ecran } } eliberare();//daca am ajuns aici inseamna ca am inchis aplicatia , deci eliberam directx return msg.wParam;}/* functia pentru procesare mesaje windows */LRESULT CALLBACK WindowProc(HWND hWnd, UINT mesaj, WPARAM wParam, LPARAM lParam){ switch(mesaj)//procesam mesaje(de ex. daca s-a apasat o tasta putem efectua anumite actiuni) { case WM_DESTROY://necesar pentu a iesi corect din aplicatie PostQuitMessage(0); break; default: return DefWindowProc(hWnd, mesaj, wParam, lParam);//mesajele care nu ne intereseaza le lasam in seama widows sa le proceseze break; } return 0;}void initializare(HWND hWnd)//functia de initializare d3d{ // declaram o structura care sa tina informatii despre swap and chain DXGI_SWAP_CHAIN_DESC scd; //setam NULL toti memmbri structurii declarate anterior ZeroMemory(&scd, sizeof(DXGI_SWAP_CHAIN_DESC)); // descriem structura scd.BufferCount = 1; // un back buffer scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; // folosim culoare pe 32 de biti scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; // metoda de utilizare swap and chain scd.OutputWindow = hWnd; // ferastra pe care von lucra scd.SampleDesc.Count = 4; // multisample scd.Windowed = TRUE; // vom lucra in window mode // creem interfata directx folosind informatiile de mai sus D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, NULL, NULL, NULL, D3D11_SDK_VERSION, &scd, &swapchain, &dev, NULL, &devcon); // adresa backbuffer ID3D11Texture2D *pBackBuffer; swapchain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&pBackBuffer); // folosim adreasa backbuffer pentru a seta unde va desena directx dev->CreateRenderTargetView(pBackBuffer, NULL, &backbuffer); pBackBuffer->Release(); // setam sa deseneze pe backbuffer devcon->OMSetRenderTargets(1, &backbuffer, NULL); // declaram viewport D3D11_VIEWPORT viewport; //setam NULL toti membrii viewport ZeroMemory(&viewport, sizeof(D3D11_VIEWPORT)); viewport.TopLeftX = 0;//coordonata x de unde incepe sa deseneze(coltul din stanga sus) viewport.TopLeftY = 0;//coordonata y de unde incepe sa deseneze(coltul din stanga sus) viewport.Width = 800;//latime viewport.Height = 600;//inaltime devcon->RSSetViewports(1, &viewport);//setam viewport}void eliberare(){ // eliberam memoria folosita si inchidem directx swapchain->Release(); dev->Release(); backbuffer->Release(); devcon->Release();}void deseneaza(){ // eliberam backbuffer si setam culoarea de background devcon->ClearRenderTargetView(backbuffer, D3DXCOLOR(0.0f, 0.3f, 0.4f, 1.0f)); /* aici se poate punde cod pentru desenare pe backbuffer */ // interschimbam backbuffer cu front buffer swapchain->Present(0, 0);} Edited December 21, 2010 by 50cent update Quote