Advertisement
Guest User

Gens rerecodring DirectDraw init code

a guest
Dec 5th, 2011
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.79 KB | None | 0 0
  1. int Init_DDraw(HWND hWnd)
  2.  
  3. {
  4.  
  5.     int Rend;
  6.  
  7.     HRESULT rval;
  8.  
  9.     DDSURFACEDESC2 ddsd;
  10.  
  11.  
  12.  
  13.     int oldBits32 = Bits32;
  14.  
  15.  
  16.  
  17.     End_DDraw();
  18.  
  19.    
  20.  
  21.     if (Full_Screen) Rend = Render_FS;
  22.  
  23.     else Rend = Render_W;
  24.  
  25.  
  26.  
  27.     if (FAILED(DirectDrawCreate(NULL, &lpDD_Init, NULL)))
  28.  
  29.         return Init_Fail(hWnd, "Error with DirectDrawCreate !");
  30.  
  31.  
  32.  
  33.     if (FAILED(lpDD_Init->QueryInterface(IID_IDirectDraw4, (LPVOID *) &lpDD)))
  34.  
  35.         return Init_Fail(hWnd, "Error with QueryInterface !\nUpgrade your DirectX version.");
  36.  
  37.  
  38.  
  39.     lpDD_Init->Release();
  40.  
  41.     lpDD_Init = NULL;
  42.  
  43.  
  44.  
  45.     if (!(Mode_555 & 2))
  46.  
  47.     {
  48.  
  49.         memset(&ddsd, 0, sizeof(ddsd));
  50.  
  51.         ddsd.dwSize = sizeof(ddsd);
  52.  
  53.  
  54.  
  55.         lpDD->GetDisplayMode(&ddsd);
  56.  
  57.  
  58.  
  59.         if (ddsd.ddpfPixelFormat.dwGBitMask == 0x03E0) Mode_555 = 1;
  60.  
  61.         else Mode_555 = 0;
  62.  
  63.  
  64.  
  65.         Recalculate_Palettes();
  66.  
  67.     }
  68.  
  69.  
  70.  
  71. #ifdef DISABLE_EXCLUSIVE_FULLSCREEN_LOCK
  72.  
  73.     FS_VSync = 0;
  74.  
  75.     rval = lpDD->SetCooperativeLevel(hWnd, DDSCL_NORMAL);
  76.  
  77. #else
  78.  
  79.     if (Full_Screen)
  80.  
  81.         rval = lpDD->SetCooperativeLevel(hWnd, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
  82.  
  83.     else
  84.  
  85.         rval = lpDD->SetCooperativeLevel(hWnd, DDSCL_NORMAL);
  86.  
  87. #endif
  88.  
  89.  
  90.  
  91.     if (FAILED(rval))
  92.  
  93.         return Init_Fail(hWnd, "Error with lpDD->SetCooperativeLevel !");
  94.  
  95.    
  96.  
  97.     if (Res_X < (320 << (int) (Render_FS > 0))) Res_X = 320 << (int) (Render_FS > 0); //Upth-Add - Set a floor for the resolution
  98.  
  99.     if (Res_Y < (240 << (int) (Render_FS > 0))) Res_Y = 240 << (int) (Render_FS > 0); //Upth-Add - 320x240 for single, 640x480 for double and other
  100.  
  101.  
  102.  
  103.     //if (Full_Screen && Render_FS >= 2) //Upth-Modif - Since software blits don't stretch right, we'll use 640x480 for those render modes
  104.  
  105.     // Modif N. removed the forced 640x480 case because it caused "windowed fullscreen mode" to be ignored and because I fixed the fullscreen software blit stretching
  106.  
  107.  
  108.  
  109.     if (Full_Screen && !(FS_No_Res_Change))
  110.  
  111.     {
  112.  
  113.         if (FAILED(lpDD->SetDisplayMode(Res_X, Res_Y, 16, 0, 0)))
  114.  
  115.             return Init_Fail(hWnd, "Error with lpDD->SetDisplayMode !");
  116.  
  117.     }
  118.  
  119.  
  120.  
  121.     memset(&ddsd, 0, sizeof(ddsd));
  122.  
  123.     ddsd.dwSize = sizeof(ddsd);
  124.  
  125.  
  126.  
  127.     if ((Full_Screen) && (FS_VSync))
  128.  
  129.     {
  130.  
  131.         ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
  132.  
  133.         ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_FLIP | DDSCAPS_COMPLEX;
  134.  
  135.         ddsd.dwBackBufferCount = 2;
  136.  
  137.     }
  138.  
  139.     else
  140.  
  141.     {
  142.  
  143.         ddsd.dwFlags = DDSD_CAPS;
  144.  
  145.         ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
  146.  
  147.     }
  148.  
  149.  
  150.  
  151.     if (FAILED(lpDD->CreateSurface(&ddsd, &lpDDS_Primary, NULL )))
  152.  
  153.         return Init_Fail(hWnd, "Error with lpDD->CreateSurface !");
  154.  
  155.  
  156.  
  157.     if (Full_Screen)
  158.  
  159.     {
  160.  
  161.         if (FS_VSync)
  162.  
  163.         {
  164.  
  165.             ddsd.ddsCaps.dwCaps = DDSCAPS_BACKBUFFER;
  166.  
  167.  
  168.  
  169.             if (FAILED(lpDDS_Primary->GetAttachedSurface(&ddsd.ddsCaps, &lpDDS_Flip)))
  170.  
  171.                 return Init_Fail(hWnd, "Error with lpDDPrimary->GetAttachedSurface !");
  172.  
  173.  
  174.  
  175.             lpDDS_Blit = lpDDS_Flip;
  176.  
  177.         }
  178.  
  179.         else lpDDS_Blit = lpDDS_Primary;
  180.  
  181.     }
  182.  
  183.     else
  184.  
  185.     {
  186.  
  187.         if (FAILED(lpDD->CreateClipper(0, &lpDDC_Clipper, NULL )))
  188.  
  189.             return Init_Fail(hWnd, "Error with lpDD->CreateClipper !");
  190.  
  191.  
  192.  
  193.         if (FAILED(lpDDC_Clipper->SetHWnd(0, hWnd)))
  194.  
  195.             return Init_Fail(hWnd, "Error with lpDDC_Clipper->SetHWnd !");
  196.  
  197.  
  198.  
  199.         if (FAILED(lpDDS_Primary->SetClipper(lpDDC_Clipper)))
  200.  
  201.             return Init_Fail(hWnd, "Error with lpDDS_Primary->SetClipper !");
  202.  
  203.     }
  204.  
  205.  
  206.  
  207.     memset(&ddsd, 0, sizeof(ddsd));
  208.  
  209.     ddsd.dwSize = sizeof(ddsd);
  210.  
  211.     ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH;
  212.  
  213.  
  214.  
  215.     if (Rend < 2)
  216.  
  217.     {
  218.  
  219.         ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY;
  220.  
  221.         ddsd.dwWidth = 336;
  222.  
  223.         ddsd.dwHeight = 240;
  224.  
  225.     }
  226.  
  227.     else
  228.  
  229.     {
  230.  
  231.         ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY;
  232.  
  233.         ddsd.dwWidth = 672; //Upth-Modif - was 640, but for single mode the value was 336, not 320.
  234.  
  235.         ddsd.dwHeight = 480;
  236.  
  237.     }
  238.  
  239.  
  240.  
  241.     if (FAILED(lpDD->CreateSurface(&ddsd, &lpDDS_Back, NULL)))
  242.  
  243.         return Init_Fail(hWnd, "Error with lpDD->CreateSurface !");
  244.  
  245.  
  246.  
  247.     if (!Full_Screen || (Rend >= 2 && (FS_No_Res_Change || Res_X != 640 || Res_Y != 480)))
  248.  
  249.         lpDDS_Blit = lpDDS_Back;
  250.  
  251.  
  252.  
  253.     if (Rend < 2)
  254.  
  255.     {
  256.  
  257.         memset(&ddsd, 0, sizeof(ddsd));
  258.  
  259.         ddsd.dwSize = sizeof(ddsd);
  260.  
  261.  
  262.  
  263.         if (FAILED(lpDDS_Back->GetSurfaceDesc(&ddsd)))
  264.  
  265.             return Init_Fail(hWnd, "Error with lpDD_Back->GetSurfaceDesc !");
  266.  
  267.  
  268.  
  269.         ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_PITCH | DDSD_LPSURFACE | DDSD_PIXELFORMAT;
  270.  
  271.         ddsd.dwWidth = 336;
  272.  
  273.         ddsd.dwHeight = 240;
  274.  
  275.         if (ddsd.ddpfPixelFormat.dwRGBBitCount > 16)
  276.  
  277.         {
  278.  
  279.             ddsd.lpSurface = &MD_Screen32[0];
  280.  
  281.             ddsd.lPitch = 336 * 4;
  282.  
  283.         }
  284.  
  285.         else
  286.  
  287.         {
  288.  
  289.             ddsd.lpSurface = &MD_Screen[0];
  290.  
  291.             ddsd.lPitch = 336 * 2;
  292.  
  293.         }
  294.  
  295.  
  296.  
  297.         if (FAILED(lpDDS_Back->SetSurfaceDesc(&ddsd, 0)))
  298.  
  299.             return Init_Fail(hWnd, "Error with lpDD_Back->SetSurfaceDesc !");
  300.  
  301.     }
  302.  
  303.  
  304.  
  305.     // make sure Bits32 is correct (which it could easily not be at this point)
  306.  
  307.     {
  308.  
  309.         memset(&ddsd, 0, sizeof(ddsd));
  310.  
  311.         ddsd.dwSize = sizeof(ddsd);
  312.  
  313.  
  314.  
  315.         if (FAILED(lpDDS_Blit->GetSurfaceDesc(&ddsd)))
  316.  
  317.             return Init_Fail(hWnd, "Error with lpDDS_Blit->GetSurfaceDesc !");
  318.  
  319.  
  320.  
  321.         Bits32 = (ddsd.ddpfPixelFormat.dwRGBBitCount > 16) ? 1 : 0;
  322.  
  323.  
  324.  
  325.         // also prevent the colors from sometimes being messed up for 1 frame if we changed color depth
  326.  
  327.  
  328.  
  329.         if(Bits32 && !oldBits32)
  330.  
  331.             for(int i = 0 ; i < 336 * 240 ; i++)
  332.  
  333.                 MD_Screen32[i] = DrawUtil::Pix16To32(MD_Screen[i]);
  334.  
  335.  
  336.  
  337.         if(!Bits32 && oldBits32)
  338.  
  339.             for(int i = 0 ; i < 336 * 240 ; i++)
  340.  
  341.                 MD_Screen[i] = DrawUtil::Pix32To16(MD_Screen32[i]);
  342.  
  343.     }
  344.  
  345.  
  346.  
  347.     // make sure the render mode is still valid (changing options in a certain order can make it invalid at this point)
  348.  
  349.     Set_Render(hWnd, Full_Screen, -1, false);
  350.  
  351.  
  352.  
  353.     // make sure the menu reflects the current mode (which it generally won't yet if we changed to/from 32-bit mode in this function)
  354.  
  355.     Build_Main_Menu();
  356.  
  357.  
  358.  
  359.     return 1;
  360.  
  361. }
  362.  
  363.  
  364.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement