Skip to content

ebitencm is an implementation of the cm.IDrawer interface of setanarut/cm using Ebitengine.

License

Notifications You must be signed in to change notification settings

setanarut/ebitencm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ebitencm

ebitencm (Ebitengine Chipmunk drawer) is an implementation of the cm.IDrawer Interface from setanarut/cm. This implementation utilizes hajimehoshi/ebiten/v2, making it possible to run across multiple platforms. Coordinate system is top-left by default, same as Ebitengine

scr

Usage

Within your Draw() method, invoke the cm.DrawSpace() function, passing in both a *cm.Space and a *ebitencm.Drawer as parameters.

type Game struct {
	space  *cm.Space
	drawer *ebitencm.Drawer
}
func main() {
	game := &Game{}
	game.space = space
	game.drawer = ebitencm.NewDrawer()
}
func (g *Game) Draw(screen *ebiten.Image) {
	// Drawing with Ebitengine/v2
	cm.DrawSpace(g.space, g.drawer.WithScreen(screen))
}

Dragging

If you want to enable dragging, call the HandleMouseEvent() function within the Update method, passing the *cm.Space object. This will allow objects to be dragged using a mouse or touch device.

func (g *Game) Update() error {
	// Handling dragging
	g.drawer.HandleMouseEvent(g.space)
	g.space.Step(1 / 60.0)
	return nil
}

Camera transform

Use Drawer.GeoM{} for camera transform. The cursor position is calculated according to this matrix. Don't forget to reset GeoM with GeoM.Reset() in Update(). Here is an example with the setanarut/kamera package.

func (g *Game) Update() error {
	g.space.Step(1 / 60.0)

	// Apply camera transform to drawer
	g.drawer.GeoM.Reset()
	g.cam.ApplyCameraTransform(g.drawer.GeoM)

	// Enable cursor dragging
	g.drawer.HandleMouseEvent(g.space)

Camera demo

Run camera example on your local machine

go run github.com/setanarut/ebitencm/examples/camera@latest
  • Camera Position = WASD
  • Camera Rotation = Q / E
  • Camera Zoom = Z / X
  • Drag object = Cursor
  • Reset camera = Backspace

Examples

Browse to the examples folder for all examples.