From 5edb561b2bd425e02b5bd66f4b03f5b4db01370f Mon Sep 17 00:00:00 2001 From: Toni Spets Date: Fri, 5 Nov 2010 17:45:32 +0200 Subject: [PATCH] Try to avoid race condition on startup, more work on limiting the scrolling speed --- Makefile | 2 +- main.c | 2 +- main.h | 1 + surface.c | 58 +++++++++++++++++++++++++++++-------------------------- 4 files changed, 34 insertions(+), 29 deletions(-) diff --git a/Makefile b/Makefile index 3148c08..a63814e 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ all: - i586-mingw32msvc-gcc -Wall -Wl,--enable-stdcall-fixup -shared -s -o ddraw.dll main.c mouse.c palette.c surface.c clipper.c ddraw.def -lgdi32 -lopengl32 + i586-mingw32msvc-gcc -D_DEBUG -Wall -Wl,--enable-stdcall-fixup -shared -s -o ddraw.dll main.c mouse.c palette.c surface.c clipper.c ddraw.def -lgdi32 -lopengl32 clean: rm -f ddraw.dll diff --git a/main.c b/main.c index 68138e8..fdfd8de 100644 --- a/main.c +++ b/main.c @@ -316,6 +316,7 @@ HRESULT __stdcall ddraw_WaitForVerticalBlank(IDirectDrawImpl *This, DWORD a, HAN #if _DEBUG printf("DirectDraw::WaitForVerticalBlank(This=%p, ...)\n", This); #endif + WaitForSingleObject(ddraw->ev, INFINITE); return DD_OK; } @@ -407,7 +408,6 @@ HRESULT WINAPI DirectDrawCreate(GUID FAR* lpGUID, LPDIRECTDRAW FAR* lplpDD, IUnk printf(" This = %p\n", This); *lplpDD = (LPDIRECTDRAW)This; ddraw = This; - InitializeCriticalSection(&This->cs); This->windowed = TRUE; diff --git a/main.h b/main.h index 592daac..58b5b05 100644 --- a/main.h +++ b/main.h @@ -39,6 +39,7 @@ typedef struct IDirectDrawImpl DWORD freq; BOOL windowed; CRITICAL_SECTION cs; + HANDLE ev; HWND hWnd; LRESULT CALLBACK (*WndProc)(HWND, UINT, WPARAM, LPARAM); diff --git a/surface.c b/surface.c index c4b522f..d566c42 100644 --- a/surface.c +++ b/surface.c @@ -99,8 +99,7 @@ HRESULT __stdcall ddraw_surface_Blt(IDirectDrawSurfaceImpl *This, LPRECT lpDestR if(This->caps & DDSCAPS_PRIMARYSURFACE) { - EnterCriticalSection(&ddraw->cs); - LeaveCriticalSection(&ddraw->cs); + WaitForSingleObject(ddraw->ev, INFINITE); } if(Source) @@ -130,12 +129,6 @@ HRESULT __stdcall ddraw_surface_Blt(IDirectDrawSurfaceImpl *This, LPRECT lpDestR } } - if(This->caps & DDSCAPS_PRIMARYSURFACE) - { - EnterCriticalSection(&ddraw->cs); - LeaveCriticalSection(&ddraw->cs); - } - return DD_OK; } @@ -334,12 +327,6 @@ HRESULT __stdcall ddraw_surface_Unlock(IDirectDrawSurfaceImpl *This, LPVOID lpRe printf("DirectDrawSurface::Unlock(This=%p, lpRect=%p)\n", This, lpRect); #endif - if(This->caps & DDSCAPS_PRIMARYSURFACE) - { - EnterCriticalSection(&ddraw->cs); - LeaveCriticalSection(&ddraw->cs); - } - return DD_OK; } @@ -427,11 +414,6 @@ HRESULT __stdcall ddraw_CreateSurface(IDirectDrawImpl *This, LPDDSURFACEDESC lpD Surface->width = This->width; Surface->height = This->height; Surface->hWnd = This->hWnd; -#if USE_OPENGL - Surface->dThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ogl_Thread, (void *)Surface, 0, NULL); -#else - Surface->dThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)dd_Thread, (void *)Surface, 0, NULL); -#endif } dump_ddscaps(lpDDSurfaceDesc->ddsCaps.dwCaps); @@ -458,6 +440,15 @@ HRESULT __stdcall ddraw_CreateSurface(IDirectDrawImpl *This, LPDDSURFACEDESC lpD Surface->Ref = 0; ddraw_surface_AddRef(Surface); + if(Surface->caps & DDSCAPS_PRIMARYSURFACE) + { +#if USE_OPENGL + Surface->dThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ogl_Thread, (void *)Surface, 0, NULL); +#else + Surface->dThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)dd_Thread, (void *)Surface, 0, NULL); +#endif + } + return DD_OK; } @@ -563,37 +554,50 @@ DWORD WINAPI dd_Thread(IDirectDrawSurfaceImpl *This) IDirectDrawClipper_SetHWnd(clipper, 0, This->hWnd); IDirectDrawSurface_SetClipper(primary, clipper); +#ifdef FRAME_LIMIT DWORD tick_start; DWORD tick_end; - DWORD frame_len = 1000.0f / 60.0f /*This->parent->freq*/; + DWORD frame_len = 1000.0f / This->parent->freq; +#endif + + InitializeCriticalSection(&ddraw->cs); + ddraw->ev = CreateEvent(NULL, TRUE, FALSE, NULL); while(This->dRun) { - tick_start = GetTickCount(); + ResetEvent(ddraw->ev); +#ifdef FRAME_LIMIT + tick_start = GetTickCount(); +#endif IDirectDrawSurface_Lock(primary, NULL, &ddsd, DDLOCK_WRITEONLY|DDLOCK_WAIT, NULL); - /* convert ddraw surface to opengl texture */ - for(i=0; iheight; i++) + if(This->palette) { - for(j=0; jwidth; j++) + for(i=0; iheight; i++) { - ((int *)ddsd.lpSurface)[(i+This->parent->winpos.y)*width+(j+This->parent->winpos.x)] = This->palette->data[((unsigned char *)This->surface)[i*This->lPitch + j*This->lXPitch]]; + for(j=0; jwidth; j++) + { + ((int *)ddsd.lpSurface)[(i+This->parent->winpos.y)*width+(j+This->parent->winpos.x)] = This->palette->data[((unsigned char *)This->surface)[i*This->lPitch + j*This->lXPitch]]; + } } } IDirectDrawSurface_Unlock(primary, NULL); +#ifdef FRAME_LIMIT tick_end = GetTickCount(); if(tick_end - tick_start < frame_len) { - EnterCriticalSection(&ddraw->cs); Sleep( frame_len - (tick_end - tick_start) ); - LeaveCriticalSection(&ddraw->cs); } +#endif + SetEvent(ddraw->ev); } + CloseHandle(ddraw->ev); + IDirectDrawClipper_Release(clipper); IDirectDrawSurface_Release(primary);