Skip to content

Instantly share code, notes, and snippets.

@davesade
Last active April 4, 2020 22:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davesade/4c6a73472a9e6ddd9e58ddaf451d25c7 to your computer and use it in GitHub Desktop.
Save davesade/4c6a73472a9e6ddd9e58ddaf451d25c7 to your computer and use it in GitHub Desktop.
There were too many recommendations for GM older versions, how to set a camera and proper scaling of pixelart, so I decided to translate it into GMS2
// CREATE EVENT
/// @description Set camera
global.debug = true
// Scaling based on: https://www.youtube.com/watch?v=wv3R2Q536ZU
ideal_width = 0; //Doesn't matter because we are going to calculate this.
ideal_height = 256;
zoom = 3;
max_zoom = 1;
view_zoom = 5
view_max_zoom = 10
//Aspect ratio
aspect_ratio = display_get_width() / display_get_height();
aspect_ratio = clamp(aspect_ratio, 16 / 9, 21 / 9)
//Calculate new ideal width and height.
ideal_width = round(ideal_height * aspect_ratio);
ideal_height =round(ideal_width/aspect_ratio);
// Enable perfect pixel scaling
if(display_get_width() mod ideal_width != 0){
var d = round(display_get_width() / ideal_width)
ideal_width = display_get_width() / d
}
if(display_get_height() mod ideal_height != 0){
var d = round(display_get_height() / ideal_height)
ideal_height = display_get_height() / d
}
ideal_width = round(ideal_width);
ideal_height = round(ideal_height);
//Check to make sure our ideal width and height isn't an odd number, as that's usually not good.
if (ideal_width & 1)
ideal_width++;
if (ideal_height & 1)
ideal_height++;
max_zoom = floor(display_get_width() / ideal_width)
// Resize surface, GUI and game window base on newly calculated values
surface_resize(application_surface,ideal_width,ideal_height);
display_set_gui_size(ideal_width,ideal_height);
window_set_size(ideal_width*zoom,ideal_height*zoom);
// Create camera - permanent object, accessible from global
globalvar CAMERA;
CAMERA = camera_create();
camera_set_view_size(CAMERA,ideal_width,ideal_height)
// Center window
alarm[0] = 1
// STEP EVENT
/// @description Follows player object, zooming
if (keyboard_check_pressed(ord("Z"))) {
zoom++
if (zoom > max_zoom) zoom = 1
window_set_size(ideal_width * zoom, ideal_height * zoom)
surface_resize(application_surface, ideal_width * zoom, ideal_height * zoom)
alarm[0]=1
}
var zoom_speed = 0.1
if (keyboard_check(vk_up)) view_zoom += zoom_speed
if (keyboard_check(vk_down)) view_zoom -= zoom_speed
view_zoom = clamp(view_zoom, 1, view_max_zoom)
// View zoom only resizes certain elements of the screen, not whole screen... weird
surface_resize(application_surface, ideal_width * view_zoom, ideal_height * view_zoom);
if instance_exists(obj_parent_player) {
var behindX = obj_parent_player.x - ideal_width / 2
var behindY = obj_parent_player.y - ideal_height / 2
camera_set_view_pos(CAMERA, behindX, behindY)
}
// Reset the game
var resetKey
resetKey = keyboard_check_pressed(ord("R"));
if (resetKey) {
game_restart();
}
// Room start - very important!
/// @description Enable viewport
view_set_camera(0, CAMERA)
view_enabled = true
view_visible[0] = true
// ALARM 0
window_center()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment