Skip to content

Instantly share code, notes, and snippets.

@haxpor
Created August 29, 2012 15:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haxpor/3514418 to your computer and use it in GitHub Desktop.
Save haxpor/3514418 to your computer and use it in GitHub Desktop.
Example to scale content to fit the current screen resolution with the option to recieve "width" and "height" from external or from browser's available client screen.
/****************************************************************************
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2008-2010 Ricardo Quesada
Copyright (c) 2011 Zynga Inc.
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
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
var cc = cc = cc || {};
/**
@brief The cocos2d Application.
The reason for implement as private inheritance is to hide some interface call by CCDirector.
*/
cc.AppDelegate = cc.Application.extend({
ctor:function () {
this._super();
},
/**
@brief Implement for initialize OpenGL instance, set source path, etc...
*/
initInstance:function () {
return true;
},
/**
@brief Implement CCDirector and CCScene init code here.
@return true Initialize success, app continue.
@return false Initialize failed, app terminate.
*/
applicationDidFinishLaunching:function () {
// initialize director
var pDirector = cc.Director.sharedDirector();
// save the self pointer
var selfPointer = this;
// set the flag of finish loading
global.isAppFinishedLoading = 1;
// TODO: Insert the first time (one time) initialization here.
// get winSize and set centerPos
//winSize = cc.Director.sharedDirector().getWinSize();
util.detectDevice();
// calculate the global's scale factor based on whether it's fixed scaling, or automatically detect from device's screen
if(global.isFixedScaling)
{
global.scale = util.getScaleFactorFromResolution(global.targetResolutionWidth, global.targetResolutionHeight);
cc.canvas.width = cc.originalCanvasSize.width * global.scale;
cc.canvas.height = cc.originalCanvasSize.height * global.scale;
cc.renderContext.translate(0, cc.canvas.height);
cc.renderContext.scale(global.scale, global.scale);
cc.Director.sharedDirector().setContentScaleFactor(global.scale);
}
else
{
this.adjustSizeForWindow();
window.addEventListener("resize", function (event) {
selfPointer.adjustSizeForWindow();
});
}
// get winSize (based on scaled resolution)
// cache winSize and centerPos
winSize = cc.Director.sharedDirector().getWinSize();
centerPos = cc.ccp(winSize.width/2, winSize.height/2);
util.log("baseResolution: " + global.baseResolutionWidth + ", " + global.baseResolutionHeight);
util.log("targetResolutionWidth: " + global.targetResolutionWidth + ", " + global.targetResolutionHeight);
util.log("winSize: " + winSize.width + ", " + winSize.height);
util.log("centerPos: " +centerPos.x + ", " + centerPos.y);
util.log("global.scale: " + global.scale);
// enable High Resource Mode(2x, such as iphone4) and maintains low resource on other devices.
// pDirector->enableRetinaDisplay(true);
// show FPS
if(global.isShowFPS)
pDirector.setDisplayFPS(true);
// pDirector->setDeviceOrientation(kCCDeviceOrientationLandscapeLeft);
// set FPS. the default value is 1.0/60 if you don't call this
pDirector.setAnimationInterval(1.0 / 60);
// create a scene. it's an autorelease object
var pScene = MainMenu.scene();
// run
pDirector.runWithScene(pScene);
return true;
},
/**
@brief The function be called when the application enter background
@param the pointer of the application
*/
applicationDidEnterBackground:function () {
cc.Director.sharedDirector().pause();
},
/**
@brief The function be called when the application enter foreground
@param the pointer of the application
*/
applicationWillEnterForeground:function () {
cc.Director.sharedDirector().resume();
},
// Adjust size for the current window's resolution
adjustSizeForWindow:function () {
if(global.isIntegrateTest)
global.scale = util.getScaleFactorFromResolution(contentWidth, contentHeight);
else
global.scale = util.getScaleFactorFromResolution(window.innerWidth, window.innerHeight);
cc.canvas.width = cc.originalCanvasSize.width * global.scale;
cc.canvas.height = cc.originalCanvasSize.height * global.scale;
cc.renderContext.translate(0, cc.canvas.height);
cc.renderContext.scale(global.scale, global.scale);
cc.Director.sharedDirector().setContentScaleFactor(global.scale);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment