   DDraw problem
  Over time, as I've learned, I've written a simple graphics engine in GDI that is pretty adequate for a simple 2d game. It's fast enough to run full speed until you get crazy with the resolution, at which point I began to think of other choices.DX9 was an obvious one, but before I really went nuts with it I figured I should basically be able to drop my drawing routines into DirectDraw. I quickly put something together... but my surface isn't on my actual window; it is off to the top left of the screen, completely disconnected from my window, though it is functional.Is this something stupid I missed? Am I not getting how Blt/BltFast work? There isn't much documentation or anything I can use to guide myself here...#include <Windows.h>#include <ddraw.h>#pragma comment (lib, "ddraw.lib")#pragma comment (lib, "dxguid.lib")//////////////////////////////////////////////////////////////////////////#define SCREEN_WIDTH  800#define SCREEN_HEIGHT 600//////////////////////////////////////////////////////////////////////////LPDIRECTDRAW7 dd;LPDIRECTDRAWSURFACE7 dds1;LPDIRECTDRAWSURFACE7 dds2;DDSURFACEDESC2 ddsd;//////////////////////////////////////////////////////////////////////////void GFX_DD_Init(HWND hwnd){memset(&ddsd, 0, sizeof(ddsd));ddsd.dwSize = sizeof(ddsd);ddsd.dwFlags = DDSD_CAPS;ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;DirectDrawCreateEx(NULL, (LPVOID*)&dd, IID_IDirectDraw7, NULL);dd->SetCooperativeLevel(hwnd, DDSCL_NORMAL);dd->CreateSurface(&ddsd, &dds1, NULL);memset(&ddsd, 0, sizeof(ddsd));ddsd.dwSize= sizeof(ddsd);ddsd.dwFlags= DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH;ddsd.dwWidth= SCREEN_WIDTH;ddsd.dwHeight= SCREEN_HEIGHT;ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY;dd->CreateSurface(&ddsd, &dds2, NULL);}void GFX_DD_Draw(){dds2->Lock(NULL, &ddsd, DDLOCK_WAIT, 0);//draw a pixel to testDWORD* dst = (DWORD*)ddsd.lpSurface + (50 * SCREEN_WIDTH + 50);*dst = 0x00FFFF00; //yellowdds2->Unlock(NULL);dds1->BltFast(0, 0, dds2, NULL, DDBLTFAST_WAIT);}void GFX_DD_Destroy(void){dd->Release();dds1->Release();dds2->Release();}//////////////////////////////////////////////////////////////////////////LRESULT CALLBACK WindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){switch(message){case WM_DESTROY:{PostQuitMessage(0);return 0;} break;}return DefWindowProc (hwnd, message, wParam, lParam);}//////////////////////////////////////////////////////////////////////////int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){HWNDhwnd;MSGmsg;char*dong = "bnr";WNDCLASSEX wc = { 0 };wc.cbSize= sizeof(WNDCLASSEX);wc.style= CS_HREDRAW | CS_VREDRAW;wc.lpfnWndProc= WindowProc;wc.hInstance= hInstance;wc.hCursor= LoadCursor(NULL, IDC_ARROW);wc.lpszClassName = dong;RegisterClassEx(&wc);hwnd = CreateWindowEx(0, dong, NULL, WS_OVERLAPPEDWINDOW, 300, 300, SCREEN_WIDTH, SCREEN_HEIGHT, NULL, NULL, hInstance, NULL);GFX_DD_Init(hwnd);ShowWindow(hwnd, nCmdShow);for(;;){while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)){TranslateMessage(&msg);DispatchMessage(&msg);}if(msg.message == WM_QUIT)break;GFX_DD_Draw();}GFX_DD_Destroy();return msg.wParam;}
  It's been a long time since I touched DirectDraw, but I seem to recall that you need a clipper in windowed mode - I don't know if that'll fix the disconnectedness completely though.However, unless this is just for a small learning exercise, I'd strongly recommend against using DirectDraw. It's outdated, is emulated using D3D in Vista and above, is a pain to use, has almost no hardware support, and you'll have to write your own functions for alpha blending and rotation.The usual way to do 2D is to use D3D9, and use an orthographic projection (So you get a view like the 2D views in CAD applications). Because it's using the 3D hardware, you get scaling, rotation, alpha blending, hardware acceleration, and even shaders if you want for free.   ;Quote:Original post by Evil SteveIt's been a long time since I touched DirectDraw, but I seem to recall that you need a clipper in windowed mode - I don't know if that'll fix the disconnectedness completely though.However, unless this is just for a small learning exercise, I'd strongly recommend against using DirectDraw. It's outdated, is emulated using D3D in Vista and above, is a pain to use, has almost no hardware support, and you'll have to write your own functions for alpha blending and rotation.The usual way to do 2D is to use D3D9, and use an orthographic projection (So you get a view like the 2D views in CAD applications). Because it's using the 3D hardware, you get scaling, rotation, alpha blending, hardware acceleration, and even shaders if you want for free.Yeah, it's only for messing with. I do already have functions written for scaling, blending, so I was just curious as what kind of speed I would get just porting over my GDI code. All the drawing there was done per-pixel on a DIB section.Anyway, this is what I mean by it being 'detached'. The surface lives in it's own world in the corner while my window does whatever.http://i296.photobucket.com/albums/mm164/clashonovich/pos.jpgI'll try attaching a clipper...edit:Yeah, a clipper helped... but it the surface still seems to live off to the side. I have to actually move my window to where the pixel I drew is, to get it to show up.DDraw is bizarre.[Edited by - clashie on June 30, 2009 3:46:05 AM];Quote:Original post by clashiethe surface still seems to live off to the side. I have to actually move my window to where the pixel I drew is, to get it to show up.Anyone have any ideas on this? Last bump before I let this project rot.edit: I figured it out.The surface did actually live in screen space, apparently.Just did something like this and bam. Solved.RECT rect;GetClientRect(hwnd, ▭); ClientToScreen(hwnd, (LPPOINT)&rect.left); ClientToScreen(hwnd, (LPPOINT)&rect.right); ...dds1->Blt(&rect, dds2, NULL, DDBLT_WAIT, NULL);[Edited by - clashie on June 30, 2009 10:40:43 PM];  Mind you I wrote this code 6 years ago, but it may help out. Take a look at the work area and clipper:HRESULT DDUT::InitializeWindowed(HWND hWnd, DWORD WindowWidth, DWORD WindowHeight){DDSURFACEDESC2 SurfaceDescription;DWORD   WindowStyle;RECTWorkArea;HRESULT Result;   SAFE_RELEASE(this->m_BackBuffer);    SAFE_RELEASE(this->m_PrimaryBuffer);    if(this->m_DDraw){this->m_DDraw->SetCooperativeLevel(this->m_WindowHWND, DDSCL_NORMAL);}    SAFE_RELEASE(this->m_DDraw);    if(FAILED(Result = DirectDrawCreateEx(NULL, (VOID**)&this->m_DDraw, IID_IDirectDraw7, NULL))){return E_FAIL;}    Result = this->m_DDraw->SetCooperativeLevel(hWnd, DDSCL_NORMAL);    if(FAILED(Result)){return E_FAIL;}this->InitializeClipper();    WindowStyle  = GetWindowStyle(hWnd);    WindowStyle &= ~WS_POPUP;    WindowStyle |= WS_OVERLAPPED | WS_CAPTION | WS_THICKFRAME | WS_MINIMIZEBOX;    SetWindowLong(hWnd, GWL_STYLE, WindowStyle);    SetRect(&this->m_WindowRECT, 0, 0, WindowWidth, WindowHeight);    AdjustWindowRectEx(&this->m_WindowRECT, GetWindowStyle(hWnd), GetMenu(hWnd) != NULL, GetWindowExStyle(hWnd));    SetWindowPos(hWnd, NULL, 0, 0, this->m_WindowRECT.right - this->m_WindowRECT.left, this->m_WindowRECT.bottom - this->m_WindowRECT.top, SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);    SetWindowPos(hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);    SystemParametersInfo(SPI_GETWORKAREA, 0, &WorkArea, 0);    GetWindowRect(hWnd, &this->m_WindowRECT);    if(this->m_WindowRECT.left < WorkArea.left) this->m_WindowRECT.left = WorkArea.left;    if(this->m_WindowRECT.top  < WorkArea.top)  this->m_WindowRECT.top  = WorkArea.top;    SetWindowPos(hWnd, NULL, this->m_WindowRECT.left, this->m_WindowRECT.top, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);    ZeroMemory(&SurfaceDescription, sizeof(SurfaceDescription));    SurfaceDescription.dwSize  = sizeof(SurfaceDescription);    SurfaceDescription.dwFlags = DDSD_CAPS;    SurfaceDescription.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;    if(FAILED(this->m_DDraw->CreateSurface(&SurfaceDescription, &this->m_PrimaryBuffer, NULL))){return E_FAIL;}if(FAILED(Result = this->m_PrimaryBuffer->SetClipper(this->m_Clipper))){return Result;}this->m_WindowBPP = SurfaceDescription.ddpfPixelFormat.dwRGBBitCount;    SurfaceDescription.dwFlags   = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;   SurfaceDescription.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE;    SurfaceDescription.dwWidth   = WindowWidth;    SurfaceDescription.dwHeight  = WindowHeight;if(FAILED(Result = this->m_DDraw->CreateSurface(&SurfaceDescription, &this->m_BackBuffer, NULL))){return E_FAIL;}    this->m_WindowHWND   = hWnd;    this->m_Fullscreen   = false;    return S_OK;}Clipper Code:HRESULT DDUT::InitializeClipper(void){LPRGNDATA Region;HRESULT Result;   if(FAILED(Result = this->m_DDraw->CreateClipper(NULL, &this->m_Clipper, NULL))){return Result;}Region = (LPRGNDATA)malloc(sizeof(RGNDATAHEADER) + sizeof(RECT));memcpy(Region->Buffer, &this->m_WindowRECT, sizeof(RECT));Region->rdh.dwSize   = sizeof(RGNDATAHEADER);Region->rdh.iType    = RDH_RECTANGLES;Region->rdh.nCount   = 1;Region->rdh.nRgnSize = 1 * sizeof(RECT);Region->rdh.rcBound.left    =  64000;Region->rdh.rcBound.top=  64000;Region->rdh.rcBound.right   = -64000;Region->rdh.rcBound.bottom  = -64000;if(this->m_WindowRECT.left < Region->rdh.rcBound.left)Region->rdh.rcBound.left = this->m_WindowRECT.left;if(this->m_WindowRECT.right > Region->rdh.rcBound.right)    Region->rdh.rcBound.right = this->m_WindowRECT.right;if(this->m_WindowRECT.top < Region->rdh.rcBound.top)    Region->rdh.rcBound.top = this->m_WindowRECT.top;if(this->m_WindowRECT.bottom > Region->rdh.rcBound.bottom)    Region->rdh.rcBound.bottom = this->m_WindowRECT.bottom;if(FAILED(this->m_Clipper->SetClipList(Region, 0))){free(Region);return E_FAIL;}    this->m_Clipper->SetHWnd(0, this->m_WindowHWND);free(Region);    return S_OK;}Hope it helps and best of luck.   