This repository was archived by the owner on Oct 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine.go
More file actions
173 lines (140 loc) · 3.83 KB
/
engine.go
File metadata and controls
173 lines (140 loc) · 3.83 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
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path"
"path/filepath"
"ghostlang.org/x/engine/engine"
"ghostlang.org/x/ghost/ghost"
"ghostlang.org/x/ghost/object"
)
var (
flagVersion bool
flagHelp bool
)
// Console holds the passed values through the command line.
type Console struct {
args []string
}
func init() {
flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage: %s [options] [<filename>]\n", path.Base(os.Args[0]))
flag.PrintDefaults()
os.Exit(0)
}
flag.BoolVar(&flagHelp, "h", false, "display help information")
flag.BoolVar(&flagVersion, "v", false, "display version information")
}
func main() {
flag.Parse()
args := flag.Args()
if flagVersion {
fmt.Printf("%s %s\n", path.Base(os.Args[0]), "dev-nightly")
os.Exit(0)
}
if flagHelp {
showHelp()
os.Exit(2)
}
console := Console{args}
var f *os.File
var err error
if len(console.args) == 0 {
// Do we have a main.ghost file present?
ex, err := os.Executable()
if err != nil {
panic(err)
}
exPath := filepath.Dir(ex)
fmt.Println(exPath)
mainFile, _ := filepath.Abs(exPath + "/main.ghost")
f, err = os.Open(mainFile)
if err != nil {
log.Fatalf("could not find main.ghost: %s", err)
showHelp()
os.Exit(2)
}
} else {
f, err = os.Open(console.args[0])
if err != nil {
log.Fatalf("could not open source file %s: %s", console.args[0], err)
}
}
b, err := ioutil.ReadAll(f)
if err != nil {
fmt.Fprintf(os.Stderr, "error reading source file: %s", err)
return
}
engine := engine.NewEngine("Engine")
engine.SetWindow(1280, 720)
engine.SetLoadFunction(load)
engine.SetUpdateFunction(update)
engine.SetDrawFunction(draw)
// Graphics Functions
ghost.RegisterFunction("Graphics.clear", engine.GraphicsClearFunction)
ghost.RegisterFunction("Graphics.draw", engine.GraphicsDrawFunction)
ghost.RegisterFunction("Graphics.filledRectangle", engine.GraphicsFilledRectangleFunction)
ghost.RegisterFunction("Graphics.line", engine.GraphicsLineFunction)
ghost.RegisterFunction("Graphics.pixel", engine.GraphicsPixelFunction)
ghost.RegisterFunction("Graphics.print", engine.GraphicsPrintFunction)
ghost.RegisterFunction("Graphics.rectangle", engine.GraphicsRectangleFunction)
ghost.RegisterFunction("Graphics.setColor", engine.GraphicsSetColorFunction)
// Keyboard Functions
ghost.RegisterFunction("Keyboard.isDown", engine.KeyboardIsDownFunction)
// Mouse Functions
ghost.RegisterFunction("Mouse.hideCursor", engine.MouseHideCursorFunction)
ghost.RegisterFunction("Mouse.showCursor", engine.MouseShowCursorFunction)
// Window Functions
ghost.RegisterFunction("Window.bordered", engine.WindowBorderedFunction)
ghost.RegisterFunction("Window.borderless", engine.WindowBorderlessFunction)
ghost.RegisterFunction("Window.fullscreen", engine.WindowFullscreenFunction)
ghost.RegisterFunction("Window.height", engine.WindowHeightFunction)
ghost.RegisterFunction("Window.setSize", engine.WindowSetSizeFunction)
ghost.RegisterFunction("Window.title", engine.WindowTitleFunction)
ghost.RegisterFunction("Window.width", engine.WindowWidthFunction)
ghost.NewScript(string(b))
env := ghost.Evaluate()
engine.Run(env)
}
func load(env *object.Environment) {
ghost.Call(`
if (load) {
load()
}
`, env)
}
func update(env *object.Environment) {
ghost.Call(`
if (update) {
update()
}
`, env)
}
func draw(env *object.Environment) {
ghost.Call(`
if (draw) {
draw()
}
`, env)
}
func showHelp() {
fmt.Println("Usage:")
fmt.Println()
fmt.Println(" engine [flags] {file}")
fmt.Println()
fmt.Println("Flags:")
fmt.Println()
fmt.Println(" -h show help")
fmt.Println(" -v show version")
fmt.Println()
fmt.Println("Examples:")
fmt.Println()
fmt.Println(" engine game.ghost")
fmt.Println()
fmt.Println(" Execute source file (game.ghost)")
fmt.Println()
fmt.Println()
}