Posted: 28th Oct 2023 7:42
HI

I have adapted a code to AppGameKit, to filled a polygon, it works fine (no antialiasing, but it's good)

+ Code Snippet
// Project: filledpolygon 
// Created: 2023-10-27
// code adapted from pb to agk by blendman

// show all errors
SetErrorMode(2)

// set window properties
SetWindowTitle( "filledpolygon" )

gw = 1024
gh = 768
SetWindowSize( 1024, 768, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window

// set display properties
SetVirtualResolution( 1024, 768 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 30, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts

SetClearColor(150,150,150)


type tpoint
	x#
	y#
endtype
//the points for the polygon
global dim p[] as tpoint
global pNm =-1

//create a rendertotimage and a sprite to draw the polygon
CreateRenderImage(1, gw,gh,0,0)
SetRenderToImage(1,0)
clearscreen()
SetRenderToScreen()
createsprite(1,1)
SetSpriteTransparency(1,1) // set the sprite with alpha chanel

// the constantcolors 
#constant c_white = MakeColor(255,255,255)
#constant C_black = MakeColor(0,0,0)
#constant C_Colorrandom = MakeColor(random(35,255),random(35,255), random(35, 255))

// to start to draw
start =-1

do
    If GetRawKeyPressed(27) // key escape
		end
	endif
	
    If GetRawKeyPressed(65) // key A
		SetRenderToImage(1,0)
		clearscreen()
		SetRenderToScreen()
	endif

	if GetPointerPressed()
		start =1
		//reset the point for the polygon
		p.length=-1
	endif
	if GetPointerReleased()
		start =0
		SetRenderToImage(1,0)
		FilledPolygon(gw, gh)
		SetRenderToScreen()
		
	endif
	
	if start = 1
		MouseX = getpointerX()
        MouseY = getpointerY()
		if p.length>1
			u=p.length 
			For i=1 to p.length 
              drawLine(p[i-1].x#, p[i-1].y#, p[i].x#, p[i].y#, c_white, c_white)
            next
        EndIf
		j=P.length+1
		p.length =j
		pNm= j
		P[j].x# = MouseX
		P[j].y# = Mousey
            
	endif

    Print("FPS :" +str(ScreenFPS(),2)+ " - Press A to erase the drawing" )
    Sync()
loop



Function FilledPolygon(width, height)
	
  // adapted from a pb code for AGK by blendman 2023
  
	//set the color for polygon
	color= C_Colorrandom // C_WHITE
	
	// create 
	dim nodes2[pNm] as integer
	
	// Find least and most y values to restrict area to transverse when filling..
	miny =height-1
	maxy =0
	
	for i=0 to p.length
		If p[i].y# <miny
			miny=p[i].y#
		EndIf
		If p[i].y#>maxy
			maxy=p[i].y#
		EndIf
	next
	
	// define variables used
	x as float
	y as float
	x1 as float
	y1 as float
	x2 as float
	y2 as float
	
	For cy=miny To maxy

		y = cy
		//reset Nodes2[]
		nodes2.length =-1
		
		// get the last points
		u = p.length
		x1 = p[u].x#
		y1 = p[u].y#
   
		// add a ne node if point is ok
		for i=0 to p.length
			x2 = p[i].x#
			y2 = p[i].y#
			If ((y2<Y) And y1>=Y) Or (y1<Y And y2>=Y)
				j = nodes2.length+1
				nodes2.length = j
				nodes2[j]=(x2+(Y-y2) / (y1-y2) * (x1-x2))
			EndIf
			x1=x2
			y1=y2
		Next
    
		// sort the node 
		nodes2.sort()
		
		// then draw the polygon
		for i=0 to nodes2.length-1 step 2
			x1=nodes2[i]
			x2=nodes2[i+1]
			drawLine(x1,Y,x2,y,color,color)
		next
		
	Next
  
	//drawn the border of the polygon : you can replace that by drawing line made with sprite, to get "smooth" border.
	oldx= p[0].x#
	oldy= p[0].y#
	For i=1 to p.length
		x = p[i].x#
		y = p[i].y#
		drawLine(oldx,oldy,x,y,color, color)
		oldx = p[i].x#
		oldy = p[i].y#
	next 
	
	undim nodes2[]
	
Endfunction





Cheers !
Posted: 1st Dec 2023 20:58
Very interesting.

I was working on a 2D top-down game that needed to create shadows, based on line of sight and needed the ability to draw triangles to create the shadow. Sadly I found the DrawLine() function too slow for complicated scenes. In the end I used the SetCameraFOV(1, 0) method and manipulated the vertices of a 3D triangle to do it. Still not fantastic performance, but it was good enough for what I needed.