Skip to content
This repository was archived by the owner on Jul 20, 2022. It is now read-only.

Commit ad93281

Browse files
committed
v0.0.1
1 parent 30173ab commit ad93281

File tree

5 files changed

+179
-1
lines changed

5 files changed

+179
-1
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
*.ttf
2+
*.cpp
3+
*.h
4+
*.hpp

README.md

+22-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,23 @@
11
# Pinwheel
2-
🍭 An awesome little fantasy computer designed to be simple.
2+
> 🍭 An awesome little fantasy computer designed to be simple.
3+
4+
Pinwheel is an all new fantasy computer, developed in Go and designed for ease of use and simplicity,
5+
and highly customizable where applicable (and wanted..!).
6+
It is currently in the VERY early prototype-ish stage, and isn't meant for normal use yet.
7+
Stay tuned though, I'm spending my entire holiday to work on this little thing as much I can.
8+
9+
# Planned Features
10+
- Customizable Palette of 64 COLORS (by program and/or user)
11+
- Easily scriptable
12+
- Simplicity
13+
- Joypad, keyboard and mouse input
14+
15+
# Documentation
16+
## Functions
17+
**Expected to change A LOT during development.**
18+
19+
- `Spin()` - Called every CPU cycle
20+
21+
- `termprint(text)` - Print text to the terminal
22+
- `vpoke(address, value)` - Write `value` to VRAM `address`
23+
- `plot(x, y, color)` - Place pixel at `x, y` with palette color number `color`

TODO.md

Whitespace-only changes.

main.go

+136
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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+
}

program.lua

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function Spin()
2+
--[[for i = 1000, 0, -1 do
3+
x = math.random(127)
4+
y = math.random(127)
5+
6+
offset = ( 128 * 4 * y ) + x * 4
7+
vpoke(offset + 0, math.random(255))
8+
vpoke(offset + 1, math.random(255))
9+
vpoke(offset + 2, math.random(255))
10+
vpoke(offset + 3, 255)
11+
end]]--
12+
-- Ignore above, pixel noise for initial testing
13+
14+
for i = 128, 0, -1 do
15+
plot(i, 64, i % 64)
16+
end
17+
end

0 commit comments

Comments
 (0)