Skip to content

Instantly share code, notes, and snippets.

@yankeppey
Created March 5, 2015 12:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yankeppey/3c5fbe80cdd92e380311 to your computer and use it in GitHub Desktop.
Save yankeppey/3c5fbe80cdd92e380311 to your computer and use it in GitHub Desktop.
Cocos2d-x 3.2. When user clicks "close" button or presses Cmd+Q, this code snippet shows a message asking user to confirm the game exit. Particular this code is used on mac desktop versions, but I guess the glfwSetWindowCloseCallback can be used on other desktop platforms too. Things can be changed on later versions of cocos2d-x.
/*
Cocos2d-x 3.2
When user clicks "close" button or presses Cmd+Q, this code snippet shows a message asking user to confirm the game exit.
Particular this code is used on mac desktop versions, but I guess the glfwSetWindowCloseCallback can be used on other desktop platforms too.
Things can be changed on later versions of cocos2d-x.
*/
bool AppDelegate::applicationDidFinishLaunching() {
// initialize director
auto director = Director::getInstance();
auto glview = director->getOpenGLView();
//...
#if CC_TARGET_PLATFORM == CC_PLATFORM_MAC
//auto window = (dynamic_cast<GLViewImpl *>(glview))->getWindow(); // dirty variant for cocos2d-x 3.4
auto window = glView->getWindow();
glfwSetWindowCloseCallback(window, [](GLFWwindow* window){
auto alert = [[NSAlert alloc] init];
[alert addButtonWithTitle:@"Yes"];
[alert addButtonWithTitle:@"No"];
[alert setMessageText:@"Exit the game?"];
[alert setInformativeText:@"Do you really want to exit the game?"];
[alert setAlertStyle:NSWarningAlertStyle];
if ([alert runModal] != NSAlertFirstButtonReturn) {
glfwSetWindowShouldClose(window, GL_FALSE);
}
});
#endif
//...
// run
director->runWithScene(scene);
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment