mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-06 03:22:39 +08:00
Merge pull request #455 from zilongshanren/add-show-fps-on-mac-simulator
add show FPS on mac simulator
This commit is contained in:
@@ -1,18 +1,18 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2010 cocos2d-x.org
|
||||
|
||||
|
||||
http://www.cocos2d-x.org
|
||||
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
@@ -68,7 +68,7 @@ std::string getCurAppName(void)
|
||||
int found = appName.find(" ");
|
||||
if (found!=std::string::npos)
|
||||
appName = appName.substr(0,found);
|
||||
|
||||
|
||||
return appName;
|
||||
}
|
||||
|
||||
@@ -78,7 +78,7 @@ static void glfwDropFunc(GLFWwindow *window, int count, const char **files)
|
||||
AppEvent forwardEvent(kAppEventDropName, APP_EVENT_DROP);
|
||||
std::string firstFile(files[0]);
|
||||
forwardEvent.setDataString(firstFile);
|
||||
|
||||
|
||||
Director::getInstance()->getEventDispatcher()->dispatchEvent(&forwardEvent);
|
||||
}
|
||||
#endif
|
||||
@@ -101,7 +101,7 @@ static void glfwDropFunc(GLFWwindow *window, int count, const char **files)
|
||||
{
|
||||
return NO;
|
||||
}
|
||||
|
||||
|
||||
if (isDirectory)
|
||||
{
|
||||
// check src folder
|
||||
@@ -121,7 +121,7 @@ static void glfwDropFunc(GLFWwindow *window, int count, const char **files)
|
||||
_project.setProjectDir([path cStringUsingEncoding:NSUTF8StringEncoding]);
|
||||
_entryPath = [path cStringUsingEncoding:NSUTF8StringEncoding];
|
||||
}
|
||||
|
||||
|
||||
return YES;
|
||||
}
|
||||
|
||||
@@ -129,17 +129,17 @@ static void glfwDropFunc(GLFWwindow *window, int count, const char **files)
|
||||
{
|
||||
SIMULATOR = self;
|
||||
player::PlayerMac::create();
|
||||
|
||||
|
||||
_debugLogFile = 0;
|
||||
|
||||
|
||||
[self parseCocosProjectConfig:&_project];
|
||||
[self updateProjectFromCommandLineArgs:&_project];
|
||||
|
||||
|
||||
if (_entryPath.length())
|
||||
{
|
||||
_project.setScriptFile(_entryPath);
|
||||
}
|
||||
|
||||
|
||||
[self createWindowAndGLView];
|
||||
[self startup];
|
||||
}
|
||||
@@ -193,17 +193,17 @@ static void glfwDropFunc(GLFWwindow *window, int count, const char **files)
|
||||
string arg = [[nsargs objectAtIndex:i] cStringUsingEncoding:NSUTF8StringEncoding];
|
||||
if (arg.length()) args.push_back(arg);
|
||||
}
|
||||
|
||||
|
||||
if (args.size() && args.at(1).at(0) == '/')
|
||||
{
|
||||
// FIXME:
|
||||
// for Code IDE before RC2
|
||||
tmpConfig.setProjectDir(args.at(1));
|
||||
}
|
||||
|
||||
|
||||
tmpConfig.parseCommandLine(args);
|
||||
}
|
||||
|
||||
|
||||
// set project directory as search root path
|
||||
string solutionDir = tmpConfig.getProjectDir();
|
||||
string spath = solutionDir;
|
||||
@@ -249,7 +249,7 @@ static void glfwDropFunc(GLFWwindow *window, int count, const char **files)
|
||||
if(!FileUtils::getInstance()->isFileExist(configPath))
|
||||
configPath = solutionDir.append(CONFIG_FILE);
|
||||
parser->readConfig(configPath);
|
||||
|
||||
|
||||
// set information
|
||||
config->setConsolePort(parser->getConsolePort());
|
||||
config->setFileUploadPort(parser->getUploadPort());
|
||||
@@ -277,7 +277,7 @@ static void glfwDropFunc(GLFWwindow *window, int count, const char **files)
|
||||
string arg = [[nsargs objectAtIndex:i] cStringUsingEncoding:NSUTF8StringEncoding];
|
||||
if (arg.length()) args.push_back(arg);
|
||||
}
|
||||
|
||||
|
||||
if (args.size() && args.at(1).at(0) == '/')
|
||||
{
|
||||
// for Code IDE before RC2
|
||||
@@ -297,7 +297,7 @@ static void glfwDropFunc(GLFWwindow *window, int count, const char **files)
|
||||
options:NSWorkspaceLaunchNewInstance
|
||||
configuration:configuration
|
||||
error:&error];
|
||||
|
||||
|
||||
if (error.code != 0)
|
||||
{
|
||||
NSLog(@"Failed to launch app: %@", [error localizedDescription]);
|
||||
@@ -325,20 +325,20 @@ static void glfwDropFunc(GLFWwindow *window, int count, const char **files)
|
||||
- (float) titleBarHeight
|
||||
{
|
||||
NSRect frame = NSMakeRect (0, 0, 100, 100);
|
||||
|
||||
|
||||
NSRect contentRect;
|
||||
contentRect = [NSWindow contentRectForFrameRect: frame
|
||||
styleMask: NSTitledWindowMask];
|
||||
|
||||
|
||||
return (frame.size.height - contentRect.size.height);
|
||||
|
||||
|
||||
}
|
||||
|
||||
- (void) createWindowAndGLView
|
||||
{
|
||||
GLContextAttrs glContextAttrs = {8, 8, 8, 8, 24, 8};
|
||||
GLView::setGLContextAttrs(glContextAttrs);
|
||||
|
||||
|
||||
// create console window **MUST** before create opengl view
|
||||
#if (CC_CODE_IDE_DEBUG_SUPPORT == 1)
|
||||
if (_project.isShowConsole())
|
||||
@@ -348,11 +348,11 @@ static void glfwDropFunc(GLFWwindow *window, int count, const char **files)
|
||||
}
|
||||
#endif
|
||||
float frameScale = _project.getFrameScale();
|
||||
|
||||
|
||||
// get frame size
|
||||
cocos2d::Size frameSize = _project.getFrameSize();
|
||||
ConfigParser::getInstance()->setInitViewSize(frameSize);
|
||||
|
||||
|
||||
// check screen workarea size
|
||||
NSRect workarea = [NSScreen mainScreen].visibleFrame;
|
||||
float workareaWidth = workarea.size.width;
|
||||
@@ -369,35 +369,35 @@ static void glfwDropFunc(GLFWwindow *window, int count, const char **files)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (frameScale < 0.25f) frameScale = 0.25f;
|
||||
_project.setFrameScale(frameScale);
|
||||
CCLOG("FRAME SCALE = %0.2f", frameScale);
|
||||
|
||||
|
||||
// check window offset
|
||||
Vec2 pos = _project.getWindowOffset();
|
||||
if (pos.x < 0) pos.x = 0;
|
||||
if (pos.y < 0) pos.y = 0;
|
||||
|
||||
|
||||
// create opengl view
|
||||
const cocos2d::Rect frameRect = cocos2d::Rect(0, 0, frameSize.width, frameSize.height);
|
||||
std::stringstream title;
|
||||
title << "Cocos Simulator (" << _project.getFrameScale() * 100 << "%)";
|
||||
GLViewImpl *eglView = GLViewImpl::createWithRect(title.str(), frameRect, frameScale, true);
|
||||
|
||||
|
||||
auto director = Director::getInstance();
|
||||
director->setOpenGLView(eglView);
|
||||
|
||||
|
||||
_window = eglView->getCocoaWindow();
|
||||
[[NSApplication sharedApplication] setDelegate: self];
|
||||
[_window center];
|
||||
|
||||
|
||||
[self setZoom:_project.getFrameScale()];
|
||||
if (pos.x != 0 && pos.y != 0)
|
||||
{
|
||||
[_window setFrameOrigin:NSMakePoint(pos.x, pos.y)];
|
||||
}
|
||||
|
||||
|
||||
#if (PLAYER_SUPPORT_DROP > 0)
|
||||
glfwSetDropCallback(eglView->getWindow(), glfwDropFunc);
|
||||
#endif
|
||||
@@ -407,7 +407,7 @@ static void glfwDropFunc(GLFWwindow *window, int count, const char **files)
|
||||
{
|
||||
NSApplication *thisApp = [NSApplication sharedApplication];
|
||||
NSMenu *mainMenu = [thisApp mainMenu];
|
||||
|
||||
|
||||
NSMenuItem *editMenuItem = [mainMenu itemWithTitle:@"Edit"];
|
||||
if (editMenuItem)
|
||||
{
|
||||
@@ -421,9 +421,9 @@ static void glfwDropFunc(GLFWwindow *window, int count, const char **files)
|
||||
- (void) startup
|
||||
{
|
||||
FileUtils::getInstance()->setPopupNotify(false);
|
||||
|
||||
|
||||
_project.dump();
|
||||
|
||||
|
||||
const string projectDir = _project.getProjectDir();
|
||||
if (projectDir.length())
|
||||
{
|
||||
@@ -433,23 +433,23 @@ static void glfwDropFunc(GLFWwindow *window, int count, const char **files)
|
||||
[self writeDebugLogToFile:_project.getDebugLogFilePath()];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
const string writablePath = _project.getWritableRealPath();
|
||||
if (writablePath.length())
|
||||
{
|
||||
FileUtils::getInstance()->setWritablePath(writablePath.c_str());
|
||||
}
|
||||
|
||||
|
||||
// path for looking Lang file, Studio Default images
|
||||
NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
|
||||
FileUtils::getInstance()->addSearchPath(resourcePath.UTF8String);
|
||||
|
||||
|
||||
// app
|
||||
_app = new AppDelegate();
|
||||
|
||||
|
||||
[self setupUI];
|
||||
[self adjustEditMenuIndex];
|
||||
|
||||
|
||||
RuntimeEngine::getInstance()->setProjectConfig(_project);
|
||||
Application::getInstance()->run();
|
||||
CC_SAFE_DELETE(_app);
|
||||
@@ -460,7 +460,7 @@ static void glfwDropFunc(GLFWwindow *window, int count, const char **files)
|
||||
- (void) setupUI
|
||||
{
|
||||
auto menuBar = player::PlayerProtocol::getInstance()->getMenuService();
|
||||
|
||||
|
||||
// VIEW
|
||||
menuBar->addItem("VIEW_MENU", tr("View"));
|
||||
SimulatorConfig *config = SimulatorConfig::getInstance();
|
||||
@@ -471,34 +471,42 @@ static void glfwDropFunc(GLFWwindow *window, int count, const char **files)
|
||||
std::stringstream menuId;
|
||||
menuId << "VIEWSIZE_ITEM_MENU_" << i;
|
||||
auto menuItem = menuBar->addItem(menuId.str(), size.title.c_str(), "VIEW_MENU");
|
||||
|
||||
|
||||
if (i == current)
|
||||
{
|
||||
menuItem->setChecked(true);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
menuBar->addItem("DIRECTION_MENU_SEP", "-", "VIEW_MENU");
|
||||
menuBar->addItem("DIRECTION_PORTRAIT_MENU", tr("Portrait"), "VIEW_MENU")
|
||||
->setChecked(_project.isPortraitFrame());
|
||||
menuBar->addItem("DIRECTION_LANDSCAPE_MENU", tr("Landscape"), "VIEW_MENU")
|
||||
->setChecked(_project.isLandscapeFrame());
|
||||
|
||||
|
||||
menuBar->addItem("VIEW_SCALE_MENU_SEP", "-", "VIEW_MENU");
|
||||
|
||||
|
||||
auto displayStats = Director::getInstance()->isDisplayStats();
|
||||
string fpsItemName = displayStats ? tr("Hide FPS") : tr("Show FPS");
|
||||
menuBar->addItem("VIEW_SHOW_FPS", fpsItemName, "VIEW_MENU")
|
||||
->setChecked(displayStats);
|
||||
|
||||
menuBar->addItem("VIEW_SHOW_FPS_SEP", "-", "VIEW_MENU");
|
||||
|
||||
|
||||
std::vector<player::PlayerMenuItem*> scaleMenuVector;
|
||||
auto scale100Menu = menuBar->addItem("VIEW_SCALE_MENU_100", tr("Zoom Out").append(" (100%)"), "VIEW_MENU");
|
||||
scale100Menu->setShortcut("super+0");
|
||||
|
||||
|
||||
auto scale75Menu = menuBar->addItem("VIEW_SCALE_MENU_75", tr("Zoom Out").append(" (75%)"), "VIEW_MENU");
|
||||
scale75Menu->setShortcut("super+7");
|
||||
|
||||
|
||||
auto scale50Menu = menuBar->addItem("VIEW_SCALE_MENU_50", tr("Zoom Out").append(" (50%)"), "VIEW_MENU");
|
||||
scale50Menu->setShortcut("super+6");
|
||||
|
||||
|
||||
auto scale25Menu = menuBar->addItem("VIEW_SCALE_MENU_25", tr("Zoom Out").append(" (25%)"), "VIEW_MENU");
|
||||
scale25Menu->setShortcut("super+5");
|
||||
|
||||
|
||||
int frameScale = int(_project.getFrameScale() * 100);
|
||||
if (frameScale == 100)
|
||||
{
|
||||
@@ -520,15 +528,15 @@ static void glfwDropFunc(GLFWwindow *window, int count, const char **files)
|
||||
{
|
||||
scale100Menu->setChecked(true);
|
||||
}
|
||||
|
||||
|
||||
scaleMenuVector.push_back(scale100Menu);
|
||||
scaleMenuVector.push_back(scale75Menu);
|
||||
scaleMenuVector.push_back(scale50Menu);
|
||||
scaleMenuVector.push_back(scale25Menu);
|
||||
|
||||
|
||||
menuBar->addItem("REFRESH_MENU_SEP", "-", "VIEW_MENU");
|
||||
menuBar->addItem("REFRESH_MENU", tr("Refresh"), "VIEW_MENU")->setShortcut("super+r");
|
||||
|
||||
|
||||
ProjectConfig &project = _project;
|
||||
auto dispatcher = Director::getInstance()->getEventDispatcher();
|
||||
auto window = _window;
|
||||
@@ -541,7 +549,7 @@ static void glfwDropFunc(GLFWwindow *window, int count, const char **files)
|
||||
if (dArgParse.HasMember("name"))
|
||||
{
|
||||
string strcmd = dArgParse["name"].GetString();
|
||||
|
||||
|
||||
if (strcmd == "menuClicked")
|
||||
{
|
||||
player::PlayerMenuItem *menuItem = static_cast<player::PlayerMenuItem*>(menuEvent->getUserData());
|
||||
@@ -551,7 +559,7 @@ static void glfwDropFunc(GLFWwindow *window, int count, const char **files)
|
||||
{
|
||||
return ;
|
||||
}
|
||||
|
||||
|
||||
string data = dArgParse["data"].GetString();
|
||||
if ((data == "CLOSE_MENU") || (data == "EXIT_MENU"))
|
||||
{
|
||||
@@ -566,7 +574,7 @@ static void glfwDropFunc(GLFWwindow *window, int count, const char **files)
|
||||
string tmp = data.erase(0, strlen("VIEW_SCALE_MENU_"));
|
||||
float scale = atof(tmp.c_str()) / 100.0f;
|
||||
[SIMULATOR setZoom:scale];
|
||||
|
||||
|
||||
// update scale menu state
|
||||
for (auto &it : scaleMenuVector)
|
||||
{
|
||||
@@ -579,12 +587,12 @@ static void glfwDropFunc(GLFWwindow *window, int count, const char **files)
|
||||
string tmp = data.erase(0, strlen("VIEWSIZE_ITEM_MENU_"));
|
||||
int index = atoi(tmp.c_str());
|
||||
SimulatorScreenSize size = SimulatorConfig::getInstance()->getScreenSize(index);
|
||||
|
||||
|
||||
if (project.isLandscapeFrame())
|
||||
{
|
||||
std::swap(size.width, size.height);
|
||||
}
|
||||
|
||||
|
||||
project.setFrameSize(cocos2d::Size(size.width, size.height));
|
||||
[SIMULATOR relaunch];
|
||||
}
|
||||
@@ -597,13 +605,19 @@ static void glfwDropFunc(GLFWwindow *window, int count, const char **files)
|
||||
{
|
||||
project.changeFrameOrientationToLandscape();
|
||||
[SIMULATOR relaunch];
|
||||
}else if (data == "VIEW_SHOW_FPS")
|
||||
{
|
||||
auto director = cocos2d::Director::getInstance();
|
||||
director->setDisplayStats(director->isDisplayStats() == false);
|
||||
menuItem->setTitle(director->isDisplayStats() ? tr("Hide FPS") : tr("Show FPS"));
|
||||
// [SIMULATOR relaunch];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}), 1);
|
||||
|
||||
|
||||
// drop
|
||||
AppDelegate *app = _app;
|
||||
auto listener = EventListenerCustom::create(kAppEventDropName, [&project, app](EventCustom* event)
|
||||
@@ -613,17 +627,17 @@ static void glfwDropFunc(GLFWwindow *window, int count, const char **files)
|
||||
{
|
||||
string dirPath = dropEvent->getDataString() + "/";
|
||||
string configFilePath = dirPath + CONFIG_FILE;
|
||||
|
||||
|
||||
if (FileUtils::getInstance()->isDirectoryExist(dirPath) &&
|
||||
FileUtils::getInstance()->isFileExist(configFilePath))
|
||||
{
|
||||
// parse config.json
|
||||
ConfigParser::getInstance()->readConfig(configFilePath);
|
||||
|
||||
|
||||
project.setProjectDir(dirPath);
|
||||
project.setScriptFile(ConfigParser::getInstance()->getEntryFile());
|
||||
project.setWritablePath(dirPath);
|
||||
|
||||
|
||||
RuntimeEngine::getInstance()->setProjectConfig(project);
|
||||
// app->setProjectConfig(project);
|
||||
// app->reopenProject();
|
||||
@@ -640,11 +654,11 @@ static void glfwDropFunc(GLFWwindow *window, int count, const char **files)
|
||||
_consoleController = [[ConsoleWindowController alloc] initWithWindowNibName:@"ConsoleWindow"];
|
||||
}
|
||||
[_consoleController.window orderFrontRegardless];
|
||||
|
||||
|
||||
//set console pipe
|
||||
_pipe = [NSPipe pipe] ;
|
||||
_pipeReadHandle = [_pipe fileHandleForReading] ;
|
||||
|
||||
|
||||
int outfd = [[_pipe fileHandleForWriting] fileDescriptor];
|
||||
if (dup2(outfd, fileno(stderr)) != fileno(stderr) || dup2(outfd, fileno(stdout)) != fileno(stdout))
|
||||
{
|
||||
@@ -676,7 +690,7 @@ static void glfwDropFunc(GLFWwindow *window, int count, const char **files)
|
||||
[_pipeReadHandle readInBackgroundAndNotify] ;
|
||||
NSData *data = [[note userInfo] objectForKey:NSFileHandleNotificationDataItem];
|
||||
NSString *str = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
|
||||
|
||||
|
||||
if (str)
|
||||
{
|
||||
//show log to console
|
||||
@@ -702,7 +716,7 @@ static void glfwDropFunc(GLFWwindow *window, int count, const char **files)
|
||||
return NO;
|
||||
}
|
||||
|
||||
#pragma mark -
|
||||
#pragma mark -
|
||||
|
||||
-(IBAction)onFileClose:(id)sender
|
||||
{
|
||||
@@ -713,7 +727,7 @@ static void glfwDropFunc(GLFWwindow *window, int count, const char **files)
|
||||
-(IBAction)onWindowAlwaysOnTop:(id)sender
|
||||
{
|
||||
NSInteger state = [sender state];
|
||||
|
||||
|
||||
if (state == NSOffState)
|
||||
{
|
||||
[_window setLevel:NSFloatingWindowLevel];
|
||||
|
||||
Reference in New Issue
Block a user