This repository has been archived by the owner on Nov 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
window.go
89 lines (74 loc) · 2.07 KB
/
window.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
Copyright (C) 2017-2018 Andreas T Jonsson
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package game
import (
"image"
"log"
)
type window struct {
title string
putch func(int, int, string)
tileset map[string]*image.Paletted
rect, canvas image.Rectangle
}
func newWindow(title string, rect image.Rectangle, tileset map[string]*image.Paletted, putch func(int, int, string)) *window {
one := image.Pt(1, 1)
return &window{
title,
putch,
tileset,
rect,
image.Rectangle{Min: rect.Add(one).Min, Max: rect.Sub(one).Max},
}
}
func (w *window) clear() {
for y := w.canvas.Min.Y; y < w.canvas.Max.Y; y++ {
for x := w.canvas.Min.X; x < w.canvas.Max.X; x++ {
if w == nil || w.putch == nil {
log.Fatalln("aaaaaa", w, w.putch)
}
w.putch(x, y, string(" "))
}
}
x, y := w.rect.Min.X, w.rect.Min.Y
sz := w.rect.Size()
for i := 1; i < sz.X-1; i++ {
w.putch(x+i, y, "#196")
w.putch(x+i, y+(sz.Y-1), "#196")
}
w.putch(x, y, "#218")
w.putch(x+(sz.X-1), y, "#191")
w.putch(x, y+(sz.Y-1), "#192")
w.putch(x+(sz.X-1), y+(sz.Y-1), "#217")
for i := 1; i < sz.Y-1; i++ {
w.putch(x, y+i, "#179")
w.putch(x+(sz.X-1), y+i, "#179")
}
c := sz.X/2 - len(w.title)/2
w.putch(x+c-1, y, " ")
for i, r := range w.title {
w.putch(x+c+i, y, string(r))
}
w.putch(x+c+len(w.title), y, " ")
}
func (w *window) print(x, y int, text string) {
x += w.canvas.Min.X
y += w.canvas.Min.Y
for i, r := range text {
p := x + i
if image.Pt(p, y).In(w.canvas) {
w.putch(p, y, string(r))
}
}
}