Skip to content

Commit

Permalink
Merge pull request #10 from sergeysinyavsky/master
Browse files Browse the repository at this point in the history
Added new elements, blending and anti-aliasing
  • Loading branch information
AGulev authored May 6, 2020
2 parents 2485c9a + c991a44 commit 9d6188e
Show file tree
Hide file tree
Showing 18 changed files with 2,241 additions and 231 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
build
*.pyc
.project
.vscode
.cproject
builtins
.internal
113 changes: 109 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ First of all you need to create a table with a buffer information that contain n
`width` - buffer width, same as your texture width<br />
`height` - buffer height, same as your texture height<br />
`channels` - 3 for rgb, 4 for rgba<br />
`premultiply_alpha` - alpha value will be premultiplied into the RGB color values. Optional parameter. Default is false.<br />

For example:
```lua
Expand All @@ -35,12 +36,13 @@ local buffer_info = {
}}),
width = 1024,
height = 2048,
channels = 4
channels = 4,
premultiply_alpha = true
}
```
Then when you have a buffer info, you can use next methods:

#### drawpixels.circle(buffer_info, pox_x, pox_y, diameter, red, green, blue, alpha)
#### drawpixels.circle(buffer_info, pox_x, pox_y, diameter, red, green, blue, alpha, antialiasing, width)
Method for drawing circle:

`buffer_info` - buffer information<br />
Expand All @@ -51,8 +53,10 @@ Method for drawing circle:
`green` - green channel of the color 0..255<br />
`blue` - blue channel of the color 0..255<br />
`alpha` - alpha channel 0..255. Optional parameter for rgba textures<br />
`antialiasing` - adds anti-aliasing. Only for 4 channels. Optional parameter.<br />
`width` - indicates the circle width. Only for circle with anti-aliasing. Optional parameter.<br />

#### drawpixels.filled_circle(buffer_info, pos_x, pos_y, diameter, red, green, blue, alpha)
#### drawpixels.filled_circle(buffer_info, pos_x, pos_y, diameter, red, green, blue, alpha, antialiasing)
Method for drawing filled circle:

`buffer_info` - buffer information<br />
Expand All @@ -63,6 +67,7 @@ Method for drawing filled circle:
`green` - green channel of the color 0..255<br />
`blue` - blue channel of the color 0..255<br />
`alpha` - alpha channel 0..255. Optional parameter for rgba textures<br />
`antialiasing` - adds anti-aliasing. Only for 4 channels. Optional parameter.<br />

#### drawpixels.rect(buffer_info, pos_x, pos_y, rect_width, rect_height, red, green, blue, alpha)
Method for drawing rectangle:
Expand Down Expand Up @@ -100,7 +105,7 @@ Fill buffer with the color:
`blue` - blue channel of the color 0..255<br />
`alpha` - alpha channel 0..255. Optional parameter for rgba textures<br />

#### drawpixels.line(buffer_info, x0, y0, x1, y1, red, green, blue, alpha)
#### drawpixels.line(buffer_info, x0, y0, x1, y1, red, green, blue, alpha, antialiasing, width)
Draw a line between two points:

`buffer_info` - buffer information<br />
Expand All @@ -112,6 +117,71 @@ Draw a line between two points:
`green` - green channel of the color 0..255<br />
`blue` - blue channel of the color 0..255<br />
`alpha` - alpha channel 0..255. Optional parameter for rgba textures<br />
`antialiasing` - adds anti-aliasing. Only for 4 channels. Optional parameter.<br />
`width` - indicates the line width. Only for line with anti-aliasing. Optional parameter.<br />

#### drawpixels.gradient_line(buffer_info, x0, y0, x1, y1, red1, green1, blue1, red2, green2, blue2, alpha, antialiasing, width)
Draw a gradient line between two points:

`buffer_info` - buffer information<br />
`x0` - x position of one end of the line<br />
`y0` - y position of one end of the line<br />
`x1` - x position of the other end of the line<br />
`y1` - y position of the other end of the line<br />
`red1` - first red channel of the color 0..255<br />
`green1` - first green channel of the color 0..255<br />
`blue1` - first blue channel of the color 0..255<br />
`red2` - second red channel of the color 0..255<br />
`green2` - second green channel of the color 0..255<br />
`blue2` - second blue channel of the color 0..255<br />
`alpha` - alpha channel 0..255<br />
`antialiasing` - adds anti-aliasing. Only for 4 channels. Optional parameter.<br />
`width` - indicates the line width. Only for line with anti-aliasing. Optional parameter.<br />

#### drawpixels.arc(buffer_info, x, y, radius, from, to, red, green, blue, alpha)
Draw a arc between two corners. If from < to the arc is drawn counterclockwise. If from > to the arc is drawn clockwise. Only for 4 channels:

`buffer_info` - buffer information<br />
`x` - x position center of the circle<br />
`y` - y position center of the circle<br />
`radius` - radius of the circle<br />
`from` - first arc angle in radians. May be negative<br />
`to` - second arc angle in radians. May be negative<br />
`red` - red channel of the color 0..255<br />
`green` - green channel of the color 0..255<br />
`blue` - blue channel of the color 0..255<br />
`alpha` - alpha channel 0..255<br />

#### drawpixels.filled_arc(buffer_info, x, y, radius, from, to, red, green, blue, alpha)
Draw a filled arc between two corners. If from < to the arc is drawn counterclockwise. If from > to the arc is drawn clockwise. Only for 4 channels:

`buffer_info` - buffer information<br />
`x` - x position center of the circle<br />
`y` - y position center of the circle<br />
`radius` - radius of the circle<br />
`from` - first arc angle in radians. May be negative<br />
`to` - second arc angle in radians. May be negative<br />
`red` - red channel of the color 0..255<br />
`green` - green channel of the color 0..255<br />
`blue` - blue channel of the color 0..255<br />
`alpha` - alpha channel 0..255<br />

