for(int i = 0; i < 24;) { systemGbPalette[i++] = (0x1f) | (0x1f << 5) | (0x1f << 10); systemGbPalette[i++] = (0x15) | (0x15 << 5) | (0x15 << 10); systemGbPalette[i++] = (0x0c) | (0x0c << 5) | (0x0c << 10); systemGbPalette[i++] = 0; }
bool DirectDrawDisplay::initializeOffscreen(int w, int h){  DDSURFACEDESC2 ddsd;    ZeroMemory(&ddsd, sizeof(ddsd));  ddsd.dwSize = sizeof(ddsd);  ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH;  ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;  if(theApp.ddrawUseVideoMemory)    ddsd.ddsCaps.dwCaps |= (DDSCAPS_LOCALVIDMEM|DDSCAPS_VIDEOMEMORY);  ddsd.dwWidth = w;  ddsd.dwHeight = h;  HRESULT hret = pDirectDraw->CreateSurface(&ddsd, &ddsOffscreen, NULL);  if(hret != DD_OK) {    winlog("Error offscreen CreateSurface %08x\n", hret);        if(theApp.ddrawUseVideoMemory) {      regSetDwordValue("ddrawUseVideoMemory", 0);    }        //    errorMessage(myLoadString(IDS_ERROR_DISP_DRAWSURFACE2), hret);    return false;  }  if(theApp.ddrawDebug) {    DDSCAPS2 caps;    ZeroMemory(&caps, sizeof(caps));    ddsOffscreen->GetCaps(&caps);    winlog("Offscreen CAPS 1: %08x\n", caps.dwCaps);    winlog("Offscreen CAPS 2: %08x\n", caps.dwCaps2);    winlog("Offscreen CAPS 3: %08x\n", caps.dwCaps3);    winlog("Offscreen CAPS 4: %08x\n", caps.dwCaps4);  }    DDPIXELFORMAT px;  px.dwSize = sizeof(px);  hret = ddsOffscreen->GetPixelFormat(&px);  if(theApp.ddrawDebug) {    DWORD *pdword = (DWORD *)&px;    for(int ii = 0; ii < 8; ii++) {      winlog("Pixel format %d %08x\n", ii, pdword[ii]);    }  }    switch(px.dwRGBBitCount) {  case 15:  case 16:    systemColorDepth = 16;    break;  case 24:    systemColorDepth = 24;    theApp.filterFunction = NULL;    break;  case 32:    systemColorDepth = 32;    break;  default:    systemMessage(IDS_ERROR_DISP_COLOR, "Unsupported display setting for color depth: %d bits. \nWindows desktop must be in either 16-bit, 24-bit or 32-bit mode for this program to work in window mode.",px.dwRGBBitCount);    return FALSE;  }  if(theApp.ddrawDebug) {    winlog("R Mask: %08x\n", px.dwRBitMask);    winlog("G Mask: %08x\n", px.dwGBitMask);    winlog("B Mask: %08x\n", px.dwBBitMask);  }    systemRedShift = ffs(px.dwRBitMask);  systemGreenShift = ffs(px.dwGBitMask);  systemBlueShift = ffs(px.dwBBitMask);#ifdef MMX  if(!theApp.disableMMX)    cpu_mmx = theApp.detectMMX();  else    cpu_mmx = 0;#endif    if((px.dwFlags&DDPF_RGB) != 0 &&     px.dwRBitMask == 0xF800 &&     px.dwGBitMask == 0x07E0 &&     px.dwBBitMask == 0x001F) {    systemGreenShift++;    Init_2xSaI(565);    RGB_LOW_BITS_MASK=0x821;  } else if((px.dwFlags&DDPF_RGB) != 0 &&            px.dwRBitMask == 0x7C00 &&            px.dwGBitMask == 0x03E0 &&            px.dwBBitMask == 0x001F) {    Init_2xSaI(555);    RGB_LOW_BITS_MASK=0x421;  } else if((px.dwFlags&DDPF_RGB) != 0 &&            px.dwRBitMask == 0x001F &&            px.dwGBitMask == 0x07E0 &&            px.dwBBitMask == 0xF800) {    systemGreenShift++;    Init_2xSaI(565);    RGB_LOW_BITS_MASK=0x821;  } else if((px.dwFlags&DDPF_RGB) != 0 &&            px.dwRBitMask == 0x001F &&            px.dwGBitMask == 0x03E0 &&            px.dwBBitMask == 0x7C00) {    Init_2xSaI(555);    RGB_LOW_BITS_MASK=0x421;  } else {    // 32-bit or 24-bit    if(systemColorDepth == 32 || systemColorDepth == 24) {      systemRedShift += 3;      systemGreenShift += 3;      systemBlueShift += 3;      if(systemColorDepth == 32)        Init_2xSaI(32);    }  }  if(theApp.ddrawDebug) {    winlog("R shift: %d\n", systemRedShift);    winlog("G shift: %d\n", systemGreenShift);    winlog("B shift: %d\n", systemBlueShift);  }    switch(systemColorDepth) {  case 16:    {      for(int i = 0; i < 0x10000; i++) {        systemColorMap16[i] = ((i & 0x1f) << systemRedShift) |          (((i & 0x3e0) >> 5) << systemGreenShift) |          (((i & 0x7c00) >> 10) << systemBlueShift);      }    }    break;  case 24:  case 32:    {      for(int i = 0; i < 0x10000; i++) {        systemColorMap32[i] = ((i & 0x1f) << systemRedShift) |          (((i & 0x3e0) >> 5) << systemGreenShift) |          (((i & 0x7c00) >> 10) << systemBlueShift);      }          }    break;  }  for(int i = 0; i < 24;) {    systemGbPalette[i++] = (0x1f) | (0x1f << 5) | (0x1f << 10);    systemGbPalette[i++] = (0x15) | (0x15 << 5) | (0x15 << 10);    systemGbPalette[i++] = (0x0c) | (0x0c << 5) | (0x0c << 10);    systemGbPalette[i++] = 0;  }  width = w;  height = h;  return true;}