Advertisement
bkit4s0

Create_Surface

Sep 28th, 2014
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.43 KB | None | 0 0
  1. // Beginning Game Programming, Second Edition
  2. // Chapter 6
  3. // Create_Surface program
  4. //header files to include
  5. #include <d3d9.h>
  6. #include <time.h>
  7. //application title
  8. #define APPTITLE L"Create_Surface"
  9. //macros to read the keyboard asynchronously
  10. #define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
  11. #define KEY_UP(vk_code)((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
  12. //screen resolution
  13. #define SCREEN_WIDTH 640
  14. #define SCREEN_HEIGHT 480
  15. //forward declarations
  16. LRESULT WINAPI WinProc(HWND,UINT,WPARAM,LPARAM);
  17. ATOM MyRegisterClass(HINSTANCE);
  18. int Game_Init(HWND);
  19. void Game_Run(HWND);
  20. void Game_End(HWND);
  21. //Direct3D objects
  22. LPDIRECT3D9 d3d = NULL;
  23. LPDIRECT3DDEVICE9 d3ddev = NULL;
  24. LPDIRECT3DSURFACE9 backbuffer = NULL;
  25. LPDIRECT3DSURFACE9 surface = NULL;
  26. //window event callback function
  27. LRESULT WINAPI WinProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam ) {
  28.     switch( msg ) {
  29.         case WM_DESTROY:
  30.             Game_End(hWnd);
  31.             PostQuitMessage(0);
  32.         return 0;
  33.     }
  34.     return DefWindowProc( hWnd, msg, wParam, lParam );
  35. }
  36. //helper function to set up the window properties
  37. ATOM MyRegisterClass(HINSTANCE hInstance) {
  38.     //create the window class structure
  39.     WNDCLASSEX wc;
  40.     wc.cbSize = sizeof(WNDCLASSEX);
  41.     //fill the struct with info
  42.     wc.style = CS_HREDRAW | CS_VREDRAW;
  43.     wc.lpfnWndProc = (WNDPROC)WinProc;
  44.     wc.cbClsExtra = 0;
  45.     wc.cbWndExtra = 0;
  46.     wc.hInstance = hInstance;
  47.     wc.hIcon = NULL;
  48.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  49.     wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
  50.     wc.lpszMenuName = NULL;
  51.     wc.lpszClassName = APPTITLE;
  52.     wc.hIconSm = NULL;
  53.     //set up the window with the class info
  54.     return RegisterClassEx(&wc);
  55. }
  56. //entry point for a Windows program
  57. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
  58.     // declare variables
  59.     MSG msg;
  60.     // register the class
  61.     MyRegisterClass(hInstance);
  62.     // initialize application
  63.     HWND hWnd;
  64.     //create a new window
  65.     hWnd = CreateWindow(
  66.     APPTITLE, //window class
  67.     APPTITLE, //title bar
  68.     WS_EX_TOPMOST | WS_VISIBLE | WS_POPUP, //window style
  69.     CW_USEDEFAULT, //x position of window
  70.     CW_USEDEFAULT, //y position of window
  71.     SCREEN_WIDTH, //width of the window
  72.     SCREEN_HEIGHT, //height of the window
  73.     NULL, //parent window
  74.     NULL, //menu
  75.     hInstance, //application instance
  76.     NULL); //window parameters
  77.     //was there an error creating the window?
  78.     if (!hWnd)
  79.         return FALSE;
  80.     //display the window
  81.     ShowWindow(hWnd, nCmdShow);
  82.     UpdateWindow(hWnd);
  83.     //initialize the game
  84.     if (!Game_Init(hWnd))
  85.         return 0;
  86.     // main message loop
  87.     int done = 0;
  88.     while (!done) {
  89.         if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
  90.             //look for quit message
  91.             if (msg.message == WM_QUIT)
  92.                 done = 1;
  93.             //decode and pass messages on to WndProc
  94.             TranslateMessage(&msg);
  95.             DispatchMessage(&msg);
  96.         }
  97.         else
  98.         //process game loop (else prevents running after window is closed)
  99.         Game_Run(hWnd);
  100.     }
  101.     return msg.wParam;
  102. }
  103. int Game_Init(HWND hwnd) {
  104.     HRESULT result;
  105.     //initialize Direct3D
  106.     d3d = Direct3DCreate9(D3D_SDK_VERSION);
  107.     if (d3d == NULL) {
  108.         MessageBox(hwnd, L"Error initializing Direct3D", L"Error", MB_OK);
  109.         return 0;
  110.     }
  111.     //set Direct3D presentation parameters
  112.     D3DPRESENT_PARAMETERS d3dpp;
  113.     ZeroMemory(&d3dpp, sizeof(d3dpp));
  114.     d3dpp.Windowed = FALSE;
  115.     d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
  116.     d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
  117.     d3dpp.BackBufferCount = 1;
  118.     d3dpp.BackBufferWidth = SCREEN_WIDTH;
  119.     d3dpp.BackBufferHeight = SCREEN_HEIGHT;
  120.     d3dpp.hDeviceWindow = hwnd;
  121.     //create Direct3D device
  122.     d3d->CreateDevice(
  123.     D3DADAPTER_DEFAULT,
  124.     D3DDEVTYPE_HAL,
  125.     hwnd,
  126.     D3DCREATE_SOFTWARE_VERTEXPROCESSING,
  127.     &d3dpp,
  128.     &d3ddev);
  129.     if (d3ddev == NULL) {
  130.         MessageBox(hwnd, L"Error creating Direct3D device", L"Error", MB_OK);
  131.         return 0;
  132.     }
  133.     //set random number seed
  134.     srand(time(NULL));
  135.     //clear the backbuffer to black
  136.     d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f, 0);
  137.     //create pointer to the back buffer
  138.     d3ddev->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &backbuffer);
  139.     //create surface
  140.     result = d3ddev->CreateOffscreenPlainSurface(
  141.     100, //width of the surface
  142.     100, //height of the surface
  143.     D3DFMT_X8R8G8B8, //surface format
  144.     D3DPOOL_DEFAULT, //memory pool to use
  145.     &surface, //pointer to the surface
  146.     NULL); //reserved (always NULL)
  147.     if (!result)
  148.         return 1;
  149.     //return okay
  150.     return 1;
  151. }
  152. void Game_Run(HWND hwnd) {
  153.     RECT rect;
  154.     int r,g,b;
  155.     //make sure the Direct3D device is valid
  156.     if (d3ddev == NULL)
  157.         return;
  158.     //start rendering
  159.     if (d3ddev->BeginScene()) {
  160.         //fill the surface with random color
  161.         r = rand() % 255;
  162.         g = rand() % 255;
  163.         b = rand() % 255;
  164.         d3ddev->ColorFill(surface, NULL, D3DCOLOR_XRGB(r,g,b));
  165.         //copy the surface to the backbuffer
  166.         rect.left = rand() % SCREEN_WIDTH/2;
  167.         rect.right = rect.left + rand() % SCREEN_WIDTH/2;
  168.         rect.top = rand() % SCREEN_HEIGHT;
  169.         rect.bottom = rect.top + rand() % SCREEN_HEIGHT/2;
  170.         d3ddev->StretchRect(surface, NULL, backbuffer, &rect, D3DTEXF_NONE);
  171.         //stop rendering
  172.         d3ddev->EndScene();
  173.     }
  174.     //display the back buffer on the screen
  175.     d3ddev->Present(NULL, NULL, NULL, NULL);
  176.     //check for escape key (to exit program)
  177.     if (KEY_DOWN(VK_ESCAPE))
  178.         PostMessage(hwnd, WM_DESTROY, 0, 0);
  179. }
  180. void Game_End(HWND hwnd) {
  181.     //free the surface
  182.     surface->Release();
  183.     //release the Direct3D device
  184.     if (d3ddev != NULL)
  185.     d3ddev->Release();
  186.     //release the Direct3D object
  187.     if (d3d != NULL)
  188.     d3d->Release();
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement