Jump to content
  • Advertisement
Sign in to follow this  
MrSqweaky

C++ D3D9 window

This topic is 2502 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey GameDev'ers

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;
};
M
Main.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

Share this post


Link to post
Share on other sites
Advertisement
Moved.

The "twoking.exe: Native' has exited with code 1" is because WinMain() returns 1. I don't see anything at a glance that would cause the window to pop up then close, what does your debugger tell you? Does your app get as far as one render loop (I.e. does it hit the Clear() call in WinMain()?)

Share this post


Link to post
Share on other sites
Hello Steve,

I have been busy with some pragma messages this is final result:

1>------ Rebuild All started: Project: twoking, Configuration: Debug Win32 ------
1>Deleting intermediate and output files for project 'twoking', configuration 'Debug|Win32'

1>Compiling...
1>capplication.cpp
1>-Public CApplication prototypes - CApp.h
1>-Public inline functions - CApp.h
1>-Declarations -CApp.h
1>-D3DVERTEX Structure
1>Loading Main.h
1>Loading CApplication::CApplication
1>Loading CApplication::~CApplication
1>Loading InitWindow()
1>Loading InitD3D()
1>Loading InitScene
1>Loading KillD3D
1>Loading LoadSettings
1>Loading SaveSettinfs
1>Loadin KillWindow()
1>main.cpp
1>-Public CApplication prototypes
1>-Public inline functions
1>-Declarations
1>-D3DVERTEX Structure
1>Loading Main.h
1>winproc.cpp
1>-Public CApplication prototypes
1>-Public inline functions
1>-Declarations
1>-D3DVERTEX Structure
1>Loading Main.h
1>Loading WinProc
1>Case ID_Start
1>Case ID_CANCEL
1>Case Escapekey
1>Case WM_DESTROY
1>Generating Code...
1>Linking...
1>LINK : C:\Users\Alx\Documents\Visual Studio 2008\Projects\twoking\Debug\twoking.exe not found or not built by the last incremental link; performing full link
1>Embedding manifest...
1>Build log was saved at "file://c:\Users\Alx\Documents\Visual Studio 2008\Projects\twoking\twoking\Debug\BuildLog.htm"
1>twoking - 0 error(s), 0 warning(s)
========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========

Share this post


Link to post
Share on other sites
#pragma message is a message emitted during compilation, which isn't much use for what you're trying to use it for :)

You want to place breakpoints and use the debugger. I'd recommend having a read of This.

Share this post


Link to post
Share on other sites
The program stopped here in main.cpp


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);

Can u try compile the code, my compiler having serious issues when it comes down to debugging(crashes)

Share this post


Link to post
Share on other sites

Can u try compile the code, my compiler having serious issues when it comes down to debugging(crashes)


What compiler are you using? If you can't debug with it, you should either try to find a fix or migrate to a new IDE.
Apart from that, you can try to actually store HRESULT return codes and spit them back to yourself so you can use the error lookup tool to find out what's wrong.

EDIT: It seems as if you're following the D3D9 tuts from two-kings.de. Just useful information other people might want to know.

Share this post


Link to post
Share on other sites

The program stopped here in main.cpp
g_App.GetDevice()->CreateVertexBuffer(sizeof(aTriangle),D3DUSAGE_WRITEONLY,D3DFVF_CUSTOMVERTEX,D3DPOOL_MANAGED,&pTriangleVB,
Then one of two things is happening:
1. You have "Break on D3D Error" enabled in the DirectX Control Panel, and CreateVertexBuffer() is failing.
2. Your device pointer is invalid (Probably null) because CreateDevice() failed.

The debug output window will tell you exactly what the problem is.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!