Giter VIP home page Giter VIP logo

ebiten-camera's Introduction

ebiten-camera

A simple camera implementation based on vrld's hump for Lร–VE

Usage

๐Ÿ“– Docs
Look at the examples to see how to use the library.

Here is a stripped-down version of the example code, highlighting the most important functions.

It won't run, so please don't bother trying it ๐Ÿ˜„

var (
    cam *ebitenCamera.Camera
    // other vars excluded, such as tiles, PlayerX etc
)


func main() {
    w,h := 640, 480
    // excluding normal ebiten setup
    cam = camera.NewCamera(w, h, 0, 0, 0, 1)
}

func (g *Game) Update() error {
    // Follows the player
    cam.SetPosition(PlayerX+float64(PlayerSize)/2, PlayerY+float64(PlayerSize)/2)
    return nil
}

func (g *Game) Draw(screen *ebiten.Image) {
    // Clear camera surface
    cam.Surface.Clear()
    cam.Surface.Fill(color.RGBA{255, 128, 128, 255})
    // Draw tiles
    tileOps := &ebiten.DrawImageOptions{}
    cam.Surface.DrawImage(tiles, cam.GetTranslation(tileOps, 0, 0))
    // Draw the player
    playerOps := &ebiten.DrawImageOptions{}
    playerOps = cam.GetRotation(playerOps, PlayerRot, -float64(PlayerSize)/2, -float64(PlayerSize)/2)
    playerOps = cam.GetScale(playerOps, 0.5, 0.5)
    playerOps = cam.GetSkew(playerOps, 0, -0.5)
    playerOps = cam.GetTranslation(playerOps, PlayerX, PlayerY)
    cam.Surface.DrawImage(player, playerOps)
    
    // Draw to screen and zoom
    cam.Blit(screen)
}

Considerations

  1. When setting the *ebiten.DrawImageOptions in the ebitenCamera.Surface.DrawImage function, the order of operation is important!
    Rotate, Scale, Skew and then Translate!

  2. My example doesn't include a range for the camera's zoom to highlight what happens if you zoom too far in either direction. This is because I use a render texture to draw everything to, and I simply resize this when zooming in or out. This texture has a max size, causing the positioning logic to stop centering it. I'll probably change this at some point (unless you beat me to it with a PR, of course ๐Ÿ˜†)

ebiten-camera's People

Contributors

melonfunction 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  avatar

Watchers

 avatar

Forkers

dezren39

ebiten-camera's Issues

Inaccessible `*ebiten.DrawImageOptions`

*ebiten.DrawImageOptions must belong to the camera. The ebiten.FilterLinear option is inaccessible.

op := &ebiten.DrawImageOptions{}

v2.6.3 compatible enhanced version can be like this:

// Blit draws the camera's surface to the screen and applies zoom
func (c *Camera) Blit(screen *ebiten.Image) {
	centerX := float64(c.Width) / 2.0
	centerY := float64(c.Height) / 2.0
	c.CamDrawOps.GeoM.Reset()
	c.CamDrawOps.GeoM.Translate(-centerX, -centerY)
	c.CamDrawOps.GeoM.Scale(c.Scale, c.Scale)
	c.CamDrawOps.GeoM.Rotate(c.Rot)
	c.CamDrawOps.GeoM.Translate(centerX*c.Scale, centerY*c.Scale)
	screen.DrawImage(c.Surface, c.CamDrawOps)
}

All settings are now accessible., for example:

game.MyCamera.CamDrawOps.Filter = ebiten.FilterNearest

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.