Code Monkey home page Code Monkey logo

ebitengine-cheatsheet's Introduction

ebiten-cheatsheet Ebitengine Cheatsheet

Useful tips and snippets for Ebitengine.

Contents

Relative Crop

back If you try to make a sub image from another sub image, you may run into unusual behavior due to the fact that absolute coordinates are used for each crop, instead you can use this function so as not to constantly remember this nuance.

func RelativeCrop(source *ebiten.Image, r image.Rectangle) *ebiten.Image {
    rx, ry := source.Bounds().Min.X+r.Min.X, source.Bounds().Min.Y+r.Min.Y
    return source.SubImage(image.Rect(rx, ry, rx+r.Max.X, ry+r.Max.Y)).(*ebiten.Image)
}

Center text

back Text bounds behave differently than image bounds. To calculate center point you will need to subtract the minimum point and add half the size.

func DrawCenteredText(screen *ebiten.Image, font font.Face, s string, cx, cy int) {
    bounds := text.BoundString(font, s)
    x, y := cx-bounds.Min.X-bounds.Dx()/2, cy-bounds.Min.Y-bounds.Dy()/2
    text.Draw(screen, s, font, x, y, colornames.Red)
}

Flip Image

back Scale the image in the opposite direction by its size, and then move it into place.

func FlipHorizontal(source *ebiten.Image) *ebiten.Image {
    result := ebiten.NewImage(source.Size())
    op := &ebiten.DrawImageOptions{}
    op.GeoM.Scale(-1, 1)
    op.GeoM.Translate(float64(img.Bounds().Dx()), 0)
    return result.DrawImage(source, op)
}

func FlipVertical(source *ebiten.Image) *ebiten.Image {
    result := ebiten.NewImage(source.Size())
    op := &ebiten.DrawImageOptions{}
    op.GeoM.Scale(1, -1)
    op.GeoM.Translate(0, float64(img.Bounds().Dy()))
    return result.DrawImage(source, op)
}

Quick start

back Minimal game, vsync and resize are usually always helpful when debugging.

func main() {
	ebiten.SetVsyncEnabled(true)
	ebiten.SetWindowResizingMode(ebiten.WindowResizingModeEnabled)
	if err := ebiten.RunGame(NewGame()); err != nil {
		log.Fatal(err)
	}
}

type Game struct{}

func NewGame() *Game {
	return &Game{}
}

func (g *Game) Update() error {
	return nil
}

func (g *Game) Draw(screen *ebiten.Image) {
	screen.Fill(colornames.White)
}

func (g *Game) Layout(w, h int) (int, int) {
	return w, h
}

ebitengine-cheatsheet's People

Contributors

sedyh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.