Yet another question for u guys. I have compiled my code succesfully. BUT there is a flaw/bug the code that makes the window pop up and disappear again, in matter of 0,2 seconds. My debug out put said this on exiting:
The program '[5044] twoking.exe: Native' has exited with code 1 (0x1).
I assume that 0x1 it is correctly closed, i cant seem to find any code that would terminate the window. Disable a few snippets and keeps happening. And a thing i found out is that its is probably the D3D code, when in only used the WIN32 code in worked fine.
Here is my code:
CApplication.h
#ifndef capplication_h
#define capplication_h
#include"main.h"
class CApplication
{
public:
CApplication(void);
~CApplication(void);
//win32 stuff
void InitWindow(void);
void SaveSettings(void);
void LoadSettings(void);
void KillWindow(void);
//d3d stuff
void InitD3D(void);
void InitScene(void);
void CheckDeviceCaps(void);
bool CheckDevice(void);
void KillD3D(void);
//win32 prototypes
inline bool GetWindowStatus(void)
{
return m_bRunningWindow;
}
inline HWND GetWindowHandle(void)
{
return m_hWindow;
}
inline void SetWindowStatus(bool bRunningWindow)
{
m_bRunningWindow = bRunningWindow;
}
//d3d
inline bool GetD3DStatus(void)
{
return m_bRunningD3D;
}
inline LPDIRECT3DDEVICE9 GetDevice(void)
{
return m_pDirect3DDevice;
}
inline void SetD3DStatus(bool bRunningD3D)
{
m_bRunningD3D = bRunningD3D;
}
//win32
private:
bool m_bRunningWindow,
m_bRunningD3D;
HWND m_hWindow,
m_hBtnStart,
m_hBtnCancel,
m_hLblResolution,
m_hCbResolution,
m_hLblBackBuffer,
m_hCbBackBuffer,
m_hLblDepthStencil,
m_hCbDepthStencil,
m_hLblVertexProcessing,
m_hCbVertexProcessing,
m_hLblMultiSampling,
m_hCbMultiSampling,
m_hLblAnisotropy,
m_hCbAnisotropy;
LPDIRECT3D9 m_pDirect3DObject;
LPDIRECT3DDEVICE9 m_pDirect3DDevice;
D3DPRESENT_PARAMETERS m_PresentParameters;
D3DCAPS9 m_DeviceCaps;
DWORD m_dwWidth,
m_dwHeight,
m_dwVertexProcessing,
m_dwAnisotropy;
D3DFORMAT m_ColorFormat,
m_DepthStencilFormat;
D3DMULTISAMPLE_TYPE m_MultiSampling;
D3DXMATRIX m_matProjection;
float m_fAspectRatio,
m_fFieldOfView,
m_fNearPlane,
m_fFarPlane;
};
#endif
d3ddefs.h
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ | D3DFVF_DIFFUSE)
struct D3DVERTEX
{
float fX, fY,fZ;
DWORD dwColor;
};
MMain.h
#ifndef main_cpp
#define main_cpp
//Includes
#include<windows.h>
#include<commctrl.h>
#include<d3d9.h>
#include<d3dx9.h>
#include<fstream>
#include "capplication.h"
#include "d3ddefs.h"
//pragma
#pragma comment(lib,"d3d9.lib")
#pragma comment(lib,"d3dx9.lib")
#pragma comment(lib, "comctl32.lib")
//Constants
#define TITLE "My First app"
#define WINDOW_X 800
#define WINDOW_Y 600
//ids
#define ID_START 1
#define ID_CANCEL 2
//globals
extern CApplication g_App;
//prototypes
LRESULT CALLBACK WindowProcedure(HWND,UINT,WPARAM,LPARAM);
#endif
CApplication.cpp
#include "main.h"
CApplication::CApplication(void)
{
m_pDirect3DObject = NULL;
m_pDirect3DDevice = NULL;
m_dwWidth = 800;
m_dwHeight = 600;
m_ColorFormat = D3DFMT_R5G6B5;
m_DepthStencilFormat = D3DFMT_D16;
m_dwVertexProcessing = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
m_MultiSampling = D3DMULTISAMPLE_NONE;
m_dwAnisotropy = 0;
m_fFieldOfView = D3DX_PI / 4.0f;
m_fNearPlane = 1.0f;
m_fFarPlane = 1000.0f;
m_fAspectRatio = (float)m_dwWidth / (float)m_dwHeight;
m_bRunningWindow = true;
m_bRunningD3D = false;
InitWindow();
}
CApplication::~CApplication(void)
{
KillD3D();
KillWindow();
}
void CApplication::InitWindow(void)
{
WNDCLASSEX screen;
RECT size;
InitCommonControls();
GetClientRect(GetDesktopWindow(), &size);
screen.cbSize = sizeof(WNDCLASSEX);
screen.style = CS_HREDRAW | CS_VREDRAW;
screen.lpfnWndProc = WindowProcedure;
screen.cbClsExtra = 0;
screen.cbWndExtra = 0;
screen.hInstance = GetModuleHandle(NULL);
screen.hIcon = NULL;
screen.hCursor = NULL;
screen.hbrBackground = GetSysColorBrush(COLOR_BTNFACE);
screen.lpszMenuName = NULL;
screen.lpszClassName = "Classname";
screen.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
RegisterClassEx(&screen);
m_hWindow = CreateWindowEx(WS_EX_CONTROLPARENT,
"Classname",
TITLE,
WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_VISIBLE,
((1650 / 2) - WINDOW_X) + (WINDOW_X / 100 * 55) ,
((1050 / 2) - WINDOW_Y) + (WINDOW_Y / 100 * 45) ,
WINDOW_X,
WINDOW_Y,
NULL,
NULL,
GetModuleHandle(NULL),
NULL);
m_hLblResolution = CreateWindow("static",
"Hai:3",
WS_CHILD | WS_VISIBLE | SS_LEFT,
20,
20,
200,
18,
m_hWindow,
NULL,
(HINSTANCE)GetWindowLong(m_hWindow, GWL_HINSTANCE),
NULL);
SendMessage(m_hCbResolution,CB_ADDSTRING,0,(long)"640 x 480");
SendMessage(m_hCbResolution,CB_ADDSTRING,0,(long)"800 x 600");
LoadSettings();
}
void CApplication::InitD3D(void)
{
if((m_pDirect3DObject = Direct3DCreate9(D3D_SDK_VERSION)) == NULL)
{
MessageBox(m_hWindow, "[0x0]Failed to create Direct3D Object", "Init D3D", MB_OK);
m_bRunningD3D = false;
}
ZeroMemory(&m_PresentParameters, sizeof(m_PresentParameters));
m_PresentParameters.Windowed = false;
m_PresentParameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
m_PresentParameters.EnableAutoDepthStencil = true;
m_PresentParameters.AutoDepthStencilFormat = m_DepthStencilFormat;
m_PresentParameters.hDeviceWindow = m_hWindow;
m_PresentParameters.BackBufferWidth = m_dwWidth;
m_PresentParameters.BackBufferHeight = m_dwHeight;
m_PresentParameters.BackBufferFormat = m_ColorFormat;
m_PresentParameters.MultiSampleType = m_MultiSampling;
if(FAILED(m_pDirect3DObject->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,m_hWindow,m_dwVertexProcessing,&m_PresentParameters,&m_pDirect3DDevice)))
{
MessageBox(m_hWindow, "[0x1]Failed to create Direct3D Device", "Init D3D", MB_OK);
m_bRunningD3D = false;
}
ShowCursor(false);
InitScene();
CheckDeviceCaps();
}
void CApplication::InitScene(void)
{
D3DXMatrixPerspectiveFovLH(&m_matProjection, m_fFieldOfView,m_fAspectRatio,m_fNearPlane,m_fFarPlane);
m_pDirect3DDevice->SetTransform(D3DTS_PROJECTION,&m_matProjection);
m_pDirect3DDevice->SetRenderState(D3DRS_AMBIENT, RGB(255,255,255));
m_pDirect3DDevice->SetRenderState(D3DRS_LIGHTING, false);
m_pDirect3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
m_pDirect3DDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
m_pDirect3DDevice->SetFVF(D3DFVF_CUSTOMVERTEX);
for(unsigned i = 0;i < 8;++i)
{
m_pDirect3DDevice->SetSamplerState(i,D3DSAMP_MINFILTER,D3DTEXF_LINEAR);
m_pDirect3DDevice->SetSamplerState(i,D3DSAMP_MAGFILTER,D3DTEXF_LINEAR);
m_pDirect3DDevice->SetSamplerState(i,D3DSAMP_MIPFILTER,D3DTEXF_ANISOTROPIC);
m_pDirect3DDevice->SetSamplerState(i,D3DSAMP_MAXANISOTROPY,m_dwAnisotropy);
}
}
bool CApplication::CheckDevice(void)
{
switch(m_pDirect3DDevice->TestCooperativeLevel())
{
case D3DERR_DEVICELOST:
{
MessageBox(m_hWindow,"[0x2]Device has been lost","CheckDevice()",MB_OK);
return false;
}
case D3DERR_DEVICENOTRESET:
{
if(FAILED(m_pDirect3DDevice->Reset(&m_PresentParameters)))
{
MessageBox(m_hWindow,"[0x3]Device failed to reset","CheckDevice()",MB_OK);
return false;
}
return true;
}
default: return true;
}
}
void CApplication::CheckDeviceCaps(void)
{
//Do Nothing.
}
void CApplication::KillD3D(void)
{
if(m_pDirect3DDevice != NULL)
{
m_pDirect3DDevice->Release();
m_pDirect3DDevice = NULL;
}
if(m_pDirect3DObject != NULL)
{
m_pDirect3DObject->Release();
m_pDirect3DObject = NULL;
}
}
void CApplication::LoadSettings(void)
{
}
void CApplication::SaveSettings(void)
{
}
void CApplication::KillWindow(void)
{
UnregisterClass("ClassName",GetModuleHandle(NULL));
}
Main.cpp
#include "main.h"
//obj
CApplication g_App;
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPreviousInstance,LPSTR lpcmdline,int nCmdShow) {
MSG Message;
LPDIRECT3DVERTEXBUFFER9 pTriangleVB = NULL;
LPDIRECT3DVERTEXBUFFER9 pQuadVB = NULL;
VOID* pData;
D3DVERTEX aTriangle[] = {{-2.0f, 1.0f,10.0f,0xffff0000},{-3.0f,-1.0f,10.0f,0xff00ff00},{-1.0f,-1.0f,10.0f,0xff0000ff}};
D3DVERTEX aQuad[] = {{1.0f,-1.0f,10.0f,0xffffff00}, {3.0f,-1.0f,10.0f,0xffffff00}, {1.0f, 1.0f,10.0f,0xffffff00},{3.0f, 1.0f,10.0f,0xffffff00}};
g_App.GetDevice()->CreateVertexBuffer(sizeof(aTriangle),D3DUSAGE_WRITEONLY,D3DFVF_CUSTOMVERTEX,D3DPOOL_MANAGED,&pTriangleVB,NULL);
g_App.GetDevice()->CreateVertexBuffer(sizeof(aQuad),D3DUSAGE_WRITEONLY,D3DFVF_CUSTOMVERTEX,D3DPOOL_MANAGED,&pQuadVB,NULL);
pTriangleVB->Lock(0,sizeof(pData),(void**)&pData,0);
memcpy(pData, aTriangle, sizeof(aTriangle));
pTriangleVB->Unlock();
pQuadVB->Lock(0, sizeof(pData),(void**)&pData, 0);
memcpy(pData, aQuad, sizeof(aQuad));
pQuadVB->Unlock();
while(g_App.GetD3DStatus())
{
if(PeekMessage(&Message, NULL, 0,0 , PM_REMOVE))
{
TranslateMessage(&Message);
DispatchMessage(&Message);
}
if(g_App.CheckDevice())
{
g_App.GetDevice()->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,0), 1.0f, 0);
g_App.GetDevice()->BeginScene();
driehoek
g_App.GetDevice()->SetStreamSource(0,pTriangleVB,0,sizeof(D3DVERTEX));
g_App.GetDevice()->DrawPrimitive(D3DPT_TRIANGLELIST,0,1);
Quad
g_App.GetDevice()->SetStreamSource(0,pQuadVB,0,sizeof(D3DVERTEX));
g_App.GetDevice()->DrawPrimitive(D3DPT_TRIANGLESTRIP,0,2);
*/
g_App.GetDevice()->EndScene();
g_App.GetDevice()->Present(NULL,NULL,NULL,NULL);
}
}
return 1;
}//Winmain
Winproc.cpp
#include "main.h"
LRESULT CALLBACK WindowProcedure(HWND hWindow,UINT uMessage,WPARAM wparam, LPARAM lparam)
{
switch(uMessage)
{
case WM_COMMAND:
{
switch(LOWORD(wparam))
{
case ID_START:
{
g_App.SaveSettings();
g_App.SetWindowStatus(false);
g_App.SetD3DStatus(true);
SetFocus(hWindow);
g_App.InitD3D();
break;
}
case ID_CANCEL:
{
DestroyWindow(hWindow);
break;
}
}
return 0;
}
case WM_KEYDOWN:
{
switch(wparam)
{
case VK_ESCAPE:
{
DestroyWindow(hWindow);
break;
}
}
return 0;
}
case WM_DESTROY:
{
g_App.SetWindowStatus(false);
g_App.SetD3DStatus(false);
break;
}
}
return DefWindowProc(hWindow,uMessage,wparam,lparam);
} //WindowProcedure