ebiten/examples/moire/main.go

109 lines
2.5 KiB
Go
Raw Normal View History

2017-12-05 15:18:47 +01:00
// Copyright 2017 The Ebiten Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// +build example jsgo
2017-12-05 15:18:47 +01:00
2018-01-28 19:09:30 +01:00
// This example is just to check if Ebiten can draw fine checker pattern evenly.
// If there is something wrong in the implementation, the result might include
// uneven patterns (#459).
2017-12-05 15:18:47 +01:00
package main
import (
"log"
2020-10-03 19:35:13 +02:00
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/inpututil"
2017-12-05 15:18:47 +01:00
)
const (
2018-01-28 19:09:30 +01:00
screenWidth = 640
screenHeight = 480
initScreenScale = 1
2017-12-05 15:18:47 +01:00
)
var (
dots []byte
2017-12-05 15:18:47 +01:00
dotsWidth int
dotsHeight int
)
func getDots(width, height int) []byte {
2017-12-05 15:18:47 +01:00
if dotsWidth == width && dotsHeight == height {
return dots
}
dotsWidth = width
dotsHeight = height
dots = make([]byte, width*height*4)
2017-12-05 15:18:47 +01:00
for j := 0; j < height; j++ {
for i := 0; i < width; i++ {
if (i+j)%2 == 0 {
dots[(i+j*width)*4+0] = 0xff
dots[(i+j*width)*4+1] = 0xff
dots[(i+j*width)*4+2] = 0xff
dots[(i+j*width)*4+3] = 0xff
}
}
}
return dots
}
type game struct {
scale float64
}
func (g *game) Layout(outsideWidth, outsideHeight int) (int, int) {
return screenWidth, screenHeight
}
func (g *game) Update(screen *ebiten.Image) error {
2017-12-05 15:18:47 +01:00
fullscreen := ebiten.IsFullscreen()
2018-02-04 15:18:32 +01:00
if inpututil.IsKeyJustPressed(ebiten.KeyS) {
switch g.scale {
case 0.5:
g.scale = 1
2017-12-05 15:18:47 +01:00
case 1:
g.scale = 1.5
2017-12-05 15:18:47 +01:00
case 1.5:
g.scale = 2
2017-12-05 15:18:47 +01:00
case 2:
g.scale = 0.5
2017-12-05 15:18:47 +01:00
default:
panic("not reached")
}
ebiten.SetWindowSize(int(screenWidth*g.scale), int(screenHeight*g.scale))
2017-12-05 15:18:47 +01:00
}
2018-02-04 15:18:32 +01:00
if inpututil.IsKeyJustPressed(ebiten.KeyF) {
2017-12-05 15:18:47 +01:00
fullscreen = !fullscreen
ebiten.SetFullscreen(fullscreen)
2017-12-05 15:18:47 +01:00
}
return nil
}
2017-12-05 15:18:47 +01:00
func (g *game) Draw(screen *ebiten.Image) {
2017-12-05 15:18:47 +01:00
screen.ReplacePixels(getDots(screen.Size()))
}
func main() {
g := &game{
scale: initScreenScale,
}
ebiten.SetWindowSize(screenWidth*initScreenScale, screenHeight*initScreenScale)
ebiten.SetWindowTitle("Moire (Ebiten Demo)")
ebiten.SetWindowResizable(true)
if err := ebiten.RunGame(g); err != nil {
2017-12-05 15:18:47 +01:00
log.Fatal(err)
}
}