IMAGE BLITTING: BASIC MASKING

A classic use for blitting is to render transparent sprites onto a background. In this example a background image, a sprite, and a 1-bit mask are used.

source compile sandbox issues? back to examples

SOURCE CODE ()

REM Note that this example will be executed everywhere 
REM BUT Commodore VIC-20 and Philips VG5000.

PROCEDURE example ON ALL BUT VIC20, VG5000

	BITMAP ENABLE(16)
	
	DEFINE PALETTE NOT PRESERVE
	
	CLS BLACK
	
	mask := LOAD IMAGE("blit_mask.png") EXACT TRANSPARENCY BLACK OPACITY LIGHT WHITE
	background := LOAD IMAGE("blit_background.png")
	sprite := LOAD IMAGE("blit_sprite.png")
	
	CONST x = ( SCREEN WIDTH - IMAGE WIDTH(background) ) \ #2
	CONST y = ( SCREEN HEIGHT - IMAGE HEIGHT(background) ) \ #2
	
	PUT IMAGE background AT x,y
	
	REM In the first blit, the mask is blitted onto the background using 
	REM the raster operator AND. Because any value ANDed with 0 equals 0, 
	REM and any value ANDed with 1 is unchanged, black areas are created 
	REM where the actual sprites will appear, while leaving the rest of 
	REM the background alone.
	
	BLIT bop1 AS ( ( SOURCE ) AND ( DESTINATION ) )
	BLIT IMAGE mask AT x+10,y+10 WITH bop1
	
	REM In the second blit, the sprite is blitted onto the newly altered 
	REM background using the raster operator of OR. Because any value 
	REM ORed with 0 is unchanged, the background is unaffected and the 
	REM black areas are filled with the actual sprite image.
	
	BLIT bop2 AS ( ( ( INVERSE ( SOURCE 2 ) ) AND ( SOURCE 1 ) ) OR ( DESTINATION ) )
	BLIT IMAGES sprite, mask AT x+10,y+10 WITH bop2
	
END PROC

CALL example ON ALL BUT VIC20, VG5000
	
	
	

How to compile and run the example

The instructions here refer to compiling the example from the command line. For Microsoft Windows users we suggest using UGBASIC-IDE, which allows you to download and compile each single example with just one click.



Are instructions for your specific home computer / console missing? First of all, check if your computer is supported by clicking here. If so, since ugBASIC is a language which does not provide abstractions, it is possible that this example will not work on your target. If you think this is an issue, please click here.

Coleco Vision, Dina (Chuang Zao Zhe 50), SpectraVideo SV-603 VGA

In order to compile the example, type this command on the command line:

Linux

ugbc.coleco -O rom -o blit_basic_mask.rom blit_basic_mask.bas

Windows

ugbc.coleco.exe -O rom -o blit_basic_mask.rom blit_basic_mask.bas

For Microsoft Windows users we suggest using UGBASIC-IDE, which allows you to download and compile this example with just one click.

Dragon 32

In order to compile the example, type this command on the command line:

Linux

ugbc.d32 -O bin -o blit_basic_mask.bin blit_basic_mask.bas

Windows

ugbc.d32.exe -O bin -o blit_basic_mask.bin blit_basic_mask.bas

For Microsoft Windows users we suggest using UGBASIC-IDE, which allows you to download and compile this example with just one click.

Dragon 64

In order to compile the example, type this command on the command line:

Linux

ugbc.d64 -O bin -o blit_basic_mask.bin blit_basic_mask.bas

Windows

ugbc.d64.exe -O bin -o blit_basic_mask.bin blit_basic_mask.bas

For Microsoft Windows users we suggest using UGBASIC-IDE, which allows you to download and compile this example with just one click.

SEGA SC-3000

In order to compile the example, type this command on the command line:

Linux

ugbc.sc3000 -O rom -o blit_basic_mask.rom blit_basic_mask.bas

Windows

ugbc.sc3000.exe -O rom -o blit_basic_mask.rom blit_basic_mask.bas

For Microsoft Windows users we suggest using UGBASIC-IDE, which allows you to download and compile this example with just one click.

SEGA SG-1000

In order to compile the example, type this command on the command line:

Linux

ugbc.sg1000 -O rom -o blit_basic_mask.rom blit_basic_mask.bas

Windows

ugbc.sg1000.exe -O rom -o blit_basic_mask.rom blit_basic_mask.bas

For Microsoft Windows users we suggest using UGBASIC-IDE, which allows you to download and compile this example with just one click.

TRS-80 Color Computer, TRS-80 Color Computer 2

In order to compile the example, type this command on the command line:

Linux

ugbc.coco -O bin -o blit_basic_mask.bin blit_basic_mask.bas

Windows

ugbc.coco.exe -O bin -o blit_basic_mask.bin blit_basic_mask.bas

For Microsoft Windows users we suggest using UGBASIC-IDE, which allows you to download and compile this example with just one click.

Any problem?

If you have found a problem trying to run this example, if you think there is a bug or, more simply, you would like it to be improved, open an issue for this example on GitHub.
Thank you!

open an issue BACK TO EXAMPLES