Skip to content

Instantly share code, notes, and snippets.

@peterhellberg
Last active April 30, 2020 20:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterhellberg/d67ab163deeb38dc2d9f043b256b2a40 to your computer and use it in GitHub Desktop.
Save peterhellberg/d67ab163deeb38dc2d9f043b256b2a40 to your computer and use it in GitHub Desktop.
Nimgobus example without package global variables
package main
import (
_ "image/png"
"time"
"github.com/adamstimb/nimgobus"
"github.com/hajimehoshi/ebiten"
)
func main() {
ebiten.SetWindowSize(670, 540)
ebiten.SetWindowResizable(true)
ebiten.SetWindowTitle("Nimgobus Example (resizeable)")
game := NewGame(ebiten.WindowSize())
game.Setup()
ebiten.RunGame(game)
}
type Game struct {
width int
height int
nimgobus.Nimbus
}
func NewGame(width, height int) *Game {
game := &Game{width: width, height: height}
game.Init()
return game
}
func (g *Game) Setup() {
g.SetMode(40)
g.SetBorder(LowGreyDark)
g.SetPaper(LowGrey)
}
func (g *Game) Update(screen *ebiten.Image) error {
if ebiten.IsKeyPressed(ebiten.KeyEscape) {
return Error{"done"}
}
g.width = screen.Bounds().Dx()
g.height = screen.Bounds().Dy()
g.Cls()
op := nimgobus.PlotOptions{
SizeX: 2,
SizeY: 3,
Brush: LowGreenDark,
}
g.Plot(op, "Nimgobus Example", 35, 150)
op.Brush = LowGreen
g.Plot(op, "Nimgobus Example", 37, 152)
op.SizeX = 1
op.SizeY = 1
op.Brush = LowBlueDark
g.Plot(op, "No package global variables!", 50, 90)
g.PlonkLogo(8, 30)
op.Brush = LowWhite
g.Plot(op, time.Now().Format(time.Stamp), 200, 0)
g.Nimbus.Update()
return nil
}
func (g *Game) Draw(screen *ebiten.Image) {
mw, mh := g.Monitor.Size()
mr := float64(mw) / float64(mh)
var scale, offsetX, offsetY float64
r := g.ratio()
switch {
case r > mr:
scale = float64(g.height) / float64(mh)
offsetX = (float64(g.width) - float64(mw)*scale) / 2
offsetY = 0
case r <= mr:
scale = float64(g.width) / float64(mw)
offsetX = 0
offsetY = (float64(g.height) - float64(mh)*scale) / 2
}
op := &ebiten.DrawImageOptions{}
op.GeoM.Scale(scale, scale)
op.GeoM.Translate(offsetX, offsetY)
op.Filter = ebiten.FilterLinear
screen.DrawImage(g.Monitor, op)
}
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
return outsideWidth, outsideHeight
}
func (g *Game) ratio() float64 {
return float64(g.width) / float64(g.height)
}
type Error struct {
Message string
}
func (e Error) Error() string {
return e.Message
}
// Low resolution colors
var (
LowBlack = 0
LowBlueDark = 1
LowRedDark = 2
LowPurpleDark = 3
LowGreenDark = 4
LowCyanDark = 5
LowBrown = 6
LowGrey = 7
LowGreyDark = 8
LowBlue = 9
LowRed = 10
LowPurple = 11
LowGreen = 12
LowCyan = 13
LowYellow = 14
LowWhite = 15
)
@peterhellberg
Copy link
Author

Screenshot 2020-04-30 at 22 40 52

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment