// Adriaan Larmuseau
// Sylvarant Studios
// Project-Mage version 0.1
//------------------------------------------------------------------------------
// WINDOW SOURCE CODE VERSION 0.1
//------------------------------------------------------------------------------
// fullscreen global var
#define fullscreen FALSE
//include Window header
#include <Window.h>
#include "Error_check.h"
// set function so we can use it in WNDCLASSEX
LRESULT WINAPI MainMessageProcedure(HWND handle,UINT message,WPARAM wParam,LPARAM lParam);
// set constructor
Window::Window() {
// initializeer alle variablen
window_handle = NULL;
paused = FALSE;
active = TRUE;
alive = TRUE;
windowed = FALSE;
window_height = 600;
window_width = 700;
strcpy(window_class,"Project_Mage");
strcpy(window_title,"Project_Mage Version 0.1");
pointer_interfaced3d = NULL;
pointer_deviced3d = NULL;
// store recieved messages
ZeroMemory(&structure_present, sizeof(D3DPRESENT_PARAMETERS));
ZeroMemory(&structure_device, sizeof(D3DDEVICE_CREATION_PARAMETERS));
}
// set destructor
Window::~Window(){
// get rid of the interfaces using our macro
safe_release(pointer_deviced3d);
safe_release(pointer_interfaced3d);
/*
if (window_handle)
DestroyWindow(window_handle);
UnregisterClass(window_class, GetModuleHandle(NULL)); */
}
// set create window function
BOOL Window::Create_window(int f_width , int f_height , BOOL in_window){
Error_check * check2 = new Error_check("window can create");
//create structure for window properties
WNDCLASSEX wc;
// set size
wc.cbSize = sizeof(WNDCLASSEX);
// set window style
wc.style = CS_CLASSDC;
// set message handler
wc.lpfnWndProc = MainMessageProcedure;
// set extra bytes for structure
wc.cbClsExtra = 0;
// set extra bytes for window instance
wc.cbWndExtra = 0;
// set unique HINSTANCE
wc.hInstance = GetModuleHandle(NULL);
// set cursor
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
// set background
wc.hbrBackground = NULL;
// set windows Icon
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
// set Icon Sm
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
// set class
wc.lpszClassName = window_class;
// set menu
wc.lpszMenuName = NULL;
// register structuur
RegisterClassEx(&wc);
//if not in window mode go to fullscreen
if (!in_window){
f_width = GetSystemMetrics(SM_CXFULLSCREEN);
f_height = GetSystemMetrics(SM_CYFULLSCREEN);
}
// create window
window_handle = CreateWindow(window_class,window_title,((in_window)?WS_OVERLAPPEDWINDOW:WS_POPUPWINDOW),
300,200,f_width,f_height,NULL,NULL,wc.hInstance,NULL);
// check for valid hanlde
if(window_handle == NULL){
Error_check * check3 = new Error_check("Valid Handle Error");
return FALSE;}
// windows shows window
ShowWindow(window_handle,SW_SHOW);
// windows updates window
UpdateWindow(window_handle);
// windows shows the cursor
ShowCursor(TRUE);
return TRUE;
// end create_window function
}
// initialize direct 3d
BOOL Window::InitializeD3D(D3DDEVICE_CREATION_PARAMETERS dcp, BOOL windowed_d3d, int width, int height){
Error_check * check4 = new Error_check("Initializing D3D");
// structure to put the desktop settings
D3DDISPLAYMODE structure_desktop;
// store if our application is fullscreen
windowed = windowed_d3d;
// store device creation paramaters
structure_device.hFocusWindow = dcp.hFocusWindow;
structure_device.AdapterOrdinal = dcp.AdapterOrdinal;
structure_device.DeviceType = dcp.DeviceType;
structure_device.BehaviorFlags = dcp.BehaviorFlags;
// initialize the d3d interface
pointer_interfaced3d = Direct3DCreate9(D3D_SDK_VERSION);
// check if p_i is NULL
if(pointer_interfaced3d == NULL){
Error_check * check5 = new Error_check("Pointer interface is NULL");
return FALSE;}
// set hardware settings in structure
if(FAILED(pointer_interfaced3d -> GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&structure_desktop))){
Error_check * check6 = new Error_check("failed to get hardware settings");
return FALSE;}
// fill our present structure with info
// set backbuffer
structure_present.BackBufferCount = 1;
// set swap effect
structure_present.SwapEffect = D3DSWAPEFFECT_DISCARD;
// Multisample Type
structure_present.MultiSampleType = D3DMULTISAMPLE_NONE;
// draw 3d pixels
structure_present.EnableAutoDepthStencil = TRUE;
// set format pixels
structure_present.AutoDepthStencilFormat = D3DFMT_D16;
// now will split up in window or fullscreen
if (windowed){
// backbuffer width
structure_present.BackBufferWidth = window_width;
// backbuffer height
structure_present.BackBufferHeight = window_height;
// backbuffer format
structure_present.BackBufferFormat = structure_desktop.Format;
// window mode ?
structure_present.Windowed = TRUE;
}
else {
// backbuffer width
structure_present.BackBufferWidth = structure_desktop.Width;
// backbuffer height
structure_present.BackBufferHeight = structure_desktop.Height;
// backbuffer format
structure_present.BackBufferFormat = structure_desktop.Format;
// window mode ?
structure_present.Windowed = FALSE;
// backbuffer swap rate
structure_present.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
// refresh rate
structure_present.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
}
// create device
if(FAILED(pointer_interfaced3d->CreateDevice(dcp.AdapterOrdinal, dcp.DeviceType,dcp.hFocusWindow,dcp.BehaviorFlags,
&structure_present,&pointer_deviced3d))){
Error_check * check7 = new Error_check("failed to create device");
return FALSE;}
// end function initialize d3d
return TRUE;
}
// set function to see if the device hasn't been lost yet
BOOL Window::IsDeviceThere(){
// variable to store the result
HRESULT hresult;
// check state of the device
hresult = pointer_deviced3d ->TestCooperativeLevel();
// set false if check failed
if(FAILED(hresult))
return FALSE;
// end function
return TRUE;
}
// erase data
BOOL Window::ResetDevice(){
// use reset function to clear structure
if(FAILED(pointer_deviced3d->Reset(&structure_present))){
Error_check * check8 = new Error_check("failed to erase data");
return FALSE;}
// end function
return TRUE;
}
// check if erasing data is possible
BOOL Window::CanReset(){
HRESULT hresult;
// test pointer_device
hresult = pointer_deviced3d->TestCooperativeLevel();
// if TCL returns D3DERR_DEVICENOTRESET we can erase
if(hresult == D3DERR_DEVICENOTRESET)
return TRUE;
// end function
return FALSE;
}
// set the execute function
void Window::Run(){
Error_check * check9 = new Error_check("run 1");
// create window
if(!Create_window(window_width,window_height,fullscreen?FALSE:TRUE))
return;
// pass all variables to function that will initialize D3D
D3DDEVICE_CREATION_PARAMETERS dcp;
// use default graphics card
dcp.AdapterOrdinal = D3DADAPTER_DEFAULT;
// 3D card does as much as possible
dcp.DeviceType = D3DDEVTYPE_HAL;
// the handle to our window
dcp.hFocusWindow = window_handle;
// T&L is done in Hardware
dcp.BehaviorFlags = D3DCREATE_HARDWARE_VERTEXPROCESSING;
// call initialize
if(!InitializeD3D(dcp, fullscreen?FALSE:TRUE, window_width, window_height))
return;
// call initialize function
if(!FirstInitialize())
return;
// go into loop
Loop();
// call clean function
FinalCleanup();
}
// set the loop
void Window::Loop(){
// store recieved messages
MSG message_store;
ZeroMemory(&message_store, sizeof(MSG));
// create loop to continue window display
while (message_store.message != WM_QUIT && alive != FALSE){
// use peak message to see if there's a message
if (PeekMessage(&message_store,NULL,0,0,PM_REMOVE)){
// translate the message
TranslateMessage(&message_store);
// send to message handler
DispatchMessage(&message_store);}
else {
// if we are in else there's no message and we can render
if(active) {
// check if device is there before rendering
if(IsDeviceThere()){
if(!paused){
// call prerender and render
if(!PreRender())
break;
if(!Render())
break;
// close paused and is device there
}}
// if device is not there
else {
// check if we can reset
if(CanReset()) {
// call function to release
InvalidateObjects();
// reset
if(!ResetDevice())
return;
// restore device objects
RestoreObjects();
}
// and all open if's and elses + function
}}}}}
// message handler
LRESULT WINAPI Window::MessageProcedure(HWND handle,UINT message,WPARAM wParam,LPARAM lParam){
// message contains all messages sent through
switch(message) {
// when not clicking our window
case WM_ACTIVATE:
switch(wParam){
// aplication is activate
case WA_ACTIVE:
// set active to TRUE to start rendering again
active = TRUE;
break;
// when clicking in the window
case WA_CLICKACTIVE:
// set active to TRUE to start rendering again
active = TRUE;
break;
// aplication is no longer selected
case WA_INACTIVE:
// set active to false
active = FALSE;
break;
// end switch wParam
}return 0;
//end case WM_Activate
break;
// window is being destroyed
case WM_DESTROY:
PostQuitMessage(0);
return 0;
// end case WM_DESTROY
break;
// change size window (maximize,minimize)
case WM_SIZE:
switch(wParam){
// window is minimized
case SIZE_MINIMIZED:
// de-activate
active = FALSE;
break;
// window is restored
case SIZE_RESTORED:
// set active to TRUE to start rendering again
active = TRUE;
break;
// window is maximized
case SIZE_MAXIMIZED:
// window is active
active = TRUE;
break;
// end switch wParam
}return 0;
// end case WM_SIZE
break;
// Key is pressed
case WM_KEYDOWN:
switch(wParam){
// escape key is pressed
case VK_ESCAPE:
PostQuitMessage(0);
break;
// pause key is pressed
case VK_PAUSE:
// change value pause to unpause or pause
paused = !paused;
break;
// end switch
}return 0;
// end case WM_KEYDOWN
break;
// send all other messages to the default message handler
default: return DefWindowProc(handle,message, wParam, lParam);
// end switvh (message)
}
// end message handler
}
// end
Failed to create D3d Device
I've been trying to create a window using DirectX but the D3D device doesn't seem to work
thanx in advance
In order to find out what is going wrong, you need to use the D3D Debug runtimes. These will tell you exactly why the device creation has failed. If you don't know what the debug runtimes are (or how to use them), check out the FAQ.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement