Blit.sel

#!./Selene
-- Blit image example

Selene.UseDirectFB("--dfb:quiet,no-vt")

DirectFB.init( DirectFB.CooperativeConst('FULLSCREEN') )	-- Set display cooperative mode

local img,err = SelImage.create("/usr/local/share/directfb-examples/gnu-keys.png")

if img then
	local srf = SelSurface.create {		-- create a surface of image size
		caps=SelSurface.CapabilityConst('PRIMARY')
	}

	local isrf = img:toSurface() -- convert the image to a surface
	img:destroy()	-- The image is not needed anymore

	print "Partial blit"
	srf:Blit(
		isrf,	-- source surface
		{ 10,20,30,40 }, -- source rectangle (restrict source to this rectangle)
		50,50	-- where to blit
	)

	print "Hit 'enter' key to exit"
	io.stdin:read'*l'	-- wait for enter

		-- Based on Gfx.sel
	print "Create a background"
	srf:Clear(0,0,0,0)
	w,h = srf:GetSize()

	math.randomseed( os.time() )
	for i = 1, 75 do
		local c = math.random(10)

		if c > 6 then
			srf:SetColor( math.random(0,255), math.random(0,255), math.random(0,255), math.random(0,255))
		elseif c == 3 then
			srf:DrawLine( math.random(0,w), math.random(0,h), math.random(0,w), math.random(0,h) )
		elseif c == 4 then
			srf:DrawRectangle( math.random(0,w), math.random(0,h), math.random(0,w), math.random(0,h) )
		elseif c == 5 then
			srf:FillRectangle( math.random(0,w), math.random(0,h), math.random(0,w), math.random(0,h) )
		elseif c == 6 then
			srf:FillTriangle( math.random(0,w), math.random(0,h), math.random(0,w), math.random(0,h), math.random(0,w), math.random(0,h) )
		end
	end
	local backup = srf:clone()

	print "Hit 'enter' key to exit"
	io.stdin:read'*l'	-- wait for enter

	print "Blit with transparency"
	_,err = srf:restore(backup)
	if err then
		print("*E* ".. err)
	end

	srf:SetBlittingFlags( SelSurface.BlittingFlagsConst('BLEND_ALPHACHANNEL') )
	srf:Blit(
		isrf, 	-- source surface
		nil,	-- source rectangle
		150,150	-- where to blit
	)

	print "Hit 'enter' key to exit"
	io.stdin:read'*l'	-- wait for enter

	print "TileBlit with transparency"
	srf:restore(backup)	-- Caution the Blitting flag and clipping are errased
	srf:SetBlittingFlags( SelSurface.BlittingFlagsConst('BLEND_ALPHACHANNEL') )
	srf:TileBlit(
		isrf, 	-- source surface
		nil, 	-- source rectangle
		150,150	-- offset (move the blitting by these offset but tiling is done on all the surface)
	)

	print "Hit 'enter' key to exit"
	io.stdin:read'*l'	-- wait for enter

	print "manual TileBlit with transparency and clip"
	srf:restore(backup)	-- Caution the Blitting flag is errased
	srf:SetBlittingFlags( SelSurface.BlittingFlagsConst('BLEND_ALPHACHANNEL') )
	srf:SetClipS( 0, h/2, w, isrf:GetHight() )
		-- x and y argument have to be used to 'move' the image to the origin of the blit clip
	srf:TileBlit(
		isrf,	-- source surface
		nil,	-- source rectangle
		0,h/2	-- offset
	)

	print "Hit 'enter' key to exit"
	io.stdin:read'*l'	-- wait for enter

	print "Builtin TileBlit with transparency and clip"
	srf:restore(backup)	-- Caution the Blitting flag is errased
	srf:SetBlittingFlags( SelSurface.BlittingFlagsConst('BLEND_ALPHACHANNEL') )
	_,err = srf:TileBlitClip(
		isrf,	-- source surface
		nil, 	-- source rectangle
		{ 20, 20, w-40, isrf:GetHight() }	-- Restrict blitting to this area
	)

	if err then
		print(err)
	end

	print "Hit 'enter' key to exit"
	io.stdin:read'*l'	-- wait for enter

	print "StretchBlit with transparency"
	srf:restore(backup)
	srf:SetBlittingFlags( SelSurface.BlittingFlagsConst('BLEND_ALPHACHANNEL') )
	srf:StretchBlit( isrf, nil, {50,50, h-100,w-100} )

	print "Hit 'enter' key to exit"
	io.stdin:read'*l'	-- wait for enter

	backup:destroy()
	srf:destroy()
else
	print(err)
end
generated by LDoc 1.4.6 Last updated 2022-09-05 22:24:51