mirror of
https://github.com/FunkyFr3sh/cnc-ddraw.git
synced 2025-03-26 10:29:23 +01:00
separate into multiple functions
This commit is contained in:
parent
15686ce5d3
commit
fa6f9e197e
@ -23,6 +23,7 @@ const BYTE PalettePixelShaderSrc[] =
|
|||||||
|
|
||||||
typedef struct CUSTOMVERTEX { float x, y, z, rhw, u, v; } CUSTOMVERTEX;
|
typedef struct CUSTOMVERTEX { float x, y, z, rhw, u, v; } CUSTOMVERTEX;
|
||||||
|
|
||||||
|
static HMODULE hD3D9;
|
||||||
static LPDIRECT3D9 D3d;
|
static LPDIRECT3D9 D3d;
|
||||||
static LPDIRECT3DDEVICE9 D3ddev;
|
static LPDIRECT3DDEVICE9 D3ddev;
|
||||||
static LPDIRECT3DVERTEXBUFFER9 D3dvb;
|
static LPDIRECT3DVERTEXBUFFER9 D3dvb;
|
||||||
@ -32,95 +33,48 @@ static IDirect3DPixelShader9 *PixelShader;
|
|||||||
static D3DPRESENT_PARAMETERS D3dpp;
|
static D3DPRESENT_PARAMETERS D3dpp;
|
||||||
static float ScaleW;
|
static float ScaleW;
|
||||||
static float ScaleH;
|
static float ScaleH;
|
||||||
|
static int MaxFPS;
|
||||||
|
static DWORD FrameLength;
|
||||||
|
|
||||||
|
static BOOL CreateDirect3D();
|
||||||
|
static BOOL CreateResources();
|
||||||
|
static void SetStates();
|
||||||
|
static void UpdateVertices(BOOL inCutscene);
|
||||||
|
static void Reset();
|
||||||
|
static void SetMaxFPS(int baseMaxFPS);
|
||||||
|
static void Render();
|
||||||
|
static void ReleaseDirect3D();
|
||||||
|
|
||||||
BOOL detect_cutscene();
|
BOOL detect_cutscene();
|
||||||
DWORD WINAPI render_soft_main(void);
|
DWORD WINAPI render_soft_main(void);
|
||||||
|
|
||||||
static void UpdateVertices(BOOL inCutscene)
|
|
||||||
{
|
|
||||||
float vpX = (float)ddraw->render.viewport.x;
|
|
||||||
float vpY = (float)ddraw->render.viewport.y;
|
|
||||||
|
|
||||||
float vpW = (float)(ddraw->render.viewport.width + ddraw->render.viewport.x);
|
|
||||||
float vpH = (float)(ddraw->render.viewport.height + ddraw->render.viewport.y);
|
|
||||||
|
|
||||||
float sH = inCutscene ? ScaleH * ((float)CUTSCENE_HEIGHT / ddraw->height) : ScaleH;
|
|
||||||
float sW = inCutscene ? ScaleW * ((float)CUTSCENE_WIDTH / ddraw->width) : ScaleW;
|
|
||||||
|
|
||||||
CUSTOMVERTEX vertices[] =
|
|
||||||
{
|
|
||||||
{ vpX - 0.5f, vpH - 0.5f, 0.0f, 1.0f, 0.0f, sH },
|
|
||||||
{ vpX - 0.5f, vpY - 0.5f, 0.0f, 1.0f, 0.0f, 0.0f },
|
|
||||||
{ vpW - 0.5f, vpH - 0.5f, 0.0f, 1.0f, sW, sH },
|
|
||||||
{ vpW - 0.5f, vpY - 0.5f, 0.0f, 1.0f, sW, 0.0f }
|
|
||||||
};
|
|
||||||
|
|
||||||
void *data;
|
|
||||||
if (D3dvb && SUCCEEDED(D3dvb->lpVtbl->Lock(D3dvb, 0, 0, (void**)&data, 0)))
|
|
||||||
{
|
|
||||||
memcpy(data, vertices, sizeof(vertices));
|
|
||||||
D3dvb->lpVtbl->Unlock(D3dvb);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void SetStates()
|
|
||||||
{
|
|
||||||
D3ddev->lpVtbl->SetFVF(D3ddev, D3DFVF_XYZRHW | D3DFVF_TEX1);
|
|
||||||
D3ddev->lpVtbl->SetStreamSource(D3ddev, 0, D3dvb, 0, sizeof(CUSTOMVERTEX));
|
|
||||||
D3ddev->lpVtbl->SetTexture(D3ddev, 0, (IDirect3DBaseTexture9 *)SurfaceTex);
|
|
||||||
D3ddev->lpVtbl->SetTexture(D3ddev, 1, (IDirect3DBaseTexture9 *)PaletteTex);
|
|
||||||
D3ddev->lpVtbl->SetPixelShader(D3ddev, PixelShader);
|
|
||||||
|
|
||||||
D3DVIEWPORT9 viewData = {
|
|
||||||
ddraw->render.viewport.x,
|
|
||||||
ddraw->render.viewport.y,
|
|
||||||
ddraw->render.viewport.width,
|
|
||||||
ddraw->render.viewport.height,
|
|
||||||
0.0f,
|
|
||||||
1.0f };
|
|
||||||
|
|
||||||
D3ddev->lpVtbl->SetViewport(D3ddev, &viewData);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void CreateResources()
|
|
||||||
{
|
|
||||||
int width = ddraw->width;
|
|
||||||
int height = ddraw->height;
|
|
||||||
|
|
||||||
int texWidth =
|
|
||||||
width <= 1024 ? 1024 : width <= 2048 ? 2048 : width <= 4096 ? 4096 : width;
|
|
||||||
|
|
||||||
int texHeight =
|
|
||||||
height <= texWidth ? texWidth : height <= 2048 ? 2048 : height <= 4096 ? 4096 : height;
|
|
||||||
|
|
||||||
texWidth = texWidth > texHeight ? texWidth : texHeight;
|
|
||||||
|
|
||||||
ScaleW = (float)width / texWidth;;
|
|
||||||
ScaleH = (float)height / texHeight;
|
|
||||||
|
|
||||||
D3ddev->lpVtbl->CreateVertexBuffer(
|
|
||||||
D3ddev, sizeof(CUSTOMVERTEX) * 4, 0, D3DFVF_XYZRHW | D3DFVF_TEX1, D3DPOOL_MANAGED, &D3dvb, NULL);
|
|
||||||
|
|
||||||
UpdateVertices(InterlockedExchangeAdd(&ddraw->incutscene, 0));
|
|
||||||
D3ddev->lpVtbl->CreateTexture(D3ddev, texWidth, texHeight, 1, 0, D3DFMT_L8, D3DPOOL_MANAGED, &SurfaceTex, 0);
|
|
||||||
D3ddev->lpVtbl->CreateTexture(D3ddev, 256, 256, 1, 0, D3DFMT_X8R8G8B8, D3DPOOL_MANAGED, &PaletteTex, 0);
|
|
||||||
D3ddev->lpVtbl->CreatePixelShader(D3ddev, (DWORD *)PalettePixelShaderSrc, &PixelShader);
|
|
||||||
|
|
||||||
SetStates();
|
|
||||||
}
|
|
||||||
|
|
||||||
static void Reset()
|
|
||||||
{
|
|
||||||
if (SUCCEEDED(D3ddev->lpVtbl->Reset(D3ddev, &D3dpp)))
|
|
||||||
{
|
|
||||||
SetStates();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
DWORD WINAPI render_d3d9_main(void)
|
DWORD WINAPI render_d3d9_main(void)
|
||||||
{
|
{
|
||||||
Sleep(500);
|
Sleep(500);
|
||||||
|
|
||||||
|
BOOL useDirect3D = CreateDirect3D() && CreateResources();
|
||||||
|
if (useDirect3D)
|
||||||
|
{
|
||||||
|
SetMaxFPS(ddraw->render.maxfps);
|
||||||
|
SetStates();
|
||||||
|
|
||||||
|
Render();
|
||||||
|
}
|
||||||
|
|
||||||
|
ReleaseDirect3D();
|
||||||
|
|
||||||
|
if (!useDirect3D)
|
||||||
|
{
|
||||||
|
ShowDriverWarning = TRUE;
|
||||||
|
ddraw->renderer = render_soft_main;
|
||||||
|
render_soft_main();
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static BOOL CreateDirect3D()
|
||||||
|
{
|
||||||
D3d = NULL;
|
D3d = NULL;
|
||||||
D3ddev = NULL;
|
D3ddev = NULL;
|
||||||
SurfaceTex = NULL;
|
SurfaceTex = NULL;
|
||||||
@ -128,25 +82,7 @@ DWORD WINAPI render_d3d9_main(void)
|
|||||||
D3dvb = NULL;
|
D3dvb = NULL;
|
||||||
PixelShader = NULL;
|
PixelShader = NULL;
|
||||||
|
|
||||||
DWORD tick_start = 0;
|
hD3D9 = LoadLibrary("d3d9.dll");
|
||||||
DWORD tick_end = 0;
|
|
||||||
DWORD frame_len = 0;
|
|
||||||
|
|
||||||
int maxfps = ddraw->render.maxfps;
|
|
||||||
|
|
||||||
if (maxfps < 0)
|
|
||||||
maxfps = ddraw->mode.dmDisplayFrequency;
|
|
||||||
|
|
||||||
if (maxfps == 0)
|
|
||||||
maxfps = 125;
|
|
||||||
|
|
||||||
if (maxfps >= 1000 || ddraw->vsync)
|
|
||||||
maxfps = 0;
|
|
||||||
|
|
||||||
if (maxfps > 0)
|
|
||||||
frame_len = 1000.0f / maxfps;
|
|
||||||
|
|
||||||
HMODULE hD3D9 = LoadLibrary("d3d9.dll");
|
|
||||||
if (hD3D9)
|
if (hD3D9)
|
||||||
{
|
{
|
||||||
IDirect3D9 *(WINAPI *D3DCreate9)(UINT) =
|
IDirect3D9 *(WINAPI *D3DCreate9)(UINT) =
|
||||||
@ -184,15 +120,116 @@ DWORD WINAPI render_d3d9_main(void)
|
|||||||
&D3ddev)))
|
&D3ddev)))
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (D3ddev)
|
|
||||||
CreateResources();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOL useDirect3D = D3d && D3ddev && SurfaceTex && PaletteTex && D3dvb && PixelShader;
|
return D3d && D3ddev;
|
||||||
|
}
|
||||||
|
|
||||||
while (useDirect3D && ddraw->render.run && WaitForSingleObject(ddraw->render.sem, INFINITE) != WAIT_FAILED)
|
static BOOL CreateResources()
|
||||||
|
{
|
||||||
|
int width = ddraw->width;
|
||||||
|
int height = ddraw->height;
|
||||||
|
|
||||||
|
int texWidth =
|
||||||
|
width <= 1024 ? 1024 : width <= 2048 ? 2048 : width <= 4096 ? 4096 : width;
|
||||||
|
|
||||||
|
int texHeight =
|
||||||
|
height <= texWidth ? texWidth : height <= 2048 ? 2048 : height <= 4096 ? 4096 : height;
|
||||||
|
|
||||||
|
texWidth = texWidth > texHeight ? texWidth : texHeight;
|
||||||
|
|
||||||
|
ScaleW = (float)width / texWidth;;
|
||||||
|
ScaleH = (float)height / texHeight;
|
||||||
|
|
||||||
|
D3ddev->lpVtbl->CreateVertexBuffer(
|
||||||
|
D3ddev, sizeof(CUSTOMVERTEX) * 4, 0, D3DFVF_XYZRHW | D3DFVF_TEX1, D3DPOOL_MANAGED, &D3dvb, NULL);
|
||||||
|
|
||||||
|
UpdateVertices(InterlockedExchangeAdd(&ddraw->incutscene, 0));
|
||||||
|
D3ddev->lpVtbl->CreateTexture(D3ddev, texWidth, texHeight, 1, 0, D3DFMT_L8, D3DPOOL_MANAGED, &SurfaceTex, 0);
|
||||||
|
D3ddev->lpVtbl->CreateTexture(D3ddev, 256, 256, 1, 0, D3DFMT_X8R8G8B8, D3DPOOL_MANAGED, &PaletteTex, 0);
|
||||||
|
D3ddev->lpVtbl->CreatePixelShader(D3ddev, (DWORD *)PalettePixelShaderSrc, &PixelShader);
|
||||||
|
|
||||||
|
return SurfaceTex && PaletteTex && D3dvb && PixelShader;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void SetStates()
|
||||||
|
{
|
||||||
|
D3ddev->lpVtbl->SetFVF(D3ddev, D3DFVF_XYZRHW | D3DFVF_TEX1);
|
||||||
|
D3ddev->lpVtbl->SetStreamSource(D3ddev, 0, D3dvb, 0, sizeof(CUSTOMVERTEX));
|
||||||
|
D3ddev->lpVtbl->SetTexture(D3ddev, 0, (IDirect3DBaseTexture9 *)SurfaceTex);
|
||||||
|
D3ddev->lpVtbl->SetTexture(D3ddev, 1, (IDirect3DBaseTexture9 *)PaletteTex);
|
||||||
|
D3ddev->lpVtbl->SetPixelShader(D3ddev, PixelShader);
|
||||||
|
|
||||||
|
D3DVIEWPORT9 viewData = {
|
||||||
|
ddraw->render.viewport.x,
|
||||||
|
ddraw->render.viewport.y,
|
||||||
|
ddraw->render.viewport.width,
|
||||||
|
ddraw->render.viewport.height,
|
||||||
|
0.0f,
|
||||||
|
1.0f };
|
||||||
|
|
||||||
|
D3ddev->lpVtbl->SetViewport(D3ddev, &viewData);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void UpdateVertices(BOOL inCutscene)
|
||||||
|
{
|
||||||
|
float vpX = (float)ddraw->render.viewport.x;
|
||||||
|
float vpY = (float)ddraw->render.viewport.y;
|
||||||
|
|
||||||
|
float vpW = (float)(ddraw->render.viewport.width + ddraw->render.viewport.x);
|
||||||
|
float vpH = (float)(ddraw->render.viewport.height + ddraw->render.viewport.y);
|
||||||
|
|
||||||
|
float sH = inCutscene ? ScaleH * ((float)CUTSCENE_HEIGHT / ddraw->height) : ScaleH;
|
||||||
|
float sW = inCutscene ? ScaleW * ((float)CUTSCENE_WIDTH / ddraw->width) : ScaleW;
|
||||||
|
|
||||||
|
CUSTOMVERTEX vertices[] =
|
||||||
|
{
|
||||||
|
{ vpX - 0.5f, vpH - 0.5f, 0.0f, 1.0f, 0.0f, sH },
|
||||||
|
{ vpX - 0.5f, vpY - 0.5f, 0.0f, 1.0f, 0.0f, 0.0f },
|
||||||
|
{ vpW - 0.5f, vpH - 0.5f, 0.0f, 1.0f, sW, sH },
|
||||||
|
{ vpW - 0.5f, vpY - 0.5f, 0.0f, 1.0f, sW, 0.0f }
|
||||||
|
};
|
||||||
|
|
||||||
|
void *data;
|
||||||
|
if (D3dvb && SUCCEEDED(D3dvb->lpVtbl->Lock(D3dvb, 0, 0, (void**)&data, 0)))
|
||||||
|
{
|
||||||
|
memcpy(data, vertices, sizeof(vertices));
|
||||||
|
D3dvb->lpVtbl->Unlock(D3dvb);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void Reset()
|
||||||
|
{
|
||||||
|
if (SUCCEEDED(D3ddev->lpVtbl->Reset(D3ddev, &D3dpp)))
|
||||||
|
{
|
||||||
|
SetStates();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void SetMaxFPS(int baseMaxFPS)
|
||||||
|
{
|
||||||
|
MaxFPS = baseMaxFPS;
|
||||||
|
|
||||||
|
if (MaxFPS < 0)
|
||||||
|
MaxFPS = ddraw->mode.dmDisplayFrequency;
|
||||||
|
|
||||||
|
if (MaxFPS == 0)
|
||||||
|
MaxFPS = 125;
|
||||||
|
|
||||||
|
if (MaxFPS >= 1000 || ddraw->vsync)
|
||||||
|
MaxFPS = 0;
|
||||||
|
|
||||||
|
if (MaxFPS > 0)
|
||||||
|
FrameLength = 1000.0f / MaxFPS;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void Render()
|
||||||
|
{
|
||||||
|
DWORD tick_start = 0;
|
||||||
|
DWORD tick_end = 0;
|
||||||
|
|
||||||
|
while (ddraw->render.run && WaitForSingleObject(ddraw->render.sem, INFINITE) != WAIT_FAILED)
|
||||||
{
|
{
|
||||||
#if _DEBUG
|
#if _DEBUG
|
||||||
static DWORD tick_fps = 0;
|
static DWORD tick_fps = 0;
|
||||||
@ -222,7 +259,7 @@ DWORD WINAPI render_d3d9_main(void)
|
|||||||
frame_count++;
|
frame_count++;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (maxfps > 0)
|
if (MaxFPS > 0)
|
||||||
tick_start = timeGetTime();
|
tick_start = timeGetTime();
|
||||||
|
|
||||||
EnterCriticalSection(&ddraw->cs);
|
EnterCriticalSection(&ddraw->cs);
|
||||||
@ -298,15 +335,18 @@ DWORD WINAPI render_d3d9_main(void)
|
|||||||
if (frame_count == 1) frameTime = CounterStop();
|
if (frame_count == 1) frameTime = CounterStop();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (maxfps > 0)
|
if (MaxFPS > 0)
|
||||||
{
|
{
|
||||||
tick_end = timeGetTime();
|
tick_end = timeGetTime();
|
||||||
|
|
||||||
if (tick_end - tick_start < frame_len)
|
if (tick_end - tick_start < FrameLength)
|
||||||
Sleep(frame_len - (tick_end - tick_start));
|
Sleep(FrameLength - (tick_end - tick_start));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void ReleaseDirect3D()
|
||||||
|
{
|
||||||
if (D3dvb)
|
if (D3dvb)
|
||||||
D3dvb->lpVtbl->Release(D3dvb);
|
D3dvb->lpVtbl->Release(D3dvb);
|
||||||
|
|
||||||
@ -327,13 +367,4 @@ DWORD WINAPI render_d3d9_main(void)
|
|||||||
|
|
||||||
if (hD3D9)
|
if (hD3D9)
|
||||||
FreeLibrary(hD3D9);
|
FreeLibrary(hD3D9);
|
||||||
|
|
||||||
if (!useDirect3D)
|
|
||||||
{
|
|
||||||
ShowDriverWarning = TRUE;
|
|
||||||
ddraw->renderer = render_soft_main;
|
|
||||||
render_soft_main();
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user