1
0
mirror of https://github.com/FunkyFr3sh/cnc-ddraw.git synced 2025-03-14 22:03:27 +01:00

fully separate render thread to prevent slowdowns in main thread

This commit is contained in:
FunkyFr3sh 2017-11-11 23:32:07 +01:00
parent 79b83bcb7a
commit 9bd708d8f5
7 changed files with 28 additions and 84 deletions

View File

@ -1,2 +1,8 @@
make EXT=.exe
pause
@echo off
REM
REM cnc-patch environment config
REM
set PATH=C:\win-builds-patch-32\bin
gmake clean
gmake
pause

BIN
ddraw.dll

Binary file not shown.

54
main.c
View File

@ -156,18 +156,6 @@ HRESULT __stdcall ddraw_RestoreDisplayMode(IDirectDrawImpl *This)
return DD_OK;
}
/* only stop drawing in GL mode when minimized */
if (This->renderer == render_main)
{
EnterCriticalSection(&This->cs);
This->render.run = FALSE;
ReleaseSemaphore(ddraw->render.sem, 1, NULL);
LeaveCriticalSection(&This->cs);
WaitForSingleObject(This->render.thread, INFINITE);
This->render.thread = NULL;
}
if(!ddraw->windowed)
{
ChangeDisplaySettings(&This->mode, 0);
@ -209,15 +197,6 @@ HRESULT __stdcall ddraw_SetDisplayMode(IDirectDrawImpl *This, DWORD width, DWORD
This->render.run = TRUE;
if (This->renderer == render_dummy_main)
{
if(This->render.thread == NULL)
{
This->render.thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)This->renderer, NULL, 0, NULL);
}
return DD_OK;
}
mouse_unlock();
const HANDLE hbicon = LoadImage(GetModuleHandle(0), MAKEINTRESOURCE(IDR_MYMENU), IMAGE_ICON, GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON), 0);
@ -275,11 +254,6 @@ HRESULT __stdcall ddraw_SetDisplayMode(IDirectDrawImpl *This, DWORD width, DWORD
}
}
if(This->render.thread == NULL)
{
This->render.thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)This->renderer, NULL, 0, NULL);
}
return DD_OK;
}
@ -429,17 +403,9 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
}
break;
/* make sure we redraw when WM_PAINT is requested */
case WM_PAINT:
EnterCriticalSection(&ddraw->cs);
ReleaseSemaphore(ddraw->render.sem, 1, NULL);
LeaveCriticalSection(&ddraw->cs);
break;
case WM_ERASEBKGND:
EnterCriticalSection(&ddraw->cs);
FillRect(ddraw->render.hDC, &rc, (HBRUSH) GetStockObject(BLACK_BRUSH));
ReleaseSemaphore(ddraw->render.sem, 1, NULL);
LeaveCriticalSection(&ddraw->cs);
break;
}
@ -545,15 +511,11 @@ ULONG __stdcall ddraw_Release(IDirectDrawImpl *This)
PostMessage(This->hWnd, WM_USER, 0, 0);
}
if(This->render.run)
if (This->render.thread)
{
EnterCriticalSection(&This->cs);
This->render.run = FALSE;
ReleaseSemaphore(ddraw->render.sem, 1, NULL);
LeaveCriticalSection(&This->cs);
WaitForSingleObject(This->render.thread, INFINITE);
HANDLE thread = This->render.thread;
This->render.thread = NULL;
WaitForSingleObject(thread, INFINITE);
}
if(This->render.hDC)
@ -562,12 +524,6 @@ ULONG __stdcall ddraw_Release(IDirectDrawImpl *This)
This->render.hDC = NULL;
}
if(This->render.ev)
{
CloseHandle(This->render.ev);
ddraw->render.ev = NULL;
}
if(This->real_dll)
{
FreeLibrary(This->real_dll);
@ -664,8 +620,6 @@ HRESULT WINAPI DirectDrawCreate(GUID FAR* lpGUID, LPDIRECTDRAW FAR* lplpDD, IUnk
}
InitializeCriticalSection(&This->cs);
This->render.ev = CreateEvent(NULL, TRUE, FALSE, NULL);
This->render.sem = CreateSemaphore(NULL, 0, 1, NULL);
/* load configuration options from ddraw.ini */
char cwd[MAX_PATH];
@ -741,7 +695,7 @@ HRESULT WINAPI DirectDrawCreate(GUID FAR* lpGUID, LPDIRECTDRAW FAR* lplpDD, IUnk
This->boxing = TRUE;
}
This->render.maxfps = GetPrivateProfileIntA("ddraw", "maxfps", 0, ini_path);
This->render.maxfps = GetPrivateProfileIntA("ddraw", "max_fps", 60, ini_path);
This->render.width = GetPrivateProfileIntA("ddraw", "width", 0, ini_path);
This->render.height = GetPrivateProfileIntA("ddraw", "height", 0, ini_path);

View File

@ -59,12 +59,6 @@ HRESULT __stdcall ddraw_palette_SetEntries(IDirectDrawPaletteImpl *This, DWORD d
}
}
/* FIXME: only refresh the screen when the primary palette is changed */
if(ddraw->primary && ddraw->render.run)
{
ReleaseSemaphore(ddraw->render.sem, 1, NULL);
}
return DD_OK;
}

