CanvasDrawer - Grisgram/gml-raptor GitHub Wiki
The Canvas library is a pretty cool wrapper for the surface
system of GameMaker. It ensures, that the surface does not get lost, can save it as image, has a cache and lots of useful features.
The only thing, Canvas does not have, is a companion object to draw the canvas to the scene without the need of a draw
event in any of my objects, tracking the position, etc. Now, CanvasDrawer
is exactly this missing link.
You just drag it to the scene, set the variables and draw to it from anywhere.
It can be found in the _gml_raptor_/Objects
folder of the project template.
The variables are self-explanatory, maybe with the exception of the canvas_origin
setting.
To make it easy, I simply took the numeric keypad of your keyboard as a template. Just look at the keys and you know, which number means which origin:
7 | 8 | 9 |
4 | 5 | 6 |
1 | 2 | 3 |
0 |
The zero is the special case here, it simply means "custom origin". Set the canvas_origin
to 0
and the drawer will use the values of canvas_origin_custom_x/y
as the origin.
Aside of being a drawable object, the CanvasDrawer
offers some methods to work at runtime:
/// @function create_canvas(width, height, origin = 7, origin_custom_x = 0, origin_custom_y = 0)
/// @description Create a new Canvas instance. The method returns the created Canvas.
/// @function free_canvas()
/// @description Release the canvas
/// @function set_canvas(_canvas, origin = 7, origin_custom_x = 0, origin_custom_y = 0)
/// @description Assign an already existing canvas to this drawer.
/// NOTE: If the _canvas supplied is not a valid Canvas instance, the entire function is ignored.
/// @function set_animation(sub_image_count, frames_per_second)
/// @description Set animation frames and speed
/// @function clone_sprite(_sprite, _frame = -1)
/// @description Clones a sprite asset into a surface and sets the subimages and animation
/// based on the sprite's data. Let frame be -1 to clone all frames or set a desired frame to clone
/// NOTE: Any existing surface of this drawer will be destroyed!