#include <ddraw.h>
#include <windows.h>
#include <iostream>
#include <stdio.h>

const char*
DDErrorMessage(int code)
{
  static const char *error;
  static char errormsg [1024];

  switch (code) {
    case DDERR_GENERIC:
      error = "Undefined error!";
      break;
    case DDERR_EXCEPTION:
      error = "Exception encountered";
      break;
    case DDERR_INVALIDOBJECT:
      error = "Invalid object";
      break;
    case DDERR_INVALIDPARAMS:
      error = "Invalid parameters";
      break;
    case DDERR_NOTFOUND:
      error = "Object not found";
      break;
    case DDERR_INVALIDRECT:
      error = "Invalid rectangle";
      break;
    case DDERR_INVALIDCAPS:
      error = "Invalid caps member";
      break;
    case DDERR_INVALIDPIXELFORMAT:
      error = "Invalid pixel format";
      break;
    case DDERR_OUTOFMEMORY:
      error = "Out of memory";
      break;
    case DDERR_OUTOFVIDEOMEMORY:
      error = "Out of video memory";
      break;
    case DDERR_SURFACEBUSY:
      error = "Surface busy";
      break;
    case DDERR_SURFACELOST:
      error = "Surface was lost";
      break;
    case DDERR_WASSTILLDRAWING:
      error = "DirectDraw is still drawing";
      break;
    case DDERR_INVALIDSURFACETYPE:
      error = "Invalid surface type";
      break;
    case DDERR_NOEXCLUSIVEMODE:
      error = "Not in exclusive access mode";
      break;
    case DDERR_NOPALETTEATTACHED:
      error = "No palette attached";
      break;
    case DDERR_NOPALETTEHW:
      error = "No palette hardware";
      break;
    case DDERR_NOT8BITCOLOR:
      error = "Not 8-bit color";
      break;
    case DDERR_EXCLUSIVEMODEALREADYSET:
      error = "Exclusive mode was already set";
      break;
    case DDERR_HWNDALREADYSET:
      error = "Window handle already set";
      break;
    case DDERR_HWNDSUBCLASSED:
      error = "Window handle is subclassed";
      break;
    case DDERR_NOBLTHW:
      error = "No blit hardware";
      break;
    case DDERR_IMPLICITLYCREATED:
      error = "Surface was implicitly created";
      break;
    case DDERR_INCOMPATIBLEPRIMARY:
      error = "Incompatible primary surface";
      break;
    case DDERR_NOCOOPERATIVELEVELSET:
      error = "No cooperative level set";
      break;
    case DDERR_NODIRECTDRAWHW:
      error = "No DirectDraw hardware";
      break;
    case DDERR_NOEMULATION:
      error = "No emulation available";
      break;
    case DDERR_NOFLIPHW:
      error = "No flip hardware";
      break;
    case DDERR_NOTFLIPPABLE:
      error = "Surface not flippable";
      break;
    case DDERR_PRIMARYSURFACEALREADYEXISTS:
      error = "Primary surface already exists";
      break;
    case DDERR_UNSUPPORTEDMODE:
      error = "Unsupported mode";
      break;
    case DDERR_WRONGMODE:
      error = "Surface created in different mode";
      break;
    case DDERR_UNSUPPORTED:
      error = "Operation not supported";
      break;
    case E_NOINTERFACE:
      error = "Interface not present";
      break;
    default:
      snprintf (errormsg, sizeof (errormsg), "%u", code);
      return (errormsg);
  }

  snprintf (errormsg, sizeof(errormsg), "%s (%u)", error, code);
  return (error);
}

int main (void)
{
  DDSURFACEDESC ddSurfaceDesc;
  HRESULT ddResult;
  LPDIRECTDRAW _DDraw;
  LPDIRECTDRAWSURFACE overlay;

  // create a DirectDraw object
  ddResult = DirectDrawCreate (NULL, &_DDraw, NULL);
  if (ddResult != DD_OK) {
    std::cout << "DrawCreate failed - cannot do the test of CreateSurface" << ddResult;
    return false;
  }

  memset (&ddSurfaceDesc, 0, sizeof (DDSURFACEDESC));
  ddSurfaceDesc.dwSize = sizeof (DDSURFACEDESC);
  ddSurfaceDesc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_BACKBUFFERCOUNT | DDSD_PIXELFORMAT;
  ddSurfaceDesc.ddsCaps.dwCaps = DDSCAPS_OVERLAY | DDSCAPS_FLIP | DDSCAPS_COMPLEX;

  /*if (PIPimageWidth > imageWidth)
    ddSurfaceDesc.dwWidth = PIPimageWidth;
  else
    ddSurfaceDesc.dwWidth = imageWidth;

  if (PIPimageHeight > imageHeight)
    ddSurfaceDesc.dwHeight = PIPimageHeight;
  else
    ddSurfaceDesc.dwHeight = imageHeight;
  */
  ddSurfaceDesc.dwWidth = 144;
  ddSurfaceDesc.dwHeight = 176;


  ddSurfaceDesc.dwBackBufferCount = 1;
  ddSurfaceDesc.ddpfPixelFormat.dwSize = sizeof( DDPIXELFORMAT);
  ddSurfaceDesc.ddpfPixelFormat.dwFlags = DDPF_FOURCC;
  ddSurfaceDesc.ddpfPixelFormat.dwFourCC = mmioFOURCC ('Y', 'V', '1', '2');
  ddResult = _DDraw->CreateSurface (&ddSurfaceDesc, &overlay, NULL);
  if (ddResult == DD_OK)
    std::cout << "DirectX\tCreateSurface(Overlay) succeeded!!";
  else
    std::cout << "DirectX\tCreateSurface(Overlay) failed, error code " << DDErrorMessage (ddResult) << " (" << ddResult << ")";
  Sleep (10000);
}