BASIC8
Not enough ratings
Understanding Map
By Tony Wang
Difficulty: beginner
Category: programming, graphics
   
Award
Favorite
Favorited
Unfavorite
Glossary and fundamental concepts
You can create, edit maps that contains one or more layers. All layers in a map asset can be rendered as a batch, or respectively rendered layer by layer.



The beginning index of map layer is 0, the number 0 layer of a map asset is for logic mark, it's up to you how to interpret the meaning of this layer; while all other number 1, 2, 3 layers are for rendering.



Map is loaded by LOAD_RESOURCE or LOAD_BLANK. The former function loads already edited map asset from disk, the latter loads blank for runtime accessing only, and all layers including number 0 loaded by LOAD_BLANK are for rendering.

A map is not filled by pixels directly, it's filled by tiles from the only "tiles asset" in each disk instead.


Operations
MAP m, x, y, r = 0: draws one or more map layers at a specific position.

The load functions return a LIST of layers. Pass either the whole list or a single layer to the m parameter of this MAP function for rendering, it doesn't renders logic marks.

Try the following code:

layers = load_resource("galaxy_scroll.map") def update(delta) map layers, 0, 0 enddef update_with(driver(), call(update))

It's equivalent to:

layers = load_resource("galaxy_scroll.map") layer1 = get(layers, 1) layer2 = get(layers, 2) layer3 = get(layers, 3) def update(delta) map layer1, 0, 0 map layer2, 0, 0 map layer3, 0, 0 enddef update_with(driver(), call(update))
Getting, setting tiles
These functions are used to get and set tiles of map. They apply to maps from both disk assets or blank. Read the manual for details:

  • MGET m, i, x, y
  • MSET m, i, x, y, v
Automatic sorting
SET_ORDERBY(drv, rule ...): sets ordering rules of graphics commands; defaults to "nil" without calling this function. rule ... can be one or more in "nil", "map", "spr", "all"; "all" is shortcut to "spr" and "map", I'll only explain "map" in this guide.

Sometimes we need to specify rendering orders of multiple objects, it is possible to simply make it without modifying few lines of code. After calling the above function with a "map" argument, BASIC8 will automatically sort all map layers, sprites and other primitives according to queue indices as follow top-down:

  • 0. Layer 1 of map
  • 1. Layer 2 of map
  • 2. Sprites
  • 3. Layer 3 of map
  • 4. Other shape primitives such as CIRC, RECT, etc.

You can specify the queue index of quantized images under the "map" ordered mode, read the manual for details.

Try the following code and move it around:

set_orderby(driver(), "map") ' Comment this line to see the difference. galaxy_scroll = load_resource("galaxy_scroll.map") spaceship = load_resource("spaceship.sprite") spaceship.play() x = 8 y = 56 v = 20 def update(delta) if btn(0) then x = x - delta * v elseif btn(1) then x = x + delta * v elseif btn(2) then y = y - delta * v elseif btn(3) then y = y + delta * v endif map galaxy_scroll, 0, 0 spr spaceship, x, y enddef update_with(driver(), call(update))