ebiten/examples/moire/main.go

110 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.
2021-06-24 14:49:37 +02:00
//go:build example
2020-10-06 17:45:54 +02:00
// +build example
2017-12-05 15:18:47 +01:00
// This example is just to check if Ebitengine can draw fine checker pattern evenly.
2018-01-28 19:09:30 +01:00
// 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() 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) {
screen.WritePixels(getDots(screen.Size()))
2017-12-05 15:18:47 +01:00
}
func main() {
g := &game{
scale: initScreenScale,
}
ebiten.SetWindowSize(screenWidth*initScreenScale, screenHeight*initScreenScale)
ebiten.SetWindowTitle("Moire (Ebitengine Demo)")
ebiten.SetWindowResizable(true)
if err := ebiten.RunGame(g); err != nil {
2017-12-05 15:18:47 +01:00
log.Fatal(err)
}
}