Zooming in or out

Most games have the view size and view port the same size, but by changing the view size without changing the port size you can easily create a zoom effect.

Setup
There are three variables that the objects needs. First is the default width and height of the view. These won't be changed so they could be constants instead.

The other variable is the current zoom. This will be multiplied with the base width and height to get the zoomed width and height. base_width = 640; base_height = 480; zoom = 100;

The zoom is set to 100 because of an issue with doubles, which is the data type Game Maker uses.

Zoom
The way that the player zooms in or out can vary. You can make two objects that act as buttons that the player clicks on, or you could map them to the "+" and "-" buttons. You could also have both. You can even have an automatic zooming system to give the player the best view.

Whatever you decide, the code for zooming in and out will be almost identical. There are four steps.

//Find the center of the view /*Change the zoom variable, this is the only difference between zooming in and zooming out*/ //Zoom the view //Reset the center of the view

Find the center of the view
Changing the view size will reduce or add to the right and bottom edge of the view, which will change the center point of the view. So the view position will need to be corrected after zooming.

The center of the view is the view position + half of it's width and height. vpx = view_xview[0] + view_wview[0] / 2; vpy = view_yview[0] + view_hview[0] / 2;

Change the zoom variable
This line is the only difference between zooming in and zooming out. You need to choose a good amount for the zoom to change by. If it's too little it will require many presses to zoom in. If it's too much the view size will change so drastically that the player will be confused.

10 is a good amount for this setup, it will require 5 zooms to zoom in fully. If you use the mouse wheel a smaller number is suitable.

When you change the zoom you need to check that it hasn't gone outside of the limits. While the following code works. zoom -= 10; if(zoom < max_zoom) zoom = max_zoom; These three lines can be reduced to one with the and  functions. zoom = max(zoom - 1, max_zoom);

Zoom the view
To zoom the view you need to multiply the base size with the zoom. Since the zoom variable is using multiples of 10 instead of 0.1 it needs to be divided by 100.

view_wview[0] = base_width * (zoom / 100); view_hview[0] = base_height * (zoom / 100);

Reset the center of the view
Now that the view has been zoomed the view position needs to be corrected so that the center is in the same place.

view_xview[0] = vpx - view_wview[0] / 2; view_yview[0] = vpy - view_hview[0] / 2;

Complete code
Create event: base_width = 640; base_height = 480; zoom = 100;

Zoom in: var vpx, vpy, max_zoom=50; //Find the center of the view vpx = view_xview[0] + view_wview[0] / 2; vpy = view_yview[0] + view_hview[0] / 2; zoom = max(zoom-10,max_zoom); //Zoom the view view_wview[0] = base_width * (zoom / 100); view_hview[0] = base_height * (zoom / 100); //Reset the center of the view view_xview[0] = vpx - view_wview[0] / 2; view_yview[0] = vpy - view_hview[0] / 2;

Zoom out: var vpx, vpy, min_zoom=150; //Find the center of the view vpx = view_xview[0] + view_wview[0] / 2; vpy = view_yview[0] + view_hview[0] / 2; zoom = min(zoom+10,min_zoom); //Zoom the view view_wview[0] = base_width * (zoom / 100); view_hview[0] = base_height * (zoom / 100); //Reset the center of the view view_xview[0] = vpx - view_wview[0] / 2; view_yview[0] = vpy - view_hview[0] / 2;