hret = ddsPrimary->Blt(NULL, ddsFlip, NULL, DDBLT_WAIT, NULL);
hret = ddsPrimary->Blt(&dest, ddsFlip, &dest, DDBLT_ASYNC, NULL);
void systemDrawScreen(){  HRESULT hret;  if(pDirectDraw == NULL ||     ddsOffscreen == NULL ||     ddsPrimary == NULL)    return;  for(std::list<IUpdateListener *>::iterator it = updateList.begin();      it != updateList.end(); ) {    IUpdateListener *up = *it;    it++;    up->update();  }  if(aviRecording) {    int width = 240;    int height = 160;    switch(cartridgeType) {    case 0:      width = 240;      height = 160;      break;    case 1:      if(gbBorderOn) {        width = 256;        height = 224;      } else {        width = 160;        height = 144;      }      break;    }        if(aviRecorder == NULL) {      aviRecorder = new CAVIFile();            if(cartridgeType == 0) {        aviRecorder->SetRate(60/(frameSkip+1));      } else {         aviRecorder->SetRate(60/(gbFrameSkip+1));      }            BITMAPINFOHEADER bi;      memset(&bi, 0, sizeof(bi));            bi.biSize = 0x28;          bi.biPlanes = 1;      bi.biBitCount = 24;      bi.biWidth = width;      bi.biHeight = height;      bi.biSizeImage = 3*width*height;      aviRecorder->SetFormat(&bi);      aviRecorder->Open(aviRecordName);    }        char *bmp = new char[width*height*3];        utilWriteBMP(bmp, width, height, pix);    aviRecorder->AddFrame(bmp);        delete bmp;  }    if(ifbFunction) {    if(systemColorDepth == 16)      ifbFunction(pix+filterWidth*2+2, filterWidth*2+2,                  filterWidth, filterHeight);    else      ifbFunction(pix+filterWidth*4+4, filterWidth*4+4,                  filterWidth, filterHeight);  }    if(vsync && !speedup) {    hret = pDirectDraw->WaitForVerticalBlank(DDWAITVB_BLOCKBEGIN, 0);  }    DDSURFACEDESC2 ddsDesc;    ZeroMemory(&ddsDesc, sizeof(ddsDesc));    ddsDesc.dwSize = sizeof(ddsDesc);    hret = ddsOffscreen->Lock(NULL,                            &ddsDesc,                            DDLOCK_WRITEONLY|#ifndef FINAL_VERSION                            DDLOCK_NOSYSLOCK|#endif                            DDLOCK_SURFACEMEMORYPTR,                            NULL);  if(hret == DDERR_SURFACELOST) {    hret = ddsPrimary->Restore();    if(hret == DD_OK) {      hret = ddsOffscreen->Restore();            if(hret == DD_OK) {        hret = ddsOffscreen->Lock(NULL,                                  &ddsDesc,                                  DDLOCK_WRITEONLY|#ifndef FINAL_VERSION                                  DDLOCK_NOSYSLOCK|#endif                                  DDLOCK_SURFACEMEMORYPTR,                                  NULL);              }    }  }      if(hret == DD_OK) {    if(filterFunction) {      if(systemColorDepth == 16)        (*filterFunction)(pix+filterWidth*2+2,                          filterWidth*2+2,                          (u8*)delta,                          (u8*)ddsDesc.lpSurface,                          ddsDesc.lPitch,                          filterWidth,                          filterHeight);      else        (*filterFunction)(pix+filterWidth*4+4,                          filterWidth*4+4,                          (u8*)delta,                          (u8*)ddsDesc.lpSurface,                          ddsDesc.lPitch,                          filterWidth,                          filterHeight);            } else {      if(cartridgeType == 0) {        __asm {          push esi;          push edi;          push ecx;          push edx;                    mov esi, pix;          mov edi, ddsDesc.lpSurface;          mov edx, ddsDesc.lPitch;          mov ecx, 160;          cmp systemColorDepth, 16;          jnz gbaOtherColor;          sub edx,480;          add esi,482;        gbaLoop16bit:          push ecx;          mov ecx, 240;          repz movsw;          inc esi;          inc esi;          add edi, edx;          pop ecx;          dec ecx;          jnz gbaLoop16bit;          jmp gbaLoopEnd;        gbaOtherColor:          cmp systemColorDepth, 24;          jnz gbaOtherColor2;          sub edx, 240*3;        gbaLoop24bit:          push ecx;          mov ecx, 240*3;          repz movsb;          add edi, edx;          pop ecx;          dec ecx;          jnz gbaLoop24bit;          jmp gbaLoopEnd;        gbaOtherColor2:          sub edx, 240*4;          add esi, 241*4;        gbaLoop32bit:          push ecx;          mov ecx, 240;          repz movsd;          add esi, 4;          add edi, edx;          pop ecx;          dec ecx;          jnz gbaLoop32bit;        gbaLoopEnd:          pop edx;          pop ecx;          pop edi;          pop esi;        }      } else {        if(gbBorderOn) {          __asm {            push esi;            push edi;            push ecx;            push edx;                        mov esi, pix;            mov edi, ddsDesc.lpSurface;            mov edx, ddsDesc.lPitch;            mov ecx, 224;            cmp systemColorDepth, 16;            jnz otherColor;            add esi, 514;          loop123:             push ecx;                        mov ecx, 256;            repz movsw;            inc esi;            inc esi;            add edi, edx;            sub edi, 256 * 2;                        pop ecx;            dec ecx;            jnz loop123;            jmp loopEnd;          otherColor:            cmp systemColorDepth, 24;            jnz otherColor2;          loop456:             push ecx;                        mov ecx, 256 * 3;            repz movsb;                        add edi, edx;            sub edi, 256 * 3;                        pop ecx;            dec ecx;            jnz loop456;            jmp loopEnd;          otherColor2:            add esi, 257*4;          loop789:            push ecx;            mov ecx, 256;            repz movsd;            add edi, edx;            add esi, 4;            sub edi, 256 * 4;                        pop ecx;            dec ecx;            jnz loop789;          loopEnd:            pop edx;            pop ecx;            pop edi;            pop esi;          }        } else {          __asm {            push esi;            push edi;            push ecx;            push edx;                        mov esi, pix;            mov edi, ddsDesc.lpSurface;            mov edx, ddsDesc.lPitch;            mov ecx, 144;            cmp systemColorDepth, 16;            jnz otherColor3;            add esi, 322;          loop1234:             push ecx;                        mov ecx, 160;            repz movsw;            inc esi;            inc esi;            add edi, edx;            sub edi, 160 * 2;                        pop ecx;            dec ecx;            jnz loop1234;            jmp loopEnd2;          otherColor3:            cmp systemColorDepth, 24;            jnz otherColor4;          loop4567:             push ecx;                        mov ecx, 160 * 3;            repz movsb;                        add edi, edx;            sub edi, 160 * 3;                        pop ecx;            dec ecx;            jnz loop4567;            jmp loopEnd2;          otherColor4:            add esi, 161 * 4;          loop7890:            push ecx;            mov ecx, 160;            repz movsd;            add esi, 4;            add edi, edx;            sub edi, 160 * 4;                        pop ecx;            dec ecx;            jnz loop7890;          loopEnd2:            pop edx;            pop ecx;            pop edi;            pop esi;          }        }            }    }    if(videoOption > VIDEO_4X && showSpeed) {      char buffer[30];      sprintf(buffer, "%d%%", systemSpeed);      fontDisplayStringTransp((u8*)ddsDesc.lpSurface,                              ddsDesc.lPitch,                              rect.left+10,                              rect.bottom-10,                              buffer);    }  }    hret = ddsOffscreen->Unlock(NULL);    if(hret == DD_OK) {    ddsOffscreen->PageLock(0);    if(tripleBuffering && videoOption > VIDEO_4X) {      hret = ddsFlip->Blt(&dest, ddsOffscreen, &rect, DDBLT_WAIT, NULL);      if(hret == DD_OK) {        if(menuToggle) {          ddsPrimary->SetClipper(ddsClipper);          hret = ddsPrimary->Blt(NULL, ddsFlip, NULL, DDBLT_WAIT, NULL);        } else          hret = ddsPrimary->Flip(NULL, 0);      }    } else {      hret = ddsPrimary->Blt(&dest, ddsOffscreen, &rect,DDBLT_ASYNC,NULL);            if(hret == DDERR_SURFACELOST) {        hret = ddsPrimary->Restore();                if(hret == DD_OK) {          hret = ddsPrimary->Blt(&dest, ddsOffscreen, &rect, DDBLT_ASYNC, NULL);        }      }    }    ddsOffscreen->PageUnlock(0);  }  if(screenMessage) {    if(((GetTickCount() - screenMessageTime) < 3000) &&       !disableStatusMessage) {      ddsPrimary->SetClipper(ddsClipper);      HDC hdc;      ddsPrimary->GetDC(&hdc);      SetTextColor(hdc, RGB(255,0,0));      SetBkMode(hdc,TRANSPARENT);            TextOut(hdc, dest.left+10, dest.bottom - 20, screenMessageBuffer,              strlen(screenMessageBuffer));      ddsPrimary->ReleaseDC(hdc);    } else {      screenMessage = false;    }  }    if(hret != DD_OK) {    //    if(drawdebug) {      // don't translate. Debug only    //      log("Error on update screen: %08xl\n", hret);    //    }  }}