Created
March 5, 2015 12:27
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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