2019-06-01 15:11:25 +00:00
|
|
|
package main
|
2019-04-13 18:29:31 +00:00
|
|
|
|
|
|
|
import (
|
2019-04-21 22:18:14 +00:00
|
|
|
"unsafe"
|
|
|
|
|
2019-06-01 15:11:25 +00:00
|
|
|
"github.com/ivanizag/apple2"
|
2019-04-13 18:29:31 +00:00
|
|
|
"github.com/veandco/go-sdl2/sdl"
|
|
|
|
)
|
|
|
|
|
2019-06-01 15:11:25 +00:00
|
|
|
func main() {
|
|
|
|
a := apple2.MainApple()
|
|
|
|
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
|
|
|
|
2019-04-27 22:13:34 +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()
|
2019-06-09 21:54:27 +00:00
|
|
|
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)
|
2019-10-19 22:19:00 +00:00
|
|
|
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 {
|
2019-05-01 14:54:53 +00:00
|
|
|
surface, err := sdl.CreateRGBSurfaceFrom(unsafe.Pointer(&img.Pix[0]),
|
2019-05-02 10:21:07 +00:00
|
|
|
int32(img.Bounds().Dx()), int32(img.Bounds().Dy()),
|
|
|
|
32, 4*img.Bounds().Dx(),
|
2019-04-27 22:13:34 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
}
|