-
Notifications
You must be signed in to change notification settings - Fork 1
/
recorder.go
288 lines (230 loc) · 6.79 KB
/
recorder.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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package console
import (
"fmt"
"image"
"image/color"
"image/gif"
"image/png"
"os"
"github.com/veandco/go-sdl2/sdl"
)
const (
P1_BUTT_RIGHT = 0x4f
P1_BUTT_LEFT = 0x50
P1_BUTT_DOWN = 0x51
P1_BUTT_UP = 0x52
//Z,X / C,V / N,M
P1_BUTT_01 = 0x1d // Z
P1_BUTT_02 = 0x1b // X
P1_BUTT_03 = 0x6 // C
P1_BUTT_04 = 0x19 // V
P1_BUTT_05 = 0x11 // N
P1_BUTT_06 = 0x10 // M
)
type recorder struct {
frames []*sdl.Surface
palettes []Paletter
current int
frameSkip int
fps int
seconds int
totalFrames int
}
type Recorder interface {
AddFrame(surface *sdl.Surface, palette Paletter)
SaveVideo(filename string, scale int) error
SaveScreenshot(filename string, scale int) error
}
func NewRecorder(fps, secondsToRecord int) Recorder {
r := &recorder{
fps: fps,
seconds: secondsToRecord,
totalFrames: fps * secondsToRecord,
}
r.frames = make([]*sdl.Surface, r.totalFrames, r.totalFrames)
r.palettes = make([]Paletter, r.totalFrames, r.totalFrames)
return r
}
func (r *recorder) AddFrame(surface *sdl.Surface, palette Paletter) {
r.frameSkip++
// only save every 3rd frame @ 60fps
// 20 frames at 60 fps
// save every frame @ 30fps
saveEvery := 3
if r.fps == 30 {
saveEvery = 1
}
if r.frameSkip%saveEvery == 0 {
// save copy of current frame
newSurface, _ := surface.Convert(surface.Format, 0)
r.frames[r.current] = newSurface
// take a copy of the palette
r.palettes[r.current] = palette.PaletteCopy()
r.current++
if r.current > r.totalFrames-1 {
r.current = 0
}
}
}
func (r *recorder) SaveVideo(filename string, scale int) error {
fmt.Printf("Please wait, saving video\n")
// count used frames
totalFrames := 0
for i := 0; i < len(r.frames); i++ {
if r.frames[i] != nil {
totalFrames++
}
}
if totalFrames == 0 {
return fmt.Errorf("No frames to save to video")
}
var startFrame int
// if recording has never looped - start from first frame
if totalFrames < len(r.frames) {
startFrame = 0
} else {
// recording has looped so start from NEXT frame
startFrame = r.current + 1
if startFrame > len(r.frames)-1 {
startFrame = 0
}
}
sampledFrames := totalFrames
images := make([]*image.Paletted, sampledFrames, sampledFrames)
delays := make([]int, sampledFrames, sampledFrames)
//Delay Time (1/100ths of a second)
// Sampling at 60 fps
// - one frame every 16.666 milliseconds
// - one frame every 1.6666 1/100th of a second
// all good... except delays can only be an int in 1/100ths of a second..
// so we either delay to 1/100th and be too quick or 2/100th and be too slow
// Convert to a 20 fps GIF?
// Select every 3rd frame
// Delay = 5/100th :)
// Sampling at 30 fps
// - one frame every 33.333 milliseconds
// - one frame every 3.3333 1/100th of a second
// all good... except delays can only be an int in 1/100ths of a second..
// so we either delay to 1/100th and be too quick or 2/100th and be too slow
// Convert to a 30 fps GIF?
// Select every frame
// Delay = 3/100th :) (Close enough..)
delay := 5
if r.fps == 30 {
delay = 3
}
srcRect := &sdl.Rect{X: 0, Y: 0, W: r.frames[0].W, H: r.frames[0].H}
targetRect := &sdl.Rect{X: 0, Y: 0, W: r.frames[0].W * int32(scale), H: r.frames[0].H * int32(scale)}
target32Surface, err := sdl.CreateRGBSurface(0, srcRect.W, srcRect.H, 32, rmask, gmask, bmask, amask)
if err != nil {
return err
}
target32ScaledSurface, err := sdl.CreateRGBSurface(0, targetRect.W, targetRect.H, 32, rmask, gmask, bmask, amask)
if err != nil {
return err
}
framesProcessed := totalFrames
fr := startFrame
for framesProcessed > 0 {
delays[fr] = delay
frame := r.frames[fr]
palette := r.palettes[fr]
colors := make([]color.Color, len(palette.GetSDLColors()))
for i, c := range palette.GetSDLColors() {
colors[i] = color.RGBA{R: c.R, G: c.G, B: c.B, A: c.A}
}
// scale original frame to video size
err = frame.Blit(srcRect, target32Surface, srcRect)
if err != nil {
return err
}
err = target32Surface.BlitScaled(srcRect, target32ScaledSurface, targetRect)
if err != nil {
return err
}
img := image.NewPaletted(image.Rect(0, 0, int(target32ScaledSurface.W), int(target32ScaledSurface.H)), colors)
// copy framePixels to image pixels
pixels := target32ScaledSurface.Pixels()
w := int(target32ScaledSurface.W)
// TODO - improve perf
// this works but could be much faster
for i := 0; i < len(pixels); i += 4 {
// convert index i to x,y coords
x := (i % (w * 4)) / 4
y := (i - (x * 4)) / (w * 4)
a := pixels[i]
b := pixels[i+1]
g := pixels[i+2]
r := pixels[i+3]
// lookup color index
pixelColor := rgba{R: r, G: g, B: b, A: a}
colorID := palette.GetColorID(pixelColor)
img.SetColorIndex(x, y, uint8(colorID))
}
images[fr] = img
// increment index
fr++
framesProcessed--
if fr > totalFrames-1 {
fr = 0
}
}
f, _ := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE, 0600)
defer f.Close()
fmt.Printf("Saving video to %s\n", f.Name())
return gif.EncodeAll(f, &gif.GIF{
Image: images,
Delay: delays,
})
}
func (r *recorder) SaveScreenshot(filename string, scale int) error {
// screenshot uses last frame
lastFrame := r.current - 1
if lastFrame < 0 {
lastFrame = r.totalFrames - 1
}
sourceSurface := r.frames[lastFrame]
srcRect := &sdl.Rect{X: 0, Y: 0, W: sourceSurface.W, H: sourceSurface.H}
target32Surface, err := sdl.CreateRGBSurface(0, sourceSurface.W, sourceSurface.H, 32, rmask, gmask, bmask, amask)
if err != nil {
return err
}
defer target32Surface.Free()
// convert 8 bit palette image to 32 bit RGBA
err = sourceSurface.Blit(srcRect, target32Surface, srcRect)
if err != nil {
return err
}
// convert 32 bit RGBA to scaled 32 bit RGBA
target32ScaledRect := &sdl.Rect{X: 0, Y: 0, W: sourceSurface.W * int32(scale), H: sourceSurface.H * int32(scale)}
target32ScaledSurface, err := sdl.CreateRGBSurface(0, sourceSurface.W*int32(scale), sourceSurface.H*int32(scale), 32, rmask, gmask, bmask, amask)
if err != nil {
return err
}
err = target32Surface.BlitScaled(srcRect, target32ScaledSurface, target32ScaledRect)
if err != nil {
return err
}
imageRect := image.Rect(0, 0, int(target32ScaledRect.W), int(target32ScaledRect.H))
rgbaImage := image.NewRGBA(imageRect)
pixels := target32ScaledSurface.Pixels()
w := int(sourceSurface.W) * scale
// convert SDL surface to RGBA image
// process 4 bytes at a time
for i := 0; i < len(pixels); i += 4 {
// convert index i to x,y coords
x := (i % (w * 4)) / 4
y := (i - (x * 4)) / (w * 4)
a := pixels[i]
b := pixels[i+1]
g := pixels[i+2]
r := pixels[i+3]
c := color.RGBA{R: r, G: g, B: b, A: a}
rgbaImage.Set(x, y, c)
}
// save to out.gif
f, _ := os.OpenFile("out.png", os.O_WRONLY|os.O_CREATE, 0600)
defer f.Close()
fmt.Printf("Saving screenshot to %s\n", f.Name())
return png.Encode(f, rgbaImage)
}