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: ") fmt.Print("\033[KLine: ")
} }
saveSnapshot(fe.apple2) //saveSnapshot(fe.apple2)
} }
time.Sleep(refreshDelayMs * time.Millisecond) time.Sleep(refreshDelayMs * time.Millisecond)
} }

View File

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

View File

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