Direct3D Problem



  • Hallo, ich finde meinen Fehler im Programm nicht. Ich habe alles so gemacht, wie es in meinem Buch steht aber es kommt immer die Nachricht XXX.exe funktioniert nicht mehr.

    Kann mir jemand helfen? Vielen Dank im Voraus ūüôā

    #include <Windows.h>
    #include <d3d10.h>
    #include <d3dx10.h>
    
    #pragma comment(lib,"d3d10.lib")
    
    HWND hWnd;
    
    ID3D10Device *device;
    ID3D10RenderTargetView *rtv;
    IDXGISwapChain *swapChain;
    
    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
    
    void initD3D(HWND);
    void render_frame();
    void cleanD3D();
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR pszCmdLine, int showWnd) {
    	WNDCLASS wc;
    	wc.cbClsExtra = 0;
    	wc.cbWndExtra = 0;
    	wc.hbrBackground = (HBRUSH)(GetStockObject(WHITE_BRUSH));
    	wc.hCursor = LoadCursor(0, IDC_ARROW);
    	wc.hIcon = LoadIcon(0, IDC_ICON);
    	wc.hInstance = hInstance;
    	wc.lpfnWndProc = WndProc;
    	wc.lpszClassName = L"MyWindow";
    	wc.lpszMenuName = NULL;
    	wc.style = CS_HREDRAW | CS_VREDRAW;
    
    	RegisterClass(&wc);
    
    	hWnd = CreateWindow(L"MyWindow", L"Title", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 
    		CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, hInstance, 0);
    
    	ShowWindow(hWnd, showWnd);
    	UpdateWindow(hWnd);
    
    	MSG msg;
    
    	initD3D(hWnd);
    
    	while(true) {
    		if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
    			TranslateMessage(&msg);
    			DispatchMessage(&msg);
    			if(msg.message = WM_QUIT)
    				break;
    		}
    		render_frame();
    	}
    	while(GetMessage(&msg, NULL, 0, 0)) {
    		TranslateMessage(&msg);
    		DispatchMessage(&msg);
    	}
    
    	cleanD3D();
    
    	return msg.wParam;
    }
    
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
    	PAINTSTRUCT ps;
    	//HDC hdc;
    	//const wchar_t *text = L"Der Test";
    
    	switch(message) {
    		case WM_DESTROY:
    			PostQuitMessage(0);
    			return 0;
    		/*case WM_PAINT:
    			hdc = BeginPaint(hWnd, &ps);
    			TextOut(hdc, 30, 30, text, sizeof(text));
    			EndPaint(hWnd, &ps);
    			return 0;
    		case WM_CLOSE:
    			DestroyWindow(hWnd);
    			return 0;*/
    	}
    
    	return DefWindowProc(hWnd, message, wParam, lParam);
    }
    
    void initD3D(HWND hWnd) {
    	DXGI_SWAP_CHAIN_DESC scd;
    	ZeroMemory(&scd, sizeof(DXGI_SWAP_CHAIN_DESC));
    	scd.BufferCount = 1;
    	scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
    	scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
    	scd.OutputWindow = hWnd;
    	scd.SampleDesc.Count = 1;
    	scd.SampleDesc.Quality = 0;
    	scd.Windowed = TRUE;
    
    	D3D10CreateDeviceAndSwapChain(NULL, D3D10_DRIVER_TYPE_HARDWARE, NULL, 0,
    		D3D10_SDK_VERSION, &scd, &swapChain, &device);
    
    	ID3D10Texture2D *pBackBuffer = NULL;
    	swapChain->GetBuffer(0, __uuidof(ID3D10Texture2D), reinterpret_cast<void**>(pBackBuffer));
    	device->CreateRenderTargetView(pBackBuffer, NULL, &rtv);
    	pBackBuffer->Release();
    	device->OMSetRenderTargets(1, &rtv, NULL);
    
    	D3D10_VIEWPORT viewport;
    	ZeroMemory(&viewport, sizeof(D3D10_VIEWPORT));
    	viewport.TopLeftX = 0;
    	viewport.TopLeftY = 0;
    	viewport.Width = 800;
    	viewport.Height = 600;
    	device->RSSetViewports(1, &viewport);
    }
    
    void render_frame() {
    	device->ClearRenderTargetView(rtv, D3DXCOLOR(0.0f, 0.2f, 0.4f, 1.0f));
    	swapChain->Present(0, 0);
    }
    
    void cleanD3D() {
    	swapChain->Release();
    	rtv->Release();
    	device->Release();
    }
    


  • Du bist lustig, sag doch wenigstens in welcher Zeile das Programm abschmiert.



  • Und noch ein Tipp: Die DirectX-Typennamen √§ndern sich mit jeder Version. D.h. wenn du das ding mit DX11 kompilieren w√∂lltest m√ľsstest du die ganzen Sachen von Hand √§ndern. Mach lieber nen Header voller Typedefs, z.B. D3D10_VIEWPORT -> VIEWPORT. So musst du dann nur den Header √§ndern und nicht immer den ganzen Code durchsuchen.


Log in to reply