#include "DDUtils.h"

LPDIRECTDRAWSURFACE DDLoadBitmap(LPDIRECTDRAW pDD,
						  LPCSTR szBitmap,
						  int dx,
						  int dy){
	HBITMAP hBitmap; 
    BITMAP Bitmap;
	DDSURFACEDESC ddsd; 
    LPDIRECTDRAWSURFACE pDDS;
	HDC hdcSurface;
	HDC hdcBitmap;

	// Win32 API로 비트맵을 읽어들임
	hBitmap = (HBITMAP)LoadImage(GetModuleHandle(NULL), szBitmap, IMAGE_BITMAP, dx, dy, LR_CREATEDIBSECTION);
	if (hBitmap == NULL) 
		hBitmap = (HBITMAP)LoadImage(NULL, szBitmap, IMAGE_BITMAP, dx, dy, LR_LOADFROMFILE|LR_CREATEDIBSECTION); 
	if (hBitmap == NULL)
		return NULL;

	// 비트맵의 크기를 얻어냄
	GetObject(hBitmap, sizeof(Bitmap), &Bitmap);  

    // 비트맵을 저장할 다이렉트 드로우 표면을 만듦
	ddsd.dwSize = sizeof(ddsd); 
    ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT |DDSD_WIDTH; 
    ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN; 
    ddsd.dwWidth = Bitmap.bmWidth;
	ddsd.dwHeight = Bitmap.bmHeight;  
    if (pDD->CreateSurface(&ddsd, &pDDS, NULL) != DD_OK)
		return NULL;  

	// 비트맵을 표면으로 복사한다.
	if(pDDS->GetDC(&hdcSurface) != DD_OK){
		DeleteObject(hBitmap);
		return NULL;
	}
	hdcBitmap = CreateCompatibleDC(hdcSurface);
	SelectObject(hdcBitmap, hBitmap);
	BitBlt(hdcSurface, 0, 0, Bitmap.bmWidth, Bitmap.bmHeight, 
		hdcBitmap, 0, 0, SRCCOPY);
	DeleteDC(hdcBitmap);
	pDDS->ReleaseDC(hdcSurface);
	DeleteObject(hBitmap);

    return pDDS;
} 