better to use the using existing method by passing the required []color.RGBA instead of the unsafe.Pointer
This commit is contained in:
parent
f529bc2897
commit
6c3961f894
2 changed files with 8 additions and 14 deletions
|
@ -2,6 +2,7 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"image/color"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
rl "github.com/gen2brain/raylib-go/raylib"
|
rl "github.com/gen2brain/raylib-go/raylib"
|
||||||
|
@ -30,12 +31,12 @@ func main() {
|
||||||
// WARNING: It's not recommended to use this technique for sprites animation,
|
// WARNING: It's not recommended to use this technique for sprites animation,
|
||||||
// use spritesheets instead, like illustrated in textures_sprite_anim example
|
// use spritesheets instead, like illustrated in textures_sprite_anim example
|
||||||
var texScarfyAnim rl.Texture2D = rl.LoadTextureFromImage(imScarfyAnim)
|
var texScarfyAnim rl.Texture2D = rl.LoadTextureFromImage(imScarfyAnim)
|
||||||
|
var texScarfyAnimSize int32 = imScarfyAnim.Width * imScarfyAnim.Height
|
||||||
|
|
||||||
var nextFrameDataOffset uint32 = 0 // Current byte offset to next frame in image.data
|
var nextFrameDataOffset uint32 = 0 // Current byte offset to next frame in image.data
|
||||||
|
var currentAnimFrame int32 = 0 // Current animation frame to load and draw
|
||||||
var currentAnimFrame int32 = 0 // Current animation frame to load and draw
|
var frameDelay int32 = 8 // Frame delay to switch between animation frames
|
||||||
var frameDelay int32 = 8 // Frame delay to switch between animation frames
|
var frameCounter int32 = 0 // General frames counter
|
||||||
var frameCounter int32 = 0 // General frames counter
|
|
||||||
|
|
||||||
rl.SetTargetFPS(60) // Set our game to run at 60 frames-per-second
|
rl.SetTargetFPS(60) // Set our game to run at 60 frames-per-second
|
||||||
|
|
||||||
|
@ -43,6 +44,7 @@ func main() {
|
||||||
for !rl.WindowShouldClose() { // Detect window close button or ESC key
|
for !rl.WindowShouldClose() { // Detect window close button or ESC key
|
||||||
// Update
|
// Update
|
||||||
frameCounter++
|
frameCounter++
|
||||||
|
|
||||||
if frameCounter >= frameDelay {
|
if frameCounter >= frameDelay {
|
||||||
// Move to next frame
|
// Move to next frame
|
||||||
// NOTE: If final frame is reached we return to first frame
|
// NOTE: If final frame is reached we return to first frame
|
||||||
|
@ -53,11 +55,11 @@ func main() {
|
||||||
|
|
||||||
// Get memory offset position for next frame data in image.data
|
// Get memory offset position for next frame data in image.data
|
||||||
nextFrameDataOffset = uint32(imScarfyAnim.Width * imScarfyAnim.Height * int32(4) * currentAnimFrame)
|
nextFrameDataOffset = uint32(imScarfyAnim.Width * imScarfyAnim.Height * int32(4) * currentAnimFrame)
|
||||||
|
|
||||||
// Update GPU texture data with next frame image data
|
// Update GPU texture data with next frame image data
|
||||||
// WARNING: Data size (frame size) and pixel format must match already created texture
|
// WARNING: Data size (frame size) and pixel format must match already created texture
|
||||||
// here we needed to make the Data as public
|
// here we needed to make the Data as public
|
||||||
rl.UpdateTextureUnsafe(texScarfyAnim, unsafe.Pointer(uintptr(imScarfyAnim.Data)+uintptr(nextFrameDataOffset)))
|
rl.UpdateTexture(texScarfyAnim,
|
||||||
|
unsafe.Slice((*color.RGBA)(unsafe.Pointer(uintptr(imScarfyAnim.Data)+uintptr(nextFrameDataOffset))), texScarfyAnimSize))
|
||||||
|
|
||||||
frameCounter = 0
|
frameCounter = 0
|
||||||
}
|
}
|
||||||
|
|
|
@ -246,14 +246,6 @@ func UpdateTexture(texture Texture2D, pixels []color.RGBA) {
|
||||||
C.UpdateTexture(*ctexture, cpixels)
|
C.UpdateTexture(*ctexture, cpixels)
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateTexture - Update GPU texture with new data
|
|
||||||
// NOTE: pixels data must match texture.format
|
|
||||||
func UpdateTextureUnsafe(texture Texture2D, pixels unsafe.Pointer) {
|
|
||||||
ctexture := texture.cptr()
|
|
||||||
cpixels := pixels
|
|
||||||
C.UpdateTexture(*ctexture, cpixels)
|
|
||||||
}
|
|
||||||
|
|
||||||
// UpdateTextureRec - Update GPU texture rectangle with new data
|
// UpdateTextureRec - Update GPU texture rectangle with new data
|
||||||
func UpdateTextureRec(texture Texture2D, rec Rectangle, pixels []color.RGBA) {
|
func UpdateTextureRec(texture Texture2D, rec Rectangle, pixels []color.RGBA) {
|
||||||
ctexture := texture.cptr()
|
ctexture := texture.cptr()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue