izapple2/izapple2sdl/main.go

125 lines
2.6 KiB
Go
Raw Normal View History

package main
2019-04-13 18:29:31 +00:00
import (
"fmt"
"image"
2019-04-21 22:18:14 +00:00
"unsafe"
2020-10-03 21:38:26 +00:00
"github.com/ivanizag/izapple2"
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() {
2020-10-03 21:38:26 +00:00
a := izapple2.MainApple()
2019-12-22 13:32:54 +00:00
if a != nil {
2020-09-23 16:09:18 +00:00
if a.IsProfiling() {
// See the log with:
2020-10-03 21:38:26 +00:00
// go tool pprof --pdf ~/go/bin/izapple2sdl /tmp/profile329536248/cpu.pprof > profile.pdf
2020-09-23 16:09:18 +00:00
defer profile.Start().Stop()
}
2019-12-22 13:32:54 +00:00
SDLRun(a)
}
}
2019-04-13 18:29:31 +00:00
// SDLRun starts the Apple2 emulator on SDL
2020-10-03 21:38:26 +00:00
func SDLRun(a *izapple2.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()
2020-10-03 21:38:26 +00:00
window.SetTitle("iz-" + 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(true)
2019-08-05 22:37:27 +00:00
a.SetJoysticksProvider(j)
2019-09-23 21:35:39 +00:00
go a.Run()
2019-04-13 18:29:31 +00:00
2020-03-14 02:29:12 +00:00
paused := false
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:
2020-10-03 21:38:26 +00:00
a.SendCommand(izapple2.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)
case *sdl.MouseMotionEvent:
w, h := window.GetSize()
j.putMouseMotionEvent(t, w, h)
case *sdl.MouseButtonEvent:
j.putMouseButtonEvent(t)
2019-04-13 18:29:31 +00:00
}
}
2019-04-21 22:18:14 +00:00
2020-03-14 02:29:12 +00:00
if paused != a.IsPaused() {
if a.IsPaused() {
2020-10-03 21:38:26 +00:00
window.SetTitle("iz-" + a.Name + " - PAUSED!")
2020-03-14 02:29:12 +00:00
} else {
2020-10-03 21:38:26 +00:00
window.SetTitle("iz-" + a.Name)
2020-03-14 02:29:12 +00:00
}
paused = a.IsPaused()
}
if !a.IsPaused() {
var img *image.RGBA
if kp.showPages {
img = a.SnapshotParts()
window.SetTitle(fmt.Sprintf("%v %v %vx%v", a.Name, a.VideoModeName(), img.Rect.Dx()/2, img.Rect.Dy()/2))
} else {
img = a.Snapshot()
}
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)
if err != nil {
panic(err)
}
texture, err := renderer.CreateTextureFromSurface(surface)
if err != nil {
panic(err)
}
renderer.Clear()
w, h := window.GetSize()
renderer.Copy(texture, nil, &sdl.Rect{X: 0, Y: 0, W: w, H: h})
renderer.Present()
surface.Free()
texture.Destroy()
2019-04-26 16:08:30 +00:00
}
}
2019-05-04 17:49:11 +00:00
sdl.Delay(1000 / 30)
2019-04-13 18:29:31 +00:00
}
}