/
crosshair.go
151 lines (133 loc) · 3.38 KB
/
crosshair.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
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
package main
import (
"fmt"
"github.com/veandco/go-sdl2/sdl"
"os"
"reflect"
"runtime"
"unsafe"
)
type Pixel uint32
// Creates a slice of Pixel from a raw pointer
func sliceFromPixelPtr(data unsafe.Pointer, length int) []Pixel {
var pixels []Pixel
sliceHeader := (*reflect.SliceHeader)(unsafe.Pointer(&pixels))
sliceHeader.Cap = int(length)
sliceHeader.Len = int(length)
sliceHeader.Data = uintptr(data)
return pixels
}
func lockTexture(tex *sdl.Texture, width int, height int) (pixels []Pixel, pitch int) {
var data unsafe.Pointer
err := tex.Lock(nil, &data, &pitch)
if err != nil {
fmt.Fprintf(os.Stderr, "tex.Lock: %v", err)
panic(err)
}
// Convert pitch units from byte to pixels
pitch /= 4
pixels = sliceFromPixelPtr(data, width*height)
return
}
// Clip x to half-open interval [a,b)
func clip(x int, a int, b int) int {
if x < a {
return a
}
if x >= b {
return b
}
return x
}
var escape *string
var counter int
// Generate some heap-allocated stuff
func generateGarbage() {
escape = new(string)
*escape = fmt.Sprintf("%v", counter)
}
// Draw cross-hairs through (x0,y0) onto the given pixel buffer.
func drawCross(pixels []Pixel, pitch int, width int, height int, x0 int, y0 int) {
x0 = clip(x0, 0, width)
y0 = clip(y0, 0, height)
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
pixels[y*pitch+x] = 0xFF0000 // red
}
}
for y := 0; y < height; y++ {
generateGarbage()
for x := 0; x < width; x++ {
var color Pixel
if y == y0 || x == x0 {
color = 0xFFFFFFFF // white
} else {
color = 0xFF000000 // black
}
pixels[y*pitch+x] = color
}
}
}
var winTitle string = "Crosshair"
var winWidth, winHeight int = 800, 600
func run() int {
// See discussion at https://github.com/golang/go/wiki/LockOSThread
runtime.LockOSThread()
defer runtime.UnlockOSThread()
// Create window
window, err := sdl.CreateWindow(winTitle, sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED,
winWidth, winHeight, sdl.WINDOW_SHOWN)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to create window: %v\n", err)
panic(err)
}
defer window.Destroy()
// Create renderer
width, height := window.GetSize()
renderer, err := sdl.CreateRenderer(window, -1, sdl.RENDERER_ACCELERATED|sdl.RENDERER_PRESENTVSYNC)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to create renderer: %v\n", err)
panic(err)
}
defer renderer.Destroy()
// Create texture
tex, err := renderer.CreateTexture(sdl.PIXELFORMAT_ARGB8888, sdl.TEXTUREACCESS_STREAMING, width, height)
if err != nil {
fmt.Fprintf(os.Stderr, "renderer.CreateTexture: %v\n", err)
panic(err)
}
defer tex.Destroy()
// Show crosshair animation. Quit when a key comes up.
var mouseX, mouseY int
for {
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
switch t := event.(type) {
case *sdl.QuitEvent:
return 0
case *sdl.MouseMotionEvent:
mouseX = int(t.X)
mouseY = int(t.Y)
case *sdl.KeyUpEvent:
sdl.PushEvent(&sdl.QuitEvent{Type: sdl.QUIT})
}
}
pixels, pitch := lockTexture(tex, width, height)
drawCross(pixels, pitch, width, height, mouseX, mouseY)
tex.Unlock()
err := renderer.Clear()
if err != nil {
fmt.Fprintf(os.Stderr, "renderer.Clear: %v", err)
panic(err)
}
renderer.Copy(tex, nil, nil)
if err != nil {
fmt.Fprintf(os.Stderr, "renderer.Copy: %v", err)
panic(err)
}
renderer.Present()
}
return 0
}
func main() {
run()
}