Skip to content
Browse files

Implement support for colors

  • Loading branch information...
1 parent bf68b27 commit 28ef149378c0b2fc0e90ef740ec75217d0f4cb49 @kballard committed
Showing with 37 additions and 1 deletion.
  1. +37 −1 dcpu/video.go
View
38 dcpu/video.go
@@ -36,10 +36,46 @@ func (v *Video) HandleChanges() {
termWidth, termHeight := termbox.Size()
if row >= termHeight || column >= termWidth {
+ // this is offscreen, so do nothing
return
}
ch := rune(v.words[offset] & 0x7F)
- fg, bg := termbox.ColorDefault, termbox.ColorDefault // figure out colors later
+ // color seems to be in the top 2 nibbles, MSB being FG and LSB are BG
+ // Within each nibble, from LSB to MSB, is blue, green, red, highlight
+ // Lastly, the bit at 0x80 is apparently blink.
+ flag := (v.words[offset] & 0x80) != 0
+ colors := byte((v.words[offset] & 0xFF00) >> 8)
+ fgNibble := (colors & 0xF0) >> 4
+ bgNibble := colors & 0x0F
+ colorToAttr := func(color byte) termbox.Attribute {
+ attr := termbox.ColorDefault
+ // bold
+ if color&0x8 != 0 {
+ attr |= termbox.AttrBold
+ }
+ // cheat a bit here. We know the termbox color attributes go in the
+ // same order as the ANSI colors, and they're monotomically-incrementing.
+ // Just figure out the ANSI code and add ColorBlack
+ ansi := termbox.Attribute(0)
+ if color&0x1 != 0 {
+ // blue
+ ansi |= 0x4
+ }
+ if color&0x2 != 0 {
+ // green
+ ansi |= 0x2
+ }
+ if color&0x4 != 0 {
+ // red
+ ansi |= 0x1
+ }
+ attr |= ansi + termbox.ColorBlack
+ return attr
+ }
+ fg, bg := colorToAttr(fgNibble), colorToAttr(bgNibble)
+ if flag {
+ fg |= termbox.AttrBlink
+ }
termbox.SetCell(column, row, ch, fg, bg)
return
}

0 comments on commit 28ef149

Please sign in to comment.
Something went wrong with that request. Please try again.