Hmm okay.
Could I replace the BeginScene() and EndScene() functions with my own somehow?
What I'm at right now: Those functions are defined as COM IDirect3DDevice9 interface functions. pd3ddev holds a pointer to that device/ interface ("d3d9.h") at runtime. The functions are stored in a lpVtbl table but I can ignore that in lite-C.
The function address is ((LPDIRECT3DDEVICE9)pd3ddev)->BeginScene (?). I can save that and replace it with my own function (?). This leads me to a code as follows:
HRESULT BeginSceneOld(void *This);
HRESULT EndSceneOld(void *This);
HRESULT EndSceneKu(void *This);
HRESULT BeginSceneKu(void *This)
{
cprintf1("nBeginSceneKu at frame %d",(int)total_frames);
LPDIRECT3DDEVICE9 pd3dDevKu = (LPDIRECT3DDEVICE9)pd3ddev;
return BeginSceneOld(pd3dDevKu); //pd3dDevKu->lpVtbl->
}
HRESULT EndSceneKu(void *This)
{
cprintf1("nEndSceneKu at frame %d",(int)total_frames);
LPDIRECT3DDEVICE9 pd3dDevKu = (LPDIRECT3DDEVICE9)pd3ddev;
return EndSceneOld(pd3dDevKu);
}
void main()
{
fps_max = 60;
level_load(NULL);
wait(1);
LPDIRECT3DDEVICE9 pd3dDevKu = (LPDIRECT3DDEVICE9)pd3ddev;
BeginSceneOld = pd3dDevKu->BeginScene;
pd3dDevKu->BeginScene = BeginSceneKu;
EndSceneOld = pd3dDevKu->EndScene;
pd3dDevKu->EndScene = EndSceneKu;
}
Probably an absolute mess to everyone who knows what they are doing. This leads to a script crash after the first execution of BeginSceneKu (does not close the program) - returning the old BeginSceneOld() (with or without parameter) is correct, right?
I'm guessing the fact that I'm dealing with an interface is leading to a crash ("this" not working right for me/ ...).
I can code it even worse though, that is switch around function pointers in every Begin/EndScene call:
HRESULT BeginSceneKu(void *This)
{
cprintf1("nBeginSceneKu at frame %d",(int)total_frames);
LPDIRECT3DDEVICE9 pd3dDevKu = (LPDIRECT3DDEVICE9)pd3ddev;
pd3dDevKu->EndScene = EndSceneKu;
pd3dDevKu->BeginScene = BeginSceneOld;
return pd3dDevKu->BeginScene();
}
HRESULT EndSceneKu(void *This)
{
cprintf1("nEndSceneKu at frame %d",(int)total_frames);
LPDIRECT3DDEVICE9 pd3dDevKu = (LPDIRECT3DDEVICE9)pd3ddev;
pd3dDevKu->BeginScene = BeginSceneKu;
pd3dDevKu->EndScene = EndSceneOld;
return pd3dDevKu->EndScene();
}
This prints both functions to the console "at frame 1", then waits a second and closes the program without error message.
Could I easily hook those functions somehow (to let's say put a WaitForSingleObject(MUTEX) in BeginScene and a release in EndScene)?
Thanks!
EDIT: If I wait for more than 1 frame at the start of the main function my pointer shenanigans don't seem to have any effect, the game runs as normal but doesn't print anything to the console either.