#### drawpixels.gradient_arc(buffer_info, x, y, radius, from, to, red1, green1, blue1, red2, green2, blue2, alpha)
Draw a gradient arc between two corners. If from < to the arc is drawn counterclockwise. If from > to the arc is drawn clockwise. Only for 4 channels:

`buffer_info` - buffer information<br />
`x` - x position center of the circle<br />
`y` - y position center of the circle<br />
`radius` - radius of the circle<br />
`from` - first arc angle in radians. May be negative<br />
`to` - second arc angle in radians. May be negative<br />
`red1` - first red channel of the color 0..255<br />
`green1` - first green channel of the color 0..255<br />
`blue1` - first blue channel of the color 0..255<br />
`red2` - second red channel of the color 0..255<br />
`green2` - second green channel of the color 0..255<br />
`blue2` - second blue channel of the color 0..255<br />
`alpha` - alpha channel 0..255<br />

#### drawpixels.pixel(buffer_info, x, y, red, green, blue, alpha)
Draw a pixel:
Expand Down Expand Up @@ -153,6 +223,41 @@ Draw a bezier line between two points and one control point:
`blue` - blue channel of the color 0..255<br />
`alpha` - alpha channel 0..255. Optional parameter for rgba textures<br />

You can fill in a specific area. To do this, you need to identify the borders with `start_fill` method and call the `fill_area` method to fill the area. Borders work with: lines, gradient lines, circles, filled circles with anti-aliasing, pixels, arcs. <b>NOT WORK WITH FILLED ARCS AND GRADIENT ARCS.</b> The arcs themselves use this method, so the fill may not be predictable. Do not create arcs until you are done with the fill. Be sure to call the `end_fill` method when you stop working with the fill.

#### drawpixels.start_fill()
Indicates the start of border preservation.

#### drawpixels.end_fill()
Indicates the stop of border preservation.

#### drawpixels.fill_area(buffer_info, x, y, red, green, blue, alpha)
Fills an area at specified boundaries. Only for 4 channels:

`buffer_info` - buffer information<br />
`x` - x position of the start point<br />
`y` - y position of the start point<br />
`red` - red channel of the color 0..255<br />
`green` - green channel of the color 0..255<br />
`blue` - blue channel of the color 0..255<br />
`alpha` - alpha channel 0..255<br />

In order to draw this into a sprite, you need a separate atlas with power of two texture. Then we can use `resource.set_texture`:

```lua
resource.set_texture(go.get("#sprite", "texture0"), self.header, self.buffer_info.buffer)
```

In order to render this in gui, we just need to create a box and create a new texture.
We can use `gui.new_texture`. Then you need to set this texture to the box using `gui.set_texture`:

```lua
local data = buffer.get_bytes(self.buffer_info.buffer, hash("rgba"))
gui.new_texture("name", width, height, image.TYPE_RGBA, data)
gui.set_texture(gui.get_node("box"), "name")
gui.set_size(gui.get_node("box"), vmath.vector3(width, height, 0))
```

--------

If you have any questions or suggestions contact me: [email protected]
20 changes: 17 additions & 3 deletions drawpixels/idea_autocomplite.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,32 @@
---@class DRAW_PIXELS
drawpixels = {}

---Indicates border should be maintained:
function drawpixels.start_fill() end
---Stop border recording:
function drawpixels.stop_fill() end
---Fills the area considering the boundaries:
function drawpixels.fill_area(buffer_info, x,y, red, green, blue, alpha) end
---Method for drawing circle:
function drawpixels.circle(buffer_info, pox_x, pox_y, diameter, red, green, blue, alpha)end
function drawpixels.circle(buffer_info, pox_x, pox_y, diameter, red, green, blue, alpha, antialiasing, width) end
---Method for drawing filled circle:
function drawpixels.filled_circle(buffer_info, pos_x, pos_y, diameter, red, green, blue, alpha) end
function drawpixels.filled_circle(buffer_info, pos_x, pos_y, diameter, red, green, blue, alpha, antialiasing) end
---Method for drawing rectangle:
function drawpixels.rect(buffer_info, pos_x, pos_y, rect_width, rect_height, red, green, blue, alpha) end
---Method for drawing filled rectangle:
function drawpixels.filled_rect(buffer_info, pos_x, pos_y, rect_width, rect_height, red, green, blue, alpha, angle) end
---Fill buffer with the color:
function drawpixels.fill(buffer_info, red, green, blue, alpha) end
---Draw a line between two points:
function drawpixels.line(buffer_info, x0, y0, x1, y1, red, green, blue, alpha) end
function drawpixels.line(buffer_info, x0, y0, x1, y1, red, green, blue, alpha, antialiasing, width) end
---Draw a gradient line between two points:
function drawpixels.gradient_line(buffer_info, x0, y0, x1, y1, red1, green1, blue1, red2, green2, blue2, alpha, antialiasing, width) end
---Draw a arc between two corners:
function drawpixels.arc(buffer_info, x, y, radius, from, to, red, green, blue, alpha) end
---Draw a filled arc between two corners:
function drawpixels.filled_arc(buffer_info, x, y, radius, from, to, red, green, blue, alpha) end
---Draw a gradient arc between two corners:
function drawpixels.gradient_arc(buffer_info, x, y, radius, from, to, red1, green1, blue1, red2, green2, blue2, alpha) end
---Draw a pixel:
function drawpixels.pixel(buffer_info, x, y, red, green, blue, alpha) end
---Read color from a position in the buffer:
Expand Down
Loading

0 comments on commit 9d6188e

Please sign in to comment.