izapple2/apple2/screen.go

93 lines
1.9 KiB
Go
Raw Normal View History

2019-04-21 19:04:02 +00:00
package apple2
import (
"fmt"
"image"
"image/color"
"image/png"
"os"
)
2019-04-21 22:18:14 +00:00
// Snapshot the currently visible screen
func Snapshot(a *Apple2) *image.RGBA {
2019-04-21 19:04:02 +00:00
isTextMode := a.io.isSoftSwitchActive(ioFlagGraphics)
is80ColMode := a.io.isSoftSwitchActive(ioFlag80Col)
pageIndex := 0
if a.io.isSoftSwitchActive(ioFlagSecondPage) {
pageIndex = 1
}
if isTextMode && !is80ColMode {
//Text mode
return snapshotTextMode(a, pageIndex)
}
fmt.Printf("t: %v, 8: %v\n", isTextMode, is80ColMode)
return nil
//panic("Screen mode not supported")
}
func saveSnapshot(a *Apple2) {
2019-04-21 22:18:14 +00:00
img := Snapshot(a)
2019-04-21 19:04:02 +00:00
if img == nil {
return
}
f, err := os.Create("snapshot.png")
if err != nil {
panic(err)
}
defer f.Close()
fmt.Println("Saving snapshot")
png.Encode(f, img)
}
const (
charWidth = 7
charHeight = 8
textColumns = 40
textLines = 24
textPage1Address = uint16(0x400)
textPage2Address = uint16(0x400)
)
func getTextChar(a *Apple2, col int, line int, page int) uint8 {
address := textPage1Address
if page == 1 {
address = textPage2Address
}
// See "Understand the Apple II", page 5-10
// http://www.applelogic.org/files/UNDERSTANDINGTHEAII.pdf
section := line / 8 // Top, middle and bottom
eigth := line % 8
address += uint16(section*40 + eigth*0x80 + col)
return a.mmu.internalPeek(address)
}
2019-04-21 22:18:14 +00:00
func snapshotTextMode(a *Apple2, page int) *image.RGBA {
2019-04-21 19:04:02 +00:00
width := textColumns * charWidth
height := textLines * charHeight
size := image.Rect(0, 0, width, height)
2019-04-21 22:18:14 +00:00
img := image.NewRGBA(size)
2019-04-21 19:04:02 +00:00
for x := 0; x < width; x++ {
for y := 0; y < 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)
2019-04-21 22:18:14 +00:00
colour := color.Black
2019-04-21 19:04:02 +00:00
if pixel {
2019-04-21 22:18:14 +00:00
colour = color.White
2019-04-21 19:04:02 +00:00
}
2019-04-21 22:18:14 +00:00
img.Set(x, y, colour)
2019-04-21 19:04:02 +00:00
}
}
return img
}