Offset with resolution policy = showall - cocos2d-x

When using ResolutionPolicy::SHOW_ALL as resolution policy, you get some black gaps on both right and left sides.
I used
Director* director = Director::getInstance();
offset = director->getVisibleOrigin();
for getting the offset (the width of the gaps), but offset.x and offset.y is giving always 0, so I wonder: How should I calculate the offset of my main screen?

Have you read this documentation, http://www.cocos2d-x.org/wiki/Multi_resolution_support.
I am not sure, but you don't want those gaps on screen, do you ?
Use relative coordinate, and you will be fine.
Also, if you want gap's width anyways, you can use getFrameSize() (Also on that doc) to get screen's actual display resolution , and do some calculations to get that width.

Related

pygame: how to display full-screen without cutting off edges

My game is designed to work with a 16:9 display ratio.
However, my computer monitor does not have a 16:9 display. So, I've tried various methods to tell pygame to stretch the game window to full-screen, And I've encountered various problems such as:
1- The screen goes black, and my monitor says: "resolution mismatch".
2- The game window gets stretched to fit, and this messes up the graphics.
3- The edges of the screen get cut off, this is VERY unacceptable as it would give some players a disadvantage concerning how much of the playing field they can see!
I want pygame to display the game in full-screen without cutting off edges...I want it to add black bars to ether the top and bottom, or the left and right edges of the screen when necessary-depending on the players monitor.
Thanks in advance!
(And honestly, I can't believe I'm having so much trouble with what should be just a simple command, but I can't find answers anywhere!)
This is how you would scale the screen to fit any monitor, while still keeping the aspect ratio.
First you would use this code (or similar) to calculate what the screen needs to be scaled to:
import pygame
pygame.init()
infostuffs = pygame.display.Info() # gets monitor info
monitorx, monitory = infostuffs.current_w, infostuffs.current_h # puts monitor length and height into variables
dispx, dispy = <insert what you want your display length to be>, <and height>
if dispx > monitorx: # scales screen down if too long
dispy /= dispx / monitorx
dispx = monitorx
if dispy > monitory: # scales screen down if too tall
dispx /= dispy / monitory
dispy = monitory
dispx = int(dispx) # So your resolution does not contain decimals
dispy = int(dispy)
This gives you dispx and dispy, which are the dimensions that you should scale your display to every loop before you update the display. Also, just to warn you, I have not been able to test this code. If there is anything wrong, please tell me in the comments so I can fix it.
EDIT: Added two more lines of code.
I have not tried it, but my approach would be:
1. 16 / 9 ~= 1.778
2. `pygame.init()` ; `scr = pygame.display.Info()` ; `win_size = width, height = scr.current_w, scr.current_h` should give the display width and height.
3. Multiply height by 1.778, `x = int(height * 1.778)`.
4. If x < width, then width = x.
5. If not, then divide width by 1.7788, `y = int(width / 1.778)`. Now, height = y
6. `win_size = width, height` ; `screen = pygame.display.set_mode(win_size, FULLSCREEN)`
7. Scale and center align your graphics to fit.

Cocos-2dx v3 centering background sprite problems

Here is my background image:
And here is some code that I would assume scales this image to fully fit the screen.
Size visibleSize = Director::getInstance()->getVisibleSize();
auto bg = Sprite::create("grad.png");
bg->setScale(visibleSize.width / bg->getContentSize().width, visibleSize.height / bg->getContentSize().height);
bg->setAnchorPoint(Vec2(0,0));
addChild(bg);
I would expect those 4 lines to create a background sprite that would cover the entire screen size. However, here's a screenshot of what I am actually getting on my iPhone6+:
If I change the first line to
Size visibleSize = Director::getInstance()->getWinSize();
Then this is what I get, which isn't quite right either:
Using VisibleSize is correct, you just need one more change:
bg->setPosition(director->getVisibleOrigin());
By default, cocos2d-x uses ResolutionPolicy::NO_BORDER, so the bottom part of winSize is likely to be cropped.
getVisibleSize() returns the visible origin in Point rather then pixel.

Increase an Image's clickable area

Edit: I am using LibGDX framework.
There is an Image Actor, which is:
Attached to a Stage.
Has an OnClickListener, e.g.:image.addListener(new OnClickListener() { ... });
This Image's touchable area is fixed on the image's width and height.
I want to increase the touchable area by N pixels.
How can I achieve this?
Here's an illustration:
(red rectangle = touchable/clickable area)
Image already supports this out of the box. The actor can be bigger than the drawn image itself. You can supply a Scaling strategy for the drawn picture and in case you use Scaling.none, the drawn picture will be independent of the actor's size.
image.setScaling(Scaling.none)
int N = 30;
image.setSize(image.getImageWidth() + N, image.getImageHeight() + N);
The way I would approach it, is to have a custom image view, with the actual Image View inside a RelativeLayout. The relative layout has padding and/or margin set, so that it is bigger than the imageView. then, when you set the onClicklistener, set it on the relative layout as well as the image layout (in your custom class)

how to get the global width and height of movieClip after scaling parent(s)?

I do work on some already developped project.
myMovieClip has been scaled and is nested into many movieclips which may have been scaled themselves
When I trace his width and height, it does not give me the right width and size:
How can I get the absolute width and height ?
(the width and height it takes on the screen)
(a kind of localToGlobalWidth function)
regards
You can use the getBounds method of a display object to get the bounds (actual width/height and position) relative to whatever display object you pass to the method as an argument. (doucmentation)
myScaledObj.getBounds(stage); //would return a rectangle of where on the stage the display object is.
The width and height property of the returned rectangle would be what you'd use.
Do you know how many times it's been scaled, and by how much it's being scaled? In that case, you could trace the width * the scaling, for example:
mc1 has a width of 100, and is being scaled by 2:
mc1.scaleX = 2;
trace (mc1.width*2);
Extra information: if it's being scaled by a variable, replace 2 with the variable name.
This method should work even if it's being scaled multiple times, by using a variable to pile up the scaling:
var scaler1:int = 2;
var scaler2:int = 7;
var scalePiler:int = scaler1*scaler2;
mc1.scaleX = scalePiler;
trace (mc1.width*scalePiler);
Hope I helped and covered all possibilities, good luck with your program! ^^

Resize an image with height/width constraints and constant ratio

I want to insert an image in a webpage and I want it to fit in a 120*40 space.
The problem is, original images can have about any size (400*40, 30*220, etc.)
so if I set height attribute to 40, I might find myself with images larger than 120 width. The same goes if I set a 120px width.
If I set both width to 120 and height to 40, well it fits, but the original ratio is lost, and I don't want that.
What would you suggest ?
Get the original properties of the image in javascript and then set one of them (either to 120 width or 40 height) so that the other fits in 120*40 ?
There are a lot images like that in one page so I think this method is a bit heavy...
PHP solution :
<?php
list($width, $height, $type, $attr) = getimagesize($image);
if($width/$height>3)
$height *= 120/$width;
else
$height = 40;
?>
<img src="<?=$image?>" height=<?=$height?>>
see below for a javascript solution and a CSS solution
css properties max-width and max-height are what you need.
My guess is that it will resize itself if it reaches one of these.
I have used this alot in previous web projects.
But i havent used the combination of both yet.
EDIT: I've sais this in a comment, but setting both those properties does work in my tests. It keeps the ratio and resizes by the limit it reaches first. Do not set any width or height properties, these might cause problems
JavaScript is quite fast, so why not try it?
I'd just stick to finding the aspect ratio and adding some checks:
var width = image.width;
var height = image.height;
var ratio = width / height;
if (width > 120) {
width = 120;
height = 120 * ratio;
} else if (height > 80) {
height = 80;
width = 80 * ratio;
}
image.width = width + 'px';
image.height = height + 'px';
As you seem to be using PHP, ImageMagick can resize an image to fit inside of a predefined box. I only know how to do it via CLI, as I don't use PHP, but I bet the PHP code would be simple.
I was actually searching for an answer to a different query but came across yours.
I use this to resize images which I am finding is very handy in a number of my scripts, but what I would suggest is that you resize the image to a little bigger than the longest side of the container and then use css to center the image both horizontally and vertically and set the container with overflow:hidden;
You lose a small bit of the image around the edges but at least they are all inserted without any stretching or squashing.
Hope that helps you or anyone else trying something similar.