los-cocos / cocos

graphic library for games and multimedia, for python language on PC-like hardware
http://los-cocos.github.io/cocos-site/
Other
632 stars 146 forks source link

layer size is a quarter of the window size on Mac #303

Open yousiku opened 6 years ago

yousiku commented 6 years ago

1511099214077

My enviroment: macOS 10.12.6 python 3.6.3 cocos2d 0.6.5

it's the same result running any of the samples on my computer. When I print director.get_window_size() it returns (640, 480) but when I set label.postion = 320, 240, it's obviously not in the center of window, and the layer's size is always a quarter of the window. why?

ccanepa commented 6 years ago

Hello, yousiku

Is that image from a demo_grid_effects.py without any changes in the code ?

That mac has a retina display ?

Can you try this pure pyglet script? It should show the string '---o---' with the 'o' at the screen center. If it shows not at center the problem comes from pyglet, and you should open an issue in their bug tracker

"""shows the string '---o---', screen center should be on the 'o'
"""

from pyglet.gl import *
import pyglet
from pyglet.window import key

window = pyglet.window.Window(640, 480)

@window.event
def on_key_press(symbol, modifiers):
    if symbol == key.ESCAPE:
        window.has_exit = True

@window.event
def on_draw():
    window.clear()
    label.draw()

def update(dt):
    pass
pyglet.clock.schedule_interval(update, 1/30.)

label = pyglet.text.Label('---o---',
                          font_size=14,
                          x=window.width // 2, y=window.height // 2, 
                          anchor_x='center')

if __name__ == '__main__':
    pyglet.app.run()
yousiku commented 6 years ago

@ccanepa I tried this script and the 'O' is at the center. All the images was without changes, the Mac has Retina display, is that related to the display? I guess there was something wrong when added the Layer to the Scene, or it created a window with width and height double then the normal.

ccanepa commented 6 years ago

To support the high resolution in Retina displays sometimes a software layer do a "behind your back" "double the widths and heights", so it may be related.

But if pyglet displays ok, then we can make cocos to perform ok.

I don't have access to a mac so I need to collect some info. Could you insert, in the pyglet script above, the line print(window.width, window.height) after the line window = pyglet.window.Window(640, 480) run from a console and report what numbers were printed?

ccanepa commented 6 years ago

Also, which pyglet version are you running? I found some references that suggest pyglet changed behavior for retina displays in v1.3 https://bitbucket.org/pyglet/pyglet/issues/45/retina-display-scaling-on-os-x https://groups.google.com/forum/#!topic/pyglet-users/M6mkxdg0EJc

yousiku commented 6 years ago

@ccanepa

The line prints 640 480, and the pyglet version is 1.3.0

It seems that the problem is the Retina display, everything worked perfectly after applied this patch file from https://bitbucket.org/pyglet/pyglet/issues/45/retina-display-scaling-on-os-x

Thank you so much!

fpierfed commented 6 years ago

Hello,

I am having the same issue on Macs with HiDPI screen resolutions (not just macbooks).

One solution is to detect HiDPI resolutions and increase the window size values accordingly. This can be done in Director.scaled_resize_window and Director.unscaled_resize_window as per the patch attached below.

Bear in mind however that I do not know enough about cocos or pyglet to know if this is a terribly inefficient way to go about it or not...

I should add that this change requires pyglet.window.Window.get_viewport_size() which was introduced only fairly recently.

hidpi.patch.txt

a69e commented 5 years ago

Thanks for fixing this! I've met the same problem today, and after the path everything's cool now. 👍 BTW, @ccanepa could you possibly merge this patch so that no one needs to manually patch no more?

ghost commented 3 years ago

The patch provided above seems to have a bug which causes a crash when you resize the window. I have a fix that could be merged and works even when resizing the window. I've posted it here as PR #340 for review and have some extra pairs of eyes looking at it.

ghost commented 3 years ago

I fixed the issue in my original PR that was preventing the unit tests from completing. This is still probably mostly relevant for macOS but at least other architectures that provide get_viewport_size() on pyglet.Window should work too. New PR is #346

MTurandot commented 1 year ago

@ccanepa

The line prints 640 480, and the pyglet version is 1.3.0

It seems that the problem is the Retina display, everything worked perfectly after applied this patch file from https://bitbucket.org/pyglet/pyglet/issues/45/retina-display-scaling-on-os-x

Thank you so much!

I almost have the same problem but the link you listed above is not available now. Could you please tell me how to solve it?

shayneoneill commented 1 year ago

Whats the current fix for this?

ccanepa commented 11 months ago

The patch by Didier Malenfant is on master.

Release is lagging due to python 3.12 compatibility issues; one has been fixed, https://github.com/pyglet/pyglet/issues/964 , theres an import one in cocos that should be not hard but I'm missing the time to look at..