i can't figure thst out please help!
why does my window close when i run it.
the code compiled and linked just fine
so why des it not open
here is the code
#include <windows.h> // windows header file
#include <d3d8.h> // direct 3D header file
// thease are constants for designing the window
const int WINDOW_HEIGHT = 640;
const int WINDOW_WIDTH = 480;
const int WINDOW_X = 0;
const int WINDOW_Y = 0;
const char *WINDOW_TITLE="Raja''s Window";
const char *WINDOW_CLASS_NAME="Raja''s Window";
// some variables for the handels to the window
HINSTANCE g_hInst;
HWND g_hWnd;
// another variable for fullscreen mode
bool g_bFullscreen;
// More variabels and pointers but these are for direct X
IDirect3D8 *g_pDirect3D;
IDirect3DDevice8 *g_pDevice;
// a vary important time and finger saver variabe
// for you techies its a variable to hold function call results
HRESULT g_hr;
//the first function for direct 3D and if statement
bool Direct3DInit()
{
g_pDirect3D=Direct3DCreate8(D3D_SDK_VERSION);
if (g_pDirect3D==NULL)
return false;
D3DDISPLAYMODE displayMode; // structure to hold display mode info
if(g_bFullscreen==false)
{
g_hr=g_pDirect3D->GetAdapterDisplayMode( D3DADAPTER_DEFAULT, &displayMode);
if(FAILED(g_hr))
return false;
}
else // stuff for Fullscreen Mode
{
displayMode.Width=WINDOW_WIDTH;
displayMode.Height=WINDOW_HEIGHT;
displayMode.RefreshRate=0;
displayMode.Format=D3DFMT_X8R8G8B8;
}
// this stuff is to tell direct 3D how to display stuff he he
D3DPRESENT_PARAMETERS presentParameters;
memset(&presentParameters, 0, sizeof(D3DPRESENT_PARAMETERS));
// this stuff is for the message box that comes up if the user says no then...
if(g_bFullscreen==false)
{
presentParameters.Windowed = TRUE; // set windowed to true
}
else // otherwise
{
presentParameters.Windowed = FALSE; // set windowed to false
}
// tells Direct 3D to display the graphics the quickest way
presentParameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
presentParameters.BackBufferFormat = displayMode.Format;
presentParameters.BackBufferWidth = displayMode.Width;
presentParameters.BackBufferHeight = displayMode.Height;
// create device
g_hr=g_pDirect3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, g_hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&presentParameters, &g_pDevice );
if(FAILED(g_hr)) // if it failed...
return false;
}
// next Function
void DrawScene()
{
//clear the screen
g_pDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0,0), 1.0f, 0 );
// make it so we can draw stuff
g_pDevice->EndScene();
// Tell it to display stuff on to the screen
// for techie put the stuff thats on the backbuffer onto the screen!
g_pDevice->Present(NULL, NULL, NULL, NULL);
}
void Direct3DRelease()
{
// release the device
if(g_pDevice)
g_pDevice->Release();
g_pDevice=NULL;
// release the Direct3D object
if(g_pDirect3D)
g_pDirect3D->Release();
g_pDirect3D=NULL;
}
// The Direct X Stuff ends HERE! BOO HOO!
// The Windows Message Processing Function
LRESULT CALLBACK WndProc(HWND wpHWnd,
UINT msg,
WPARAM wParam,
LPARAM lParam)
{
switch(msg)
{
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
} break;
default:break;
}
return DefWindowProc(wpHWnd, msg, wParam, lParam);
}
// the main windows function
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,
int nShowCmd)
{
// two very important veriables
WNDCLASSEX winClass;
MSG msg;
// set are global HINSTANCE to the one we get passed
g_hInst=hInstance;
// setup and register the window
winClass.cbSize = sizeof(WNDCLASSEX);
winClass.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
winClass.lpfnWndProc = WndProc;
winClass.cbClsExtra = 0;
winClass.cbWndExtra = 0;
winClass.hInstance = g_hInst;
winClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
winClass.hCursor = LoadCursor(NULL, IDC_ARROW);
winClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
winClass.lpszMenuName = NULL;
winClass.lpszClassName = WINDOW_CLASS_NAME;
winClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&winClass))
return 0;
// no wait this is the message box he he...oh boy
if(MessageBox(NULL, "Would you like to run in Fullscreen Mode?",
"Mode", MB_YESNO)==IDYES)
g_bFullscreen=true;
else
g_bFullscreen=false;
if(g_bFullscreen==false)
{
// if so than just windowed
g_hWnd = CreateWindowEx(WS_EX_CLIENTEDGE,
WINDOW_CLASS_NAME,
WINDOW_TITLE,
WS_SYSMENU | WS_BORDER | WS_CAPTION | WS_VISIBLE,
WINDOW_X, WINDOW_Y,
WINDOW_WIDTH,
WINDOW_HEIGHT,
NULL,
NULL,
g_hInst,
NULL);
}
else
{
// Now For Fullscreen )
g_hWnd = CreateWindowEx(NULL,
WINDOW_CLASS_NAME,
WINDOW_TITLE,
WS_POPUP |
WS_VISIBLE,
WINDOW_X, WINDOW_Y,
WINDOW_WIDTH,
WINDOW_HEIGHT,
NULL,
NULL,
g_hInst,
NULL);
}
// some error checking
if (!g_hWnd)
return 0;
if(Direct3DInit()==false)
{
Direct3DRelease();
return 0;
}
// oh no th infinte loop
while(1)
{
// windows message stuff
while(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
if (msg.message == WM_QUIT)
break;
}
if(msg.message==WM_QUIT)
break;
//quit if the user presses a key
if(GetAsyncKeyState(VK_ESCAPE))
PostQuitMessage(0);
//draw the scene each frame
DrawScene();
}
Direct3DRelease();
return 0;
}
i hope you guys can help
Sir Darkan Fireblade
in the Direct3DInit() function you don''t have a default return path. So:
if(FAILED(g_hr) ) // if it failed...
return false;
return true; // Add this default return
if(FAILED(g_hr) ) // if it failed...
return false;
return true; // Add this default return
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement