FastPixel - a Pixel drawing Library

Windows specific forum
Mr.L
Enthusiast
Enthusiast
Posts: 146
Joined: Sun Oct 09, 2011 7:39 am

FastPixel - a Pixel drawing Library

Post 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
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: FastPixel - a Pixel drawing Library

Post by Kwai chang caine »

Very nice effect of moving wall numerous square :shock:
Thanks of sharing 8)
ImageThe happiness is a road...
Not a destination
User avatar
IceSoft
Addict
Addict
Posts: 1694
Joined: Thu Jun 24, 2004 8:51 am
Location: Germany

Re: FastPixel - a Pixel drawing Library

Post by IceSoft »

A very specially kind filling a defined "rectangle".
Belive! C++ version of Puzzle of Mystralia
Bug Planet
<Wrapper>4PB, PB<game>, =QONK=, PetriDish, Movie2Image, PictureManager,...
Post Reply