Render text mode into SDL window

This commit is contained in:
Ivan Izaguirre 2019-04-22 00:18:14 +02:00
parent 2cd3ed3a48
commit d4d47f3968
3 changed files with 36 additions and 14 deletions

View File

@ -179,7 +179,7 @@ func (fe *ansiConsoleFrontend) textModeGoRoutine() {
fmt.Print("\033[KLine: ")
}
saveSnapshot(fe.apple2)
//saveSnapshot(fe.apple2)
}
time.Sleep(refreshDelayMs * time.Millisecond)
}

View File

@ -8,7 +8,8 @@ import (
"os"
)
func snapshot(a *Apple2) image.Image {
// Snapshot the currently visible screen
func Snapshot(a *Apple2) *image.RGBA {
isTextMode := a.io.isSoftSwitchActive(ioFlagGraphics)
is80ColMode := a.io.isSoftSwitchActive(ioFlag80Col)
pageIndex := 0
@ -26,7 +27,7 @@ func snapshot(a *Apple2) image.Image {
}
func saveSnapshot(a *Apple2) {
img := snapshot(a)
img := Snapshot(a)
if img == nil {
return
}
@ -65,29 +66,26 @@ func getTextChar(a *Apple2, col int, line int, page int) uint8 {
return a.mmu.internalPeek(address)
}
func snapshotTextMode(a *Apple2, page int) image.Image {
func snapshotTextMode(a *Apple2, page int) *image.RGBA {
width := textColumns * charWidth
height := textLines * charHeight
size := image.Rect(0, 0, width, height)
bwPalette := []color.Color{color.Black, color.White}
img := image.NewPaletted(size, bwPalette)
img := image.NewRGBA(size)
for x := 0; x < width; x++ {
for y := 0; y < height; y++ {
//yRev := height - y
line := y / charHeight
col := x / charWidth
rowInChar := y % charHeight
colInChar := x % charWidth
char := getTextChar(a, col, line, page)
pixel := a.cg.getPixel(char, rowInChar, colInChar)
color := uint8(0)
colour := color.Black
if pixel {
color = 1
colour = color.White
}
img.SetColorIndex(x, y, color)
img.Set(x, y, colour)
}
}
return img

View File

@ -1,6 +1,8 @@
package apple2sdl
import (
"unsafe"
"github.com/veandco/go-sdl2/sdl"
"go6502/apple2"
@ -8,15 +10,16 @@ import (
// SDLRun starts the Apple2 emulator on SDL
func SDLRun(a *apple2.Apple2) {
window, renderer, err := sdl.CreateWindowAndRenderer(800, 600, sdl.WINDOW_SHOWN)
window, renderer, err := sdl.CreateWindowAndRenderer(800, 600,
sdl.WINDOW_SHOWN)
if err != nil {
panic("Failed to create window")
}
window.SetResizable(true)
defer window.Destroy()
defer renderer.Destroy()
window.SetTitle("Apple2")
renderer.Clear()
renderer.Present()
kp := newSDLKeyBoard()
a.SetKeyboardProvider(&kp)
@ -38,6 +41,27 @@ func SDLRun(a *apple2.Apple2) {
kp.putText(t)
}
}
img := *apple2.Snapshot(a)
surface, err := sdl.CreateRGBSurfaceFrom(unsafe.Pointer(&img.Pix[0]), 40*7, 24*8, 32, 40*7*4,
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()
sdl.Delay(1000 / 60)
}