EXPORT void CALL RomOpen (void)
{
	RECT bigrect, smallrect, statusrect;
	
	GetWindowRect(gfx.hWnd,&bigrect);
	GetClientRect(gfx.hWnd,&smallrect);
	int rightdiff = screen_width - smallrect.right;
	int bottomdiff = screen_height - smallrect.bottom;
	if (gfx.hStatusBar)
	{
		GetClientRect(gfx.hStatusBar, &statusrect);
		bottomdiff += statusrect.bottom;
	}
	MoveWindow(gfx.hWnd, bigrect.left, bigrect.top, bigrect.right - bigrect.left + rightdiff, bigrect.bottom - bigrect.top + bottomdiff, TRUE);
	

	DDPIXELFORMAT ftpixel;
	LPDIRECTDRAWCLIPPER lpddcl;

	res = DirectDrawCreateEx(0, (LPVOID*)&lpdd, IID_IDirectDraw7, 0);
	if(res != DD_OK) 
		fatalerror("Couldn't create a DirectDraw object");
	res = IDirectDraw_SetCooperativeLevel(lpdd, gfx.hWnd, DDSCL_NORMAL);
	if(res != DD_OK) 
		fatalerror("Couldn't set a cooperative level. Error code %x", res);

	memset(&ddsd, 0, sizeof(ddsd));
	ddsd.dwSize = sizeof(ddsd);
	ddsd.dwFlags = DDSD_CAPS;
	ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
	
	
	res = IDirectDraw_CreateSurface(lpdd, &ddsd, &lpddsprimary, 0);
	if(res != DD_OK)
		fatalerror("CreateSurface for a primary surface failed. Error code %x", res);

	
	ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT;
	ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
	ddsd.dwWidth = PRESCALE_WIDTH;
	ddsd.dwHeight = PRESCALE_HEIGHT;
	memset(&ftpixel, 0, sizeof(ftpixel));
	ftpixel.dwSize = sizeof(ftpixel);
	ftpixel.dwFlags = DDPF_RGB;
	ftpixel.dwRGBBitCount = 32;
	ftpixel.dwRBitMask = 0xff0000;
	ftpixel.dwGBitMask = 0xff00;
	ftpixel.dwBBitMask = 0xff;
	ddsd.ddpfPixelFormat = ftpixel;
	res = IDirectDraw_CreateSurface(lpdd, &ddsd, &lpddsback, 0);
	if (res == DDERR_INVALIDPIXELFORMAT)
		fatalerror("ARGB8888 is not supported. You can try changing desktop color depth to 32-bit, but most likely that won't help.");
	else if(res != DD_OK)
		fatalerror("CreateSurface for a secondary surface failed. Error code %x", res);

	
	res = IDirectDrawSurface_GetSurfaceDesc(lpddsback, &ddsd);
	if (res != DD_OK)
		fatalerror("GetSurfaceDesc failed.");
	if ((ddsd.lPitch & 3) || ddsd.lPitch < (PRESCALE_WIDTH << 2))
		fatalerror("Pitch of a secondary surface is either not 32 bit aligned or two small.");
	pitchindwords = ddsd.lPitch >> 2;

	
	res = IDirectDraw_CreateClipper(lpdd, 0, &lpddcl, 0);
	if (res != DD_OK)
		fatalerror("Couldn't create a clipper.");
	res = IDirectDrawClipper_SetHWnd(lpddcl, 0, gfx.hWnd);
	if (res != DD_OK)
		fatalerror("Couldn't register a windows handle as a clipper.");
	res = IDirectDrawSurface_SetClipper(lpddsprimary, lpddcl);
	if (res != DD_OK)
		fatalerror("Couldn't attach a clipper to a surface.");

	
	src.top = src.left = 0; 
	src.bottom = 0;
	src.right = PRESCALE_WIDTH;

	POINT p;
	p.x = p.y = 0;
	GetClientRect(gfx.hWnd, &dst);
	ClientToScreen(gfx.hWnd, &p);
	OffsetRect(&dst, p.x, p.y);
	GetClientRect(gfx.hStatusBar, &statusrect);
	dst.bottom -= statusrect.bottom;

	rdp_init();


}
Beispiel #2
0
BOOL DDInit(int mode)
{
	LPDIRECTDRAW lpdd;
	DDCAPS ddcaps, ddcaps2;
	HRESULT ddresult;
	int num;

	DDWnd = GetLibraryWindow();

//	Create Direct Draw Object (Use Emulation if Hardware is off)
	if (!_lpDD)	{
		ddresult = DirectDrawCreate(NULL, &lpdd, NULL);
		
		if (ddresult == DDERR_NODIRECTDRAWHW) {
         ddresult = DirectDrawCreate( (LPVOID) DDCREATE_EMULATIONONLY, &lpdd, NULL );
			if (!CheckDDResult(ddresult, "InitDD:DirectDrawCreate emulation"))
				return FALSE;
			DDUseEmulation = TRUE;
			logentry("DirectDraw: forcing emulation.\n");
		}
		else if (ddresult != DD_OK) return FALSE;
		logentry("DirectDraw: DirectX API hardware compliant.\n");
	}
	else return FALSE;

	atexit(DDKill);

//	Determine hardware caps
//	Determine capture mode (fullscreen takes exclusive, window is normal)
	if (mode == DDGR_FULLSCREEN) {
		DWORD flags;

		flags = DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN;

	#ifndef NDEBUG
		if (!FindArg("-nomodex")) flags |= DDSCL_ALLOWMODEX;
	#else
		flags |= DDSCL_ALLOWMODEX;
	#endif

		if (!FindArg("-disallowreboot")) flags |= DDSCL_ALLOWREBOOT;
 
		ddresult = IDirectDraw_SetCooperativeLevel(lpdd, DDWnd, flags); 

		if (!CheckDDResult(ddresult, "DDInit::SetCooperativeLevel")) {	
			IDirectDraw_Release(lpdd);
			return FALSE;
		}

		_DDExclusive = TRUE;
		_DDFullScreen = TRUE;			
	}
	else if (mode == DDGR_EXWINDOW) {
		ddresult = IDirectDraw_SetCooperativeLevel(lpdd, DDWnd,
										DDSCL_EXCLUSIVE |
										DDSCL_FULLSCREEN);
		if (!CheckDDResult(ddresult, "DDInit::SetCooperativeLevel"))
			return FALSE;
		_DDExclusive = TRUE;
		_DDFullScreen = FALSE;
	}
	else if (mode == DDGR_WINDOW) {
		ddresult = IDirectDraw_SetCooperativeLevel(lpdd, DDWnd,
										DDSCL_NORMAL);
		if (!CheckDDResult(ddresult, "DDInit::SetCooperativeLevel"))
			return FALSE;
		_DDExclusive = FALSE;
		_DDFullScreen = FALSE;
	}
	else return FALSE;
	
//	Get Display modes/Window Sizes
//	Force invalidation of all modes for now
	for (num = 0; num < 16; num++)
	{
		_DDModeList[num].rw = _DDModeList[num].w = -1;
		_DDModeList[num].rh = _DDModeList[num].h = -1;
	}

	W95DisplayMode = SM95_640x480x8;
	num = 0;
	if (mode == DDGR_FULLSCREEN) {
		ddresult = IDirectDraw_EnumDisplayModes(lpdd, 0, NULL, 0, 
									EnumDispModesCB);
		if(!CheckDDResult(ddresult, "DDInit::EnumDisplayModes")) {
			IDirectDraw_Release(lpdd);
			return FALSE;
		}
	}
	else if (mode == DDGR_EXWINDOW) {
		_DDModeList[SM95_320x200x8X].rw = 320;
		_DDModeList[SM95_320x200x8X].rh = 200;
		_DDModeList[SM95_320x200x8X].w = 640;
		_DDModeList[SM95_320x200x8X].h = 480;
		_DDModeList[SM95_320x200x8X].emul = 1;
		_DDModeList[SM95_320x200x8X].dbuf = 0;
		_DDModeList[SM95_320x200x8X].modex = 0;
		_DDModeList[SM95_320x200x8X].paged = 0; 

		_DDModeList[SM95_640x480x8].rw = 640;
		_DDModeList[SM95_640x480x8].rh = 480;
		_DDModeList[SM95_640x480x8].w = 640;
		_DDModeList[SM95_640x480x8].h = 480;
		_DDModeList[SM95_640x480x8].emul = 1;
		_DDModeList[SM95_640x480x8].dbuf = 0;
		_DDModeList[SM95_640x480x8].modex = 0;
		_DDModeList[SM95_640x480x8].paged = 0; 

		_DDModeList[SM95_800x600x8].rw = 800;
		_DDModeList[SM95_800x600x8].rh = 600;
		_DDModeList[SM95_800x600x8].w = 640;
		_DDModeList[SM95_800x600x8].h = 480;
		_DDModeList[SM95_800x600x8].emul = 1;
		_DDModeList[SM95_800x600x8].dbuf = 0;
		_DDModeList[SM95_800x600x8].modex = 0;
		_DDModeList[SM95_800x600x8].paged = 0; 
		_DDNumModes = 3;
	}
	else if (mode == DDGR_WINDOW) {	
		_DDModeList[SM95_320x200x8X].rw = 320;
		_DDModeList[SM95_320x200x8X].rh = 200;
		_DDModeList[SM95_320x200x8X].w = 640;
		_DDModeList[SM95_320x200x8X].h = 480;
		_DDModeList[SM95_320x200x8X].emul = 1;
		_DDModeList[SM95_320x200x8X].dbuf = 0;
		_DDModeList[SM95_320x200x8X].modex = 0;
		_DDModeList[SM95_320x200x8X].paged = 0; 

		_DDModeList[SM95_640x480x8].rw = 640;
		_DDModeList[SM95_640x480x8].rh = 480;
		_DDModeList[SM95_640x480x8].w = 640;
		_DDModeList[SM95_640x480x8].h = 480;
		_DDModeList[SM95_640x480x8].emul = 1;
		_DDModeList[SM95_640x480x8].dbuf = 0;
		_DDModeList[SM95_640x480x8].modex = 0;
		_DDModeList[SM95_640x480x8].paged = 0; 

		_DDModeList[SM95_800x600x8].rw = 800;
		_DDModeList[SM95_800x600x8].rh = 600;
		_DDModeList[SM95_800x600x8].w = 800;
		_DDModeList[SM95_800x600x8].h = 600;
		_DDModeList[SM95_800x600x8].emul = 1;
		_DDModeList[SM95_800x600x8].dbuf = 0;
		_DDModeList[SM95_800x600x8].modex = 0;
		_DDModeList[SM95_800x600x8].paged = 0; 
		_DDNumModes = 3;
	}
	else return FALSE;

//	Set appropriate display mode or window mode

	_lpDD = lpdd;

	memset(&ddcaps, 0, sizeof(ddcaps));
	ddcaps.dwSize = sizeof(ddcaps);
	ddcaps2.dwSize = sizeof(ddcaps);
	ddresult = IDirectDraw_GetCaps(_lpDD, &ddcaps, NULL);
	if (!CheckDDResult(ddresult, "InitDD::GetCaps")) 
		return FALSE;

	logentry("DirectDraw: VRAM free:  %d\n", ddcaps.dwVidMemFree);	
	logentry("DirectDraw: VRAM total: %d\n", ddcaps.dwVidMemTotal);

#ifndef NDEBUG
	if (FindArg("-TsengDebug1")) {
		IDirectDraw_Release(lpdd);
		return FALSE;
	}
#endif

	DDSetDisplayMode(W95DisplayMode, 0);

#ifndef NDEBUG
	if (FindArg("-TsengDebug2")) {
		IDirectDraw_Release(lpdd);
		return FALSE;
	}
#endif
	
	// If 'windowed' do this.
	if (!_DDFullScreen) 
	{
			ddresult = IDirectDraw_CreateClipper(_lpDD, 0, &_lpDDClipper, NULL);
			if (!CheckDDResult(ddresult, "DDCreateScreen::CreateClipper"))
				return FALSE;

			ddresult = IDirectDrawClipper_SetHWnd(_lpDDClipper, 0, DDWnd);
			if (!CheckDDResult(ddresult, "DDCreateScreen::SetHWnd"))
				return FALSE;

			ddresult = IDirectDrawSurface_SetClipper(_lpDDSPrimary, _lpDDClipper);
			if (!CheckDDResult(ddresult, "DDCreateScreen::SetClipper"))
				return FALSE;
	}

//	Register Optimizations

	ddcaps.dwSize = sizeof(ddcaps);
	ddcaps2.dwSize = sizeof(ddcaps);
	ddresult = IDirectDraw_GetCaps(lpdd, &ddcaps, &ddcaps2);
	if (!CheckDDResult(ddresult, "DDInit::GetCaps"))
		return FALSE;

#ifndef NDEBUG
	if (FindArg("-TsengDebug3")) {
		IDirectDraw_Release(lpdd);
		return FALSE;
	}
#endif

	if (FindArg("-vidram")) {
		logentry("DirectDraw: Forcing VRAM rendering.\n");
		_DDSysMemSurfacing = FALSE;
	}
	else if (FindArg("-sysram")) {
		logentry("DirectDraw: Forcing SRAM rendering.\n");
		_DDSysMemSurfacing = TRUE;
	}
	else if (ddcaps.dwCaps & DDCAPS_BANKSWITCHED) {
		logentry("DirectDraw: Hardware is bank-switched.  Using SRAM rendering.\n");
		_DDSysMemSurfacing = TRUE;
	}
	else {
		logentry("DirectDraw: Hardware is not bank-switched.  Using VRAM rendering.\n");
		_DDSysMemSurfacing = FALSE;
	}
		
	if (ddcaps.dwCaps	& DDCAPS_COLORKEYHWASSIST) 
		ddDriverCaps.hwcolorkey = 1;
	else 
		ddDriverCaps.hwcolorkey = 0;
	if (ddcaps.dwCaps & DDCAPS_BLTSTRETCH)
		ddDriverCaps.hwbltstretch = 1;
	else 
		ddDriverCaps.hwbltstretch = 0;
		

//@@	mprintf((0, "DD::Hardware="));
//@@	if (ddcaps.dwCaps & DDCAPS_NOHARDWARE) mprintf((0, "Off\n"));
//@@	else mprintf((0, "On\n"));
//@@
//@@	mprintf((0, "DD::VideoMem=%u bytes\n", ddcaps.dwVidMemTotal));

//@@	mprintf((0, "DD::SrcColorKey="));	
//@@	if (ddcaps.dwCKeyCaps & DDCKEYCAPS_SRCBLT) mprintf((0, "Hardware\n"));
//@@	else mprintf((0, "Emulation\n"));

	return TRUE;
}
Beispiel #3
0
static int ResizeDD(int fullscreen)
{
    HRESULT ddrval;
    DDSURFACEDESC ddsd;
    /*DDCAPS2 ddscaps; */
    LPDIRECTDRAWCLIPPER pClipper;
    int dxwidth;
    int dxheight;
    int dxbpp;

    // free DirectX objects
    if (lpSurfaces[0])
	IDirectDrawSurface_Release(lpSurfaces[0]);
    lpSurfaces[0] = NULL;
    if (dxPalette)
	IDirectDrawPalette_Release(dxPalette);
    dxPalette = NULL;
    /* Set cooperative level */
    ddrval = IDirectDraw2_SetCooperativeLevel(lpDD2, hWnd,
					      fullscreen
					      ? (DDSCL_FULLSCREEN |
						 DDSCL_EXCLUSIVE |
						 DDSCL_ALLOWREBOOT)
					      : DDSCL_NORMAL);
    if (ddrval != DD_OK) {
	DeInitDD();
	x_error("Failed to set cooperative level");
	return 0;
    }

    if (fullscreen) {
	if (sscanf(dxsize, "%ix%ix%i", &dxwidth, &dxheight, &dxbpp) != 3) {
	    dxwidth = DXWIDTH;
	    dxheight = DXHEIGHT;
	    dxbpp = DXBPP;
	}
	displayX = dxwidth;
	displayY = dxheight;
	bitDepth = dxbpp;
	if (bitDepth < 10)
	    bitDepth = 8;
	if (bitDepth >= 10 && bitDepth < 20)
	    bitDepth = 16;
	if (bitDepth >= 20 && bitDepth < 28)
	    bitDepth = 24;
	if (bitDepth >= 32 && bitDepth < 32)
	    bitDepth = 32;

	/* set resolution and bit depth */
	ddrval =
	    IDirectDraw2_SetDisplayMode(lpDD2, displayX, displayY,
					bitDepth, 0, 0);
	if (ddrval != DD_OK) {
	    /* The display mode cannot be changed. 
	       The mode is either not supported or 
	       another application has exclusive mode.

	       Try 320x200x256 and 640x480x256 modes before giving up */
	    displayX = 320;
	    displayY = 200;
	    bitDepth = 8;
	    ddrval =
		IDirectDraw2_SetDisplayMode(lpDD2, displayX, displayY,
					    bitDepth, 0, 0);
	    if (ddrval != DD_OK) {
		displayY = 240;
		if (ddrval != DD_OK) {
		    displayX = 640;
		    displayY = 480;
		    ddrval =
			IDirectDraw2_SetDisplayMode(lpDD2, displayX,
						    displayY, bitDepth, 0,
						    0);
		    if (ddrval != DD_OK) {
			/* Bad luck... give up. */
			DeInitDD();
			return 0;
		    }
		}
	    }
	}
	SetRect(&rcViewport, 0, 0, displayX, displayY);
	rcScreen = rcViewport;
    } else {
	/* Get the dimensions of the viewport and screen bounds */
	GetClientRect(hWnd, &rcViewport);
	GetClientRect(hWnd, &rcScreen);
	ClientToScreen(hWnd, (POINT *) & rcScreen.left);
	ClientToScreen(hWnd, (POINT *) & rcScreen.right);
	/*bitDepth = GetDeviceCaps (hDC, BITSPIXEL); */

	/* Create clipper object for window */
	ddrval = IDirectDraw_CreateClipper(lpDD, 0, &pClipper, NULL);
	if (ddrval != DD_OK) {
	    DeInitDD();
	    x_error("Failed to create clipper object");
	    return 0;
	}
	/* Asociate it */
	IDirectDrawClipper_SetHWnd(pClipper, 0, hWnd);
    }
    /* Create the primary surface with one back buffer */
    CalculateBITMAPINFO();	// calculate BITMAPINFO structure

    memset(&ddsd, 0, sizeof(ddsd));
    ddsd.dwSize = sizeof(ddsd);
    ddsd.dwFlags = DDSD_CAPS;
    ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;

    ddrval = IDirectDraw_CreateSurface(lpDD, &ddsd, &lpSurfaces[0], NULL);
    if (ddrval != DD_OK) {
	DeInitDD();
	x_error("Failed to create flipping surface");
	return 0;
    }

    if (!fullscreen) {
	IDirectDrawSurface_SetClipper(lpSurfaces[0], pClipper);
	IDirectDrawClipper_Release(pClipper);
	if (IDirectDrawSurface_GetSurfaceDesc(lpSurfaces[0], &ddsd) !=
	    DD_OK) {
	    DeInitDD();
	    x_error("Failed to get pixel format");
	    return 0;
	}
	bitDepth = ddsd.ddpfPixelFormat.u1.dwRGBBitCount;
    }

    if (bitDepth == 8) {
	/* create palette */
	ddrval =
	    IDirectDraw_CreatePalette(lpDD, DDPCAPS_8BIT,
				      (LPPALETTEENTRY) bmp->bmiColors,
				      &dxPalette, NULL);
	if (ddrval != DD_OK) {
	    DeInitDD();
	    x_error("Failed to create palette");
	    return 0;
	}

	/* set palette */
	IDirectDrawSurface_SetPalette(lpSurfaces[0], dxPalette);
    }
    if (fullscreen)
	SetCursor(NULL);
    needredraw = 1;
    return 1;

}
Beispiel #4
0
static boolean CreatePrimary( win32_driver_t * win32_driver )
{
  LPDIRECTDRAW			ddobj;
  DDSURFACEDESC			ddsd;
  HRESULT					result;

  /* create direct draw object */

  result = DirectDrawCreate( 0, &ddobj, 0 );
  if( result != DD_OK )
    {
      Error( 0, "DirectDrawCreate : error %ld", result );
      xprintf(win32_driver->xine, XINE_VERBOSITY_DEBUG, "vo_out_directx : DirectDrawCreate : error %ld\n", result );
      return 0;
    }

  /* set cooperative level */

  result = IDirectDraw_SetCooperativeLevel( ddobj, win32_driver->win32_visual->WndHnd, DDSCL_NORMAL );
  if( result != DD_OK )
    {
      Error( 0, "SetCooperativeLevel : error 0x%lx", result );
      return 0;
    }

  /* try to get new interface */

  result = IDirectDraw_QueryInterface( ddobj, &IID_IDirectDraw, (LPVOID *) &win32_driver->ddobj );
  if( result != DD_OK )
    {
      Error( 0, "ddobj->QueryInterface : DirectX required" );
      return 0;
    }

  /* release our old interface */

  IDirectDraw_Release( ddobj );

  /* create primary_surface */

  memset( &ddsd, 0, sizeof( ddsd ) );
  ddsd.dwSize         = sizeof( ddsd );
  ddsd.dwFlags        = DDSD_CAPS;
  ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;

  result = IDirectDraw_CreateSurface( win32_driver->ddobj, &ddsd, &win32_driver->primary, 0 );
  if( result != DD_OK )
    {
      Error( 0, "CreateSurface ( primary ) : error 0x%lx", result );
      return 0;
    }

  /* create our clipper object */

  result = IDirectDraw_CreateClipper( win32_driver->ddobj, 0, &win32_driver->ddclipper, 0 );
  if( result != DD_OK )
    {
      Error( 0, "CreateClipper : error 0x%lx", result );
      return 0;
    }

  /* associate our clipper with our window */

  result = IDirectDrawClipper_SetHWnd( win32_driver->ddclipper, 0, win32_driver->win32_visual->WndHnd );
  if( result != DD_OK )
    {
      Error( 0, "ddclipper->SetHWnd : error 0x%lx", result );
      return 0;
    }

  /* associate our primary surface with our clipper */

  result = IDirectDrawSurface_SetClipper( win32_driver->primary, win32_driver->ddclipper );
  if( result != DD_OK )
    {
      Error( 0, "ddclipper->SetHWnd : error 0x%lx", result );
      return 0;
    }

  /* store our objects in our visual struct */

  UpdateRect( win32_driver->win32_visual );

  return 1;
}
EXPORT int CALL angrylionRomOpen (void)
{
#ifndef HAVE_DIRECTDRAW
   screen_width = SCREEN_WIDTH;
   screen_height = SCREEN_HEIGHT;
   blitter_buf = (uint32_t*)calloc(screen_width * screen_height, sizeof(uint32_t));
   pitchindwords = screen_width * 4;
   screen_pitch = PRESCALE_WIDTH << 2;
#else
    DDPIXELFORMAT ftpixel;
    LPDIRECTDRAWCLIPPER lpddcl;
    RECT bigrect, smallrect, statusrect;
    POINT p;
    int rightdiff;
    int bottomdiff;
    GetWindowRect(gfx.hWnd,&bigrect);
    GetClientRect(gfx.hWnd,&smallrect);
    rightdiff = screen_width - smallrect.right;
    bottomdiff = screen_height - smallrect.bottom;

    if (gfx.hStatusBar)
    {
        GetClientRect(gfx.hStatusBar, &statusrect);
        bottomdiff += statusrect.bottom;
    }
    MoveWindow(gfx.hWnd, bigrect.left, bigrect.top, bigrect.right - bigrect.left + rightdiff, bigrect.bottom - bigrect.top + bottomdiff, true);

    res = DirectDrawCreateEx(0, (LPVOID*)&lpdd, &IID_IDirectDraw7, 0);
    if (res != DD_OK)
    {
        DisplayError("Couldn't create a DirectDraw object.");
        return; /* to-do:  move to InitiateGFX? */
    }
    res = IDirectDraw_SetCooperativeLevel(lpdd, gfx.hWnd, DDSCL_NORMAL);
    if (res != DD_OK)
    {
        DisplayError("Couldn't set a cooperative level.");
        return; /* to-do:  move to InitiateGFX? */
    }

    zerobuf(&ddsd, sizeof(ddsd));
    ddsd.dwSize = sizeof(ddsd);
    ddsd.dwFlags = DDSD_CAPS;
    ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
    res = IDirectDraw_CreateSurface(lpdd, &ddsd, &lpddsprimary, 0);
    if (res != DD_OK)
    {
        DisplayError("CreateSurface for a primary surface failed.");
        return; /* to-do:  move to InitiateGFX? */
    }

    ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT;
    ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
    ddsd.dwWidth = PRESCALE_WIDTH;
    ddsd.dwHeight = PRESCALE_HEIGHT;
    zerobuf(&ftpixel, sizeof(ftpixel));
    ftpixel.dwSize = sizeof(ftpixel);
    ftpixel.dwFlags = DDPF_RGB;
    ftpixel.dwRGBBitCount = 32;
    ftpixel.dwRBitMask = 0xff0000;
    ftpixel.dwGBitMask = 0xff00;
    ftpixel.dwBBitMask = 0xff;
    ddsd.ddpfPixelFormat = ftpixel;
    res = IDirectDraw_CreateSurface(lpdd, &ddsd, &lpddsback, 0);
    if (res == DDERR_INVALIDPIXELFORMAT)
    {
        DisplayError(
            "ARGB8888 is not supported. You can try changing desktop color "\
            "depth to 32-bit, but most likely that won't help.");
        return; /* InitiateGFX fails. */
    }
    else if (res != DD_OK)
    {
        DisplayError("CreateSurface for a secondary surface failed.");
        return; /* InitiateGFX should fail. */
    }

    res = IDirectDrawSurface_GetSurfaceDesc(lpddsback, &ddsd);
    if (res != DD_OK)
    {
        DisplayError("GetSurfaceDesc failed.");
        return; /* InitiateGFX should fail. */
    }
    if ((ddsd.lPitch & 3) || ddsd.lPitch < (PRESCALE_WIDTH << 2))
    {
        DisplayError(
            "Pitch of a secondary surface is either not 32 bit aligned or "\
            "too small.");
        return; /* InitiateGFX should fail. */
    }
    pitchindwords = ddsd.lPitch >> 2;

    res = IDirectDraw_CreateClipper(lpdd, 0, &lpddcl, 0);
    if (res != DD_OK)
    {
        DisplayError("Couldn't create a clipper.");
        return; /* InitiateGFX should fail. */
    }
    res = IDirectDrawClipper_SetHWnd(lpddcl, 0, gfx.hWnd);
    if (res != DD_OK)
    {
        DisplayError("Couldn't register a windows handle as a clipper.");
        return; /* InitiateGFX should fail. */
    }
    res = IDirectDrawSurface_SetClipper(lpddsprimary, lpddcl);
    if (res != DD_OK)
    {
        DisplayError("Couldn't attach a clipper to a surface.");
        return; /* InitiateGFX should fail. */
    }

    src.top = src.left = 0; 
    src.bottom = 0;
#if SCREEN_WIDTH < PRESCALE_WIDTH
    src.right = PRESCALE_WIDTH - 1; /* fix for undefined video card behavior */
#else
    src.right = PRESCALE_WIDTH;
#endif
    p.x = p.y = 0;
    GetClientRect(gfx.hWnd, &dst);
    ClientToScreen(gfx.hWnd, &p);
    OffsetRect(&dst, p.x, p.y);
    GetClientRect(gfx.hStatusBar, &statusrect);
    dst.bottom -= statusrect.bottom;
#endif

    rdp_init();
    overlay = 0;
    return 1;
}