C++ PauseResume
From J2Play
Contents |
J2Play Event
The J2Play wrapper is composed of different tabs, when the player switches between these tabs, or switches to other games, the main PC game has to pause until the player switches back to it. Information about the user’s behaviour with the J2Play wrapper can be obtained by hooking callback function to predefined GDK events. This section describes how to handle the toolkit's pause and resume events.
Hooks
Currently, we use __hook and __unhook keywords provide by MSVC to implement the event delegate.
Pause and Resume Events
The two events involved with pausing and resuming are:
GamePause - When the user switches to another game or any of the menus
GameResume - When the user switches back.
Event Receiver
Firstly, you need to define a class as event receiver class.
[event_receiver(native)]
class CEventReceiver
{
public:
void RegisterHooks(CGDKInstance* gdkinst);
void UnregisterHooks(CGDKInstance* gdkinst);
private:
void OnEventPause();
void OnEventResume();
// .. other GDK event handler
public:
// .. other methods and members here in help of game pause and resume implementation
};
MFC: Recommend to use the game main app class (like CYourGameApp and derived from CWinApp) as the event receiver class, append above methods to it's definition, and replace all CEventReceiver in following code with CYourGameApp (the name of app class).
WIN32: Generate a new CEventReceiver class or use other appropriate class, and you need to create a instance for CEventReciver
CEventReceiver receiver;
Then change the RegisterHooks call in initializing to
receiver.RegisterHooks(j2play);
and change the UnegisterHooks call in uninitializing to
receiver.UnegisterHooks(j2play);
Register and Unregister Hooks
Then, implement the register and unregister functions.
void CEventReceiver::RegisterHooks(CGDKInstance* gdkinst)
{
__hook(&CGDKInstance::GamePause, gdkinst, &CEventReceiver::OnEventPause);
__hook(&CGDKInstance::GameResume, gdkinst, &CEventReceiver::OnEventResume);
// ... other hooks
}
void CEventReceiver::UnregisterHooks(CGDKInstance* gdkinst)
{
__unhook(&CGDKInstance::GamePause, gdkinst, &CEventReceiver::OnEventPause);
__unhook(&CGDKInstance::GameResume, gdkinst, &CEventReceiver::OnEventResume);
// ... other unhooks
}
Event Handlers
Finally, place the code for how your game should behave when the game is paused and resumed by implementing these two functions:
void CEventReceiver::OnEventPause()
{
// TODO: add code here to pause the game, but don't suspend the message loop
}
void CEventReceiver::OnEventResume()
{
// TODO: add code here to resume the game
}
