/
graphics.go
464 lines (324 loc) · 11.4 KB
/
graphics.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
package allegro
/*
#cgo pkg-config: allegro-5.0
#include <allegro5/allegro.h>
*/
import "C"
import "unsafe"
import "reflect"
const (
// Bitmap flags
VIDEO_BITMAP = C.ALLEGRO_VIDEO_BITMAP
MEMORY_BITMAP = C.ALLEGRO_MEMORY_BITMAP
KEEP_BITMAP_FORMAT = C.ALLEGRO_KEEP_BITMAP_FORMAT
FORCE_LOCKING = C.ALLEGRO_FORCE_LOCKING
NO_PRESERVE_TEXTURE = C.ALLEGRO_NO_PRESERVE_TEXTURE
ALPHA_TEST = C.ALLEGRO_ALPHA_TEST
MIN_LINEAR = C.ALLEGRO_MIN_LINEAR
MAG_LINEAR = C.ALLEGRO_MAG_LINEAR
MIPMAP = C.ALLEGRO_MIPMAP
NO_PREMULTIPLIED_ALPHA = C.ALLEGRO_NO_PREMULTIPLIED_ALPHA
// Flip Stuff
FLIP_HORIZONTAL = C.ALLEGRO_FLIP_HORIZONTAL
FLIP_VERTICAL = C.ALLEGRO_FLIP_VERTICAL
// Blender Operations
ADD = C.ALLEGRO_ADD
DEST_MINUS_SRC = C.ALLEGRO_DEST_MINUS_SRC
SRC_MINUS_DEST = C.ALLEGRO_SRC_MINUS_DEST
ZERO = C.ALLEGRO_ZERO
ONE = C.ALLEGRO_ONE
ALPHA = C.ALLEGRO_ALPHA
INVERSE_ALPHA = C.ALLEGRO_INVERSE_ALPHA
// Pixel Formats
FORMAT_ANY = C.ALLEGRO_PIXEL_FORMAT_ANY
FORMAT_ANY_NO_ALPHA = C.ALLEGRO_PIXEL_FORMAT_ANY_NO_ALPHA
FORMAT_ANY_WITH_ALPHA = C.ALLEGRO_PIXEL_FORMAT_ANY_WITH_ALPHA
FORMAT_ANY_15_NO_ALPHA = C.ALLEGRO_PIXEL_FORMAT_ANY_15_NO_ALPHA
FORMAT_ANY_16_NO_ALPHA = C.ALLEGRO_PIXEL_FORMAT_ANY_16_NO_ALPHA
FORMAT_ANY_16_WITH_ALPHA = C.ALLEGRO_PIXEL_FORMAT_ANY_16_WITH_ALPHA
FORMAT_ANY_24_NO_ALPHA = C.ALLEGRO_PIXEL_FORMAT_ANY_24_NO_ALPHA
FORMAT_ANY_32_NO_ALPHA = C.ALLEGRO_PIXEL_FORMAT_ANY_32_NO_ALPHA
FORMAT_ANY_32_WITH_ALPHA = C.ALLEGRO_PIXEL_FORMAT_ANY_32_WITH_ALPHA
FORMAT_ARGB_8888 = C.ALLEGRO_PIXEL_FORMAT_ARGB_8888
FORMAT_RGBA_8888 = C.ALLEGRO_PIXEL_FORMAT_RGBA_8888
FORMAT_ARGB_4444 = C.ALLEGRO_PIXEL_FORMAT_ARGB_4444
FORMAT_RGB_888 = C.ALLEGRO_PIXEL_FORMAT_RGB_888
FORMAT_RGB_565 = C.ALLEGRO_PIXEL_FORMAT_RGB_565
FORMAT_RGB_555 = C.ALLEGRO_PIXEL_FORMAT_RGB_555
FORMAT_RGBA_5551 = C.ALLEGRO_PIXEL_FORMAT_RGBA_5551
FORMAT_ARGB_1555 = C.ALLEGRO_PIXEL_FORMAT_ARGB_1555
FORMAT_ABGR_8888 = C.ALLEGRO_PIXEL_FORMAT_ABGR_8888
FORMAT_XBGR_8888 = C.ALLEGRO_PIXEL_FORMAT_XBGR_8888
FORMAT_BGR_888 = C.ALLEGRO_PIXEL_FORMAT_BGR_888
FORMAT_BGR_565 = C.ALLEGRO_PIXEL_FORMAT_BGR_565
FORMAT_BGR_555 = C.ALLEGRO_PIXEL_FORMAT_BGR_555
FORMAT_RGBX_8888 = C.ALLEGRO_PIXEL_FORMAT_RGBX_8888
FORMAT_XRGB_8888 = C.ALLEGRO_PIXEL_FORMAT_XRGB_8888
FORMAT_ABGR_F32 = C.ALLEGRO_PIXEL_FORMAT_ABGR_F32
FORMAT_ABGR_8888_LE = C.ALLEGRO_PIXEL_FORMAT_ABGR_8888_LE
FORMAT_RGBA_4444 = C.ALLEGRO_PIXEL_FORMAT_RGBA_4444
// Bitmap lock flags
LOCK_READONLY = C.ALLEGRO_LOCK_READONLY
LOCK_WRITEONLY = C.ALLEGRO_LOCK_WRITEONLY
LOCK_READWRITE = C.ALLEGRO_LOCK_READWRITE
)
type Color C.ALLEGRO_COLOR
func CreateColor(r, g, b, a byte) Color {
return (Color)(C.al_map_rgba(C.uchar(r), C.uchar(g), C.uchar(b), C.uchar(a)))
}
func (c Color) GetRGBA() (byte, byte, byte, byte) {
var r, g, b, a C.uchar
C.al_unmap_rgba((C.ALLEGRO_COLOR)(c), &r, &g, &b, &a)
return byte(r), byte(g), byte(b), byte(a)
}
func (c Color) Clear() {
C.al_clear_to_color((C.ALLEGRO_COLOR)(c))
}
func (c Color) DrawPixel(x, y float32) {
C.al_draw_pixel(C.float(x), C.float(y), (C.ALLEGRO_COLOR)(c))
}
func (c Color) PutPixel(x, y int) {
C.al_put_pixel(C.int(x), C.int(y), (C.ALLEGRO_COLOR)(c))
}
func (c Color) PutBlendedPixel(x, y int) {
C.al_put_blended_pixel(C.int(x), C.int(y), (C.ALLEGRO_COLOR)(c))
}
type LockedRegion struct {
data *C.ALLEGRO_LOCKED_REGION
Width, Height int
}
func CreateLockedRegion(p *C.ALLEGRO_LOCKED_REGION, width, height int) LockedRegion {
return LockedRegion{p, width, height}
}
func (l *LockedRegion) GetRaw() *C.ALLEGRO_LOCKED_REGION {
return l.data
}
func (l *LockedRegion) GetData() []byte {
ptr := l.GetRaw()
length := l.Width * l.Height * int(ptr.pixel_size)
var slice []byte
// This make slice
sliceHeader := (*reflect.SliceHeader)((unsafe.Pointer(&slice)))
sliceHeader.Cap = length
sliceHeader.Len = length
sliceHeader.Data = uintptr(unsafe.Pointer(ptr.data))
return slice
}
func (l *LockedRegion) GetFormat() int {
return int(l.GetRaw().format)
}
func (l *LockedRegion) GetPitch() int {
return int(l.GetRaw().pitch)
}
func (l *LockedRegion) GetPixelSize() int {
return int(l.GetRaw().pixel_size)
}
func (b *Bitmap) Lock(format, flags int) LockedRegion {
w, h := b.GetDimensions()
ptr := C.al_lock_bitmap((*C.ALLEGRO_BITMAP)(b), C.int(format), C.int(flags))
return CreateLockedRegion(ptr, w, h)
}
func (b *Bitmap) LockRegion(x, y, w, h, format, flags int) LockedRegion {
ptr := C.al_lock_bitmap_region((*C.ALLEGRO_BITMAP)(b), C.int(x), C.int(y),
C.int(w), C.int(h), C.int(format), C.int(flags))
return CreateLockedRegion(ptr, w, h)
}
func (b *Bitmap) Unlock() {
C.al_unlock_bitmap((*C.ALLEGRO_BITMAP)(b))
}
func GetPixelSize(format int) int {
s := int(C.al_get_pixel_size(C.int(format)))
return s
}
func GetNewBitmapFlags() int {
f := int(C.al_get_new_bitmap_flags())
return f
}
func GetNewBitmapFormat() int {
f := int(C.al_get_new_bitmap_format())
return f
}
func SetNewBitmapFlags(flags int) {
C.al_set_new_bitmap_flags(C.int(flags))
}
func SetNewBitmapFormat(format int) {
C.al_set_new_bitmap_format(C.int(format))
}
type Bitmap C.ALLEGRO_BITMAP
func NewBitmap(w, h int) *Bitmap {
var b *Bitmap
b = (*Bitmap)(C.al_create_bitmap(C.int(w), C.int(h)))
return b
}
func GetTargetBitmap() *Bitmap {
var b *Bitmap
b = (*Bitmap)(C.al_get_target_bitmap())
return b
}
func (b *Bitmap) Destroy() {
C.al_destroy_bitmap((*C.ALLEGRO_BITMAP)(b))
}
func (b *Bitmap) CreateSubBitmap(x, y, w, h int) *Bitmap {
var sub *Bitmap
sub = (*Bitmap)(C.al_create_sub_bitmap((*C.ALLEGRO_BITMAP)(b),
C.int(x), C.int(y), C.int(w), C.int(h)))
return sub
}
func (b *Bitmap) Clone() *Bitmap {
var n *Bitmap
n = (*Bitmap)(C.al_clone_bitmap((*C.ALLEGRO_BITMAP)(b)))
return n
}
func (b *Bitmap) GetDimensions() (int, int) {
ptr := (*C.ALLEGRO_BITMAP)(b)
var w, h C.int
w = C.al_get_bitmap_width(ptr)
h = C.al_get_bitmap_height(ptr)
return int(w), int(h)
}
func (b *Bitmap) GetPixel(x, y int) Color {
var c Color
c = (Color)(C.al_get_pixel((*C.ALLEGRO_BITMAP)(b), C.int(x), C.int(y)))
return c
}
func (b *Bitmap) IsCompatible() bool {
var v bool
v = bool(C.al_is_compatible_bitmap((*C.ALLEGRO_BITMAP)(b)))
return v
}
func (b *Bitmap) IsSubBitmap() bool {
var v bool
v = bool(C.al_is_sub_bitmap((*C.ALLEGRO_BITMAP)(b)))
return v
}
func (b *Bitmap) GetParentBitmap() *Bitmap {
var bmp *Bitmap
bmp = (*Bitmap)(C.al_get_parent_bitmap((*C.ALLEGRO_BITMAP)(b)))
return bmp
}
func (b *Bitmap) Draw(dx, dy float32, flags int) {
C.al_draw_bitmap((*C.ALLEGRO_BITMAP)(b), C.float(dx), C.float(dy), C.int(flags))
}
func (b *Bitmap) DrawTinted(dx, dy float32, flags int, color Color) {
C.al_draw_tinted_bitmap((*C.ALLEGRO_BITMAP)(b),
(C.ALLEGRO_COLOR)(color), C.float(dx), C.float(dy), C.int(flags))
}
func (b *Bitmap) DrawRegion(sx, sy, sw, sh, dx, dy float32, flags int) {
C.al_draw_bitmap_region((*C.ALLEGRO_BITMAP)(b), C.float(sx), C.float(sy),
C.float(sw), C.float(sh), C.float(dx), C.float(dy), C.int(flags))
}
func (b *Bitmap) DrawTintedRegion(sx, sy, sw, sh, dx, dy float32, flags int, color Color) {
C.al_draw_tinted_bitmap_region((*C.ALLEGRO_BITMAP)(b), (C.ALLEGRO_COLOR)(color),
C.float(sx), C.float(sy),
C.float(sw), C.float(sh), C.float(dx), C.float(dy), C.int(flags))
}
func (b *Bitmap) DrawRotated(cx, cy, dx, dy, angle float32, flags int) {
C.al_draw_rotated_bitmap((*C.ALLEGRO_BITMAP)(b),
C.float(cx), C.float(cy), C.float(dx), C.float(dy),
C.float(angle), C.int(flags))
}
func (b *Bitmap) DrawTintedRotated(cx, cy, dx, dy, angle float32, flags int, color Color) {
C.al_draw_tinted_rotated_bitmap((*C.ALLEGRO_BITMAP)(b), (C.ALLEGRO_COLOR)(color),
C.float(cx), C.float(cy),
C.float(dx), C.float(dy), C.float(angle), C.int(flags))
}
func (b *Bitmap) DrawScaledRotated(cx, cy, dx, dy, xscale, yscale, angle float32, flags int) {
C.al_draw_scaled_rotated_bitmap((*C.ALLEGRO_BITMAP)(b),
C.float(cx), C.float(cy), C.float(dx), C.float(dy),
C.float(xscale), C.float(yscale), C.float(angle), C.int(flags))
}
func (b *Bitmap) DrawTintedScaledRotated(cx, cy, dx, dy, xscale, yscale, angle float32,
flags int, color Color) {
C.al_draw_tinted_scaled_rotated_bitmap((*C.ALLEGRO_BITMAP)(b),
(C.ALLEGRO_COLOR)(color), C.float(cx), C.float(cy),
C.float(dx), C.float(dy), C.float(xscale), C.float(yscale), C.float(angle),
C.int(flags))
}
func (b *Bitmap) DrawTintedScaledRotatedRegion(sx, sy, sw, sh, cx, cy, dx, dy, xscale, yscale,
angle float32, flags int, color Color) {
C.al_draw_tinted_scaled_rotated_bitmap_region((*C.ALLEGRO_BITMAP)(b),
C.float(sx), C.float(sy),
C.float(sw), C.float(sh), (C.ALLEGRO_COLOR)(color), C.float(cx), C.float(cy),
C.float(dx), C.float(dy), C.float(xscale), C.float(yscale), C.float(angle),
C.int(flags))
}
func (b *Bitmap) DrawScaled(sx, sy, sw, sh, cx, cy, cw, ch float32, flags int) {
C.al_draw_scaled_bitmap((*C.ALLEGRO_BITMAP)(b),
C.float(sx), C.float(sy), C.float(sw), C.float(sh),
C.float(cx), C.float(cy), C.float(cw), C.float(ch), C.int(flags))
}
func (b *Bitmap) DrawTintedScaled(sx, sy, sw, sh, cx, cy, cw, ch float32, flags int, color Color) {
C.al_draw_tinted_scaled_bitmap((*C.ALLEGRO_BITMAP)(b), (C.ALLEGRO_COLOR)(color),
C.float(sx), C.float(sy), C.float(sw), C.float(sh),
C.float(cx), C.float(cy), C.float(cw), C.float(ch), C.int(flags))
}
func (b *Bitmap) SetTargetBitmap() {
C.al_set_target_bitmap((*C.ALLEGRO_BITMAP)(b))
}
func (d *Display) SetTargetBackbuffer() {
C.al_set_target_backbuffer((*C.ALLEGRO_DISPLAY)(d))
}
func GetCurrentDisplay() *Display {
var d *Display
d = (*Display)(C.al_get_current_display())
return d
}
func GetBlender() (int, int, int) {
var a, b, c C.int
C.al_get_blender(&a, &b, &c)
return int(a), int(b), int(c)
}
func SetBlender(op, src, dst int) {
C.al_set_blender(C.int(op), C.int(src), C.int(dst))
}
func GetClippingRectangle() (int, int, int, int) {
var x, y, w, h C.int
C.al_get_clipping_rectangle(&x, &y, &w, &h)
return int(x), int(y), int(w), int(h)
}
func SetClippingRectangle(x, y, w, h int) {
C.al_set_clipping_rectangle(C.int(x), C.int(y), C.int(w), C.int(h))
}
func ResetClippingRectangle() {
C.al_reset_clipping_rectangle()
}
func (b *Bitmap) ConvertMaskToAlpha(color Color) {
C.al_convert_mask_to_alpha((*C.ALLEGRO_BITMAP)(b), (C.ALLEGRO_COLOR)(color))
}
func HoldBitmapDrawing(hold bool) {
C.al_hold_bitmap_drawing(C.bool(hold))
}
func IsBitmapDrawingHeld() bool {
var b bool
b = bool(C.al_is_bitmap_drawing_held())
return b
}
func LoadBitmap(fname string) *Bitmap {
cfname := C.CString(fname)
defer C.free(unsafe.Pointer(cfname))
var b *Bitmap
b = (*Bitmap)(C.al_load_bitmap(cfname))
return b
}
func (b *Bitmap) Save(fname string) {
cfname := C.CString(fname)
defer C.free(unsafe.Pointer(cfname))
C.al_save_bitmap(cfname, (*C.ALLEGRO_BITMAP)(b))
}
/*
// Can't see a way to do this (various problems over closures and jazz)
type BitmapLoader interface {
LoadBitmap(string) *Bitmap
GetExtensions() []string
}
type BitmapSaver interface {
SaveBitmap(string, *Bitmap) bool
GetExtensions() []string
}
func RegisterBitmapLoader(BitmapLoader loader) {
}
//export goLoaderCallback
func goLoaderCallback(p unsafe.Pointer, ) {
(*(*func(string) C.ALLEGRO_BITMAP))
}
*/