/
drawline.go
66 lines (57 loc) · 1.4 KB
/
drawline.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main
import "fmt"
func drawHorizontalLine(screen []byte, width int, x1, x2, y int) {
startOffset := x1 % 8
firstFullByte := x1 / 8
if startOffset != 0 {
firstFullByte++
}
endOffset := x2 % 8
lastFullByte := x2 / 8
if endOffset != 7 {
lastFullByte--
}
// set the full bytes
for b := firstFullByte; b <= lastFullByte; b++ {
screen[(width/8)*y+b] = 0xFF
}
startMask := 0xFF >> startOffset
endMask := ^(0xFF >> (endOffset + 1))
// Check if in the same byte
if (x1 / 8) == (x2 / 8) {
mask := startMask & endMask
screen[(width/8)*y+(x1/8)] |= byte(mask)
} else {
if startOffset != 0 {
byteNumber := (width/8)*y + firstFullByte - 1
screen[byteNumber] |= byte(startMask)
}
if endOffset != 7 {
byteNumber := (width/8)*y + lastFullByte + 1
screen[byteNumber] |= byte(endMask)
}
}
}
// in pixel
const width = 32
const height = 16
func drawScreen(screen []byte) {
height := len(screen) / (width / 8)
for y := 0; y < height; y++ {
for x := 0; x < (width / 8); x++ {
fmt.Printf("%08b", screen[y*(width/8)+x])
}
fmt.Println()
}
}
func main() {
screen := make([]byte, width/8*height)
drawHorizontalLine(screen, width, 0, 0, 0)
drawHorizontalLine(screen, width, 31, 31, 0)
drawHorizontalLine(screen, width, 0, 0, 15)
drawHorizontalLine(screen, width, 31, 31, 15)
for y := 1; y < height-1; y++ {
drawHorizontalLine(screen, width, 1, 1+y, y)
}
drawScreen(screen)
}