Add texture from image.Image example

This commit is contained in:
Milan Nikolic 2017-02-14 07:40:25 +01:00
parent 4f1b3206c5
commit 8fa575dce2
2 changed files with 66 additions and 0 deletions

View file

@ -0,0 +1,66 @@
package main
import (
"image/png"
"os"
"github.com/gen2brain/raylib-go/raylib"
)
func main() {
screenWidth := int32(800)
screenHeight := int32(450)
raylib.InitWindow(screenWidth, screenHeight, "raylib [textures] example - texture from image.Image")
r, err := os.Open("raylib_logo.png")
if err != nil {
raylib.TraceLog(raylib.LogError, err.Error())
}
defer r.Close()
img, err := png.Decode(r)
if err != nil {
raylib.TraceLog(raylib.LogError, err.Error())
}
// Get image size
size := img.Bounds().Size()
// Dynamic memory allocation to store pixels data (Color type)
pixels := make([]raylib.Color, size.X*size.Y)
for y := 0; y < size.Y; y++ {
for x := 0; x < size.X; x++ {
color := img.At(x, y)
r, g, b, a := color.RGBA()
pixels[x+y*size.Y] = raylib.NewColor(uint8(r), uint8(g), uint8(b), uint8(a))
}
}
// Load pixels data into an image structure and create texture
imEx := raylib.LoadImageEx(pixels, int32(size.X), int32(size.Y))
texture := raylib.LoadTextureFromImage(imEx)
// Unload CPU (RAM) image data
raylib.UnloadImage(imEx)
// Unload CPU (RAM) pixels data
pixels = nil
raylib.SetTargetFPS(60)
for !raylib.WindowShouldClose() {
raylib.BeginDrawing()
raylib.ClearBackground(raylib.RayWhite)
raylib.DrawTexture(texture, screenWidth/2-texture.Width/2, screenHeight/2-texture.Height/2, raylib.White)
raylib.DrawText("this IS a texture loaded from an image.Image!", 285, 370, 10, raylib.Gray)
raylib.EndDrawing()
}
raylib.UnloadTexture(texture)
raylib.CloseWindow()
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.7 KiB