Skip to content

Commit

Permalink
add functional wrappers to bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
stevetoro committed Sep 20, 2024
1 parent d89e9cc commit 41fa6b1
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 57 deletions.
98 changes: 54 additions & 44 deletions src/glee_gd.gleam
Original file line number Diff line number Diff line change
@@ -1,58 +1,68 @@
pub type Pid
import glee_gd/egd.{type Color, type Image, type Point, type RGB}

pub type Image =
Pid
pub fn arc(image: Image, p1: Point, p2: Point, color: Color) {
egd.arc(image, p1, p2, color)
image
}

pub type Color =
#(Float, Float, Float, Float)
pub fn arcd(image: Image, p1: Point, p2: Point, d: Int, color: Color) {
egd.arcd(image, p1, p2, d, color)
image
}

pub type RGB =
#(Int, Int, Int)
pub fn color(color: RGB) {
egd.color(color)
}

pub type Point =
#(Int, Int)
pub fn create(width: Int, height: Int) {
egd.create(width, height)
}

@external(erlang, "egd", "arc")
pub fn arc(image: Image, p1: Point, p2: Point, color: Color) -> Nil
pub fn destroy(image: Image) {
egd.destroy(image)
}

@external(erlang, "egd", "arc")
pub fn arcd(image: Image, p1: Point, p2: Point, d: Int, color: Color) -> Nil
pub fn filled_ellipse(image: Image, p1: Point, p2: Point, color: Color) {
egd.filled_ellipse(image, p1, p2, color)
image
}

@external(erlang, "egd", "color")
pub fn color(color: RGB) -> Color
pub fn filled_rectangle(image: Image, p1: Point, p2: Point, color: Color) {
egd.filled_rectangle(image, p1, p2, color)
image
}

@external(erlang, "egd", "create")
pub fn create(width: Int, height: Int) -> Image

@external(erlang, "egd", "destroy")
pub fn destroy(image: Image) -> Nil

@external(erlang, "egd", "filledEllipse")
pub fn filled_ellipse(image: Image, p1: Point, p2: Point, color: Color) -> Nil

@external(erlang, "egd", "filledRectangle")
pub fn filled_rectangle(image: Image, p1: Point, p2: Point, color: Color) -> Nil

@external(erlang, "egd", "filledTriangle")
pub fn filled_triangle(
image: Image,
p1: Point,
p2: Point,
p3: Point,
color: Color,
) -> Nil

@external(erlang, "egd", "information")
pub fn information(image: Image) -> Nil

@external(erlang, "egd", "line")
pub fn line(image: Image, p1: Point, p2: Point, color: Color) -> Nil

@external(erlang, "egd", "polygon")
pub fn polygon(image: Image, points: List(Point), color: Color) -> Nil

@external(erlang, "egd", "rectangle")
pub fn rectangle(image: Image, p1: Point, p2: Point, color: Color) -> Nil

@external(erlang, "egd", "render")
pub fn render(image: Image) -> String
) {
egd.filled_triangle(image, p1, p2, p3, color)
image
}

pub fn information(image: Image) {
egd.information(image)
image
}

pub fn line(image: Image, p1: Point, p2: Point, color: Color) {
egd.line(image, p1, p2, color)
image
}

pub fn polygon(image: Image, points: List(Point), color: Color) {
egd.polygon(image, points, color)
image
}

pub fn rectangle(image: Image, p1: Point, p2: Point, color: Color) {
egd.rectangle(image, p1, p2, color)
image
}

pub fn render(image: Image) -> String {
egd.render(image)
}
58 changes: 58 additions & 0 deletions src/glee_gd/egd.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
pub type Pid

pub type Image =
Pid

pub type Color =
#(Float, Float, Float, Float)

pub type RGB =
#(Int, Int, Int)

pub type Point =
#(Int, Int)

@external(erlang, "egd", "arc")
pub fn arc(image: Image, p1: Point, p2: Point, color: Color) -> Nil

@external(erlang, "egd", "arc")
pub fn arcd(image: Image, p1: Point, p2: Point, d: Int, color: Color) -> Nil

@external(erlang, "egd", "color")
pub fn color(color: RGB) -> Color

@external(erlang, "egd", "create")
pub fn create(width: Int, height: Int) -> Image

@external(erlang, "egd", "destroy")
pub fn destroy(image: Image) -> Nil

@external(erlang, "egd", "filledEllipse")
pub fn filled_ellipse(image: Image, p1: Point, p2: Point, color: Color) -> Nil

@external(erlang, "egd", "filledRectangle")
pub fn filled_rectangle(image: Image, p1: Point, p2: Point, color: Color) -> Nil

@external(erlang, "egd", "filledTriangle")
pub fn filled_triangle(
image: Image,
p1: Point,
p2: Point,
p3: Point,
color: Color,
) -> Nil

@external(erlang, "egd", "information")
pub fn information(image: Image) -> Nil

@external(erlang, "egd", "line")
pub fn line(image: Image, p1: Point, p2: Point, color: Color) -> Nil

@external(erlang, "egd", "polygon")
pub fn polygon(image: Image, points: List(Point), color: Color) -> Nil

@external(erlang, "egd", "rectangle")
pub fn rectangle(image: Image, p1: Point, p2: Point, color: Color) -> Nil

@external(erlang, "egd", "render")
pub fn render(image: Image) -> String
26 changes: 13 additions & 13 deletions test/glee_gd_test.gleam
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import gleam/bit_array
import glee_gd
import glee_gd as egd
import gleeunit
import gleeunit/should
import simplifile
Expand All @@ -9,26 +9,26 @@ pub fn main() {
}

pub fn render_ellipse_test() {
let image = glee_gd.create(500, 500)
let color = glee_gd.color(#(109, 74, 126))
glee_gd.filled_ellipse(image, #(100, 400), #(400, 100), color)
glee_gd.render(image)
let color = egd.color(#(109, 74, 126))
egd.create(500, 500)
|> egd.filled_ellipse(#(100, 400), #(400, 100), color)
|> egd.render
|> should_be_fixture("elixir-ellipse")
}

pub fn render_rectangle_test() {
let image = glee_gd.create(500, 500)
let color = glee_gd.color(#(0, 172, 216))
glee_gd.filled_rectangle(image, #(100, 400), #(400, 100), color)
glee_gd.render(image)
let color = egd.color(#(0, 172, 216))
egd.create(500, 500)
|> egd.filled_rectangle(#(100, 400), #(400, 100), color)
|> egd.render
|> should_be_fixture("go-rectangle")
}

pub fn render_triangle_test() {
let image = glee_gd.create(500, 500)
let color = glee_gd.color(#(255, 175, 243))
glee_gd.filled_triangle(image, #(250, 125), #(100, 375), #(400, 375), color)
glee_gd.render(image)
let color = egd.color(#(255, 175, 243))
egd.create(500, 500)
|> egd.filled_triangle(#(250, 125), #(100, 375), #(400, 375), color)
|> egd.render
|> should_be_fixture("gleam-triangle")
}

Expand Down

0 comments on commit 41fa6b1

Please sign in to comment.