Page 1 of 1

FastPixel - a Pixel drawing Library

Posted: Thu Jul 25, 2024 7:32 pm
by Mr.L
With this small library (DLL), you can draw a PixelArray. Each value in the array represents an RGBA color. It is not compatible with PureBasic screens, as it creates its own OpenGL rendering context. On my laptop, FastPixel is up to three times faster than PureBasic's internal pixel drawing commands, even with direct pixel access (via DrawingBuffer). Additionally, you can use the Alpha channel.

Here are the commands:

FPInit(windowID, x.l, y.l, width.l, height.l, *pixelArray): Initializes the library and opens a screen in the window with the given dimensions.
FPDraw(): Draws the PixelArray to the screen.
FPClear(rgba.l): Clears the PixelArray with the specified RGBA color.
FPFree(): Frees all resources used by this library.

You can find the C source, the precompiled DLL (Windows, 64-bit), the PureBasic include file, and an example program here:
https://github.com/MR-L-PB/FastPixel

Save this as a FastPixel.pbi file. (The .pbi file can also be found inside the 'examples' folder.)

Code: Select all

Prototype FPInitProto(windowID, x.l, y.l, width.l, height.l, *pixelArray)
Prototype FPDrawProto()
Prototype FPClearProto(rgba.l)
Prototype FPFreeProto()

Global FPLib
Global FPInit.FPInitProto
Global FPDraw.FPDrawProto
Global FPClear.FPClearProto
Global FPFree.FPFreeProto

Procedure FPStart(windowID, x.l, y.l, width.l, height.l, *pixelArray)
	Global result
	Global FPLib = OpenLibrary(#PB_Any, "FastPixel.dll")
	If IsLibrary(FPLib)
		FPInit = GetFunction(FPLib, "FPInit")
		FPDraw = GetFunction(FPLib, "FPDraw")
		FPClear = GetFunction(FPLib, "FPClear")
		FPFree = GetFunction(FPLib, "FPFree")
		result = FPInit(windowID, x, y, width, height, *pixelArray)
	EndIf
	ProcedureReturn result
EndProcedure

Procedure FPEnd()
	If IsLibrary(FPLib)
		FPFree()
		CloseLibrary(FPLib)
	EndIf
	FPLib = 0
EndProcedure
this is how to use the library

Code: Select all

IncludeFile "FastPixel.pbi"

#WIDTH = 1024
#HEIGHT = 768

Global Dim pixelArray.l(#WIDTH * #HEIGHT)

If OpenWindow(0, 0, 0, #WIDTH, #HEIGHT, "")
	If FPStart(WindowID(0), 0, 0, #WIDTH, #HEIGHT, @pixelArray())
		Repeat
			t.f = ElapsedMilliseconds() 
			cx = Sin(t * 0.002) * 150 : cy = Cos(t * 0.002) * 150
			o = 0
			FPClear(RGBA(0,0,0,64))
			For y = 0 To #HEIGHT - 1
				For x = 0 To #WIDTH - 1
					px = (x + cx) * 0.01
					If (px + py) & 1
						py = (y + cy) * 0.01
						pixelArray(o) = RGBA(255, 255, 255, 255)
					EndIf
					o + 1
				Next
			Next
			FPDraw()
			While WindowEvent() : If Event() = #PB_Event_CloseWindow: Break 2 : EndIf : Wend
			
			fps + 1
			If t > fpt
				SetWindowTitle(0, "FPS: " + Str(fps))
				fps = 0 : fpt = t + 1000
			EndIf
		ForEver
		FPEnd()
	EndIf
EndIf

Re: FastPixel - a Pixel drawing Library

Posted: Fri Aug 09, 2024 7:26 pm
by Kwai chang caine
Very nice effect of moving wall numerous square :shock:
Thanks of sharing 8)

Re: FastPixel - a Pixel drawing Library

Posted: Tue Aug 13, 2024 10:46 am
by IceSoft
A very specially kind filling a defined "rectangle".