Alientech 0.01
An extendable game engine
C:/Alientech/main.cpp
Go to the documentation of this file.
00001 // include the basic windows header files and the Direct3D header files
00002 #include <windows.h>
00003 #include <windowsx.h>
00004 #include <d3d10.h>
00005 #include <d3dx10.h>
00006 
00007 // include the Direct3D Library file
00008 #pragma comment (lib, "d3d10.lib")
00009 #pragma comment (lib, "d3dx10.lib")
00010 
00011 // global declarations
00012 ID3D10Device* device;    // the pointer to our Direct3D device interface
00013 ID3D10RenderTargetView* rtv;    // the pointer to the render target view
00014 IDXGISwapChain* swapchain;    // the pointer to the swap chain class
00015 
00016 // function prototypes
00017 void initD3D(HWND hWnd);    // sets up and initializes Direct3D
00018 void render_frame(void);    // renders a single frame
00019 void cleanD3D(void);    // closes Direct3D and releases memory
00020 
00021 // the WindowProc function prototype
00022 LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
00023 
00024 
00025 // the entry point for any Windows program
00026 int WINAPI WinMain(HINSTANCE hInstance,
00027                    HINSTANCE hPrevInstance,
00028                    LPSTR lpCmdLine,
00029                    int nCmdShow)
00030 {
00031     HWND hWnd;
00032     WNDCLASSEX wc;
00033 
00034     ZeroMemory(&wc, sizeof(WNDCLASSEX));
00035 
00036     wc.cbSize = sizeof(WNDCLASSEX);
00037     wc.style = CS_HREDRAW | CS_VREDRAW;
00038     wc.lpfnWndProc = WindowProc;
00039     wc.hInstance = hInstance;
00040     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
00041     wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
00042     wc.lpszClassName = L"WindowClass";
00043 
00044     RegisterClassEx(&wc);
00045 
00046     hWnd = CreateWindowEx(NULL,
00047                           L"WindowClass",
00048                           L"Our First Direct3D Program",
00049                           WS_OVERLAPPEDWINDOW,
00050                           300, 300,
00051                           800, 600,
00052                           NULL,
00053                           NULL,
00054                           hInstance,
00055                           NULL);
00056 
00057     ShowWindow(hWnd, nCmdShow);
00058 
00059     // set up and initialize Direct3D
00060     initD3D(hWnd);
00061 
00062     // enter the main loop:
00063 
00064     MSG msg;
00065 
00066     while(TRUE)
00067     {
00068         if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
00069         {
00070             TranslateMessage(&msg);
00071             DispatchMessage(&msg);
00072 
00073             if(msg.message == WM_QUIT)
00074                 break;
00075         }
00076 
00077         render_frame();
00078     }
00079 
00080     // clean up DirectX and COM
00081     cleanD3D();
00082 
00083     return msg.wParam;
00084 }
00085 
00086 
00087 // this is the main message handler for the program
00088 LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
00089 {
00090     switch(message)
00091     {
00092         case WM_DESTROY:
00093             {
00094                 PostQuitMessage(0);
00095                 return 0;
00096             } break;
00097     }
00098 
00099     return DefWindowProc (hWnd, message, wParam, lParam);
00100 }
00101 
00102 
00103 // this function initializes and prepares Direct3D for use
00104 void initD3D(HWND hWnd)
00105 {
00106     DXGI_SWAP_CHAIN_DESC scd;    // create a struct to hold various swap chain information
00107 
00108     ZeroMemory(&scd, sizeof(DXGI_SWAP_CHAIN_DESC));    // clear out the struct for use
00109 
00110     scd.BufferCount = 1;    // create two buffers, one for the front, one for the back
00111     scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;    // use 32-bit color
00112     scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;    // tell how the chain is to be used
00113     scd.OutputWindow = hWnd;    // set the window to be used by Direct3D
00114     scd.SampleDesc.Count = 1;    // set the level of multi-sampling
00115     scd.SampleDesc.Quality = 0;    // set the quality of multi-sampling
00116     scd.Windowed = TRUE;    // set to windowed or full-screen mode
00117 
00118     // create a device class and swap chain class using the information in the scd struct
00119     D3D10CreateDeviceAndSwapChain(NULL,
00120                                   D3D10_DRIVER_TYPE_HARDWARE,
00121                                   NULL,
00122                                   0,
00123                                   D3D10_SDK_VERSION,
00124                                   &scd,
00125                                   &swapchain,
00126                                   &device);
00127 
00128 
00129     // get the address of the back buffer and use it to create the render target
00130     ID3D10Texture2D* pBackBuffer;
00131     swapchain->GetBuffer(0, __uuidof(ID3D10Texture2D), (LPVOID*)&pBackBuffer);
00132     device->CreateRenderTargetView(pBackBuffer, NULL, &rtv);
00133     pBackBuffer->Release();
00134 
00135     // set the back buffer as the render target
00136     device->OMSetRenderTargets(1, &rtv, NULL);
00137 
00138 
00139     D3D10_VIEWPORT viewport;    // create a struct to hold the viewport data
00140 
00141     ZeroMemory(&viewport, sizeof(D3D10_VIEWPORT));    // clear out the struct for use
00142 
00143     viewport.TopLeftX = 0;    // set the left to 0
00144     viewport.TopLeftY = 0;    // set the top to 0
00145     viewport.Width = 800;    // set the width to the window's width
00146     viewport.Height = 600;    // set the height to the window's height
00147 
00148     device->RSSetViewports(1, &viewport);    // set the viewport
00149 }
00150 
00151 
00152 // this is the function used to render a single frame
00153 void render_frame(void)
00154 {
00155     // clear the window to a deep blue
00156     device->ClearRenderTargetView(rtv, D3DXCOLOR(0.0f, 0.2f, 0.4f, 1.0f));
00157 
00158     // do 3D rendering on the back buffer here
00159 
00160     // display the rendered frame
00161     swapchain->Present(0, 0);
00162 }
00163 
00164 
00165 // this is the function that cleans up Direct3D and COM
00166 void cleanD3D(void)
00167 {
00168     swapchain->Release();    // close and release the swap chain
00169     rtv->Release();    // close and release the render target view
00170     device->Release();    // close and release the 3D device
00171 }