Repository navigation
Expand file tree
/
Copy pathcontainer.go
More file actions
223 lines (188 loc) · 5.1 KB
/
Copy pathcontainer.go
File metadata and controls
223 lines (188 loc) · 5.1 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package main
import (
"bufio"
"fmt"
"hash/fnv"
"image/color"
"os"
"slices"
"strings"
"syscall"
"github.com/ebitengine/purego"
)
// Custom containers
type RhaeList struct {
elements []stackValue
}
func (rl RhaeList) String() string {
return fmt.Sprint(rl.elements)
}
func (rl *RhaeList) add(e stackValue) {
rl.elements = append(rl.elements, e)
}
func (rl *RhaeList) get(pos int) stackValue {
return rl.elements[pos]
}
func (rl *RhaeList) set(pos int, e stackValue) {
rl.elements[pos] = e
}
func (rl *RhaeList) size() int {
return len(rl.elements)
}
func (rl *RhaeList) pop(pos int) {
rl.elements = slices.Delete(rl.elements, pos, pos+1)
}
type RhaeDict struct {
dict map[uint32]stackValue
keys map[uint32]stackValue
}
func NewDict() RhaeDict {
return RhaeDict{
dict: map[uint32]stackValue{},
keys: map[uint32]stackValue{},
}
}
func (rd *RhaeDict) String() string {
out := strings.Builder{}
out.WriteString("{")
for hk, v := range rd.dict {
k := rd.keys[hk]
pair := fmt.Sprintf("%s: %s,", k.String(), v.String())
out.WriteString(pair)
}
out.WriteString("}")
return out.String()
}
func (rd *RhaeDict) hash(sv *stackValue) uint32 {
var s string
switch sv.t {
case NUMERIC, STRING:
s = fmt.Sprint(sv.v)
case BOOL:
s = "0"
if sv.v.(bool) {
s = "1"
}
default:
panic(fmt.Sprintf("unhashable type: t[%s] - v[%v]", sv.t, sv.v))
}
h := fnv.New32a()
_, err := h.Write([]byte(s))
if err != nil {
panic(fmt.Sprintf("[hash]: %s", err.Error()))
}
return h.Sum32()
}
func (rd *RhaeDict) putKv(k stackValue, v stackValue) {
hk := rd.hash(&k)
rd.dict[hk] = v
rd.keys[hk] = k
}
func (rd *RhaeDict) getValue(k stackValue) stackValue {
hk := rd.hash(&k)
if out, ok := rd.dict[hk]; ok {
return out
}
panic(fmt.Sprintf("[dict] key [%s] does not exist", k.String()))
}
func (rd *RhaeDict) deleteKey(k stackValue) {
hk := rd.hash(&k)
delete(rd.dict, hk)
delete(rd.keys, hk)
}
func (rd *RhaeDict) sizeDict() int {
return len(rd.dict)
}
type RhaeFile struct {
file *os.File
scanner *bufio.Scanner
}
func NewRhaeFile(filepath string) *RhaeFile {
file, err := os.OpenFile(filepath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666)
if err != nil {
panic(fmt.Sprintf("opening a file failed with error: %s", err.Error()))
}
scanner := bufio.NewScanner(file) // ScanLines is the default split function
return &RhaeFile{
file: file,
scanner: scanner,
}
}
func RhaeFileFrom(file *os.File) *RhaeFile {
return &RhaeFile{file, bufio.NewScanner(file)}
}
func (rd *RhaeFile) String() string {
return "|just a file|"
}
func (rf *RhaeFile) readline() string {
ok := rf.scanner.Scan()
if ok {
return rf.scanner.Text()
}
return ""
}
func (rf *RhaeFile) write(str string) {
_, err := rf.file.WriteString(str)
if err != nil {
panic(fmt.Sprintf("writing to a file failed with error: %s", err.Error()))
}
}
func (rf *RhaeFile) close() {
_ = rf.file.Close()
}
// Custom Containers - end
// externals
type RaylibRect = struct {
x, y, w, h float32
}
type RaylibV2 = struct {
x, y float32
}
type Raylib struct {
raylibHandle uintptr
// funcs
InitWindow func(width int32, height int32, title string)
CloseWindow func()
SetTargetFPS func(fps int32)
WindowShouldClose func() bool
BeginDrawing func()
ClearBackground func(color color.RGBA)
DrawText func(text string, posX int32, posY int32, fontSize int32, color color.RGBA)
EndDrawing func()
// new
IsKeyDown func(key int32) bool
CheckCollisionRecs func(rec1 RaylibRect, rec2 RaylibRect) bool
DrawRectangleRec func(rec RaylibRect, color color.RGBA)
DrawLine func(startPosX int32, startPosY int32, endPosX int32, endPosY int32, color color.RGBA)
DrawLineV func(startPos, endPos RaylibV2, color color.RGBA)
}
func (rd *Raylib) String() string {
return "|just raylib|"
}
// this is Windows specific
func openLibrary(name string) (uintptr, error) {
handle, err := syscall.LoadLibrary(name)
return uintptr(handle), err
}
func RaylibInit() *Raylib {
raylibHandle, err := openLibrary("./lib/raylib.dll")
if err != nil {
panic(err.Error())
}
var raylib = Raylib{}
raylib.raylibHandle = raylibHandle
purego.RegisterLibFunc(&raylib.InitWindow, raylibHandle, "InitWindow")
purego.RegisterLibFunc(&raylib.CloseWindow, raylibHandle, "CloseWindow")
purego.RegisterLibFunc(&raylib.SetTargetFPS, raylibHandle, "SetTargetFPS")
purego.RegisterLibFunc(&raylib.WindowShouldClose, raylibHandle, "WindowShouldClose")
purego.RegisterLibFunc(&raylib.BeginDrawing, raylibHandle, "BeginDrawing")
purego.RegisterLibFunc(&raylib.ClearBackground, raylibHandle, "ClearBackground")
purego.RegisterLibFunc(&raylib.EndDrawing, raylibHandle, "EndDrawing")
purego.RegisterLibFunc(&raylib.CheckCollisionRecs, raylibHandle, "CheckCollisionRecs")
purego.RegisterLibFunc(&raylib.DrawText, raylibHandle, "DrawText")
purego.RegisterLibFunc(&raylib.DrawRectangleRec, raylibHandle, "DrawRectangleRec")
purego.RegisterLibFunc(&raylib.DrawLine, raylibHandle, "DrawLine")
purego.RegisterLibFunc(&raylib.DrawLineV, raylibHandle, "DrawLineV")
purego.RegisterLibFunc(&raylib.IsKeyDown, raylibHandle, "IsKeyDown")
return &raylib
}