This program is supposed to load a bitmap and output it on the screen. However, the program compiles fine, but when it runs, it just shows a black screen, then exits with no errors.
I've traced the bug to "initDirect3D(hWnd, iWith, iHeight)" (in main.cpp). That's where the program ends.
What I'd like you to help me with is;
A: What did I do wrong? Why isn't this working.
B: Is there anything else I should change? Do you notice any pitfalls I might have stepped in or do you see any programming conventions that don't make sense?
If you need to know more about this project's setup just ask and I'll post some more details a.s.a.p
main.cpp
#include <windows.h>#include <tchar.h>#include <d3d9.h>#include <d3dx9.h>#include "direct3D.h"HINSTANCE hInst;HWND hWnd;int iWidth = 1280;int iHeight = 960;bool InitWindow(HINSTANCE hInstance, int iWidth, int iHeight);LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow){ if(!InitWindow(hInstance, iWidth, iHeight)){ return false; } if(!initDirect3D(hWnd, iWidth, iHeight)){ return false; } MSG msg = {0}; while(msg.message != WM_QUIT){ if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)){ TranslateMessage(&msg); DispatchMessage(&msg); } else { render(); } } cleanUp(); return (int) msg.wParam;}bool InitWindow(HINSTANCE hInstance, int iWidth, int iHeight){ WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = (WNDPROC)WndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = 0; wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wcex.lpszMenuName = NULL; wcex.lpszClassName = TEXT("DirectX 1"); wcex.hIconSm = 0; RegisterClassEx(&wcex); RECT rect = {0, 0, iWidth, iHeight}; AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, FALSE); hWnd = CreateWindow(TEXT("DirectX 1"), //class name TEXT("DirectX 1"), //window title WS_EX_TOPMOST | WS_POPUP | WS_VISIBLE, CW_USEDEFAULT, //initial x position CW_USEDEFAULT, //initial y postion iWidth, //width iHeight, //height NULL, //parent handle NULL, //menu handle hInstance, //instance handle NULL); if(!hWnd){ return false; } ShowWindow(hWnd, SW_SHOW); UpdateWindow(hWnd); return true;}LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){ switch(message){ case WM_KEYDOWN: switch(wParam){ case VK_ESCAPE: PostQuitMessage(0); break; } break; case WM_DESTROY: PostQuitMessage(0); break; } return DefWindowProc(hWnd, message, wParam, lParam);}
direct3D.h
LPDIRECT3D9 pD3D; //the Direct3d objectLPDIRECT3DDEVICE9 pd3dDevice; //the Direct3D deviceIDirect3DSurface9* surface;bool initDirect3D(HWND hWnd, int iWidth, int iHeight){ pD3D = NULL; pd3dDevice = NULL; HRESULT hResult; if(NULL == (pD3D = Direct3DCreate9(D3D_SDK_VERSION))){ return false; } D3DPRESENT_PARAMETERS d3dpp; ZeroMemory(&d3dpp, sizeof(d3dpp)); d3dpp.Windowed = FALSE; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8; d3dpp.BackBufferCount = 1; d3dpp.BackBufferWidth = iWidth; d3dpp.BackBufferHeight = iHeight; d3dpp.hDeviceWindow = hWnd; if(FAILED(pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_REF, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pd3dDevice))){ return false; } hResult = pd3dDevice->CreateOffscreenPlainSurface(iWidth, iHeight, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &surface, NULL); if(FAILED(hResult)){ return NULL; } hResult = D3DXLoadSurfaceFromFile(surface, NULL, NULL, L"test.bmp", NULL, D3DX_DEFAULT, 0, NULL); if(FAILED(hResult)){ return NULL; } return true;}void render(void){ IDirect3DSurface9* backbuffer = NULL; if(pd3dDevice == NULL){ return; } pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(62, 62, 68), 1.0f, 0); pd3dDevice->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &backbuffer); pd3dDevice->StretchRect(surface, NULL, backbuffer, NULL, D3DTEXF_NONE); pd3dDevice->Present(NULL, NULL, NULL, NULL);}void cleanUp(void){ if(pd3dDevice != NULL){ pd3dDevice->Release(); } if(pD3D != NULL){ pD3D->Release(); }}
thank you.
[Edited by - link102 on December 9, 2010 4:12:13 PM]