How to fuckin draw a rectangle?

This commit is contained in:
Andrey Parhomenko 2023-05-22 23:39:01 +03:00
parent a89db46fe7
commit e5dfe002a0
13 changed files with 232 additions and 35 deletions

13
go.mod
View file

@ -3,15 +3,16 @@ module github.com/surdeus/gox
go 1.20
require (
github.com/ebitengine/purego v0.1.1 // indirect
github.com/ebitengine/purego v0.4.0-alpha.4 // indirect
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20221017161538-93cebf72946b // indirect
github.com/hajimehoshi/ebiten/v2 v2.4.16 // indirect
github.com/hajimehoshi/ebiten/v2 v2.6.0-alpha.3.0.20230521122940-90562ee84b9b // indirect
github.com/hajimehoshi/file2byteslice v1.0.0 // indirect
github.com/jezek/xgb v1.1.0 // indirect
github.com/surdeus/godat v0.0.0-20230428145139-f51a8ab74bc8 // indirect
golang.org/x/exp v0.0.0-20230213192124-5e25df0256eb // indirect
golang.org/x/exp/shiny v0.0.0-20230213192124-5e25df0256eb // indirect
golang.org/x/image v0.5.0 // indirect
golang.org/x/mobile v0.0.0-20221110043201-43a038452099 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/exp/shiny v0.0.0-20230522175609-2e198f4a06a1 // indirect
golang.org/x/image v0.7.0 // indirect
golang.org/x/mobile v0.0.0-20230427221453-e8d11dd0ba41 // indirect
golang.org/x/sync v0.2.0 // indirect
golang.org/x/sys v0.8.0 // indirect
)

20
go.sum
View file

