izapple2/apple2sdl/main.go

99 lines
2.0 KiB
Go
Raw Normal View History

package main
2019-04-13 18:29:31 +00:00
import (
2019-04-21 22:18:14 +00:00
"unsafe"
"github.com/ivanizag/apple2"
2020-01-11 16:13:29 +00:00
"github.com/pkg/profile"
2019-04-13 18:29:31 +00:00
"github.com/veandco/go-sdl2/sdl"
)
func main() {
a := apple2.MainApple()
if a.IsProfiling() {
2020-01-11 16:13:29 +00:00
// See the log with:
// go tool pprof --pdf ~/go/bin/apple2sdl /tmp/profile329536248/cpu.pprof > profile.pdf
defer profile.Start().Stop()
}
2019-12-22 13:32:54 +00:00
if a != nil {
SDLRun(a)
}
}
2019-04-13 18:29:31 +00:00
// SDLRun starts the Apple2 emulator on SDL
func SDLRun(a *apple2.Apple2) {
2019-05-09 22:09:15 +00:00
window, renderer, err := sdl.CreateWindowAndRenderer(4*40*7, 4*24*8,
2019-04-21 22:18:14 +00:00
sdl.WINDOW_SHOWN)
2019-04-13 18:29:31 +00:00
if err != nil {
panic("Failed to create window")
}
2019-04-21 22:18:14 +00:00
window.SetResizable(true)
2019-04-13 18:29:31 +00:00
defer window.Destroy()
defer renderer.Destroy()
window.SetTitle(a.Name)
2019-04-13 18:29:31 +00:00
2019-05-05 10:38:24 +00:00
kp := newSDLKeyBoard(a)
2019-05-09 22:09:15 +00:00
a.SetKeyboardProvider(kp)
2019-08-05 22:37:27 +00:00
s := newSDLSpeaker()
s.start()
2019-05-09 22:09:15 +00:00
a.SetSpeakerProvider(s)
2019-08-05 22:37:27 +00:00
j := newSDLJoysticks()
a.SetJoysticksProvider(j)
2019-09-23 21:35:39 +00:00
go a.Run()
2019-04-13 18:29:31 +00:00
running := true
for running {
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
switch t := event.(type) {
case *sdl.QuitEvent:
2019-10-19 17:56:51 +00:00
a.SendCommand(apple2.CommandKill)
2019-04-13 18:29:31 +00:00
running = false
case *sdl.KeyboardEvent:
kp.putKey(t)
j.putKey(t)
2019-04-13 18:29:31 +00:00
case *sdl.TextInputEvent:
kp.putText(t)
2019-08-05 22:37:27 +00:00
case *sdl.JoyAxisEvent:
j.putAxisEvent(t)
case *sdl.JoyButtonEvent:
j.putButtonEvent(t)
2019-04-13 18:29:31 +00:00
}
}
2019-04-21 22:18:14 +00:00
2019-04-26 16:08:30 +00:00
img := apple2.Snapshot(a)
if img != nil {
surface, err := sdl.CreateRGBSurfaceFrom(unsafe.Pointer(&img.Pix[0]),
int32(img.Bounds().Dx()), int32(img.Bounds().Dy()),
32, 4*img.Bounds().Dx(),
0x0000ff, 0x0000ff00, 0x00ff0000, 0xff000000)
// Valid for little endian. Should we reverse for big endian?
// 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff)
2019-04-26 16:08:30 +00:00
if err != nil {
panic(err)
}
2019-04-21 22:18:14 +00:00
2019-04-26 16:08:30 +00:00
texture, err := renderer.CreateTextureFromSurface(surface)
if err != nil {
panic(err)
}
2019-04-21 22:18:14 +00:00
2019-04-26 16:08:30 +00:00
renderer.Clear()
w, h := window.GetSize()
renderer.Copy(texture, nil, &sdl.Rect{X: 0, Y: 0, W: w, H: h})
renderer.Present()
2019-04-21 22:18:14 +00:00
2019-04-26 16:08:30 +00:00
surface.Free()
texture.Destroy()
}
2019-05-04 17:49:11 +00:00
sdl.Delay(1000 / 30)
2019-04-13 18:29:31 +00:00
}
}