Defold engine native extension for drawing pixels and simple geometry into texture buffer.
Feel free to contribute!
You can use the Draw Pixels extension in your own project by adding this project as a Defold library dependency. Open your game.project file and in the dependencies field under project add:
Main code example is here
First of all you need to create a table with a buffer information that contain next fields:
buffer
- buffer
width
- buffer width, same as your texture width
height
- buffer height, same as your texture height
channels
- 3 for rgb, 4 for rgba
For example:
local buffer_info = {
buffer = buffer.create(1024 * 2048, -- size of the buffer width*height
{{
name = hash("my_buffer"),
type = buffer.VALUE_TYPE_UINT8,
count = 4 -- same as channels
}}),
width = 1024,
height = 2048,
channels = 4
}
Then when you have a buffer info, you can use next methods:
Method for drawing circle:
buffer_info
- buffer information
pox_x
- x position center of the circle
pox_y
- y position center of the circle
diameter
- diameter of the circle
red
- red channel of the color 0..255
green
- green channel of the color 0..255
blue
- blue channel of the color 0..255
alpha
- alpha channel 0..255. Optional parameter for rgba textures
Method for drawing filled circle:
buffer_info
- buffer information
pox_x
- x position center of the circle
pox_y
- y position center of the circle
diameter
- diameter of the circle
red
- red channel of the color 0..255
green
- green channel of the color 0..255
blue
- blue channel of the color 0..255
alpha
- alpha channel 0..255. Optional parameter for rgba textures
Method for drawing rectangle:
buffer_info
- buffer information
pox_x
- x position center of the rectangle
pox_y
- y position center of the rectangle
rect_width
- rectangle width
rect_height
- rectangle height
red
- red channel of the color 0..255
green
- green channel of the color 0..255
blue
- blue channel of the color 0..255
alpha
- alpha channel 0..255. Optional parameter for rgba textures
drawpixels.filled_rect(buffer_info, pos_x, pos_y, rect_width, rect_height, red, green, blue, alpha, angle)
Method for drawing filled rectangle:
buffer_info
- buffer information
pox_x
- x position center of the rectangle
pox_y
- y position center of the rectangle
rect_width
- rectangle width
rect_height
- rectangle height
red
- red channel of the color 0..255
green
- green channel of the color 0..255
blue
- blue channel of the color 0..255
alpha
- alpha channel 0..255. Optional parameter for rgba textures
angle
- rotation angle in degrees. Optional.
Fill buffer with the color:
buffer_info
- buffer information
red
- red channel of the color 0..255
green
- green channel of the color 0..255
blue
- blue channel of the color 0..255
alpha
- alpha channel 0..255. Optional parameter for rgba textures
Draw a line between two points:
buffer_info
- buffer information
x0
- x position of one end of the line
y0
- y position of one end of the line
x1
- x position of the other end of the line
y1
- y position of the other end of the line
red
- red channel of the color 0..255
green
- green channel of the color 0..255
blue
- blue channel of the color 0..255
alpha
- alpha channel 0..255. Optional parameter for rgba textures
Draw a pixel:
buffer_info
- buffer information
x
- x position of pixel
y
- y position of pixel
red
- red channel of the color 0..255
green
- green channel of the color 0..255
blue
- blue channel of the color 0..255
alpha
- alpha channel 0..255. Optional parameter for rgba textures
Read color from a position in the buffer:
buffer_info
- buffer information
x
- x position to get color from
y
- y position to get color from
RETURNS:
red
- red channel of the color 0..255
green
- green channel of the color 0..255
blue
- blue channel of the color 0..255
alpha
- alpha channel 0..255. Optional parameter for rgba textures
If you have any questions or suggestions contact me: me@agulev.com