Or v1 (less efficient, but easier to use) (see below for .gm6)
This is a raster-based terrain engine which supports both construction and destruction in real time. In the example, the left mouse button is used to destroy a region of terrain while the right mouse button is used to add to it.
Internally, the engine works as follows. The terrain object (obj_terrain) maintains a surface as well as a sprite to represent the terrain. To add to the terrain, an instance draws onto the surface and informs the terrain object that an update is required for the sprite. To subtract from the terrain, an instance draws onto the surface using bm_subtract and likewise informs the terrain object that an update is required. Any drawing functions can be used for these purposes, so arbitrary shapes and/or images can be used for construction and destruction.
When the terrain is being modified every step, the computational cost is substantial but very stable. No matter how extensive the modifications, the most costly operation, (sprite_create_from_surface) is called no more than once. The cost of this function depends on the size of the terrain, but it typically takes around 30 ms, giving an FPS of about 30. But remember, this is only when modifications are being made every step; if they are made periodically, the FPS will drop only for an instant and quickly climb back up.
UPDATE: The latest version splits up the terrain into small pieces, making it much more efficient. Unless you have construction/destruction going on in many different parts of the map at the same time, the computational cost is pretty negligible.
A nice feature of the engine is that it is inherently compatible with most platform engines (or other motion engines). From the perspective of other objects, obj_terrain is just a solid object with a sprite; you don't need to perform any special collision checking to interact with it.
Edited by xDanielx, 15 January 2009 - 12:03 AM.