@ -2,12 +2,16 @@ github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym
github.com/ebitengine/purego v0.0.0-20220905075623-aeed57cda744/go.mod h1:Eh8I3yvknDYZeCuXH9kRNaPuHEwvXDCk378o9xszmHg=
github.com/ebitengine/purego v0.1.1 h1:HI8nW+LniW9Yb34k34jBs8nz+PNzsw68o7JF8jWFHHE=
github.com/ebitengine/purego v0.1.1/go.mod h1:Eh8I3yvknDYZeCuXH9kRNaPuHEwvXDCk378o9xszmHg=
github.com/ebitengine/purego v0.4.0-alpha.4 h1:Y7yIV06Yo5M2BAdD7EVPhfp6LZ0tEcQo5770OhYUVes=
github.com/ebitengine/purego v0.4.0-alpha.4/go.mod h1:ah1In8AOtksoNK6yk5z1HTJeUkC1Ez4Wk2idgGslMwQ=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20220806181222-55e207c401ad/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20221017161538-93cebf72946b h1:GgabKamyOYguHqHjSkDACcgoPIz3w0Dis/zJ1wyHHHU=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20221017161538-93cebf72946b/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/hajimehoshi/bitmapfont/v2 v2.2.2/go.mod h1:Ua/x9Dkz7M9CU4zr1VHWOqGwjKdXbOTRsH7lWfb1Co0=
github.com/hajimehoshi/ebiten/v2 v2.4.16 h1:vhuMtaB78N2HlNMfImV/SZkDPNJhOxgFrEIm1uh838o=
github.com/hajimehoshi/ebiten/v2 v2.4.16/go.mod h1:BZcqCU4XHmScUi+lsKexocWcf4offMFwfp8dVGIB/G4=
github.com/hajimehoshi/ebiten/v2 v2.6.0-alpha.3.0.20230521122940-90562ee84b9b h1:QURPKDDBduvp9Zh5/4NeiPeUfpB4PokrfoH3tx4yNrg=
github.com/hajimehoshi/ebiten/v2 v2.6.0-alpha.3.0.20230521122940-90562ee84b9b/go.mod h1:+fFI6Ag5YvbX1ivNQD2TxNhpWFDPuxEoew421TTQAxI=
github.com/hajimehoshi/file2byteslice v0.0.0-20210813153925-5340248a8f41/go.mod h1:CqqAHp7Dk/AqQiwuhV1yT2334qbA/tFWQW0MD2dGqUE=
github.com/hajimehoshi/file2byteslice v1.0.0 h1:ljd5KTennqyJ4vG9i/5jS8MD1prof97vlH5JOdtw3WU=
github.com/hajimehoshi/file2byteslice v1.0.0/go.mod h1:CqqAHp7Dk/AqQiwuhV1yT2334qbA/tFWQW0MD2dGqUE=
@ -40,28 +44,39 @@ golang.org/x/exp v0.0.0-20230213192124-5e25df0256eb h1:PaBZQdo+iSDyHT053FjUCgZQ/
golang.org/x/exp v0.0.0-20230213192124-5e25df0256eb/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
golang.org/x/exp/shiny v0.0.0-20230213192124-5e25df0256eb h1:gdeQX7xJSkTNF+Sw7++XNIOo4pGL0CjQv3N2Vm1Erxk=
golang.org/x/exp/shiny v0.0.0-20230213192124-5e25df0256eb/go.mod h1:UH99kUObWAZkDnWqppdQe5ZhPYESUw8I0zVV1uWBR+0=
golang.org/x/exp/shiny v0.0.0-20230522175609-2e198f4a06a1 h1:NxHSRPlbeyFGDc6rU7YsvxV/4bXS9XhuvUt5pP63XUs=
golang.org/x/exp/shiny v0.0.0-20230522175609-2e198f4a06a1/go.mod h1:UH99kUObWAZkDnWqppdQe5ZhPYESUw8I0zVV1uWBR+0=
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/image v0.1.0/go.mod h1:iyPr49SD/G/TBxYVB/9RRtGUT5eNbo2u4NamWeQcD5c=
golang.org/x/image v0.5.0 h1:5JMiNunQeQw++mMOz48/ISeNu3Iweh/JaZU8ZLqHRrI=
golang.org/x/image v0.5.0/go.mod h1:FVC7BI/5Ym8R25iw5OLsgshdUBbT1h5jZTpA+mvAdZ4=
golang.org/x/image v0.7.0 h1:gzS29xtG1J5ybQlv0PuyfE3nmc6R4qB73m6LUUmvFuw=
golang.org/x/image v0.7.0/go.mod h1:nd/q4ef1AKKYl/4kft7g+6UyGbdiqWqTP1ZAbRoV7Rg=
golang.org/x/mobile v0.0.0-20190312151609-d3739f865fa6/go.mod h1:z+o9i4GpDbdi3rU15maQ/Ox0txvL9dWGYEHz965HBQE=
golang.org/x/mobile v0.0.0-20190415191353-3e0bab5405d6/go.mod h1:E/iHnbuqvinMTCcRqshq8CkpyQDoeVncDDYHnLhea+o=
golang.org/x/mobile v0.0.0-20220722155234-aaac322e2105/go.mod h1:pe2sM7Uk+2Su1y7u/6Z8KJ24D7lepUjFZbhFOrmDfuQ=
golang.org/x/mobile v0.0.0-20221110043201-43a038452099 h1:aIu0lKmfdgtn2uTj7JI2oN4TUrQvgB+wzTPO23bCKt8=
golang.org/x/mobile v0.0.0-20221110043201-43a038452099/go.mod h1:aAjjkJNdrh3PMckS4B10TGS2nag27cbKR1y2BpUxsiY=
golang.org/x/mobile v0.0.0-20230427221453-e8d11dd0ba41 h1:539vykMVJsmdiucRtMmdeLLZaTVhWhaAHFcPabj2lws=
golang.org/x/mobile v0.0.0-20230427221453-e8d11dd0ba41/go.mod h1:aAjjkJNdrh3PMckS4B10TGS2nag27cbKR1y2BpUxsiY=
golang.org/x/mod v0.1.0/go.mod h1:0QHyrYULN0/3qlju5TqG8bIK38QM8yzMo5ekMj3DlcY=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.2.0 h1:PUR+T4wwASmuSTYdKjYHI5TD22Wy5ogLU5qZCOLxBrI=
golang.org/x/sync v0.2.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
@ -77,19 +92,24 @@ golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220818161305-2296e01440c6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190312151545-0bb0c0a6e846/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.8-0.20211022200916-316ba0b74098/go.mod h1:LGqMHiF4EqQNHR1JncWGqT5BVaXmza+X+BDGol+dOxo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

