diff --git a/Makefile b/Makefile index 1699c5d..3148c08 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 + 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 clean: rm -f ddraw.dll diff --git a/main.c b/main.c index 3157645..68138e8 100644 --- a/main.c +++ b/main.c @@ -194,12 +194,6 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) ddraw->cursor.y = HIWORD(lParam); } break; - case WM_PAINT: - if(ddraw_primary) - { - SetEvent(ddraw_primary->flipEvent); - } - break; case WM_MOVE: ddraw->winpos.x = LOWORD(lParam); ddraw->winpos.y = HIWORD(lParam); @@ -211,11 +205,6 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { ddraw->winpos.y = 0; } - - if(ddraw_primary) - { - SetEvent(ddraw_primary->flipEvent); - } break; case WM_WINDOWPOSCHANGED: GetClientRect(ddraw->hWnd, &ddraw->cursorclip); diff --git a/main.h b/main.h index 495ee75..592daac 100644 --- a/main.h +++ b/main.h @@ -38,6 +38,7 @@ typedef struct IDirectDrawImpl DWORD bpp; DWORD freq; BOOL windowed; + CRITICAL_SECTION cs; HWND hWnd; LRESULT CALLBACK (*WndProc)(HWND, UINT, WPARAM, LPARAM); diff --git a/mouse.c b/mouse.c index dbc85ae..2ccfb0d 100644 --- a/mouse.c +++ b/mouse.c @@ -87,7 +87,6 @@ void mouse_lock() ddraw->locked = TRUE; ClipCursor(&ddraw->cursorclip); while(ShowCursor(FALSE) > 0); - SetEvent(ddraw_primary->flipEvent); } } @@ -107,11 +106,6 @@ void mouse_unlock() ClipCursor(NULL); ddraw->cursor.x = ddraw->width / 2; ddraw->cursor.y = ddraw->height / 2; - - if(ddraw_primary) - { - SetEvent(ddraw_primary->flipEvent); - } } void mouse_init(HWND hWnd) diff --git a/surface.c b/surface.c index 1d0b6f3..c4b522f 100644 --- a/surface.c +++ b/surface.c @@ -51,7 +51,6 @@ ULONG __stdcall ddraw_surface_Release(IDirectDrawSurfaceImpl *This) if(This->dThread) { This->dRun = FALSE; - SetEvent(This->flipEvent); WaitForSingleObject(This->dThread, INFINITE); This->dThread = NULL; } @@ -98,6 +97,12 @@ HRESULT __stdcall ddraw_surface_Blt(IDirectDrawSurfaceImpl *This, LPRECT lpDestR } #endif + if(This->caps & DDSCAPS_PRIMARYSURFACE) + { + EnterCriticalSection(&ddraw->cs); + LeaveCriticalSection(&ddraw->cs); + } + if(Source) { int dx=0,dy=0; @@ -125,7 +130,11 @@ HRESULT __stdcall ddraw_surface_Blt(IDirectDrawSurfaceImpl *This, LPRECT lpDestR } } - SetEvent(This->flipEvent); + if(This->caps & DDSCAPS_PRIMARYSURFACE) + { + EnterCriticalSection(&ddraw->cs); + LeaveCriticalSection(&ddraw->cs); + } return DD_OK; } @@ -327,7 +336,8 @@ HRESULT __stdcall ddraw_surface_Unlock(IDirectDrawSurfaceImpl *This, LPVOID lpRe if(This->caps & DDSCAPS_PRIMARYSURFACE) { - SetEvent(This->flipEvent); + EnterCriticalSection(&ddraw->cs); + LeaveCriticalSection(&ddraw->cs); } return DD_OK; @@ -474,12 +484,13 @@ DWORD WINAPI ogl_Thread(IDirectDrawSurfaceImpl *This) This->hRC = wglCreateContext( This->hDC ); wglMakeCurrent( This->hDC, This->hRC ); - This->flipEvent = CreateEvent(NULL, TRUE, FALSE, NULL); + DWORD tick_start; + DWORD tick_end; + DWORD frame_len = 1000.0f / 60.0f /*This->parent->freq*/; while(This->dRun) { - WaitForSingleObject(This->flipEvent, INFINITE); - ResetEvent(This->flipEvent); + tick_start = GetTickCount(); /* convert ddraw surface to opengl texture */ for(i=0; iheight; i++) @@ -505,6 +516,15 @@ DWORD WINAPI ogl_Thread(IDirectDrawSurfaceImpl *This) glEnd(); SwapBuffers(This->hDC); + + tick_end = GetTickCount(); + + if(tick_end - tick_start < frame_len) + { + EnterCriticalSection(&ddraw->cs); + Sleep( frame_len - (tick_end - tick_start) ); + LeaveCriticalSection(&ddraw->cs); + } } free(glTex); @@ -543,19 +563,15 @@ DWORD WINAPI dd_Thread(IDirectDrawSurfaceImpl *This) IDirectDrawClipper_SetHWnd(clipper, 0, This->hWnd); IDirectDrawSurface_SetClipper(primary, clipper); - This->flipEvent = CreateEvent(NULL, TRUE, FALSE, NULL); - WaitForSingleObject(This->flipEvent, INFINITE); - DWORD tick_start; DWORD tick_end; - DWORD frame_len = 1000.0f / This->parent->freq; + DWORD frame_len = 1000.0f / 60.0f /*This->parent->freq*/; while(This->dRun) { tick_start = GetTickCount(); - if(IDirectDrawSurface_Lock(primary, NULL, &ddsd, DDLOCK_WRITEONLY|DDLOCK_WAIT, NULL) != DD_OK) - continue; + IDirectDrawSurface_Lock(primary, NULL, &ddsd, DDLOCK_WRITEONLY|DDLOCK_WAIT, NULL); /* convert ddraw surface to opengl texture */ for(i=0; iheight; i++) @@ -572,7 +588,9 @@ DWORD WINAPI dd_Thread(IDirectDrawSurfaceImpl *This) if(tick_end - tick_start < frame_len) { + EnterCriticalSection(&ddraw->cs); Sleep( frame_len - (tick_end - tick_start) ); + LeaveCriticalSection(&ddraw->cs); } }