Simple Dynamic Resolution System
Tue Oct 24, 2017 1:27 pm
This simple system lets your game dynamically adjust it's resolution based on the display of the device displaying it. This can be useful if you have a game you'd like people to try out on various different display sizes, and it's especially useful if you're simply working on a prototype and want to have it be able to adjust depending on the display you're currently using.
Below is how to implement the system in your project.
An explanation of what is happening can be found afterwards should you be interested.
The system is simple;
Create an object and name it Resolution_Manager.
In the Create Event for the Resolution_Manager place the following;
Place the Resolution_Manager in an empty room - make sure that this is the first room your game opens when it is executed!
Afterwards, create an object and call it Camera.
In the Create Event of the Camera place the following;
In the Step Event of the Camera place the following;
How does it work?
Essentially, the resolution manager figures out the ideal aspect ratio and resolution based on the device's display. Once that's complete, it adjusts the size of the view port within GMS 2 to the ideals chosen. In this instance, I have chosen the ideal height to be 1080 which will make the game port fit that specification. You can likely add a way for the ideal height to adjust based on the display as well - however, this gives you a bit of control over what the resolution will be to let you plan ahead as you work.
Once the resolution manager is done then it moves over to the next room, where the Camera object will set up a GMS 2 camera to use. This GMS 2 camera will be centered on the Camera object, allowing you to move the Camera object as required. So if you want the camera to follow a player, simply set up the Camera object to follow the player's Avatar.
There are probably a few issues that might come up and you will most likely need to make a few modifications to make the system work with your project - however hopefully this will help get you started!
Below is how to implement the system in your project.
An explanation of what is happening can be found afterwards should you be interested.
The system is simple;
Create an object and name it Resolution_Manager.
In the Create Event for the Resolution_Manager place the following;
- Code:
/// @description Initiate Resolution
ideal_width = 0;
ideal_height = 1080;
aspect_ratio = display_get_width()/display_get_height();
ideal_width = round(ideal_height*aspect_ratio);
if (ideal_width & 1){
ideal_width += 1
}
for (var i = 1; i <= room_last; i++){
if room_exists(i){
view_set_wport(0,ideal_width)
view_set_hport(0,ideal_height)
}
}
surface_resize(application_surface, ideal_width, ideal_height);
window_set_size(ideal_width, ideal_height);
window_set_fullscreen(true);
room_goto_next();
Place the Resolution_Manager in an empty room - make sure that this is the first room your game opens when it is executed!
Afterwards, create an object and call it Camera.
In the Create Event of the Camera place the following;
- Code:
/// @description Create Camera
camera = camera_create();
global.zoom_level = 1;
var viewmat = matrix_build_lookat(x, y,-1000,x, y,0,0,1,0);
var projmat = matrix_build_projection_ortho(ResolutionManager.ideal_width * global.zoom_level,ResolutionManager.ideal_height * global.zoom_level,1,32000);
camera_set_view_mat(camera, viewmat);
camera_set_proj_mat(camera, projmat);
view_camera[0] = camera;
In the Step Event of the Camera place the following;
- Code:
/// @description Update Position
var viewmat = matrix_build_lookat(x, y,-100,x, y,0,0,1,0);
camera_set_view_mat(camera, viewmat);
How does it work?
Essentially, the resolution manager figures out the ideal aspect ratio and resolution based on the device's display. Once that's complete, it adjusts the size of the view port within GMS 2 to the ideals chosen. In this instance, I have chosen the ideal height to be 1080 which will make the game port fit that specification. You can likely add a way for the ideal height to adjust based on the display as well - however, this gives you a bit of control over what the resolution will be to let you plan ahead as you work.
Once the resolution manager is done then it moves over to the next room, where the Camera object will set up a GMS 2 camera to use. This GMS 2 camera will be centered on the Camera object, allowing you to move the Camera object as required. So if you want the camera to follow a player, simply set up the Camera object to follow the player's Avatar.
There are probably a few issues that might come up and you will most likely need to make a few modifications to make the system work with your project - however hopefully this will help get you started!
Permissions in this forum:
You cannot reply to topics in this forum
|
|