View file

@ -127,5 +127,16 @@ func main() {
e.Add(0, NewPlayer())
e.Add(1, &Debug{})
e.Add(100, gx.Rectangle{
W: 10000000,
H: 10000000,
T: gx.T(),
C: gx.Color{
gx.MaxColorV,
gx.MaxColorV,
gx.MaxColorV,
gx.MaxColorV,
},
})
e.Run()
}

View file

@ -1,14 +1,15 @@
package gx
// The structure represents any circles.
type Circle struct {
// Position
// Position.
P Point
// Radius
// Radius.
R Float
}
func (c Circle) ColliderSimplify() Rect {
return Rect{
func (c Circle) ColliderSimplify() Rectangle {
return Rectangle{
W: c.R * 2,
H: c.R * 2,
}

View file

@ -7,19 +7,19 @@ package gx
// contain more complicated structure
// do collide.
type ColliderSimplifier interface {
ColliderSimplify() Rect
ColliderSimplify() Triangle
}
// The structure represents all
// information on collision.
// information on collisions.
type Collision struct {
Other Collider
Current, With any
}
// Every collider has to implement
// collision with every other type of collider
// for optimization. Not good for custom colliders
// but is fast.
// Implementing the interface lets the engine
// to determine if the object collides with anything.
// Mostly will use the Collide function with some
// inner structure field as first argument.
type Collider interface {
Collides(Collider) *Collision
}
@ -30,3 +30,16 @@ type CollideEventer interface {
Collide(*Collision)
}
// Single function for all collision to remove
// functionality duplicating from the archtecture.
// Returns the collision if there is and nil if there
// is no collision.
/*func Collide(c1, c2 any) bool {
}
func triangleCollidesPoint(t Triangle, p Point) *Collision {
}
func triangleCollidesTriangle(t1, t2 Triangle) *Collision
*/

View file

@ -5,9 +5,15 @@ import (
"image"
_ "image/png"
"io"
"math"
)
type ColorV uint32
type Image = ebiten.Image
type ColorM = ebiten.ColorM
type Color struct {
R, G, B, A ColorV
}
// The interface describes anything that can be
// drawn. It will be drew corresponding to
@ -16,6 +22,10 @@ type Drawer interface {
Draw(*Engine, *Image)
}
const (
MaxColorV = math.MaxUint32
)
func LoadImage(input io.Reader) (*Image, error) {
img, _, err := image.Decode(input)
if err != nil {
@ -26,3 +36,7 @@ func LoadImage(input io.Reader) (*Image, error) {
return ret, nil
}
func (c Color) RGBA() (r, g, b, a uint32) {
return uint32(c.R), uint32(c.G), uint32(c.B), uint32(c.A)
}

View file

@ -1,7 +1,4 @@
package gx
type LineSegment struct {
P Point
V Vector
}
type LineSegment [2]Point

View file

@ -7,6 +7,7 @@ import (
"github.com/surdeus/godat/src/poolx"
//"fmt"
"time"
"math"
)
// The type is used in all Engine interactions
@ -37,6 +38,10 @@ type Engine struct {
type engine Engine
var (
Infinity = math.MaxFloat64
)
// Return current camera.
func (e *Engine) Camera() *Camera {
return e.camera
@ -76,12 +81,12 @@ func New(
func (e *Engine) Add(l Layer, b any) {
beh, ok := b.(Behaver)
if ok {
e.AddBehaver(beh)
e.addBehaver(beh)
}
drw, ok := b.(Drawer)
if ok {
e.AddDrawer(l, drw)
e.addDrawer(l, drw)
}
}
@ -101,7 +106,7 @@ func (e *Engine) Del(b any) {
}
func (e *Engine) AddDrawer(l Layer, d Drawer) {
func (e *Engine) addDrawer(l Layer, d Drawer) {
g, ok := e.layers.Get(l)
if !ok {
layer := poolx.New[Drawer]()
@ -116,7 +121,7 @@ func (e *Engine) AddDrawer(l Layer, d Drawer) {
}
func (e *Engine) AddBehaver(b Behaver) {
func (e *Engine) addBehaver(b Behaver) {
e.behavers.Append(b)
b.Start(e)
}

View file

@ -1,16 +1,84 @@
package gx
import (
"github.com/hajimehoshi/ebiten/v2"
//"github.com/hajimehoshi/ebiten/v2/vector"
"fmt"
//"image"
)
// The type describes rectangle geometry.
type Rect struct {
// Position and rotate around.
P, RA Point
type Rectangle struct {
// Position of up left corner
// and the point to
// rotate around(relatively of position, not absolute).
T Transform
// Width and height.
// In fact are needed only to specify
// relation of width and height.
// Change transform to actually change things.
W, H Float
// Rotation in radians.
R Float
S *Shader
C Color
}
func (r Rect) ColliderSimplify() Rect {
return r
// Return points of corners of the rectangle.
func (r Rectangle) Corners() []Point {
return []Point{}
}
// Get 2 triangles that the rectangle consists of.
func (r Rectangle) Triangles() Triangles {
return Triangles{}
}
/*func MustNewImage(w, h int) (*Image) {
img, err := NewImage(w, h)
if err != nil {
panic(err)
}
return img
}*/
func NewImage(w, h int) (*Image) {
return ebiten.NewImage(w, h)
}
func (r Rectangle) Draw(
e *Engine,
i *Image,
) {
fmt.Println("drawing the rectangle")
if r.S == nil {
img := NewImage(1, 1)
img.Set(0, 0, r.C)
t := r.T
t.S.X *= r.W
t.S.Y *= r.H
m := t.Matrix(e)
opts := &ebiten.DrawImageOptions{
GeoM: m,
}
i.DrawImage(img, opts)
fmt.Println("done")
return
}
opts := &ebiten.DrawRectShaderOptions{
GeoM: r.T.Matrix(e),
Images: [4]*Image{
NewImage(1000, 1000),
nil,
nil,
nil,
},
}
//w := int(r.W * r.T.S.X)
//h := int(r.H * r.T.S.Y)
i.DrawRectShader(1000, 1000, r.S, opts)
}

33
src/gx/shader.go Normal file
View file

@ -0,0 +1,33 @@
package gx
import (
"github.com/hajimehoshi/ebiten/v2"
//"fmt"
)
type Shader = ebiten.Shader
var (
// The shader does not
SolidColorShader = MustNewShader([]byte(`
package main
func Fragment(p vec4, coord vec2, color vec4) vec4 {
return vec4(1, 0, 1, 1)
}
`))
)
func MustNewShader(src []byte) (*Shader) {
shader, err := NewShader(src)
if err != nil {
panic(err)
}
return shader
}
func NewShader(src []byte) (*Shader, error) {
return ebiten.NewShader(src)
}

View file

@ -7,6 +7,7 @@ import (
type Sprite struct {
I *Image
T Transform
S *Shader
Floating bool
}
@ -14,7 +15,9 @@ func (s *Sprite) Draw(
e *Engine,
i *Image,
) {
op := &ebiten.DrawImageOptions{}
op := &ebiten.DrawImageOptions{
}
m := &Matrix{}
m.Concat(s.T.Matrix(e))
@ -26,6 +29,10 @@ func (s *Sprite) Draw(
}
op.GeoM = *m
/*if s.S != nil {
bufImg := ebiten.NewImageFromImage(s.I)
} */
i.DrawImage(s.I, op)
}

9
src/gx/time.go Normal file
View file

@ -0,0 +1,9 @@
package gx
import (
"time"
)
type Time = time.Time
type Duration = time.Duration

View file

@ -1,6 +1,24 @@
package gx
type Triangle struct {
Points [3]Point
import (
"math"
)
// The structure of a triangle. What more you want to hear?
type Triangle [3]Point
type Triangles []Triangle
// Returns the area of the triangle.
func (t Triangle) Area() Float {
x1 := t[0].X
y1 := t[0].Y
x2 := t[1].X
y2 := t[1].Y
x3 := t[2].X
y3 := t[2].Y
return math.Abs( (x1*(y2-y3) + x2*(y3-y1) + x3*(y1-y2))/2)
}