Hi,
This time I came up with something more practical - using sprites to draw stuff. I tried to replace some 2D drawing commands available in DBP (particularly line and box) with sprite-based functions that allow alpha blending and may be faster (I'm not sure about the speed of drawing commands with DBPs latest version, I haven't updated in quite a while, hence the line command is really slow for me).
It is really nothing spectacular (and there are most certainly plugins available doing the job more efficiently), but is indeed useful for myself, so I thought I'd just share it here.
The functions should be rather self-explanatory, but anyway, here's the command set:
General:
spr_init() - should be called once in the beginning
spr_prepareMedia() - usually does not need to be called, but may be useful when the media has to be reloaded (happens sometimes when leaving the window and opening it again, as far as I know)
Sprite Based:
SpriteID =
spr_make(img) - receives an image and builds a hidden sprite out of it that can be pasted later
SpriteID =
spr_load(file$) - loads an image and returns it as sprite
SpriteID =
spr_loadAni(file$,tilesX,tilesY) - loads an animated sprite
spr_Offset(ID, fx#, fy#) - offsets the sprite relative to its size
spr_Center(ID) - equal to spr_Offset(ID, 0.5, 0.5) to rotate a sprite around its center
Primitive Drawing:
spr_SetColor(r,g,b,a) - sets the color used for Box, Frame and Line
spr_Box(x1,y1,x2,y2) - draws a box using the color defined with spr_SetColor()
spr_Frame(x1,y1,x2,y2) - draws a frame
spr_ThickFrame(x1,y1,x2,y2,thickness) - draws a thick frame (thickness defined in pixels)[/b]
spr_Line(x1,y1,x2,y2) - draws a line
spr_ThickLine(x1,y1,x2,y2,thickness) - draws a thick line
spr_SetTriangleColor(r,g,b,a) - sets the color used for drawing triangles
spr_Triangle(x,y,angle,w,h) - draws a triangle at the given position with the given angle (note: kind of rotation can be changed inside this function) and size
Once again, thanks for your interest and have fun with it.
Code with example:
rem START OF EXAMPLE
rem Setup Screen
set display mode 800,600,32
set window on
sync on
sync rate 0
sync
rem Initialize Sprite Library
spr_init()
rem Main Loop
do
cls
t = timer()
rem Boxes
spr_SetColor( 255, 0, 0, 200+55*sin(0.5*t) )
spr_Box(300,100,500,300)
spr_SetColor(128,128,255,160)
spr_Box( 420+80*sin(t*0.3), 220+75*sin(t*0.35), 520, 320)
rem Frame
spr_SetColor(255,255,255, 150+100*sin(0.36*t))
spr_Frame(50,50,750,550)
rem Line
spr_SetColor( 0, 255, 0, 160+95*sin(0.25*t) )
spr_ThickLine(mousex(),mousey(),800-mousex(),600-mousey(),12)
rem Triangle
spr_SetTriangleColor(255,255,0,200+55*sin(0.32*t))
spr_Triangle(360,340, 0.14*t + 42*sin(0.08*t), 140, 70)
rem Info Output
ink 0xFFFFFFFF,0
print "FPS: ", screen fps()
sync
loop
rem END OF EXAMPLE
#constant spr_FIRST_SPRITE = 1
#constant spr_FIRST_IMAGE = 1
#constant spr_FIRST_MEMBLOCK = 1
#constant spr_TRIANGLE_RES = 256
function spr_init()
rem Sprites
global spr_dot as integer
global spr_triangle as integer
spr_prepareMedia()
endfunction
function spr_prepareMedia()
spr__MakeDotSprite()
spr__MakeTriangleSprite()
endfunction
function spr_make(img)
ID = spr__Free()
sprite ID, 0, 0xFFFFFF, img
hide sprite ID
set sprite ID, 0, 1
endfunction ID
function spr_load(f$)
img = spr__FreeImg()
load image f$, img, 1
ID = spr_make(img)
endfunction ID
function spr_loadAni(f$,tx,ty)
ID = spr__Free() : img = spr__FreeImg()
create animated sprite ID, f$, tx, ty, img
hide sprite ID
set sprite ID, 0, 1
endfunction ID
function spr_Center(ID)
offset sprite ID, sprite width(ID)*0.5, sprite height(ID)*0.5
endfunction
function spr_Offset(ID,fx#,fy#)
offset sprite ID, sprite width(ID)*fx#, sprite height(ID)*fy#
endfunction
function spr_SetColor(r,g,b,a)
set sprite alpha spr_dot, a
set sprite diffuse spr_dot, r,g,b
endfunction
function spr_Box(x1,y1,x2,y2)
w = x2-x1 : h = y2-y1
size sprite spr_dot, w, h
paste sprite spr_dot, x1, y1
endfunction
function spr_Frame(x1,y1,x2,y2)
spr_Box(x1,y1,x2,y1+1)
spr_Box(x1,y1+1,x1+1,y2)
spr_Box(x1+1,y2-1,x2,y2)
spr_Box(x2-1,y1+1,x2,y2-1)
endfunction
function spr_Line(x1,y1,x2,y2)
w = x2-x1 : h = y2-y1
a# = 90-atanfull(w,h)
L = sqrt(w*w + h*h)
size sprite spr_dot, L, 1
rotate sprite spr_dot, a#
paste sprite spr_dot, x1, y1
rotate sprite spr_dot, 0
endfunction
function spr_ThickLine(x1,y1,x2,y2,thickness)
w = x2-x1 : h = y2-y1
a# = 90-atanfull(w,h)
L = sqrt(w*w + h*h)
size sprite spr_dot, L, thickness
offset sprite spr_dot, 0, thickness*0.5
rotate sprite spr_dot, a#
paste sprite spr_dot, x1, y1
rotate sprite spr_dot, 0
offset sprite spr_dot, 0, 0
endfunction
function spr_SetTriangleColor(r,g,b,a)
set sprite alpha spr_triangle, a
set sprite diffuse spr_triangle, r,g,b
endfunction
function spr_Triangle(x,y,angle#,w,h)
size sprite spr_triangle, w, h
offset sprite spr_triangle, w*0.333, h*0.667 // ~rotation around center
`offset sprite spr_triangle, 0, h // rotation around rectangular point
rotate sprite spr_triangle, angle#
paste sprite spr_triangle, x, y
endfunction
function spr__Free()
ID = spr_FIRST_SPRITE : while sprite exist(ID) : inc ID : endwhile
endfunction ID
function spr__FreeImg()
ID = spr_FIRST_IMAGE : while image exist(ID) : inc ID : endwhile
endfunction ID
function spr__FreeMem()
ID = spr_FIRST_MEMBLOCK : while memblock exist(ID) : inc ID : endwhile
endfunction ID
function spr__MakeDotSprite()
mem = spr__FreeMem()
make memblock mem, 16
write memblock dword mem, 0, 1
write memblock dword mem, 4, 1
write memblock dword mem, 8, 32
write memblock dword mem, 12, 0xFFFFFFFF
img = spr__FreeImg()
make image from memblock img, mem
delete memblock mem
spr_dot = spr_Make(img)
endfunction
function spr__MakeTriangleSprite()
mem = spr__FreeMem()
memsize = 12 + 4*spr_TRIANGLE_RES*spr_TRIANGLE_RES
make memblock mem, memsize
write memblock dword mem, 0, spr_TRIANGLE_RES
write memblock dword mem, 4, spr_TRIANGLE_RES
write memblock dword mem, 8, 32
for y = 1 to spr_TRIANGLE_RES
p = 12 + 4*(y-1)*spr_TRIANGLE_RES
for x = 1 to y
write memblock dword mem, p, 0xFFFFFFFF
inc p, 4
next x
for x = x to spr_TRIANGLE_RES
write memblock dword mem, p, 0x00000000
inc p, 4
next x
next y
img = spr__FreeImg()
make image from memblock img, mem
delete memblock mem
spr_triangle = spr_Make(img)
endfunction
