In cocos2dx using kResolutionShowAll, how can I use the empty borders? - cocos2d-x

I'm about to develop a game for android with cocos2d-x and I'm having some issues with resolution.
I'm using kResolutionShowAll resolution policy to be sure that all of my image is shown in the device.
But I get a big borders in some devices and I'd like to find out a way to fill those borders with an image, advertisement, or whatever.
I found this: http://www.cocos2d-x.org/forums/6/topics/20512?r=21066 but it doesn't really work.
I don't know if I choose a wrong policy and maybe I should take kResolutionNoBorder instead. Or maybe I misunderstood something about resolution and I'm not in the correct way.

For the support for all devices you can use following code:
**Landscape Mode**
bool AppDelegate::applicationDidFinishLaunching()
{
// initialize director
director = CCDirector::sharedDirector();
EGLView = CCEGLView::sharedOpenGLView();
director->setOpenGLView(EGLView);
CCSize screenSize = EGLView->getFrameSize();
CCSize designSize = CCSizeMake(800, 480);
EGLView->setDesignResolutionSize(designSize.width,designSize.height, kResolutionExactFit);
if(screenSize.height > 480 && screenSize.height < 720 )
{
CCSize resourceSize = CCSizeMake(960, 540);
director->setContentScaleFactor(resourceSize.height/screenSize.height);
CCLog("Resolution Scale OF Karboon=%f",resourceSize.width/screenSize.width);
}
else if (screenSize.height >= 720 && screenSize.height < 800)
{
CCSize resourceSize = CCSizeMake(1280, 720);
director->setContentScaleFactor(resourceSize.height/screenSize.height);
CCLog("Resolution Scale OF NOTE=%f",resourceSize.width/screenSize.width);
}
else if(screenSize.height > 800)
{
CCSize resourceSize = CCSizeMake(1920, 1080);
director->setContentScaleFactor(resourceSize.height/screenSize.height);
CCLog("Resolution Scale OF Nexus=%f",resourceSize.width/screenSize.width);
}
else
{
director->setContentScaleFactor(1);
CCLog("Resolution Scale OF S Advance=%f");
}

In the AppDelegate.cpp you can try this.
// initialize director
CCDirector* director = CCDirector::sharedDirector();
CCEGLView* EGLView = CCEGLView::sharedOpenGLView();
director->setOpenGLView(EGLView);
CCSize screenSize = EGLView->getFrameSize();
CCSize designSize = CCSizeMake(800, 480);
EGLView->setDesignResolutionSize(designSize.width,designSize.height, kResolutionExactFit);

Related

Choose the design resolution size for Android devices in Cocos2dx

I'm having trouble when trying to choose design resolution sizes for Android devices.
I just want to have around 4-5 resources (images, background, sprites, etc) but can run in any Android devices. What the suitable design resolution I can do? For example, 320x480, 720x1280, what else?
Can you give me some advice to choose?
Thanks
In your AppDelegate class:
this resolution is for Landscape Mode
bool AppDelegate::applicationDidFinishLaunching()
{
/********************** CCEGLView::sharedOpenGLView()->setDesignResolutionSize() //set design resolution size and mode
********************* CCEGLView::sharedOpenGLView()->getFrameSize() //get screen resolution
*********************CCDirector::sharedDirector()->getWinSize() //get design resolution
********************* CCDirector::sharedDirector()->getVisibleSize() //get design resolution’s visable area size
********************* CCDirector::sharedDirector()->getVisibleOrigin() //get origin of the visable area of design resolution*/
// initialize director
CCDirector* director = CCDirector::sharedDirector();
CCEGLView* EGLView = CCEGLView::sharedOpenGLView();
director->setOpenGLView(EGLView);
CCSize screenSize = EGLView->getFrameSize();
CCSize designSize = CCSizeMake(1024,614);
EGLView->setDesignResolutionSize(designSize.width,designSize.height, kResolutionExactFit);
CCLog("Screen Size:%f %f",screenSize.width,screenSize.height);
if(screenSize.height >= 320 && screenSize.height <= 480)
{
CCSize resourceSize = CCSizeMake(800, 480);
director->setContentScaleFactor(resourceSize.height/screenSize.height);
CCLog("Resolution Scale OF Default =%f",resourceSize.height/screenSize.height);
}
else if(screenSize.height >= 540 && screenSize.height < 720 )
{
CCSize resourceSize = CCSizeMake(960, 540);
director->setContentScaleFactor(resourceSize.height/screenSize.height);
CCLog("Resolution Scale OF Karboon=%f",resourceSize.height/screenSize.height);
}
else if (screenSize.height >= 720 && screenSize.height < 800)
{
CCSize resourceSize = CCSizeMake(1280, 720);
director->setContentScaleFactor(resourceSize.height/screenSize.height);
CCLog("Resolution Scale OF NOTE=%f",resourceSize.height/screenSize.height);
}
else if(screenSize.height > 800)
{
CCSize resourceSize = CCSizeMake(1920, 1080);
director->setContentScaleFactor(resourceSize.height/screenSize.height);
CCLog("Resolution Scale OF Nexus=%f",resourceSize.height/screenSize.height);
}
else
{
director->setContentScaleFactor(1);
CCLog("Resolution Scale OF S Advance=%f");
}

multi resolution support for all android devices

I am porting my cocos2d iPhone game to android using cocos2d-x. I am now facing a problem with screen resolution: I want to use one high resolution image in my game which should be supportable by all screens lower then a given resolution.
I read this nice tutorial of multiple resolution on forum . It's really helpful, but I am not achieving my solution. There is explanation of scale-factor of resource of Design Resolution & Resource Resolution.
But, in my case, it scales either height wise or width wise. Not a perfect scaling of my image. Can someone clarify why for me?
In AppDeligate.cpp add the following lines to
bool AppDelegate::applicationDidFinishLaunching() after the glview is set.
CCEGLView *ev = CCEGLView::sharedOpenGLView();
ev->setDesignResolutionSize(480, 320, kResolutionShowAll);
480, 320 being the resolution you designed your app for. If you want portrait use 320, 480 instead.
This solution will show black borders if the phone aspect ratio doesn't match the 480/320 aspect ratio.
In AppDelegate.cpp
This is for landscape Mode
bool AppDelegate::applicationDidFinishLaunching()
{
// initialize director
director = CCDirector::sharedDirector();
EGLView = CCEGLView::sharedOpenGLView();
director->setOpenGLView(EGLView);
CCSize screenSize = EGLView->getFrameSize();
CCSize designSize = CCSizeMake(800, 480);
EGLView->setDesignResolutionSize(designSize.width,designSize.height, kResolutionExactFit);
if(screenSize.height > 480 && screenSize.height < 720 )
{
CCSize resourceSize = CCSizeMake(960, 540);
director->setContentScaleFactor(resourceSize.height/screenSize.height);
CCLog("Resolution Scale OF Karboon=%f",resourceSize.width/screenSize.width);
}
else if (screenSize.height >= 720 && screenSize.height < 800)
{
CCSize resourceSize = CCSizeMake(1280, 720);
director->setContentScaleFactor(resourceSize.height/screenSize.height);
CCLog("Resolution Scale OF NOTE=%f",resourceSize.width/screenSize.width);
}
else if(screenSize.height > 800)
{
CCSize resourceSize = CCSizeMake(1920, 1080);
director->setContentScaleFactor(resourceSize.height/screenSize.height);
CCLog("Resolution Scale OF Nexus=%f",resourceSize.width/screenSize.width);
}
else
{
director->setContentScaleFactor(1);
CCLog("Resolution Scale OF S Advance=%f");
}
return true;
}
Here is some code that might help You make following folders in "Resource" folder
ipadhd
ipad
iphone
Use this code in Appdelegate.cpp in applicationdidfinishing method
CCSize screenSize = pEGLView->getFrameSize();
//set design size for iPad retina
CCSize designSize = CCSize(2048, 1536);
CCEGLView::sharedOpenGLView()->setDesignResolutionSize(designSize.width, designSize.height, kResolutionExactFit);
if (screenSize.height > 768) {
CCFileUtils::sharedFileUtils()->setResourceDirectory("ipadhd");
} else if (screenSize.height > 320) {
CCFileUtils::sharedFileUtils()->setResourceDirectory("ipad");
} else {
CCFileUtils::sharedFileUtils()->setResourceDirectory("iphone");
}
pDirector->setContentScaleFactor(screenSize.height/designSize.height)
Hope this helps.Put your images accordingly for iphone in iphone folder,ipad images in ipad folder and hd images in ipadhd folder. pDirector here is CCDirector variable.
i followed this nice tutorial http://www.cocos2d-x.org/projects/cocos2d-x/wiki/Multi_resolution_support
This way it worked for me. i used one high Resolution image
AppDelegate.cpp
typedef struct tagResource
{
cocos2d::CCSize size;
char directory[100];
}Resource;
static Resource smallResource = { cocos2d::CCSizeMake(320,480), "iphone" };
static Resource mediumResource = { cocos2d::CCSizeMake(768,1024), "ipad" };
static Resource largeResource = { cocos2d::CCSizeMake(1536,2048), "ipadhd" };
static cocos2d::CCSize designResolutionSize = cocos2d::CCSizeMake(640,1024);
CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();
CCSize frameSize = pDirector->getOpenGLView()->getFrameSize();
pEGLView->setDesignResolutionSize( designResolutionSize.width, designResolutionSize.height, kResolutionExactFit);
if ((frameSize.height > mediumResource.size.height))
{
pDirector->setContentScaleFactor(largeResource.size.height/designResolutionSize.height);
}
// if the frame's height is larger than the height of small resource size, select medium resource.
else if ((frameSize.height > smallResource.size.height))
{
pDirector->setContentScaleFactor(mediumResource.size.height/designResolutionSize.height);
}
// if the frame's height is smaller than the height of medium resource size, select small resource.
else
{
pDirector->setContentScaleFactor(smallResource.size.height/designResolutionSize.height);
}
CCDirector::sharedDirector()->setContentScaleFactor(1.f);

Working with canvas to display only the drawn image and not whole canvas

With the plugin i found earlier on stackoverflow. Drawing has become smooth and nice. What i want is to only get the image part which i draw cropped from the canvas as an output and not the complete canvas. Can somebody help.
This is the code i am using for my canvas now: http://jsfiddle.net/sVsZL/1/
function canvasDisplay() {
var c=document.getElementById("canvas");
canvasImage=c.toDataURL("image/png");
document.getElementById("SSMySelectedImage").src=canvasImage;
}
Adding another answer because the other one was completely off.
Live Demo
What you need essentially is to keep track of a bounding box. What I do is create an object that holds the min values and max values of where you've drawn. This enables you to keep track of how big the image is and where it begins/ends.
this.dim = {minX : 9999, minY : 9999, maxX : 0, maxY : 0};
Then I created a function that checks the bounds.
this.setDimensions = function(x,y){
if(x < this.dim.minX){
this.dim.minX = x;
}
if(y < this.dim.minY){
this.dim.minY = y;
}
if(x > this.dim.maxX){
this.dim.maxX= x;
}
if(y > this.dim.maxY){
this.dim.maxY = y;
}
}
Make sure to check during clicking or moving.
this.mousedown = function(ev) {
tool.setDimensions(ev._x,ev._y);
};
this.mousemove = function(ev) {
tool.setDimensions(ev._x,ev._y);
};
And this is just a sample function that draws the portion to a new canvas that you could then save with toDataUrl
var button = document.getElementsByTagName("input")[0];
button.addEventListener("click", function(){
var savedCanvas = document.createElement("canvas"),
savedCtx = savedCanvas.getContext("2d"),
minX = PEN.dim.minX,
minY = PEN.dim.minY,
maxX = PEN.dim.maxX,
maxY = PEN.dim.maxY,
width = maxX - minX,
height = maxY - minY;
savedCanvas.width = width;
savedCanvas.height = height;
document.body.appendChild(savedCanvas);
savedCtx.drawImage(canvas,minX,minY,width,height,0,0,width,height);
});

Canvas Zoom out Image Quality Loss

I written a code for zooming and zoom-out the canvas image but if try to zoom the image more than once the quality of image is changing.
This is my code using canvas2image plugin i am converting canvas to normal image.
Can any body please suggest me is there any thing wrong way i am doing.
function imgZoom(operation) {
var zoomImageObj = document.getElementById("originalImage");
var zoomedCanvas = $("#zoomCanvasPreview")[0];
var zoomedContext = zoomedCanvas.getContext("2d");
var selectedImgWidth = $('#originalImage').width();
var selectedImgHeight = $('#originalImage').height();
$("#zoomCanvasPreview").attr("height", selectedImgHeight);
$("#zoomCanvasPreview").attr("width", selectedImgWidth);
var previewHeight = $("#zoomCanvasPreview").height();
var previewWidth = $("#zoomCanvasPreview").width();
var zoomFactor = $("#zoomFactor option:selected").val();
// Making the zoomfactor string as float point then parsing for addition
// zoomFactor values if user selected 5%(0.05), 10%(0.1), 15(0.15) &etc
var zoomPercent = 1 + parseFloat(zoomFactor);
if(operation == 'zoomIn') {
newZoomWidth = Math.round(previewWidth * zoomPercent) ;
newZoomHeight = Math.round(previewHeight * zoomPercent) ;
} else if(operation == 'zoomOut'){
newZoomWidth = Math.round( previewWidth / zoomPercent );
newZoomHeight = Math.round( previewHeight / zoomPercent );
}
if( (newZoomWidth >= 0) && (newZoomHeight >= 0) )
{
$("#zoomCanvasPreview").attr("width", newZoomWidth);
$("#zoomCanvasPreview").attr("height", newZoomHeight);
// Drawing the image to canvas
zoomedContext.drawImage(zoomImageObj, 0, 0, imgWidth, imgHeight, 0, 0, newZoomWidth, newZoomHeight );
//return canvas.toDataURL("image/png");
var zoomedCanvas = $("#zoomCanvasPreview")[0];
oImgConvertExt = Canvas2Image.saveAsPNG(zoomedCanvas, true);
$("#originalImage").attr("src", oImgConvertExt.src);
$("#originalImage").attr("style", "display: none; visibility: hidden; width: "+newZoomWidth+"px; height: "+newZoomHeight+"px;");
} else {
alert("Reached maximum zoom out limit");
}
}
Image quality will not get changed If you use Images of format .SVG
these image will never get distorted even if you zoom IN or zoom OUT
There is no way around quality loss if you use canvas.drawImage().
Just zoom by changing the CSS width and height values. There won't be any quality loss then.
It is also a lot simpler to code...

How can I ammend this resize script to fill/crop to container?

I've got a simple script that resizes a loaded image to fit a specific width and height, however I want the option to be able to fill i.e. centre and crop to a specific width/height - any ideas on how I can modify this?
Current script:
function resizeImg(mc:MovieClip, maxW:Number, maxH:Number=0, constrainProportions:Boolean=true):void{
maxH = maxH == 0 ? maxW : maxH;
mc.width = maxW;
mc.height = maxH;
if (constrainProportions) {
mc.scaleX < mc.scaleY ? mc.scaleY = mc.scaleX : mc.scaleX = mc.scaleY;
}
}
I tried picking through the code of the DisplayUtils AS3 class from Soulwire (http://blog.soulwire.co.uk/flash/actionscript-3/fit-a-displayobject-into-a-rectangle) but its pretty well obfusticated and has no comments so im struggling :(
To do this you'll first need to store the original width and height so you can use it to scale the image. Besides that you'll need to mask the image and use your maxW and maxH values as it's dimensions. After that you'll be able to use a function like this:
function resizeImg(mc:Sprite, maxW:Number, maxH:Number = 0, constrainProportions:Boolean = true) : void
{
maxH = maxH == 0 ? maxW : maxH;
if(constrainProportions)
{
// First of we'll make the mc fit within the viewport
// calulate the difference between the max and stored dimensions
var dX:Number = maxW - originalWidth;
var dY:Number = maxH - originalHeight;
// evaluate values
if (dY > dX)
{
// mc is wider then maxW
// set width to max and scaleY to offset of width
mc.width = maxW;
mc.scaleY = mc.scaleX;
}
else
{
// mc is heigher then maxH
// set height to max and scaleX to offset of height
mc.height = maxH;
mc.scaleX = mc.scaleY;
}
// the mc now fits within the max viewport
// now we'll use the same trick to fill the resized mc in the max viewport
var dXcorrection:Number = maxW - mc.width;
var dYcorrection:Number = maxH - mc.height;
if (dYcorrection < dXcorrection)
{
mc.width = maxW;
mc.scaleY = mc.scaleX;
}
else
{
mc.height = maxH;
mc.scaleX = mc.scaleY;
}
// finally we'll center the resized mc within the max viewport
mc.x = (maxW - mc.width)/2;
mc.y = (maxH - mc.height)/2;
}
}