zntg/allg5/primitives.go

177 lines
5.0 KiB
Go
Raw Permalink Normal View History

2018-11-21 13:35:14 +00:00
package allg5
// #include <allegro5/allegro.h>
// #include <allegro5/allegro_primitives.h>
/*
void SetTransform(ALLEGRO_TRANSFORM* t, float ix, float iy, float iz, float jx, float jy, float jz, float kx, float ky, float kz)
{
t->m[0][0] = ix;
t->m[0][1] = iy;
t->m[0][2] = iz;
t->m[1][0] = jx;
t->m[1][1] = jy;
t->m[1][2] = jz;
t->m[2][0] = kx;
t->m[2][1] = ky;
t->m[2][2] = kz;
}
bool SetTextureMatrix(ALLEGRO_TRANSFORM* t)
{
bool result = true;
result &= al_set_shader_bool(ALLEGRO_SHADER_VAR_USE_TEX_MATRIX, 1);
result &= al_set_shader_matrix(ALLEGRO_SHADER_VAR_TEX_MATRIX, t);
return result;
}
*/
import "C"
import (
"unsafe"
"opslag.de/schobers/geom"
"opslag.de/schobers/geom/lin"
)
2018-06-04 19:16:17 +00:00
func DrawFilledRectangle(x1, y1, x2, y2 float32, c Color) {
C.al_draw_filled_rectangle(C.float(x1), C.float(y1), C.float(x2), C.float(y2), c.color)
}
func DrawFilledTriangle(x1, y1, x2, y2, x3, y3 float32, c Color) {
C.al_draw_filled_triangle(C.float(x1), C.float(y1), C.float(x2), C.float(y2), C.float(x3), C.float(y3), c.color)
}
func DrawLine(x1, y1, x2, y2 float32, c Color, thickness float32) {
C.al_draw_line(C.float(x1), C.float(y1), C.float(x2), C.float(y2), c.color, C.float(thickness))
}
2017-10-23 09:18:24 +00:00
func DrawPolyline(vertices []float32, c Color, thickness float32) {
C.al_draw_polyline((*C.float)(unsafe.Pointer(&vertices[0])), 8, C.int(len(vertices)>>1), C.ALLEGRO_LINE_JOIN_ROUND, C.ALLEGRO_LINE_CAP_ROUND, c.color, C.float(thickness), 1)
}
2017-10-23 09:18:24 +00:00
func DrawRectangle(x1, y1, x2, y2 float32, c Color, thickness float32) {
C.al_draw_rectangle(C.float(x1), C.float(y1), C.float(x2), C.float(y2), c.color, C.float(thickness))
}
func DrawTexture(x1, y1, x2, y2, x3, y3, x4, y4 float32, b *Bitmap) {
var vtxs [4]C.ALLEGRO_VERTEX
var white = NewColor(255, 255, 255)
vtxs[0].x = C.float(x1)
vtxs[0].y = C.float(y1)
vtxs[0].u = 0
vtxs[0].v = 0
vtxs[0].color = white.color
vtxs[1].x = C.float(x2)
vtxs[1].y = C.float(y2)
vtxs[1].u = C.float(b.width)
vtxs[1].v = 0
vtxs[1].color = white.color
vtxs[2].x = C.float(x4)
vtxs[2].y = C.float(y4)
vtxs[2].u = 0
vtxs[2].v = C.float(b.height)
vtxs[2].color = white.color
vtxs[3].x = C.float(x3)
vtxs[3].y = C.float(y3)
vtxs[3].u = C.float(b.width)
vtxs[3].v = C.float(b.height)
vtxs[3].color = white.color
C.al_draw_prim(unsafe.Pointer(&vtxs), nil, b.bitmap, 0, 4, C.ALLEGRO_PRIM_TRIANGLE_STRIP)
}
func DrawTextureProj(x1, y1, x2, y2, x3, y3, x4, y4 float32, b *Bitmap) {
var w, h = float32(b.width), float32(b.height)
var proj = lin.Projection32(
geom.PtF32(0, 0), geom.PtF32(x1, y1),
geom.PtF32(w, 0), geom.PtF32(x2, y2),
geom.PtF32(w, h), geom.PtF32(x3, y3),
geom.PtF32(0, h), geom.PtF32(x4, y4))
var w05, h05 = .5 * w, .5 * h
var pt0 = lin.Project32(proj, geom.PtF32(w05, h05))
var pt2 = lin.Project32(proj, geom.PtF32(w05, 0))
var pt4 = lin.Project32(proj, geom.PtF32(w, h05))
var pt6 = lin.Project32(proj, geom.PtF32(w05, h))
var pt8 = lin.Project32(proj, geom.PtF32(0, h05))
var cw, ch, cw05, ch05 = C.float(w), C.float(h), C.float(w05), C.float(h05)
var vtxs [10]C.ALLEGRO_VERTEX
var white = NewColor(255, 255, 255)
vtxs[0].x = C.float(pt0.X)
vtxs[0].y = C.float(pt0.Y)
vtxs[0].u = cw05
vtxs[0].v = ch05
vtxs[0].color = white.color
vtxs[1].x = C.float(x1)
vtxs[1].y = C.float(y1)
vtxs[1].u = 0
vtxs[1].v = 0
vtxs[1].color = white.color
vtxs[2].x = C.float(pt2.X)
vtxs[2].y = C.float(pt2.Y)
vtxs[2].u = cw05
vtxs[2].v = 0
vtxs[2].color = white.color
vtxs[3].x = C.float(x2)
vtxs[3].y = C.float(y2)
vtxs[3].u = cw
vtxs[3].v = 0
vtxs[3].color = white.color
vtxs[4].x = C.float(pt4.X)
vtxs[4].y = C.float(pt4.Y)
vtxs[4].u = cw
vtxs[4].v = ch05
vtxs[4].color = white.color
vtxs[5].x = C.float(x3)
vtxs[5].y = C.float(y3)
vtxs[5].u = cw
vtxs[5].v = ch
vtxs[5].color = white.color
vtxs[6].x = C.float(pt6.X)
vtxs[6].y = C.float(pt6.Y)
vtxs[6].u = cw05
vtxs[6].v = ch
vtxs[6].color = white.color
vtxs[7].x = C.float(x4)
vtxs[7].y = C.float(y4)
vtxs[7].u = 0
vtxs[7].v = ch
vtxs[7].color = white.color
vtxs[8].x = C.float(pt8.X)
vtxs[8].y = C.float(pt8.Y)
vtxs[8].u = 0
vtxs[8].v = ch05
vtxs[8].color = white.color
vtxs[9].x = C.float(x1)
vtxs[9].y = C.float(y1)
vtxs[9].u = 0
vtxs[9].v = 0
vtxs[9].color = white.color
C.al_draw_prim(unsafe.Pointer(&vtxs), nil, b.bitmap, 0, 10, C.ALLEGRO_PRIM_TRIANGLE_FAN)
// var lineC = NewColor(0, 255, 0)
// drawLine := func(p1, p2 geom.PointF32) {
// DrawLine(p1.X, p1.Y, p2.X, p2.Y, lineC, 2)
// }
// bounds := []geom.PointF32{geom.PtF32(x1, y1), pt2, geom.PtF32(x2, y2), pt4, geom.PtF32(x3, y3), pt6, geom.PtF32(x4, y4), pt8}
// for i, pt := range bounds {
// drawLine(pt, bounds[(i+1)%8])
// drawLine(pt0, pt)
// }
// var pointC = NewColor(255, 0, 0)
// drawPt := func(p geom.PointF32) {
// DrawFilledRectangle(p.X-2, p.Y-2, p.X+2, p.Y+2, pointC)
// }
// drawPt(pt0)
// drawPt(pt2)
// drawPt(pt4)
// drawPt(pt6)
// drawPt(pt8)
}
2018-06-04 19:16:17 +00:00
func DrawTriangle(x1, y1, x2, y2, x3, y3 float32, c Color, thickness float32) {
C.al_draw_triangle(C.float(x1), C.float(y1), C.float(x2), C.float(y2), C.float(x3), C.float(y3), c.color, C.float(thickness))
2017-10-23 09:18:24 +00:00
}