Ejemplo n.º 1
0
extern "C" HRESULT
InitDirectDraw(HWND hWnd, int w, int h, void* pixelData)
{
    WNDCLASS                    wc;
    DDSURFACEDESC               ddsd;
    HRESULT                     hRet;

    hRet = DirectDrawCreate(NULL, &g_pDD, NULL);
    if (hRet != DD_OK)
        return InitFail(hWnd, hRet, szDDrawFailedMsg);

    // Get exclusive mode
    hRet = g_pDD->SetCooperativeLevel(hWnd, DDSCL_FULLSCREEN); //DDSCL_NORMAL);//
    if (hRet != DD_OK)
        return InitFail(hWnd, hRet, szSetCooperativeFailMsg);

#if 0
	DDCAPS ddCaps;
    DDCAPS ddHelCaps;

    g_pDD->GetCaps(&ddCaps, &ddHelCaps);
    if (!(ddCaps.ddsCaps.dwCaps & DDSCAPS_BACKBUFFER)) 
    {
        return InitFail(hWnd, E_FAIL, szNoBackBufferMsg);
    }

    if (!(ddCaps.ddsCaps.dwCaps & DDSCAPS_FLIP)) 
    {
        return InitFail(hWnd, E_FAIL, szNoFlipSurfacesMsg);
    }
#endif
    // Create the primary surface with 1 back buffer
    memset(&ddsd, 0, sizeof(ddsd));
    ddsd.dwSize = sizeof(ddsd);
    ddsd.dwFlags = DDSD_CAPS;// | DDSD_BACKBUFFERCOUNT;
    ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
						 //| DDSCAPS_FLIP;
    
	//ddsd.dwBackBufferCount = 1;

    hRet = g_pDD->CreateSurface(&ddsd, &g_pDDSPrimary, NULL);
    if (hRet != DD_OK)
    {
        return InitFail(hWnd, hRet, szCreateSurfaceFailMsg);
    }

	DWORD dwID;
	g_hRefreshScreen = CreateEvent(NULL, FALSE, FALSE, NULL);
	CreateThread(NULL, 0, ScreenRefresThread, NULL, 0, &dwID);
	return DD_OK;

#if 0
#define WIDTH  w // in pixels
#define HEIGHT h
#define DEPTH  2  // in bytes (2 bytes == 16 bits)
	if (ddsd.dwBackBufferCount > 0) {
		hRet = g_pDDSPrimary->EnumAttachedSurfaces(&g_pDDSBack, EnumFunction);
		if (hRet != DD_OK)
			return InitFail(hWnd, hRet, szEnumAttachedSurfacesFailMsg);
	}

	ZeroMemory(&ddsd, sizeof(DDSURFACEDESC));
    ZeroMemory(&ddsd.ddpfPixelFormat, sizeof(DDPIXELFORMAT));
    ddsd.dwSize = sizeof(ddsd);

	hRet = g_pDDSBack->Lock(NULL, &ddsd, DDLOCK_WAITNOTBUSY, NULL);
	g_pDDSPrimary->Unlock(NULL);

    ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_LPSURFACE |
                    DDSD_PITCH | DDSD_PIXELFORMAT;
    ddsd.dwWidth = WIDTH;
    ddsd.dwHeight= HEIGHT;
    //ddsd.lPitch  = (LONG)DEPTH * WIDTH;
    ddsd.lpSurface = pixelData;
 /*
    // Set up the pixel format for 24-bit RGB (8-8-8).
    ddsd.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
    ddsd.ddpfPixelFormat.dwFlags= DDPF_RGB;
    ddsd.ddpfPixelFormat.dwRGBBitCount = (DWORD)DEPTH*8;
	ddsd.ddpfPixelFormat.dwRBitMask = 0xF800;//surface->format->Rmask;
	ddsd.ddpfPixelFormat.dwGBitMask = 0x07E0;//surface->format->Gmask;
	ddsd.ddpfPixelFormat.dwBBitMask = 0x001F;//surface->format->Bmask;
    //ddsd.ddpfPixelFormat.dwRBitMask    = 0x00FF0000;
    //ddsd.ddpfPixelFormat.dwGBitMask    = 0x0000FF00;
    //ddsd.ddpfPixelFormat.dwBBitMask    = 0x000000FF;
 */
    // Create the surface
    hRet = g_pDD->CreateSurface(&ddsd, &g_pDDSBack, NULL);
	if (hRet != DD_OK)
		return InitFail(hWnd, hRet, szEnumAttachedSurfacesFailMsg);
	return DD_OK;
#endif

	if (ddsd.dwBackBufferCount > 0) {
		hRet = g_pDDSPrimary->EnumAttachedSurfaces(&g_pDDSBack, EnumFunction);
		if (hRet != DD_OK)
			return InitFail(hWnd, hRet, szEnumAttachedSurfacesFailMsg);
	}

	return DD_OK;

#if 1
	ddsd.dwSize = sizeof(ddsd);
	ddsd.dwFlags = (DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS |
					DDSD_PITCH | DDSD_LPSURFACE | DDSD_PIXELFORMAT);
	ddsd.dwWidth = w;
	ddsd.dwHeight= h;
#if defined(NONAMELESSUNION)
	ddsd.u1.lPitch = SDL_CalculatePitch(w, 2);;
#else
	ddsd.lPitch = SDL_CalculatePitch(w, 2);;
#endif
	ddsd.lpSurface = pixelData;

	const int SDL_HWSURFACE = 1;
	int flag = 1;

	if ( (flag & SDL_HWSURFACE) == SDL_HWSURFACE ) {
		ddsd.ddsCaps.dwCaps =
				(/*DDSCAPS_OFFSCREENPLAIN | */DDSCAPS_VIDEOMEMORY);
	} else {
		ddsd.ddsCaps.dwCaps =
				(/*DDSCAPS_OFFSCREENPLAIN | */DDSCAPS_SYSTEMMEMORY);
	}

	ddsd.ddpfPixelFormat.dwSize = sizeof(ddsd.ddpfPixelFormat);
	ddsd.ddpfPixelFormat.dwFlags = DDPF_RGB;
//	if ( surface->format->palette ) {
//		ddsd.ddpfPixelFormat.dwFlags |= DDPF_PALETTEINDEXED8;
//	}
#if defined(NONAMELESSUNION)
	ddsd.ddpfPixelFormat.u1.dwRGBBitCount = surface->format->BitsPerPixel;
	ddsd.ddpfPixelFormat.u2.dwRBitMask = surface->format->Rmask;
	ddsd.ddpfPixelFormat.u3.dwGBitMask = surface->format->Gmask;
	ddsd.ddpfPixelFormat.u4.dwBBitMask = surface->format->Bmask;
#else
	ddsd.ddpfPixelFormat.dwRGBBitCount = 16;//surface->format->BitsPerPixel;
	ddsd.ddpfPixelFormat.dwRBitMask = 0xF800;//surface->format->Rmask;
	ddsd.ddpfPixelFormat.dwGBitMask = 0x07E0;//surface->format->Gmask;
	ddsd.ddpfPixelFormat.dwBBitMask = 0x001F;//surface->format->Bmask;
#endif

	/* Create the DirectDraw video surface */
	//if ( requested != NULL ) {
	//	g_pDDSOne = requested;
	//} else 
	{
		hRet = g_pDD->CreateSurface(&ddsd, &g_pDDSBack, NULL); 
#if 0
		if ( hRet != DD_OK ) {
			SetDDerror("DirectDraw2::CreateSurface", hRet);
			goto error_end;
		}
		hRet = g_pDDSBack->QueryInterface(
			&IID_IDirectDrawSurface3, (LPVOID *)&g_pDDSOne);
		g_pDDSBack->Release();
		if ( hRet != DD_OK ) {
			SetDDerror("DirectDrawSurface::QueryInterface", hRet);
			goto error_end;
		}
#endif
	}
#if 0
	if ( (flag & SDL_HWSURFACE) == SDL_HWSURFACE ) {
		/* Check to see whether the surface actually ended up
		   in video memory, and fail if not.  We expect the
		   surfaces we create here to actually be in hardware!
		*/
		hRet = g_pDDSOne->GetCaps(&ddsd.ddsCaps);
		if ( hRet != DD_OK ) {
			//SetDDerror("DirectDrawSurface3::GetCaps", hRet);
			goto error_end;
		}
		if ( (ddsd.ddsCaps.dwCaps & DDSCAPS_VIDEOMEMORY) !=
							DDSCAPS_VIDEOMEMORY ) {
			//SDL_SetError("No room in video memory");
			goto error_end;
		}
	} else {
		/* Try to hook our surface memory */
		ddsd.dwFlags = DDSD_LPSURFACE;
		ddsd.lpSurface = pixelData;//surface->pixels;
		hRet = g_pDDSOne->SetSurfaceDesc(
								&ddsd, 0);
		if ( hRet != DD_OK ) {
			//SetDDerror("DirectDraw2::SetSurfaceDesc", hRet);
			goto error_end;
		}
	
	}

	/* Make sure the surface format was set properly */
	SDL_memset(&ddsd, 0, sizeof(ddsd));
	ddsd.dwSize = sizeof(ddsd);
	hRet = IDirectDrawSurface3_Lock(g_pDDSOne, NULL,
		&ddsd, (DDLOCK_NOSYSLOCK|DDLOCK_WAIT), NULL);
	if ( hRet != DD_OK ) {
		//SetDDerror("DirectDrawSurface3::Lock", hRet);
		goto error_end;
	}
	IDirectDrawSurface3_Unlock(g_pDDSOne, NULL);

	if ( (flag & SDL_HWSURFACE) == SDL_SWSURFACE ) {
		if ( ddsd.lpSurface != surface->pixels ) {
			//SDL_SetError("DDraw didn't use SDL surface memory");
			goto error_end;
		}
		if (
#if defined(NONAMELESSUNION)
			ddsd.u1.lPitch
#else
			ddsd.lPitch
#endif
				 != (LONG)surface->pitch ) {
			//SDL_SetError("DDraw created surface with wrong pitch");
			goto error_end;
		}
	} else {
#if defined(NONAMELESSUNION)
		surface->pitch = (Uint16)ddsd.u1.lPitch;
#else
		surface->pitch = (Uint16)ddsd.lPitch;
#endif
	}
#if defined(NONAMELESSUNION)
	if ( (ddsd.ddpfPixelFormat.u1.dwRGBBitCount != 
					surface->format->BitsPerPixel) ||
	     (ddsd.ddpfPixelFormat.u2.dwRBitMask != surface->format->Rmask) ||
	     (ddsd.ddpfPixelFormat.u3.dwGBitMask != surface->format->Gmask) ||
	     (ddsd.ddpfPixelFormat.u4.dwBBitMask != surface->format->Bmask) ){
#else
	if ( (ddsd.ddpfPixelFormat.dwRGBBitCount != 
					surface->format->BitsPerPixel) ||
	     (ddsd.ddpfPixelFormat.dwRBitMask != surface->format->Rmask) ||
	     (ddsd.ddpfPixelFormat.dwGBitMask != surface->format->Gmask) ||
	     (ddsd.ddpfPixelFormat.dwBBitMask != surface->format->Bmask) ){
#endif
		//SDL_SetError("DDraw didn't use SDL surface description");
		goto error_end;
	}
	if ( (ddsd.dwWidth != (DWORD)surface->w) ||
		(ddsd.dwHeight != (DWORD)surface->h) ) {
		//SDL_SetError("DDraw created surface with wrong size");
		goto error_end;
	}

	/* Set the surface private data */
	surface->flags |= flag;
	surface->hwdata->dd_surface = g_pDDSOne;
	if ( (surface->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF ) {
		LPDIRECTDRAWSURFACE3 dd_writebuf;

		ddsd.ddsCaps.dwCaps = DDSCAPS_BACKBUFFER;
		hRet = IDirectDrawSurface3_GetAttachedSurface(g_pDDSOne,
						&ddsd.ddsCaps, &dd_writebuf);
		if ( hRet != DD_OK ) {
			//SetDDerror("DirectDrawSurface3::GetAttachedSurface",
								hRet);
		} else {