background_create_from_surface

Creates a background by copying an area from a surface.

Syntax:

background_create_from_surface(index, x, y, w, h, removeback, smooth);


Argument Description
index The index of the surface to create from.
x The x position to copy from.
y The y position to copy from.
w The width of the area to be copied (from the x position).
h The height of the area to be copied (from the y position).
removeback Indicates whether to make all pixels with the same colour (left-bottom pixel) transparent.
smooth Indicates whether to smooth the edges.


Returns: Real


Description

With this function you can create a background from a previously initialised surface. The x and y coordinates that you input are relative to the (0,0) position of the surface (the top left corner) and not the game window, nor the view if you have one active. The width and height arguments are in pixels and define the width and height of the part of the surface to use.

Setting the "removeback" argument to true will remove a colour from the background, by checking the bottom left pixel of the defined area for the colour there and then using that as the colour to be removed.

If you choose the "removeback" option, you may also want GameMaker: Studio to smooth the edges of the background by setting the "smooth" argument to true. All this does is create a semi-transparent border around the edges of the background after it has had its background removed.

NOTE: When you create a background in GameMaker: Studio with this method you must remember to remove it again (with background_delete) when no longer needed, otherwise there is risk of a memory leak which will slow down and eventually crash your game.


Example:

var surf;
surf = surface_create(32, 32);
surface_set_target(surf);
draw_clear_alpha(c_black, 0);
draw_background(bck_Sky, 0, 0, 0);
draw_background(bck_Mountains, 0, 0);
bck_custom = background_create_from_surface(surf, 0, 0, 32, 32, true, true);
surface_reset_target();
surface_free(surf);

The above code creates a surface and stores its index in the local variable "surf". It then targets that surface, clears it and draws two background on top of each other. Finally it creates a new background from the composite image drawn on the surface and assigns its index to "bck_Custom" before freeing up the memory used by the surface.


Back: Backgrounds
Next: background_set_alpha_from_background
© Copyright YoYo Games Ltd. 2018 All Rights Reserved