Skip to content

Instantly share code, notes, and snippets.

@camilacanhete
Created June 9, 2018 21:55
Show Gist options
  • Save camilacanhete/7fac63a71c26d340cb23e27f25bf6994 to your computer and use it in GitHub Desktop.
Save camilacanhete/7fac63a71c26d340cb23e27f25bf6994 to your computer and use it in GitHub Desktop.
GameMaker: Studio mobile display setup (keeping aspect ratio without black bars)
/* Paste this script inside your room creation code */
view_wview[0] = floor(global.viewWidth);
view_hview[0] = floor(global.viewHeight);
view_wport[0] = global.maxWidth;
view_hport[0] = global.maxHeight;
view_xview[0] = (global.idealWidth / 2) - (floor(global.viewWidth) / 2); //csantos: center-align view width
view_yview[0] = (global.idealHeight/ 2) - (floor(global.viewHeight)/ 2); //csantos: center-align view height
display_set_gui_size(view_wview[0], view_hview[0]);
surface_resize(application_surface, view_wview[0], view_hview[0]);
/* This script must be executed once, ideally at game start */
var isPortrait = false;
//csantos: display variables (ideal width and height is the same as your room size)
global.idealWidth = 768;
global.idealHeight = 1024;
global.maxWidth = display_get_width();
global.maxHeight = display_get_height();
global.aspectRatio = display_get_width()/display_get_height();
global.viewWidth = 0;
global.viewHeight = 0;
//csantos: portrait
if(global.maxWidth < global.maxHeight) {
isPortrait = true;
global.viewWidth = min(global.idealWidth, global.maxWidth);
global.viewHeight = global.viewWidth / global.aspectRatio;
}
//csantos: landscape
else {
global.viewHeight = min(global.idealHeight, global.maxHeight);
global.viewWidth = global.viewHeight * global.aspectRatio;
}
var xView = (global.idealWidth / 2) - (floor(global.viewWidth) / 2);
var yView = (global.idealHeight / 2) - (floor(global.viewHeight) / 2);
//csantos: adjusting for retina displays
if((isPortrait && xView > 0) || (!isPortrait && yView > 0)) {
global.viewWidth = global.viewWidth*1.25;
global.viewHeight = global.viewHeight*1.25;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment