-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
80 lines (65 loc) · 2.18 KB
/
app.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package main
import (
"image"
"github.com/elmarsan/gbgo/pkg/emulator"
"github.com/elmarsan/gbgo/pkg/gameboy"
"github.com/hajimehoshi/ebiten/v2"
)
const (
// WindowWidth represents the window width of application.
WindowWidth = 640
// WindowHeight represents the window height of application.
WindowHeight = 480
)
// App represents ebiten.Game implementation.
type App struct {
emu *emulator.Emulator
keyHandler map[ebiten.Key]func(down bool)
}
// NewApp returns new instance of App.
func NewApp(emu *emulator.Emulator) *App {
return &App{
emu: emu,
keyHandler: map[ebiten.Key]func(down bool){
ebiten.KeyArrowUp: func(down bool) { emu.SetButton(emulator.UpBtn, down) },
ebiten.KeyArrowDown: func(down bool) { emu.SetButton(emulator.DownBtn, down) },
ebiten.KeyArrowLeft: func(down bool) { emu.SetButton(emulator.LeftBtn, down) },
ebiten.KeyArrowRight: func(down bool) { emu.SetButton(emulator.RightBtn, down) },
ebiten.KeySpace: func(down bool) { emu.SetButton(emulator.SelectBtn, down) },
ebiten.KeyEnter: func(down bool) { emu.SetButton(emulator.StartBtn, down) },
ebiten.KeyA: func(down bool) { emu.SetButton(emulator.ABtn, down) },
ebiten.KeyB: func(down bool) { emu.SetButton(emulator.BBtn, down) },
},
}
}
func (a *App) Run() error {
ebiten.SetWindowSize(WindowWidth, WindowHeight)
ebiten.SetWindowTitle("GBGO")
return ebiten.RunGame(a)
}
func (a *App) Update() error {
for key, handler := range a.keyHandler {
handler(ebiten.IsKeyPressed(key))
}
return nil
}
func (a *App) Draw(screen *ebiten.Image) {
img := a.pixelsToImage()
drawopts := &ebiten.DrawImageOptions{}
drawopts.GeoM.Scale(4, 3.33)
screen.DrawImage(img, drawopts)
}
func (a *App) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) {
return 640, 480
}
func (a *App) pixelsToImage() *ebiten.Image {
imgRGBA := image.NewRGBA(image.Rect(0, 0, gameboy.ScreenWidth, gameboy.ScreenHeight))
pixels := a.emu.GetRGBAPixels()
for y := 0; y < gameboy.ScreenHeight; y++ {
for x := 0; x < gameboy.ScreenWidth; x++ {
i := y*gameboy.ScreenWidth + x
imgRGBA.SetRGBA(x, y, pixels[i])
}
}
return ebiten.NewImageFromImage(imgRGBA)
}