My DirectX program doesn't work. It gives me no errors, but if I run it, it gives me errors. What's wrong with it?
I do not write my mywin.h and basic.h here, because they are not important and work correctly. Procedures from those header files are Logi, MkWin and ReggWin and TPump, so do not be confused of them, just ignore them.
My code is here:
main.cpp:
Code:
#define WIN32_LEAN_AND_MEAN
#include <D3d9.h>
#include "mywin.h"
#include "basic.h"
#pragma comment(lib,"User32.lib")
#pragma comment(lib, "d3d9.lib")
WNDPROC wndProc;
HINSTANCE hInstance;
HWND _hwnd;
HRESULT hr;
IDirect3D9 *g_D3D=NULL;
D3DFORMAT format=D3DFMT_R5G6B5;
D3DPRESENT_PARAMETERS pp;
IDirect3DDevice9 *p_device=NULL;
char logi_name []= "error.txt";
char className [] = "WinClass";
char const caption [] = "Window";
int width=500;
int height=500;
int WINAPI WinMain (HINSTANCE hInst, HINSTANCE hPrevInst, char * cmdParam, int cmdShow)
{
wndProc=WindowProcedure;
//Registrating window class.
hr=ReggWin (wndProc, className, hInst);
if(FAILED(hr)){
Logi("Error, can't regg window class.",logi_name);
}
//Making Window.
hr=MkWin (caption, className, hInst, _hwnd, cmdShow, width, height);
if(FAILED(hr)){
Logi("Error, can't create window.",logi_name);
}
//Creating DirectX object
g_D3D = Direct3DCreate9( D3D_SDK_VERSION);
if(!g_D3D){
Logi("Error, can't create DirectX device.", logi_name);
}
ZeroMemory(&pp,sizeof(D3DPRESENT_PARAMETERS));
pp.BackBufferCount= 1; //We only need a single back buffer
pp.MultiSampleType=D3DMULTISAMPLE_NONE; //No multi-sampling
pp.MultiSampleQuality=0; //No multi-sampling
pp.SwapEffect = D3DSWAPEFFECT_DISCARD; // Throw away previous frames, we don't need them
pp.hDeviceWindow=_hwnd; //This is our main (and only) window
pp.Flags=0; //No flags to set
pp.FullScreen_RefreshRateInHz=D3DPRESENT_RATE_DEFAULT; //Default Refresh Rate
pp.PresentationInterval=D3DPRESENT_INTERVAL_DEFAULT; //Default Presentation rate
pp.BackBufferFormat=format; //Display format
pp.EnableAutoDepthStencil=FALSE; //No depth/stencil buffer
pp.Windowed = TRUE;
hr=g_D3D->CreateDevice(D3DADAPTER_DEFAULT, //The default adapter, on a multi-monitor system
//there can be more than one.
D3DDEVTYPE_HAL, //Use hardware acceleration rather than the software renderer
//Our Window
_hwnd,
//Process vertices in software. This is slower than in hardware,
//But will work on all graphics cards.
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
//Our D3DPRESENT_PARAMETERS structure, so it knows what we want to build
&pp,
//This will be set to point to the new device
&p_device);
if(FAILED(hr)){
Logi("Error, can't create DirectX device.", logi_name); //I get this error message!
}
//Message pump
TPump();
//Destroying DirectX object
if(g_D3D){
g_D3D->Release();
g_D3D=NULL;
}
}
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) {
case WM_DESTROY:
PostQuitMessage (0);
return 0;
}
return DefWindowProc (hwnd, message, wParam, lParam );
}