Advertisement
bkit4s0

d3d_windowed

Sep 27th, 2014
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.22 KB | None | 0 0
  1. // Beginning Game Programming, Second Edition
  2. // Chapter 5
  3. // d3d_windowed program
  4. //header files to include
  5. #include <d3d9.h>
  6. #include <time.h>
  7. //application title
  8. #define APPTITLE L"Direct3D_Windowed"
  9. //forward declarations
  10. LRESULT WINAPI WinProc(HWND,UINT,WPARAM,LPARAM);
  11. ATOM MyRegisterClass(HINSTANCE);
  12. int Game_Init(HWND);
  13. void Game_Run(HWND);
  14. void Game_End(HWND);
  15. //Direct3D objects
  16. LPDIRECT3D9 d3d = NULL;
  17. LPDIRECT3DDEVICE9 d3ddev = NULL;
  18. //window event callback function
  19. LRESULT WINAPI WinProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam ) {
  20.     switch( msg ) {
  21.         case WM_DESTROY:
  22.         Game_End(hWnd);
  23.         PostQuitMessage(0);
  24.         return 0;
  25.     }
  26.     return DefWindowProc( hWnd, msg, wParam, lParam );
  27. }
  28. //helper function to set up the window properties
  29. ATOM MyRegisterClass(HINSTANCE hInstance) {
  30.     //create the window class structure
  31.     WNDCLASSEX wc;
  32.     wc.cbSize = sizeof(WNDCLASSEX);
  33.     //fill the struct with info
  34.     wc.style = CS_HREDRAW | CS_VREDRAW;
  35.     wc.lpfnWndProc = (WNDPROC)WinProc;
  36.     wc.cbClsExtra = 0;
  37.     wc.cbWndExtra = 0;
  38.     wc.hInstance = hInstance;
  39.     wc.hIcon = NULL;
  40.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  41.     wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
  42.     wc.lpszMenuName = NULL;
  43.     wc.lpszClassName = APPTITLE;
  44.     wc.hIconSm = NULL;
  45.     //set up the window with the class info
  46.     return RegisterClassEx(&wc);
  47. }
  48. //entry point for a Windows program
  49. int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow) {
  50.     // declare variables
  51.     MSG msg;
  52.     // register the class
  53.     MyRegisterClass(hInstance);
  54.     // initialize application
  55.     //noteโ€“โ€“got rid of initinstance
  56.     HWND hWnd;
  57.     //create a new window
  58.     hWnd = CreateWindow(
  59.     APPTITLE, //window class
  60.     APPTITLE, //title bar
  61.     WS_OVERLAPPEDWINDOW, //window style
  62.     CW_USEDEFAULT, //x position of window
  63.     CW_USEDEFAULT, //y position of window
  64.     500, //width of the window
  65.     400, //height of the window
  66.     NULL, //parent window
  67.     NULL, //menu
  68.     hInstance, //application instance
  69.     NULL); //window parameters
  70.     //was there an error creating the window?
  71.     if (!hWnd)
  72.         return FALSE;
  73.     //display the window
  74.     ShowWindow(hWnd, nCmdShow);
  75.     UpdateWindow(hWnd);
  76.     //initialize the game
  77.     if (!Game_Init(hWnd))
  78.         return 0;
  79.     // main message loop
  80.     int done = 0;
  81.     while (!done) {
  82.         if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
  83.             //look for quit message
  84.             if (msg.message == WM_QUIT) {
  85.                 MessageBox(hWnd, L"Received WM_QUIT message", L"WinMain", MB_OK);
  86.                 done = 1;
  87.             }
  88.             //decode and pass messages on to WndProc
  89.             TranslateMessage(&msg);
  90.             DispatchMessage(&msg);
  91.         }
  92.         else
  93.             //process game loop (else prevents running after window is closed)
  94.             Game_Run(hWnd);
  95.         }
  96.     return msg.wParam;
  97. }
  98. int Game_Init(HWND hwnd) {
  99.     //display init message
  100.     MessageBox(hwnd, L"Program is about to start", L"Game_Init", MB_OK);
  101.     //initialize Direct3D
  102.     d3d = Direct3DCreate9(D3D_SDK_VERSION);
  103.     if (d3d == NULL) {
  104.         MessageBox(hwnd, L"Error initializing Direct3D", L"Error", MB_OK);
  105.         return 0;
  106.     }
  107.     //set Direct3D presentation parameters
  108.     D3DPRESENT_PARAMETERS d3dpp;
  109.     ZeroMemory(&d3dpp, sizeof(d3dpp));
  110.     d3dpp.Windowed = TRUE;
  111.     d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
  112.     d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
  113.     //create Direct3D device
  114.     d3d->CreateDevice(
  115.     D3DADAPTER_DEFAULT,
  116.     D3DDEVTYPE_HAL,
  117.     hwnd,
  118.     D3DCREATE_SOFTWARE_VERTEXPROCESSING,
  119.     &d3dpp,
  120.     &d3ddev);
  121.     if (d3ddev == NULL) {
  122.         MessageBox(hwnd, L"Error creating Direct3D device", L"Error", MB_OK);
  123.         return 0;
  124.     }
  125.     //set random number seed
  126.     srand(time(NULL));
  127.     //return okay
  128.     return 1;
  129. }
  130. void Game_Run(HWND hwnd) {
  131.     //make sure the Direct3D device is valid
  132.     if (d3ddev == NULL)
  133.         return;
  134.     //clear the screen with a green color
  135.     d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,255,255), 1.0f, 0);
  136.     //start rendering
  137.     if (d3ddev->BeginScene()) {
  138.         //do something here!
  139.         //stop rendering
  140.         d3ddev->EndScene();
  141.     }
  142.     //display the back buffer on the screen
  143.     d3ddev->Present(NULL, NULL, NULL, NULL);
  144. }
  145. void Game_End(HWND hwnd) {
  146.     //display close message
  147.     MessageBox(hwnd, L"Program is about to end", L"Game_End", MB_OK);
  148.     //release the Direct3D device
  149.     if (d3ddev != NULL)
  150.         d3ddev->Release();
  151.     //release the Direct3D object
  152.     if (d3d != NULL)
  153.         d3d->Release();
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement