Code:
//------------------------------------------
//Fireup a basic d3d window
//reference: beginning game programming
//------------------------------------------
//Headers
#include <windows.h>
#include <d3d9.h>
#include <time.h>
//Application Title
#define APPTITLE "Fire up Direct-X"
//Forward declarations( prototypes )
LRESULT WINAPI WinProc(HWND,UINT,WPARAM,LPARAM);
ATOM MyRegisterClass(HINSTANCE);
//own Funtions
int App_Init (HWND);
void App_Run (HWND);
void App_Close (HWND);
//Global Variables (d3d)
LPDIRECT3D9 d3d = NULL;
LPDIRECT3DDEVICE9 d3ddev = NULL;
D3DPRESENT_PARAMETERS d3dpp;
////////////////////WINDOWS(window) FUNCTIONS/////////////////////////
//window event callback function
LRESULT WINAPI Winproc (HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
switch(msg)
{
case WM_DESTROY:
App_Close(hWnd);
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd,msg,wParam,lParam);
}
//window properties
ATOM MyRegisterClass(HINSTANCE hInstance)
{
//create window class (properties)structure
WNDCLASSEX wc;
wc.cbSize = sizeof(WNDCLASSEX);
//fill the structure (define properties)
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC)WinProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = NULL;
wc.hCursor = LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = APPTITLE;
wc.hIconSm = NULL;
//set up the window with the info
return RegisterClassEx(&wc);
}
//Entry point for a Windows program (actual APP launch)
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
//Declare function variables
MSG msg;
HWND hWnd;
int done = 0;
//register the class
MyRegisterClass(hInstance);
//Create the base window
hWnd = CreateWindow(
APPTITLE,
APPTITLE,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
500,
400,
NULL,
NULL,
hInstance,
NULL);
//check for an error during creation
if (!hWnd)
return FALSE;
//actually display the window
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
//Initialize this app (App_Init)
if (!App_Init(hWnd))
return 0;
//////THIS IS THE LOOP/////
while (!done)
{
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
//react when quit message comes by
if (msg.message == WM_QUIT)
{
//dispay an exit message
MessageBox(hWnd,"Received WM_QUIT message", "WinMain",MB_OK);
done = 1;
}
//decode and send message over to WndProc
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
//THE APP LOOP(in else to preven keep running after closing
App_Run(hWnd);
}
return (int)msg.wParam;
}
//end
////////////////////END OF WINDOWS FUNCTIONS/////////////////////////
////////////////////Continue With DirectX///////////////////////////
//App_init function
int App_Init(HWND hWnd)
{
//Declare some vars
d3d = Direct3DCreate9(D3D_SDK_VERSION);
//display an init message
MessageBox(hWnd,"Program is about to start!!","Game_Init",MB_OK);
//initialize DIRECT 3D
if (d3d == NULL)
{
MessageBox(hWnd,"Error at initialize D3D","Error App_Init",MB_OK);
return 0;
}
//Set D3Dpp ,vica vi directx options of the App
ZeroMemory(&d3dpp,sizeof(d3dpp));
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
//create the d3d device
d3d->CreateDevice(
D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp,
&d3ddev);
if(d3ddev == NULL)
{
MessageBox(hWnd,"Error creating D3D Device","error d3d device",MB_OK);
return 0;
}
//random number seed (WHY??)
//srand(time(NULL));
//i saw it was good
return 1;
}
//App_Run function
void App_Run(HWND hwnd)
{
//make sure the d3ddev is valid
if(d3ddev == NULL)
return;
//Clear the backbuffer with white
d3ddev->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(255,255,255),1.0f,0);
/////START THE RENDER////ILL BE BACK HERE
if (d3ddev->BeginScene())
{
//DO STUFF
//STOP STUFF
d3ddev->EndScene();
}
//Display the backbuffer (display something)
d3ddev->Present(NULL,NULL,NULL,NULL);
}
//App_Close function
void App_Close(HWND hWnd)
{
//display close message
MessageBox(hWnd,"Program is about to end","Game_End",MB_OK);
//Release the d3ddev and d3d
if(d3ddev != NULL)
d3ddev->Release();
if(d3d != NULL)
d3d->Release();
}
//end
/////////////////////END OF OWN(DIRECTX)FUNCTIONS//////////