How do I detect aspect ratio and change resolution?

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
User avatar
Steamgirl
Veteran
Posts: 322
Joined: Sat Jul 28, 2012 4:39 am
Completed: My Cup of Coffee, Queen at Arms (co-wrote a battle scene)
Projects: Stranded Hearts, Emma: A Lady's Maid
Deviantart: steamgirlgame
Contact:

How do I detect aspect ratio and change resolution?

#1 Post by Steamgirl »

Hello!

I'd like to support 2 different aspect ratios - widescreen and 4:3 in my game, but I don't know how to:

a) Toggle between them.
b) Detect whether the user has a widescreen display.

Any ideas?

User avatar
Kia
Eileen-Class Veteran
Posts: 1040
Joined: Fri Aug 01, 2014 7:49 am
Deviantart: KiaAzad
Discord: Kia#6810
Contact:

Re: How do I detect aspect ratio and change resolution?

#2 Post by Kia »

edit: I was way off ^_^
Last edited by Kia on Thu Jun 11, 2015 12:47 am, edited 1 time in total.

User avatar
xela
Lemma-Class Veteran
Posts: 2481
Joined: Sun Sep 18, 2011 10:13 am
Contact:

Re: How do I detect aspect ratio and change resolution?

#3 Post by xela »

Try:

in options.rpy:

Code: Select all

init python hide:
    info = renpy.display.info
    width, height = info.current_w, info.current_h
    if round(float(width)/height, 2) == 1.78:
        config.screen_width, config.screen_height = 1280, 720
    elif round(float(width)/height, 2) == 1.33:
        config.screen_width, config.screen_height = 800, 600
    else:
        raise Exception("Get a Monitor with 16:9 or 4:3 resolutions to play this game!") # Or pick a default ;)
You'll also have to make sure all game elements/resources are structured in way that can support both without displacement. That said, you can place a lot more checks trying to get resolution info from APIs getting it directly from the OS in an unlikely case that PyGame failed to log it in for some reason. Google what to use for every OS and check if Ren'Pys Python has the required modules. Obviously you can always have a default to revert to in the else clause...

I didn't try the code so there could be some spelling errors, but the approach should be sound enough.

*And keep in mind that Ren'Py scales it's window reasonably well. Sometimes having a bit of black space on the sides/top+bottom in fullscreen mode is preferable to messing with resolutions, in my opinion it's always the case unless you're making an expensive commercial game of some sorts.
Like what we're doing? Support us at:
Image

User avatar
PyTom
Ren'Py Creator
Posts: 16096
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: How do I detect aspect ratio and change resolution?

#4 Post by PyTom »

That last bit of code seems terrible - 16:10 monitors are common, and I had a 5:4 monitor once.
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

User avatar
xela
Lemma-Class Veteran
Posts: 2481
Joined: Sun Sep 18, 2011 10:13 am
Contact:

Re: How do I detect aspect ratio and change resolution?

#5 Post by xela »

LoL

That's what the slimy face was for ;)
Like what we're doing? Support us at:
Image

Jibus
Regular
Posts: 135
Joined: Tue May 26, 2015 9:45 am
Contact:

Re: How do I detect aspect ratio and change resolution?

#6 Post by Jibus »

Bump on this.

This code seems perfect me, but i have an error when i launch the application :

AttributeError: 'NoneType' object has no attribute for 'current_w'

It seems that the function returns nothing.

User avatar
xela
Lemma-Class Veteran
Posts: 2481
Joined: Sun Sep 18, 2011 10:13 am
Contact:

Re: How do I detect aspect ratio and change resolution?

#7 Post by xela »

It worked at the time of this post, this is not directly exposed to developers so there is a chance that something was changed internally or you are on an OS that doesn't return anything meaningful to pygame for whatever reason. You can try accessing os directly or figure out why you info is None now.

Something like this may still get it from pygame, I guess it was changed at some point and moved to interface class:

Code: Select all

init python:
    import pygame
    meow = pygame.display.init()
    meow = pygame.display.Info()
    width, height = meow.current_w, meow.current_h
    if round(float(width)/height, 2) == 1.78:
        config.screen_width, config.screen_height = 1280, 720
    elif round(float(width)/height, 2) == 1.33:
        config.screen_width, config.screen_height = 800, 600
    else:
        config.screen_width, config.screen_height = 800, 600
Like what we're doing? Support us at:
Image

Jibus
Regular
Posts: 135
Joined: Tue May 26, 2015 9:45 am
Contact:

Re: How do I detect aspect ratio and change resolution?

#8 Post by Jibus »

Works perfectly ! Thanks !

Post Reply

Who is online

Users browsing this forum: Ocelot