forked from gen2brain/raylib-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
textures.go
582 lines (502 loc) · 17.2 KB
/
textures.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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
package rl
/*
#include "raylib.h"
#include <stdlib.h>
*/
import "C"
import (
"image"
"unsafe"
)
// cptr returns C pointer
func (i *Image) cptr() *C.Image {
return (*C.Image)(unsafe.Pointer(i))
}
// ToImage converts a Image to Go image.Image
func (i *Image) ToImage() image.Image {
img := image.NewRGBA(image.Rect(0, 0, int(i.Width), int(i.Height)))
// Get pixel data from image (RGBA 32bit)
cimg := i.cptr()
ret := C.GetImageData(*cimg)
pixels := (*[1 << 24]uint8)(unsafe.Pointer(ret))[0 : i.Width*i.Height*4]
img.Pix = pixels
return img
}
// cptr returns C pointer
func (t *Texture2D) cptr() *C.Texture2D {
return (*C.Texture2D)(unsafe.Pointer(t))
}
// cptr returns C pointer
func (r *RenderTexture2D) cptr() *C.RenderTexture2D {
return (*C.RenderTexture2D)(unsafe.Pointer(r))
}
// LoadImage - Load an image into CPU memory (RAM)
func LoadImage(fileName string) *Image {
cfileName := C.CString(fileName)
defer C.free(unsafe.Pointer(cfileName))
ret := C.LoadImage(cfileName)
v := newImageFromPointer(unsafe.Pointer(&ret))
return v
}
// LoadImageEx - Load image data from Color array data (RGBA - 32bit)
func LoadImageEx(pixels []Color, width, height int32) *Image {
cpixels := pixels[0].cptr()
cwidth := (C.int)(width)
cheight := (C.int)(height)
ret := C.LoadImageEx(cpixels, cwidth, cheight)
v := newImageFromPointer(unsafe.Pointer(&ret))
return v
}
// LoadImagePro - Load image from raw data with parameters
func LoadImagePro(data []byte, width, height int32, format PixelFormat) *Image {
cdata := unsafe.Pointer(&data[0])
cwidth := (C.int)(width)
cheight := (C.int)(height)
cformat := (C.int)(format)
ret := C.LoadImagePro(cdata, cwidth, cheight, cformat)
v := newImageFromPointer(unsafe.Pointer(&ret))
return v
}
// LoadImageRaw - Load image data from RAW file
func LoadImageRaw(fileName string, width, height int32, format PixelFormat, headerSize int32) *Image {
cfileName := C.CString(fileName)
defer C.free(unsafe.Pointer(cfileName))
cwidth := (C.int)(width)
cheight := (C.int)(height)
cformat := (C.int)(format)
cheaderSize := (C.int)(headerSize)
ret := C.LoadImageRaw(cfileName, cwidth, cheight, cformat, cheaderSize)
v := newImageFromPointer(unsafe.Pointer(&ret))
return v
}
// LoadTexture - Load an image as texture into GPU memory
func LoadTexture(fileName string) Texture2D {
cfileName := C.CString(fileName)
defer C.free(unsafe.Pointer(cfileName))
ret := C.LoadTexture(cfileName)
v := newTexture2DFromPointer(unsafe.Pointer(&ret))
return v
}
// LoadTextureFromImage - Load a texture from image data
func LoadTextureFromImage(image *Image) Texture2D {
cimage := image.cptr()
ret := C.LoadTextureFromImage(*cimage)
v := newTexture2DFromPointer(unsafe.Pointer(&ret))
return v
}
// LoadRenderTexture - Load a texture to be used for rendering
func LoadRenderTexture(width, height int32) RenderTexture2D {
cwidth := (C.int)(width)
cheight := (C.int)(height)
ret := C.LoadRenderTexture(cwidth, cheight)
v := newRenderTexture2DFromPointer(unsafe.Pointer(&ret))
return v
}
// UnloadImage - Unload image from CPU memory (RAM)
func UnloadImage(image *Image) {
cimage := image.cptr()
C.UnloadImage(*cimage)
}
// UnloadTexture - Unload texture from GPU memory
func UnloadTexture(texture Texture2D) {
ctexture := texture.cptr()
C.UnloadTexture(*ctexture)
}
// UnloadRenderTexture - Unload render texture from GPU memory
func UnloadRenderTexture(target RenderTexture2D) {
ctarget := target.cptr()
C.UnloadRenderTexture(*ctarget)
}
// GetImageData - Get pixel data from image as a Color slice
func GetImageData(img *Image) []Color {
cimg := img.cptr()
ret := C.GetImageData(*cimg)
return (*[1 << 24]Color)(unsafe.Pointer(ret))[0 : img.Width*img.Height]
}
// GetImageDataNormalized - Get pixel data from image as Vector4 slice (float normalized)
func GetImageDataNormalized(img *Image) []Vector4 {
cimg := img.cptr()
ret := C.GetImageDataNormalized(*cimg)
return (*[1 << 24]Vector4)(unsafe.Pointer(ret))[0 : img.Width*img.Height]
}
// GetPixelDataSize - Get pixel data size in bytes (image or texture)
func GetPixelDataSize(width, height, format int32) int32 {
cwidth := (C.int)(width)
cheight := (C.int)(height)
cformat := (C.int)(format)
ret := C.GetPixelDataSize(cwidth, cheight, cformat)
return int32(ret)
}
// GetTextureData - Get pixel data from GPU texture and return an Image
func GetTextureData(texture Texture2D) *Image {
ctexture := texture.cptr()
ret := C.GetTextureData(*ctexture)
v := newImageFromPointer(unsafe.Pointer(&ret))
return v
}
// UpdateTexture - Update GPU texture with new data
func UpdateTexture(texture Texture2D, pixels []Color) {
ctexture := texture.cptr()
cpixels := unsafe.Pointer(&pixels[0])
C.UpdateTexture(*ctexture, cpixels)
}
// ExportImage - Export image as a PNG file
func ExportImage(image Image, name string) {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
cimage := image.cptr()
C.ExportImage(*cimage, cname)
}
// ImageToPOT - Convert image to POT (power-of-two)
func ImageToPOT(image *Image, fillColor Color) {
cimage := image.cptr()
cfillColor := fillColor.cptr()
C.ImageToPOT(cimage, *cfillColor)
}
// ImageFormat - Convert image data to desired format
func ImageFormat(image *Image, newFormat PixelFormat) {
cimage := image.cptr()
cnewFormat := (C.int)(newFormat)
C.ImageFormat(cimage, cnewFormat)
}
// ImageAlphaMask - Apply alpha mask to image
func ImageAlphaMask(image, alphaMask *Image) {
cimage := image.cptr()
calphaMask := alphaMask.cptr()
C.ImageAlphaMask(cimage, *calphaMask)
}
// ImageAlphaClear - Apply alpha mask to image
func ImageAlphaClear(image *Image, color Color, threshold float32) {
cimage := image.cptr()
ccolor := color.cptr()
cthreshold := (C.float)(threshold)
C.ImageAlphaClear(cimage, *ccolor, cthreshold)
}
// ImageAlphaCrop - Crop image depending on alpha value
func ImageAlphaCrop(image *Image, threshold float32) {
cimage := image.cptr()
cthreshold := (C.float)(threshold)
C.ImageAlphaCrop(cimage, cthreshold)
}
// ImageAlphaPremultiply - Premultiply alpha channel
func ImageAlphaPremultiply(image *Image) {
cimage := image.cptr()
C.ImageAlphaPremultiply(cimage)
}
// ImageDither - Dither image data to 16bpp or lower (Floyd-Steinberg dithering)
func ImageDither(image *Image, rBpp, gBpp, bBpp, aBpp int32) {
cimage := image.cptr()
crBpp := (C.int)(rBpp)
cgBpp := (C.int)(gBpp)
cbBpp := (C.int)(bBpp)
caBpp := (C.int)(aBpp)
C.ImageDither(cimage, crBpp, cgBpp, cbBpp, caBpp)
}
// ImageCopy - Create an image duplicate (useful for transformations)
func ImageCopy(image *Image) *Image {
cimage := image.cptr()
ret := C.ImageCopy(*cimage)
v := newImageFromPointer(unsafe.Pointer(&ret))
return v
}
// ImageCrop - Crop an image to a defined rectangle
func ImageCrop(image *Image, crop Rectangle) {
cimage := image.cptr()
ccrop := crop.cptr()
C.ImageCrop(cimage, *ccrop)
}
// ImageResize - Resize an image (bilinear filtering)
func ImageResize(image *Image, newWidth, newHeight int32) {
cimage := image.cptr()
cnewWidth := (C.int)(newWidth)
cnewHeight := (C.int)(newHeight)
C.ImageResize(cimage, cnewWidth, cnewHeight)
}
// ImageResizeNN - Resize an image (Nearest-Neighbor scaling algorithm)
func ImageResizeNN(image *Image, newWidth, newHeight int32) {
cimage := image.cptr()
cnewWidth := (C.int)(newWidth)
cnewHeight := (C.int)(newHeight)
C.ImageResizeNN(cimage, cnewWidth, cnewHeight)
}
// ImageResizeCanvas - Resize canvas and fill with color
func ImageResizeCanvas(image *Image, newWidth, newHeight, offsetX, offsetY int32, color Color) {
cimage := image.cptr()
cnewWidth := (C.int)(newWidth)
cnewHeight := (C.int)(newHeight)
coffsetX := (C.int)(offsetX)
coffsetY := (C.int)(offsetY)
ccolor := color.cptr()
C.ImageResizeCanvas(cimage, cnewWidth, cnewHeight, coffsetX, coffsetY, *ccolor)
}
// ImageMipmaps - Generate all mipmap levels for a provided image
func ImageMipmaps(image *Image) {
cimage := image.cptr()
C.ImageMipmaps(cimage)
}
// ImageText - Create an image from text (default font)
func ImageText(text string, fontSize int32, color Color) *Image {
ctext := C.CString(text)
defer C.free(unsafe.Pointer(ctext))
cfontSize := (C.int)(fontSize)
ccolor := color.cptr()
ret := C.ImageText(ctext, cfontSize, *ccolor)
v := newImageFromPointer(unsafe.Pointer(&ret))
return v
}
// ImageTextEx - Create an image from text (custom sprite font)
func ImageTextEx(font Font, text string, fontSize, spacing float32, tint Color) *Image {
cfont := font.cptr()
ctext := C.CString(text)
defer C.free(unsafe.Pointer(ctext))
cfontSize := (C.float)(fontSize)
cspacing := (C.float)(spacing)
ctint := tint.cptr()
ret := C.ImageTextEx(*cfont, ctext, cfontSize, cspacing, *ctint)
v := newImageFromPointer(unsafe.Pointer(&ret))
return v
}
// ImageDraw - Draw a source image within a destination image
func ImageDraw(dst, src *Image, srcRec, dstRec Rectangle, tint Color) {
cdst := dst.cptr()
csrc := src.cptr()
csrcRec := srcRec.cptr()
cdstRec := dstRec.cptr()
ctint := tint.cptr()
C.ImageDraw(cdst, *csrc, *csrcRec, *cdstRec, *ctint)
}
// ImageDrawRectangle - Draw rectangle within an image
func ImageDrawRectangle(dst *Image, x, y, width, height int32, color Color) {
cdst := dst.cptr()
cx := (C.int)(x)
cy := (C.int)(y)
cwidth := (C.int)(width)
cheight := (C.int)(height)
ccolor := color.cptr()
C.ImageDrawRectangle(cdst, cx, cy, cwidth, cheight, *ccolor)
}
// ImageDrawRectangleLines - Draw rectangle lines within an image
func ImageDrawRectangleLines(dst *Image, rec Rectangle, thick int, color Color) {
cdst := dst.cptr()
crec := rec.cptr()
cthick := (C.int)(thick)
ccolor := color.cptr()
C.ImageDrawRectangleLines(cdst, *crec, cthick, *ccolor)
}
// ImageDrawText - Draw text (default font) within an image (destination)
func ImageDrawText(dst *Image, position Vector2, text string, fontSize int32, color Color) {
cdst := dst.cptr()
cposition := position.cptr()
ctext := C.CString(text)
defer C.free(unsafe.Pointer(ctext))
cfontSize := (C.int)(fontSize)
ccolor := color.cptr()
C.ImageDrawText(cdst, *cposition, ctext, cfontSize, *ccolor)
}
// ImageDrawTextEx - Draw text (custom sprite font) within an image (destination)
func ImageDrawTextEx(dst *Image, position Vector2, font Font, text string, fontSize, spacing float32, color Color) {
cdst := dst.cptr()
cposition := position.cptr()
cfont := font.cptr()
ctext := C.CString(text)
defer C.free(unsafe.Pointer(ctext))
cfontSize := (C.float)(fontSize)
cspacing := (C.float)(spacing)
ccolor := color.cptr()
C.ImageDrawTextEx(cdst, *cposition, *cfont, ctext, cfontSize, cspacing, *ccolor)
}
// ImageFlipVertical - Flip image vertically
func ImageFlipVertical(image *Image) {
cimage := image.cptr()
C.ImageFlipVertical(cimage)
}
// ImageFlipHorizontal - Flip image horizontally
func ImageFlipHorizontal(image *Image) {
cimage := image.cptr()
C.ImageFlipHorizontal(cimage)
}
// ImageRotateCW - Rotate image clockwise 90deg
func ImageRotateCW(image *Image) {
cimage := image.cptr()
C.ImageRotateCW(cimage)
}
// ImageRotateCCW - Rotate image counter-clockwise 90deg
func ImageRotateCCW(image *Image) {
cimage := image.cptr()
C.ImageRotateCCW(cimage)
}
// ImageColorTint - Modify image color: tint
func ImageColorTint(image *Image, color Color) {
cimage := image.cptr()
ccolor := color.cptr()
C.ImageColorTint(cimage, *ccolor)
}
// ImageColorInvert - Modify image color: invert
func ImageColorInvert(image *Image) {
cimage := image.cptr()
C.ImageColorInvert(cimage)
}
// ImageColorGrayscale - Modify image color: grayscale
func ImageColorGrayscale(image *Image) {
cimage := image.cptr()
C.ImageColorGrayscale(cimage)
}
// ImageColorContrast - Modify image color: contrast (-100 to 100)
func ImageColorContrast(image *Image, contrast float32) {
cimage := image.cptr()
ccontrast := (C.float)(contrast)
C.ImageColorContrast(cimage, ccontrast)
}
// ImageColorBrightness - Modify image color: brightness (-255 to 255)
func ImageColorBrightness(image *Image, brightness int32) {
cimage := image.cptr()
cbrightness := (C.int)(brightness)
C.ImageColorBrightness(cimage, cbrightness)
}
// ImageColorReplace - Modify image color: replace color
func ImageColorReplace(image *Image, color, replace Color) {
cimage := image.cptr()
ccolor := color.cptr()
creplace := replace.cptr()
C.ImageColorReplace(cimage, *ccolor, *creplace)
}
// GenImageColor - Generate image: plain color
func GenImageColor(width, height int, color Color) *Image {
cwidth := (C.int)(width)
cheight := (C.int)(height)
ccolor := color.cptr()
ret := C.GenImageColor(cwidth, cheight, *ccolor)
v := newImageFromPointer(unsafe.Pointer(&ret))
return v
}
// GenImageGradientV - Generate image: vertical gradient
func GenImageGradientV(width, height int, top, bottom Color) *Image {
cwidth := (C.int)(width)
cheight := (C.int)(height)
ctop := top.cptr()
cbottom := bottom.cptr()
ret := C.GenImageGradientV(cwidth, cheight, *ctop, *cbottom)
v := newImageFromPointer(unsafe.Pointer(&ret))
return v
}
// GenImageGradientH - Generate image: horizontal gradient
func GenImageGradientH(width, height int, left, right Color) *Image {
cwidth := (C.int)(width)
cheight := (C.int)(height)
cleft := left.cptr()
cright := right.cptr()
ret := C.GenImageGradientH(cwidth, cheight, *cleft, *cright)
v := newImageFromPointer(unsafe.Pointer(&ret))
return v
}
// GenImageGradientRadial - Generate image: radial gradient
func GenImageGradientRadial(width, height int, density float32, inner, outer Color) *Image {
cwidth := (C.int)(width)
cheight := (C.int)(height)
cdensity := (C.float)(density)
cinner := inner.cptr()
couter := outer.cptr()
ret := C.GenImageGradientRadial(cwidth, cheight, cdensity, *cinner, *couter)
v := newImageFromPointer(unsafe.Pointer(&ret))
return v
}
// GenImageChecked - Generate image: checked
func GenImageChecked(width, height, checksX, checksY int, col1, col2 Color) *Image {
cwidth := (C.int)(width)
cheight := (C.int)(height)
cchecksX := (C.int)(checksX)
cchecksY := (C.int)(checksY)
ccol1 := col1.cptr()
ccol2 := col2.cptr()
ret := C.GenImageChecked(cwidth, cheight, cchecksX, cchecksY, *ccol1, *ccol2)
v := newImageFromPointer(unsafe.Pointer(&ret))
return v
}
// GenImageWhiteNoise - Generate image: white noise
func GenImageWhiteNoise(width, height int, factor float32) *Image {
cwidth := (C.int)(width)
cheight := (C.int)(height)
cfactor := (C.float)(factor)
ret := C.GenImageWhiteNoise(cwidth, cheight, cfactor)
v := newImageFromPointer(unsafe.Pointer(&ret))
return v
}
// GenImagePerlinNoise - Generate image: perlin noise
func GenImagePerlinNoise(width, height, offsetX, offsetY int, scale float32) *Image {
cwidth := (C.int)(width)
cheight := (C.int)(height)
coffsetX := (C.int)(offsetX)
coffsetY := (C.int)(offsetY)
cscale := (C.float)(scale)
ret := C.GenImagePerlinNoise(cwidth, cheight, coffsetX, coffsetY, cscale)
v := newImageFromPointer(unsafe.Pointer(&ret))
return v
}
// GenImageCellular - Generate image: cellular algorithm. Bigger tileSize means bigger cells
func GenImageCellular(width, height, tileSize int) *Image {
cwidth := (C.int)(width)
cheight := (C.int)(height)
ctileSize := (C.int)(tileSize)
ret := C.GenImageCellular(cwidth, cheight, ctileSize)
v := newImageFromPointer(unsafe.Pointer(&ret))
return v
}
// GenTextureMipmaps - Generate GPU mipmaps for a texture
func GenTextureMipmaps(texture *Texture2D) {
ctexture := texture.cptr()
C.GenTextureMipmaps(ctexture)
}
// SetTextureFilter - Set texture scaling filter mode
func SetTextureFilter(texture Texture2D, filterMode TextureFilterMode) {
ctexture := texture.cptr()
cfilterMode := (C.int)(filterMode)
C.SetTextureFilter(*ctexture, cfilterMode)
}
// SetTextureWrap - Set texture wrapping mode
func SetTextureWrap(texture Texture2D, wrapMode TextureWrapMode) {
ctexture := texture.cptr()
cwrapMode := (C.int)(wrapMode)
C.SetTextureWrap(*ctexture, cwrapMode)
}
// DrawTexture - Draw a Texture2D
func DrawTexture(texture Texture2D, posX int32, posY int32, tint Color) {
ctexture := texture.cptr()
cposX := (C.int)(posX)
cposY := (C.int)(posY)
ctint := tint.cptr()
C.DrawTexture(*ctexture, cposX, cposY, *ctint)
}
// DrawTextureV - Draw a Texture2D with position defined as Vector2
func DrawTextureV(texture Texture2D, position Vector2, tint Color) {
ctexture := texture.cptr()
cposition := position.cptr()
ctint := tint.cptr()
C.DrawTextureV(*ctexture, *cposition, *ctint)
}
// DrawTextureEx - Draw a Texture2D with extended parameters
func DrawTextureEx(texture Texture2D, position Vector2, rotation, scale float32, tint Color) {
ctexture := texture.cptr()
cposition := position.cptr()
crotation := (C.float)(rotation)
cscale := (C.float)(scale)
ctint := tint.cptr()
C.DrawTextureEx(*ctexture, *cposition, crotation, cscale, *ctint)
}
// DrawTextureRec - Draw a part of a texture defined by a rectangle
func DrawTextureRec(texture Texture2D, sourceRec Rectangle, position Vector2, tint Color) {
ctexture := texture.cptr()
csourceRec := sourceRec.cptr()
cposition := position.cptr()
ctint := tint.cptr()
C.DrawTextureRec(*ctexture, *csourceRec, *cposition, *ctint)
}
// DrawTexturePro - Draw a part of a texture defined by a rectangle with 'pro' parameters
func DrawTexturePro(texture Texture2D, sourceRec, destRec Rectangle, origin Vector2, rotation float32, tint Color) {
ctexture := texture.cptr()
csourceRec := sourceRec.cptr()
cdestRec := destRec.cptr()
corigin := origin.cptr()
crotation := (C.float)(rotation)
ctint := tint.cptr()
C.DrawTexturePro(*ctexture, *csourceRec, *cdestRec, *corigin, crotation, *ctint)
}