BASIC8
Not enough ratings
Understanding Main Loop And Driver
By Tony Wang
Difficulty: beginner
Category: programming
   
Award
Favorite
Favorited
Unfavorite
Basic paradigm
Event-driven and main-loop based are the two major paradigms for program structuring. They are all widely used in a large range of applications. The outer most structure of a BASIC8 program is main-loop based, in which a block of code is supposed to run multiple times per second; each cycle is called a frame. The code runs at 30 FPS, while the rendering is 60 FPS.

The minimal setup of a program could be shortened as:

def update(delta) enddef update_with(driver())

BASIC8 executes programs top-down. The DRIVER() function always returns the only active driver for current program; the UPDATE_WITH(drv [, r]) function tells the driver how to tick this program, it looks for an "UPDATE" routine in this case. Or use following code if you prefer a different routine name explicitly, for example "BLAH":

def blah(delta) enddef update_with(driver(), call(blah))

Or using a string specifier for the same purpose:

update_with(driver(), "blah")

The tick routine can be even anonymous using a LAMBDA:

update_with ( driver(), lambda (delta) ( ) )

The "delta" parameter denotes for how long (in second) has elapsed since last tick, this is useful to calculate time relevant changes. Eg. moving a circle by uniform linear motion:

x = 0 def update(delta) x = x + delta * 10 circ x, 63, 5, rgba(0, 0, 255) enddef update_with(driver(), call(update))

The reason of "delta"'s existence is, it may take a little bit shorter or longer than expected to tick a frame in practice, there need to be a way to tell the actual interval.

It's also possible to make an infinite loop manually, instead of using a routine as main-loop:

x = 0 while true delta = sync x = x + delta * 10 circ x, 63, 5, rgba(0, 0, 255) wend

You have to call the SYNC function per frame manually to commit a cycle to the driver, and it returns the "delta" time.

The main-loop paradigm is used in variant fields including games, this is the above all block of updating and rendering something. That's not to say it's not possible to organize other parts of your program with event-driven paradigm, but that's another topic.
Loading resource
The LOAD_RESOURCE(path) function is used to load an asset from the content of a disk, it can load sprite, map, or quantized image, read the manual for details.

Similar to the previous function, LOAD_BLANK(y, w, h, n = 1) returns a loaded resource as well, but it loads blank asset from nowhere rather than from disk.

There's no difference between resources loaded by either of above functions, you can access pixels with *GET, *SET functions, or render them all the same. This will be explained in other guides.

Sometimes we need to instantiate more than one instances from a resource template, eg. spawning a lot of enemies. The CLONE(g) is used to create a duplicate instance from an existing resource, can pass any of sprite, map (single or multiple layers), and quantized image to "g".

The RGBA(r, g, b, a = 255) function creates color from red, green, blue, alpha components, which is value typed other than referenced typed, usages will be explained in other guides.

The UNPACK(c, r, g, b [, a]) function is used to unpack separated components from color value.
3 Comments
Doom Destroyer 29 Nov, 2021 @ 4:42pm 
how can this be done to make a start up reather then a an update
DRUG BOSS WALTER BLANCO 12 May, 2018 @ 9:03am 
Thank you very much for this.
yutakanamura 13 Mar, 2018 @ 7:18am 
Thank you .Nice guide . I'm beginner. I need lot of simple guid like this :D