Guest User

DirectX Blank screen jams... why?

a guest
Jul 30th, 2010
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.28 KB | None | 0 0
  1. #define WIN32_LEAN_AND_MEAN
  2.  
  3. #define INITGUID
  4.  
  5. #include <windows.h>
  6. #include <windowsx.h>
  7. #include <mmsystem.h>
  8. #include <iostream>
  9. #include <stdio.h>
  10. #include "MyMacros.h"
  11. #include <conio.h>
  12. #include <stdlib.h>
  13. #include <malloc.h>
  14. #include <memory.h>
  15. #include <string.h>
  16. #include <stdarg.h>
  17. #include <math.h>
  18. #include <io.h>
  19. #include <fcntl.h>
  20. #include <ddraw.h>
  21.  
  22. /*  Declare Windows procedure  */
  23. LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
  24.  
  25. // Prototypes /////////////////////////////////////////////
  26. int Game_Init(void *parms = NULL, int num_parms = 0);
  27. int Game_Main(void *parms = NULL, int num_parms = 0);
  28. int Game_Shutdown(void *parms = NULL, int num_parms = 0);
  29.  
  30. /*  Make the class name into a global variable  */
  31. char szClassName[ ] = "Window_Class_One";
  32.  
  33. // Globals ////////////////////////////////////////////////
  34. HWND main_window_handle = NULL;
  35. HINSTANCE app_instance = NULL;
  36. LPDIRECTDRAW7 lpdd7 = NULL;
  37. LPDIRECTDRAWPALETTE lpdd7pal = NULL;
  38. // palette
  39. PALETTEENTRY palette[256];
  40. // DirectDraw Surface descriptions
  41. DDSURFACEDESC2 ddsd;
  42. // DirectDraw surface handle
  43. LPDIRECTDRAWSURFACE7 lpddsprimary;
  44.  
  45. int WINAPI WinMain (HINSTANCE hinstance,
  46.                      HINSTANCE hPrevInstance,
  47.                      LPSTR lpszArgument,
  48.                      int nCmdShow)
  49. {
  50.     HWND hwnd;               /* This is the handle for our window */
  51.     MSG msg;            /* Here messages to the application are saved */
  52.     WNDCLASSEX wincl;        /* Data structure for the windowclass */
  53.  
  54.  
  55.  
  56.     /* The Window structure */
  57.     wincl.hInstance = hinstance;
  58.     wincl.lpszClassName = szClassName;
  59.     wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
  60.     wincl.style = CS_OWNDC| CS_HREDRAW| CS_VREDRAW | CS_DBLCLKS;                 /* Catch double-clicks */
  61.     wincl.cbSize = sizeof (WNDCLASSEX);
  62.  
  63.     /* Use default icon and mouse-pointer */
  64.     wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
  65.     wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
  66.     wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
  67.     wincl.lpszMenuName = NULL;                 /* No menu */
  68.     wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
  69.     wincl.cbWndExtra = 0;                      /* structure or the window instance */
  70.     /* Use Windows's default colour as the background of the window */
  71.     wincl.hbrBackground = (HBRUSH) GetStockObject(BLACK_BRUSH);
  72.  
  73.     /* Register the window class, and if it fails quit the program */
  74.     if (!RegisterClassEx (&wincl))
  75.         return 0;
  76.  
  77.     /* The class is registered, let's create the program*/
  78.     hwnd = CreateWindowEx (
  79.            0,                   /* Extended possibilites for variation */
  80.            szClassName,         /* Classname */
  81.            "DirectX Console",       /* Title Text */
  82.            WS_POPUP | WS_VISIBLE, /* default window */
  83.            0,       /* Windows decides the position */
  84.            0,       /* where the window ends up on the screen */
  85.            SCREEN_WIDTH,                 /* The programs width */
  86.            SCREEN_HEIGHT,                 /* and height in pixels */
  87.            NULL,        /* The window is a child-window to desktop */
  88.            NULL,                /* No menu */
  89.            hinstance,       /* Program Instance handler */
  90.            NULL                 /* No Window Creation data */
  91.            );
  92.  
  93.  
  94.     // needed for when passing handle around functions
  95.     main_window_handle = hwnd;
  96.  
  97.     // needed for when passing instance around functions
  98.     app_instance = hinstance;
  99.  
  100.     /* Make the window visible on the screen */
  101.     //ShowWindow (hwnd, nCmdShow);
  102.  
  103.     Game_Init();
  104.  
  105.     /* Run the message loop. It will run until GetMessage() returns 0 */
  106.     while(true)
  107.     {
  108.         if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  109.         {
  110.             if(msg.message == WM_QUIT)
  111.             {
  112.                 std::cout << "Quit message recieved" << std::endl;
  113.                 break;
  114.             }
  115.  
  116.             /* Translate virtual-key messages into character messages */
  117.             TranslateMessage(&msg);
  118.             /* Send message to WindowProcedure */
  119.             DispatchMessage(&msg);
  120.         }
  121.  
  122.         Game_Main();
  123.     }
  124.  
  125.     std::cout << "Left main loop and quitting..." << std::endl;
  126.  
  127.     Game_Shutdown();
  128.  
  129.     /* The program return-value is 0 - The value that PostQuitMessage() gave */
  130.     return msg.wParam;
  131. }
  132.  
  133.  
  134. /*  This function is called by the Windows function DispatchMessage()  */
  135.  
  136. LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  137. {
  138.     HDC hdc;
  139.     PAINTSTRUCT ps;
  140.  
  141.     //char bufferWP[80];      //for local printing
  142.  
  143.     switch (message)                  /* handle the messages */
  144.     {
  145.         case WM_PAINT:
  146.             // simply validate the window
  147.             hdc = BeginPaint(hwnd, &ps);
  148.             // stuff here
  149.  
  150.             EndPaint(hwnd, &ps);
  151.             break;
  152.         case WM_CLOSE:
  153.             std::cout << "Close signal recieved" << std::endl;
  154.             break;
  155.         case WM_DESTROY:
  156.             std::cout << "Destroy signal recieved" << std::endl;
  157.             PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
  158.             break;
  159.         default:                      /* for messages that we don't deal with */
  160.             return DefWindowProc (hwnd, message, wParam, lParam);
  161.     }
  162.  
  163.     return 0;
  164. }
  165.  
  166. int Game_Init(void *parms, int num_parms)
  167. {
  168.  
  169.     // Instructions!
  170.     // 1. Setup the Directx Object through createex
  171.     // 2. Set the cooperation level between DX object and windows through SetCooperativeLevel
  172.     // 3. Set the Display mode with SetDisplayMode
  173.     // 4. Take your array of palette entries and fill them (can be omitted)
  174.     // 5. Create palette with CreatePalette using palette entries and DDCAPS_INITIALIZE
  175.     //          (Don't used DDCAPS_INITIALIZE if you're not setting your own palette)
  176.     // 6. Clear and then fill out DDSURFACEDESC2 with info about the surface
  177.     // 7. Create surface using the DDSURFACEDESC2 settings and the new pointer of LPDIRECTDRAWSURFACE7
  178.     //          using CreateSurface
  179.     // 8. Now combine new surface and palette with SelectPalette
  180.     // The following is done in GameMain().
  181.     // 9. clear the ddsd class and reuse it for locking the surface
  182.     // 10. Set up aliases for lpitch and the surface to reduce casting
  183.     // 11. Randomly plot some pixels
  184.     // 12. Unlock the surface area
  185.  
  186.     //DirectX setup:
  187.     HRESULT DDresult =  (DirectDrawCreateEx(NULL, (void **)&lpdd7, IID_IDirectDraw7, NULL));
  188.     switch(DDresult)
  189.     {
  190.         case DD_OK:
  191.             std::cout << "DD7 initialised." << std::endl;
  192.             break;
  193.         case DDERR_DIRECTDRAWALREADYCREATED:
  194.             std::cout << "DD already created." << std::endl;
  195.             break;
  196.         case DDERR_GENERIC:
  197.             std::cout << "Unknown problem..." << std::endl;
  198.             break;
  199.         case DDERR_INVALIDDIRECTDRAWGUID:
  200.             std::cout << "Unknown GUID." << std::endl;
  201.             break;
  202.         case DDERR_INVALIDPARAMS:
  203.             std::cout << "Invalid paramters - please double check." << std::endl;
  204.             break;
  205.         case DDERR_NODIRECTDRAWHW:
  206.             std::cout << "No DirectDraw capable hardware" << std::endl;
  207.             break;
  208.         case DDERR_OUTOFMEMORY:
  209.             std::cout << "Out of memroy." << std::endl;
  210.             break;
  211.         default:
  212.             std::cout << "Default error..." << std::endl;
  213.             break;
  214.     }
  215.  
  216.     // Set up the correct coop modes for windows
  217.     if(SUCCEEDED(lpdd7->SetCooperativeLevel(main_window_handle, DDSCL_EXCLUSIVE |
  218.                                                             DDSCL_ALLOWREBOOT |
  219.                                                             DDSCL_FULLSCREEN)))
  220.     {
  221.         std::cout << "Cooperative levels set succesfully." << std::endl;
  222.     }
  223.  
  224.     // set the video mode
  225.     if(FAILED(lpdd7->SetDisplayMode(SCREEN_WIDTH, SCREEN_HEIGHT, BPP,0,0)))
  226.     {
  227.         std::cout << "Displaymode set correctly" << std::endl;
  228.     }
  229.  
  230.     // manually fill our first element in palette
  231.     palette[0].peRed = 0;
  232.     palette[0].peGreen = 0;
  233.     palette[0].peBlue = 0;
  234.     palette[0].peFlags = PC_NOCOLLAPSE;
  235.  
  236.  
  237.     //fill the palette with random numbers
  238.     for(unsigned char colour = 1; colour < 255; colour++)
  239.     {
  240.         palette[colour].peRed = rand()%256;
  241.         palette[colour].peGreen = rand()%256;
  242.         palette[colour].peBlue = rand()%256;
  243.  
  244.         //Next part very important, stops windows and DX
  245.         // optimizing palette for us
  246.         palette[colour].peFlags = PC_NOCOLLAPSE;
  247.     }
  248.  
  249.     // manually fill out last element in palette
  250.     palette[255].peRed = 255;
  251.     palette[255].peGreen = 255;
  252.     palette[255].peBlue = 255;
  253.     palette[255].peFlags = PC_NOCOLLAPSE;
  254.  
  255.     // initialise the palatte and pass the handle back
  256.     if(SUCCEEDED(lpdd7->CreatePalette(DDPCAPS_8BIT | DDPCAPS_ALLOW256 | DDPCAPS_INITIALIZE, palette, &lpdd7pal, NULL)))
  257.         std::cout << "Palette created and initialized: " << lpdd7pal << "." << std::endl;
  258.  
  259.     //clear out the memory first!
  260.     memset(&ddsd, 0, sizeof(ddsd));
  261.  
  262.     // fill out the description for the surface
  263.     ddsd.dwSize = sizeof(DDSURFACEDESC2);
  264.     ddsd.dwFlags = DDSD_CAPS;
  265.     ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
  266.  
  267.     // now create the surface with the above descriptor...
  268.     if(SUCCEEDED(lpdd7->CreateSurface(&ddsd, &lpddsprimary, NULL)))
  269.         std::cout << "Surface created." << std::endl;
  270.  
  271.     // ... and add the palette
  272.     if(SUCCEEDED(lpddsprimary->SetPalette(lpdd7pal)))
  273.         std::cout << "Palette succesfully added." << std::endl;
  274.  
  275.  
  276.     return 0;
  277. }
  278.  
  279. int Game_Main(void *parms, int num_parms)
  280. {
  281.     if(MACKEYDOWN(VK_ESCAPE))
  282.     SendMessage(main_window_handle, WM_CLOSE, 0, 0);
  283.  
  284.     DWORD timer = GetTickCount();
  285.  
  286.     static int counter = 0;
  287.     counter++;
  288.  
  289.     //now that the surface is created, reuse the surface descriptor class
  290.     memset(&ddsd, 0, sizeof(ddsd));
  291.  
  292.     ddsd.dwSize = sizeof(ddsd);
  293.  
  294.     if(FAILED(lpddsprimary->Lock(NULL, &ddsd, DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT, NULL)))
  295.     {
  296.         std::cout << "Area lock failed at iteration " << counter << std::endl;
  297.         Sleep(5);
  298.         PostQuitMessage(0);
  299.         return(0);
  300.     }
  301.  
  302.     // aliases to stop having to cast.
  303.     int mempitch = ddsd.lPitch;
  304.     UCHAR *video_buffer = (UCHAR *)ddsd.lpSurface;
  305.  
  306.     // Unlock Screen
  307.     if(FAILED(lpddsprimary->Unlock(NULL)))
  308.     {
  309.         std::cout << "Surface area unlock failed at iteration " << counter << std::endl;
  310.         Sleep(5);
  311.         PostQuitMessage(0);
  312.         return(0);
  313.     }
  314.  
  315.     Sleep(30);
  316.  
  317.     return 0;
  318. }
  319.  
  320. int Game_Shutdown(void *parms, int num_parms)
  321. {
  322.     if(lpdd7)
  323.     {
  324.         if(lpdd7pal)
  325.         {
  326.             lpdd7pal->Release();
  327.             lpdd7pal = NULL;
  328.             std::cout << "Palette destroyed" << std::endl;
  329.         }
  330.         if(lpddsprimary)
  331.         {
  332.             lpddsprimary->Release();
  333.             lpddsprimary = NULL;
  334.             std::cout << "Surface destroyed" << std::endl;
  335.         }
  336.         if(lpdd7)
  337.         {
  338.             lpdd7->Release();
  339.             lpdd7 = NULL;
  340.             std::cout << "Interface destroyed" << std::endl;
  341.         }
  342.  
  343.     }
  344.     return 0;
  345. }
Advertisement
Add Comment
Please, Sign In to add comment