1
+ package main
2
+
3
+ import (
4
+ "math/rand"
5
+ "fmt"
6
+
7
+ "github.com/veandco/go-sdl2/sdl"
8
+ //"github.com/veandco/go-sdl2/ttf"
9
+ "github.com/yuin/gopher-lua"
10
+ )
11
+
12
+ const version = "v0.0.1"
13
+ const res int = 128 // Resolution of the *screen* ("internal") . Might change later in development. (res x res, 128 x 128)
14
+ const scale = 4 // Resolution scale (contributes to the size of the *window*)
15
+ var palette [][]uint8 = make ([][]uint8 , 64 ) // Array of array of RGB values ([[R, G, B], [R, G, B], ...])
16
+ var pixelbuf []byte = make ([]byte , res * res * 4 ) // Pixel backbuffer (basically our VRAM)
17
+
18
+ func main () {
19
+ for i := uint8 (0 ); i < 64 ; i ++ {
20
+ // Generate a palette
21
+ palette [i ] = []uint8 {i * 4 % 255 , i * 4 % 255 , i * 4 % 255 }
22
+ }
23
+
24
+ L := lua .NewState ()
25
+
26
+ // Load Lua standard library
27
+ L .OpenLibs ()
28
+ // Register our functions
29
+ L .SetGlobal ("vpoke" , L .NewFunction (PWvPoke ))
30
+ L .SetGlobal ("plot" , L .NewFunction (PWplot ))
31
+ L .SetGlobal ("termprint" , L .NewFunction (PWtermPrint ))
32
+
33
+ if err := sdl .Init (sdl .INIT_EVERYTHING ); err != nil {
34
+ panic (err )
35
+ }
36
+ defer sdl .Quit ()
37
+
38
+ wintitle := "Pinwheel " + version
39
+
40
+ window , err := sdl .CreateWindow (wintitle , sdl .WINDOWPOS_UNDEFINED , sdl .WINDOWPOS_UNDEFINED , int32 (res * scale ), int32 (res * scale ), sdl .WINDOW_SHOWN )
41
+ if err != nil {
42
+ panic (err )
43
+ }
44
+ defer window .Destroy ()
45
+
46
+ renderer , err := sdl .CreateRenderer (window , - 1 , sdl .RENDERER_ACCELERATED )
47
+ if err != nil {
48
+ panic (err )
49
+ }
50
+ defer renderer .Destroy ()
51
+
52
+ screen , err := renderer .CreateTexture (sdl .PIXELFORMAT_ARGB8888 , sdl .TEXTUREACCESS_STREAMING , int32 (res ), int32 (res ))
53
+ if err != nil {
54
+ panic (err )
55
+ }
56
+ defer screen .Destroy ()
57
+
58
+ if err := L .DoFile ("program.lua" ); err != nil {
59
+ panic (err )
60
+ }
61
+ defer L .Close ()
62
+
63
+ // "CPU Cycle," our main loop
64
+ running := true
65
+ for running {
66
+ for event := sdl .PollEvent (); event != nil ; event = sdl .PollEvent () {
67
+ switch event .(type ) {
68
+ case * sdl.QuitEvent :
69
+ running = false
70
+ break
71
+ }
72
+ }
73
+
74
+ renderer .SetDrawColor (0 , 0 , 0 , 0 )
75
+ renderer .Clear ()
76
+
77
+ // Call the Spin function from Lua
78
+ if err := L .CallByParam (lua.P {
79
+ Fn : L .GetGlobal ("Spin" ),
80
+ NRet : 0 ,
81
+ Protect : true ,
82
+ }); err != nil {
83
+ panic (err )
84
+ }
85
+
86
+ // Update the screen with our pixel backbuffer
87
+ screen .Update (nil , pixelbuf , res * 4 )
88
+ renderer .Copy (screen , nil , nil )
89
+
90
+ // Flush screen
91
+ renderer .Present ()
92
+ }
93
+ }
94
+
95
+ func randf (r int ) int {
96
+ return rand .Intn (r )
97
+ }
98
+
99
+ // vpoke(addr, val)
100
+ func PWvPoke (L * lua.LState ) int {
101
+ addr := L .ToInt (1 )
102
+ val := L .ToInt (2 )
103
+
104
+ pixelbuf [addr ] = byte (val )
105
+
106
+ return 1
107
+ }
108
+
109
+ // plot(x, y, color)
110
+ func PWplot (L * lua.LState ) int {
111
+ x := L .ToInt (1 )
112
+ y := L .ToInt (2 )
113
+ color := L .ToInt (3 )
114
+
115
+ c := palette [color ]
116
+ setpixel (x , y , int (c [0 ]), int (c [0 ]), int (c [0 ]))
117
+
118
+ return 1
119
+ }
120
+
121
+ // termprint(text)
122
+ func PWtermPrint (L * lua.LState ) int {
123
+ text := L .ToString (1 )
124
+
125
+ fmt .Println (text )
126
+
127
+ return 1
128
+ }
129
+
130
+ func setpixel (x , y , r , g , b int ) {
131
+ offset := ( res * 4 * y ) + x * 4 ;
132
+ pixelbuf [offset + 0 ] = byte (b )
133
+ pixelbuf [offset + 1 ] = byte (g )
134
+ pixelbuf [offset + 2 ] = byte (r )
135
+ pixelbuf [offset + 3 ] = sdl .ALPHA_OPAQUE ;
136
+ }
0 commit comments