Hi all,
Just started working on something to create triangular sprites using no media. I am thinking it could be useful for a simple graphic style game. You can give it 3 points (x,y), single colour or two for gradient sprites (gradient goes top to bottom) and it will create a sprite with a correct shape and image centred on the centre point of the triangle.
Here's an example of what can be produced:
type vec2Type
X as float
Y as float
endtype
#constant style_single 1
#constant style_gradient 2
type polyType
spr as integer
Style as integer
Point1 as vec2Type
Point2 as vec2Type
Point3 as vec2Type
Colour1 as integer
Colour2 as integer
endtype
global Poly as polyType[]
function PolyCreate(x1#, y1#, x2#, y2#, x3#, y3#, style, col1, col2)
p as polyType
p.Point1.X = x1#
p.Point1.Y = y1#
p.Point2.X = x2#
p.Point2.Y = y2#
p.Point3.X = x3#
p.Point3.Y = y3#
p.Style = style
p.Colour1 = col1
p.Colour2 = col2
PolyCreateSprite(p)
endfunction
function PolyCreateSprite(p ref as polyType)
`st# = timer()
// Reverse order check
ang1# = ATanFull(p.Point2.X-p.Point1.X, p.Point2.Y-p.Point1.Y)
ang2# = ATanFull(p.Point3.X-p.Point2.X, p.Point3.Y-p.Point2.Y)
da# = Wrap180(ang2#-ang1#)
if da#<0
t as polyType
t.Point2 = p.Point3
p.Point3 = p.Point2
p.Point2 = t.Point2
endif
// Get Centre point
cx# = (p.Point1.X+p.Point2.X+p.Point3.X)/3.0
cy# = (p.Point1.Y+p.Point2.Y+p.Point3.Y)/3.0
p.spr = CreateSprite(0)
// Create Physics Shape
SetSpritePhysicsOn(p.spr, 3)
SetSpriteShapePolygon(p.spr, 3, 0, p.Point1.X-cx#, p.Point1.Y-cy#)
SetSpriteShapePolygon(p.spr, 3, 1, p.Point2.X-cx#, p.Point2.Y-cy#)
SetSpriteShapePolygon(p.spr, 3, 2, p.Point3.X-cx#, p.Point3.Y-cy#)
SetSpritePositionByOffset(p.spr, cx#, cy#)
// Create image
minx = p.Point1.X
miny = p.Point1.Y
maxx = p.Point1.X
maxy = p.Point1.Y
if minx>p.Point2.X then minx = p.Point2.X
if miny>p.Point2.Y then miny = p.Point2.Y
if minx>p.Point3.X then minx = p.Point3.X
if miny>p.Point3.Y then miny = p.Point3.Y
if maxx<p.Point2.X then maxx = p.Point2.X
if maxy<p.Point2.Y then maxy = p.Point2.Y
if maxx<p.Point3.X then maxx = p.Point3.X
if maxy<p.Point3.Y then maxy = p.Point3.Y
ox# = cx#-minx
oy# = cy#-miny
w = maxx-minx+1
h = maxy-miny+1
size = 12 + w*h*4
mem = CreateMemblock(size)
SetMemblockInt(mem, 0, w)
SetMemblockInt(mem, 4, h)
SetMemblockInt(mem, 8, 32)
for x=minx to maxx
for y=miny to maxy
px = x-minx
py = y-miny
c = 12 + py*w*4 + px*4
SetMemblockByte(mem, c, 0)
SetMemblockByte(mem, c+1, 0)
SetMemblockByte(mem, c+2, 0)
SetMemblockByte(mem, c+3, 255)
next
next
for x=minx to maxx
for y=miny to maxy
px = x-minx
py = y-miny
c = 12 + py*w*4 + px*4
if GetSpriteHitTest(p.spr, x, y)>0
red = GetColorRed(p.Colour1)
green = GetColorGreen(p.Colour1)
blue = GetColorBlue(p.Colour1)
if p.Style = style_gradient
f# = (py*1.0)/(w*1.0)
dr# = red-GetColorRed(p.Colour2)
dg# = green-GetColorGreen(p.Colour2)
db# = blue-GetColorBlue(p.Colour2)
red = red + dr#*f#
green = green + dg#*f#
blue = blue + db#*f#
endif
SetMemblockByte(mem, c, red)
SetMemblockByte(mem, c+1, green)
SetMemblockByte(mem, c+2, blue)
SetMemblockByte(mem, c+3, 255)
else
SetMemblockByte(mem, c, 0)
SetMemblockByte(mem, c+1, 0)
SetMemblockByte(mem, c+2, 0)
SetMemblockByte(mem, c+3, 0)
endif
next
next
img = CreateImageFromMemblock(mem)
DeleteMemblock(mem)
SetSpriteImage(p.spr, img)
SetSpriteSize(p.spr, w, h)
SetSpritePositionByOffset(p.spr, cx#, cy#)
SetSpriteOffset(p.spr, ox#, oy#)
`ft# = timer() - st#
`message(str(ft#) + "ms")
endfunction
function Wrap180(v#)
while v#>180.0
v# = v#-360
endwhile
while v#<-180.0
v# = v#+360
endwhile
endfunction v#
Using AppGameKit V2 Tier 1