GameMaker Can I get some help with setting up a camera?

Evanski

Raccoon Lord
Forum Staff
Moderator
Hi, so i've tried tutorials on making cameras and everyone talks about them different, sets them up different.
Its just left me confused and disoriented.

I would love it if I could make a camera that has the viewport centered on a target (ex.the player) and that viewport be the size of the game window.
I would also love it if I could switch it to fullscreen keeping the resolution of the current resolution of the device being used.
and all of that work without needing the room size to be a certain aspect ratio.

I would love it if someone could explain to me how i could go about that.

Heres what I understand and have tried to use:

I have a persistent object named ctrl_camera.

in the create event Im defining variables to use in setting up the viewport that follows the camera
Code:
target = obj_player.

view_width = 640;
view_height = 360;
I know i have to use stuff like resizing the application surface and re-centering the window but it cant be in the same step for some reason. but this is where the confusion kicks in. ive been told not to do anything with the viewport variables, where others have thrown them into camera resize, and then others use camera matrixes.
 

samspade

Member
Hi, so i've tried tutorials on making cameras and everyone talks about them different, sets them up different.
Its just left me confused and disoriented.

I would love it if I could make a camera that has the viewport centered on a target (ex.the player) and that viewport be the size of the game window.
I would also love it if I could switch it to fullscreen keeping the resolution of the current resolution of the device being used.
and all of that work without needing the room size to be a certain aspect ratio.

I would love it if someone could explain to me how i could go about that.

Heres what I understand and have tried to use:

I have a persistent object named ctrl_camera.

in the create event Im defining variables to use in setting up the viewport that follows the camera
Code:
target = obj_player.

view_width = 640;
view_height = 360;
I know i have to use stuff like resizing the application surface and re-centering the window but it cant be in the same step for some reason. but this is where the confusion kicks in. ive been told not to do anything with the viewport variables, where others have thrown them into camera resize, and then others use camera matrixes.
You're really talking about several different things. Camera set up and resolution at a minimum. There is a short answer for camera set up, just watch the first video. But everything else becomes more complicated and there isn't a short answer and it depends on things like what you want your game to look like, what art style you are using, how you want resolution and resolution changing to work, do you want subpixel movement, and so on. You'll probably need to internalize some basics of how things work in GM. Personally, I think the best way to learn this is create a new project where you just mess around with the following functions:

For basic camera set up, I think this is the best video:


For resolution I would look at these:

Tech Blog: The Basics Of Scaling

Or this one (also from Pixelated Pope):


Here's a simple script for resizing everything:

Code:
/// @description Set the Resolution of the window, camera, port, and application surfaces
/// @param {int} width
/// @param {int} height

var _new_width    = floor(argument0);
var _new_height = floor(argument1);

camera_set_view_size(VIEW, _new_width, _new_height);
view_set_wport(0, _new_width);
view_set_hport(0, _new_height);
window_set_size(_new_width, _new_height);

view_wport[0] = _new_width;
view_hport[0] = _new_height;
surface_resize(application_surface, _new_width, _new_height);
display_set_gui_size(_new_width, _new_height);
Also, for all questions, and especially questions where there were major differences between 1.4 and 2, you should really say what version you're using as it makes a huge difference.
 

Evanski

Raccoon Lord
Forum Staff
Moderator
You're really talking about several different things. Camera set up and resolution at a minimum. There is a short answer for camera set up, just watch the first video. But everything else becomes more complicated and there isn't a short answer and it depends on things like what you want your game to look like, what art style you are using, how you want resolution and resolution changing to work, do you want subpixel movement, and so on. You'll probably need to internalize some basics of how things work in GM. Personally, I think the best way to learn this is create a new project where you just mess around with the following functions:

For basic camera set up, I think this is the best video:


For resolution I would look at these:

Tech Blog: The Basics Of Scaling

Or this one (also from Pixelated Pope):


Here's a simple script for resizing everything:

Code:
/// @description Set the Resolution of the window, camera, port, and application surfaces
/// @param {int} width
/// @param {int} height

var _new_width    = floor(argument0);
var _new_height = floor(argument1);

camera_set_view_size(VIEW, _new_width, _new_height);
view_set_wport(0, _new_width);
view_set_hport(0, _new_height);
window_set_size(_new_width, _new_height);

view_wport[0] = _new_width;
view_hport[0] = _new_height;
surface_resize(application_surface, _new_width, _new_height);
display_set_gui_size(_new_width, _new_height);
Also, for all questions, and especially questions where there were major differences between 1.4 and 2, you should really say what version you're using as it makes a huge difference.
Thank you I'll take a look at those
 

Evanski

Raccoon Lord
Forum Staff
Moderator
okay so I set up this object to switch to fullscreen and back
Code:
if keyboard_check_pressed(vk_f11)
{
    if window_get_fullscreen()
    {
        #region //False
        window_set_fullscreen(false);
       
        ideal_width = 0;
        ideal_height = 720;
       
        aspect_ratio = display_get_width()/display_get_height();
       
        ideal_width = round(ideal_height*aspect_ratio);
       
        //check for odd number
        if (ideal_width & 1)
        {
            ideal_width++;  
        }
       
        room_set_viewport(room,view_current,true,0,0,ideal_width,ideal_height);
        room_set_view_enabled(room,true);
        surface_resize(application_surface,ideal_width,ideal_height);
        window_set_size(ideal_width,ideal_height);
        #endregion
    }
    else
    {
        #region //true
        window_set_fullscreen(true);
   
        ideal_width = display_get_width();
        ideal_height = display_get_height();

        //check for odd number
        if (ideal_width & 1)
        {
            ideal_width++;  
        }
   
        room_set_viewport(room,view_current,true,0,0,ideal_width,ideal_height);
        room_set_view_enabled(room,true);
        surface_resize(application_surface,ideal_width,ideal_height);
        window_set_size(ideal_width,ideal_height);
        #endregion
    }
}
Is this correct or will this give me future problems?
 

Evanski

Raccoon Lord
Forum Staff
Moderator
So it turns out that thats actually stretching pixels width wise, i dont know why its the same aspect ratio it should just scale right?
 

samspade

Member
The code seems pretty good and I've used similar without issues. But here are some additional questions:
  • Have you stepped through it in the debugger to determine what the values are?
  • What is your initial resolution?
  • What is full screen resolution?
  • Is pixel interpolation off or on?
 

Evanski

Raccoon Lord
Forum Staff
Moderator
The code seems pretty good and I've used similar without issues. But here are some additional questions:
  • Have you stepped through it in the debugger to determine what the values are?
  • What is your initial resolution?
  • What is full screen resolution?
  • Is pixel interpolation off or on?
  • no, but the game gives it them in real time as im drawing the values as text
  • 1280x720
  • 1980x1080
  • off
 

samspade

Member
I'm not an expert in this, so I'm probably going to get it partially wrong, but there are a few things I notice.

First, those are different aspect ratios one is 4:3 and one is 16:9. So if you just switch between them, you'll stretch. The way to handle this is resize the camera which it doesn't seem like you're doing. In other words, your camera views, not just ports, need to change - they should actually show more or less of the room (depending upon which way you're going).

Second, when you're switching out of full screen, you don't really need to use the display resolution. You can use whatever resolution you want, so long as it fits within the display limits.

Hopefully, someone with more knowledge can answer.
 
Top