View File

@ -92,7 +92,7 @@ DWORD WINAPI render_main(void)
while(ddraw->render.run && WaitForSingleObject(ddraw->render.sem, INFINITE) != WAIT_FAILED)
while(ddraw->render.thread)
{
scale_w = (float)ddraw->width/tex_width;
scale_h = (float)ddraw->height/tex_height;
@ -159,10 +159,8 @@ DWORD WINAPI render_main(void)
if(tick_end - tick_start < frame_len)
{
Sleep( frame_len - (tick_end - tick_start));
}
}
}
SetEvent(ddraw->render.ev);
}
timeEndPeriod(1);

View File

@ -97,7 +97,7 @@ DWORD WINAPI render_soft_main(void)
frame_len = 1000.0f / ddraw->render.maxfps;
}
while (ddraw->render.run && WaitForSingleObject(ddraw->render.sem, INFINITE) != WAIT_FAILED)
while (ddraw->render.thread)
{
if(ddraw->render.maxfps > 0)
{
@ -162,7 +162,6 @@ DWORD WINAPI render_soft_main(void)
Sleep( frame_len - (tick_end - tick_start) + 1);
}
}
SetEvent(ddraw->render.ev);
}
timeEndPeriod(1);

View File

@ -120,13 +120,6 @@ HRESULT __stdcall ddraw_surface_Blt(IDirectDrawSurfaceImpl *This, LPRECT lpDestR
}
}
if(This->caps & DDSCAPS_PRIMARYSURFACE && !(This->flags & DDSD_BACKBUFFERCOUNT) && ddraw->render.run)
{
ReleaseSemaphore(ddraw->render.sem, 1, NULL);
WaitForSingleObject(ddraw->render.ev, INFINITE);
ResetEvent(ddraw->render.ev);
}
return DD_OK;
}
@ -201,13 +194,6 @@ HRESULT __stdcall ddraw_surface_Flip(IDirectDrawSurfaceImpl *This, LPDIRECTDRAWS
printf("IDirectDrawSurface::Flip(This=%p, ...)\n", This);
#endif
if(This->caps & DDSCAPS_PRIMARYSURFACE && ddraw->render.run)
{
ResetEvent(ddraw->render.ev);
ReleaseSemaphore(ddraw->render.sem, 1, NULL);
WaitForSingleObject(ddraw->render.ev, INFINITE);
}
return DD_OK;
}
@ -316,7 +302,11 @@ HRESULT __stdcall ddraw_surface_Lock(IDirectDrawSurfaceImpl *This, LPRECT lpDest
}
#endif
return ddraw_surface_GetSurfaceDesc(This, lpDDSurfaceDesc);
HRESULT ret = ddraw_surface_GetSurfaceDesc(This, lpDDSurfaceDesc);
EnterCriticalSection(&ddraw->cs);
return ret;
}
HRESULT __stdcall ddraw_surface_ReleaseDC(IDirectDrawSurfaceImpl *This, HDC a)
@ -370,11 +360,8 @@ HRESULT __stdcall ddraw_surface_Unlock(IDirectDrawSurfaceImpl *This, LPVOID lpRe
#if _DEBUG
printf("DirectDrawSurface::Unlock(This=%p, lpRect=%p)\n", This, lpRect);
#endif
if(This->caps & DDSCAPS_PRIMARYSURFACE && !(This->flags & DDSD_BACKBUFFERCOUNT) && ddraw->render.run)
{
ReleaseSemaphore(ddraw->render.sem, 1, NULL);
}
LeaveCriticalSection(&ddraw->cs);
return DD_OK;
}
@ -486,6 +473,12 @@ HRESULT __stdcall ddraw_CreateSurface(IDirectDrawImpl *This, LPDDSURFACEDESC lpD
Surface->Ref = 0;
ddraw_surface_AddRef(Surface);
if(lpDDSurfaceDesc->ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
{
This->render.thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)This->renderer, NULL, 0, NULL);
SetThreadPriority(This->render.thread, THREAD_PRIORITY_ABOVE_NORMAL);
}
return DD_OK;
}