This commit is contained in:
Konstantin8105 2022-11-22 18:50:35 +03:00
parent 0bec81c656
commit fe6d2c0ed3
190 changed files with 104835 additions and 5 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 379 KiB

View file

@ -0,0 +1,49 @@
package main
import (
"github.com/gen2brain/raylib-go/raylib"
)
func main() {
screenWidth := int32(800)
screenHeight := int32(450)
rl.InitWindow(screenWidth, screenHeight, "raylib [textures] example - texture loading and drawing")
// NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
cat := rl.LoadImage("cat.png") // Load image in CPU memory (RAM)
rl.ImageCrop(cat, rl.NewRectangle(100, 10, 280, 380)) // Crop an image piece
rl.ImageFlipHorizontal(cat) // Flip cropped image horizontally
rl.ImageResize(cat, 150, 200) // Resize flipped-cropped image
parrots := rl.LoadImage("parrots.png") // Load image in CPU memory (RAM)
// Draw one image over the other with a scaling of 1.5f
rl.ImageDraw(parrots, cat, rl.NewRectangle(0, 0, float32(cat.Width), float32(cat.Height)), rl.NewRectangle(30, 40, float32(cat.Width)*1.5, float32(cat.Height)*1.5), rl.White)
rl.ImageCrop(parrots, rl.NewRectangle(0, 50, float32(parrots.Width), float32(parrots.Height-100))) // Crop resulting image
rl.UnloadImage(cat) // Unload image from RAM
texture := rl.LoadTextureFromImage(parrots) // Image converted to texture, uploaded to GPU memory (VRAM)
rl.UnloadImage(parrots) // Once image has been converted to texture and uploaded to VRAM, it can be unloaded from RAM
rl.SetTargetFPS(60)
for !rl.WindowShouldClose() {
rl.BeginDrawing()
rl.ClearBackground(rl.RayWhite)
rl.DrawTexture(texture, screenWidth/2-texture.Width/2, screenHeight/2-texture.Height/2-40, rl.White)
rl.DrawRectangleLines(screenWidth/2-texture.Width/2, screenHeight/2-texture.Height/2-40, texture.Width, texture.Height, rl.DarkGray)
rl.DrawText("We are drawing only one texture from various images composed!", 240, 350, 10, rl.DarkGray)
rl.DrawText("Source images have been cropped, scaled, flipped and copied one over the other.", 190, 370, 10, rl.DarkGray)
rl.EndDrawing()
}
rl.UnloadTexture(texture)
rl.CloseWindow()
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 288 KiB

View file

@ -0,0 +1,88 @@
package main
import (
rl "github.com/gen2brain/raylib-go/raylib"
)
const numTextures = 7
func main() {
screenWidth := 800
screenHeight := 450
rl.InitWindow(int32(screenWidth), int32(screenHeight), "raylib [textures] example - procedural images generation")
verticalGradient := rl.GenImageGradientV(screenWidth, screenHeight, rl.Red, rl.Blue)
horizontalGradient := rl.GenImageGradientH(screenWidth, screenHeight, rl.Red, rl.Blue)
radialGradient := rl.GenImageGradientRadial(screenWidth, screenHeight, 0, rl.White, rl.Black)
checked := rl.GenImageChecked(screenWidth, screenHeight, 32, 32, rl.Red, rl.Blue)
whiteNoise := rl.GenImageWhiteNoise(screenWidth, screenHeight, 0.5)
cellular := rl.GenImageCellular(screenWidth, screenHeight, 32)
textures := make([]rl.Texture2D, numTextures)
textures[0] = rl.LoadTextureFromImage(verticalGradient)
textures[1] = rl.LoadTextureFromImage(horizontalGradient)
textures[2] = rl.LoadTextureFromImage(radialGradient)
textures[3] = rl.LoadTextureFromImage(checked)
textures[4] = rl.LoadTextureFromImage(whiteNoise)
textures[5] = rl.LoadTextureFromImage(cellular)
// Unload image data (CPU RAM)
rl.UnloadImage(verticalGradient)
rl.UnloadImage(horizontalGradient)
rl.UnloadImage(radialGradient)
rl.UnloadImage(checked)
rl.UnloadImage(whiteNoise)
rl.UnloadImage(cellular)
currentTexture := 0
rl.SetTargetFPS(60)
for !rl.WindowShouldClose() {
if rl.IsMouseButtonPressed(rl.MouseLeftButton) {
currentTexture = (currentTexture + 1) % numTextures // Cycle between the textures
}
rl.BeginDrawing()
rl.ClearBackground(rl.RayWhite)
rl.DrawTexture(textures[currentTexture], 0, 0, rl.White)
rl.DrawRectangle(30, 400, 325, 30, rl.Fade(rl.SkyBlue, 0.5))
rl.DrawRectangleLines(30, 400, 325, 30, rl.Fade(rl.White, 0.5))
rl.DrawText("MOUSE LEFT BUTTON to CYCLE PROCEDURAL TEXTURES", 40, 410, 10, rl.White)
switch currentTexture {
case 0:
rl.DrawText("VERTICAL GRADIENT", 560, 10, 20, rl.RayWhite)
break
case 1:
rl.DrawText("HORIZONTAL GRADIENT", 540, 10, 20, rl.RayWhite)
break
case 2:
rl.DrawText("RADIAL GRADIENT", 580, 10, 20, rl.LightGray)
break
case 3:
rl.DrawText("CHECKED", 680, 10, 20, rl.RayWhite)
break
case 4:
rl.DrawText("WHITE NOISE", 640, 10, 20, rl.Red)
break
case 5:
rl.DrawText("CELLULAR", 670, 10, 20, rl.RayWhite)
break
default:
break
}
rl.EndDrawing()
}
for _, t := range textures {
rl.UnloadTexture(t)
}
rl.CloseWindow()
}

View file

@ -0,0 +1,67 @@
package main
import (
"image/png"
"os"
rl "github.com/gen2brain/raylib-go/raylib"
)
func main() {
screenWidth := int32(800)
screenHeight := int32(450)
rl.InitWindow(screenWidth, screenHeight, "raylib [textures] example - texture from image.Image")
r, err := os.Open("raylib_logo.png")
if err != nil {
rl.TraceLog(rl.LogError, err.Error())
}
defer r.Close()
img, err := png.Decode(r)
if err != nil {
rl.TraceLog(rl.LogError, err.Error())
}
// Create rl.Image from Go image.Image and create texture
im := rl.NewImageFromImage(img)
texture := rl.LoadTextureFromImage(im)
// Unload CPU (RAM) image data
rl.UnloadImage(im)
rl.SetTargetFPS(60)
for !rl.WindowShouldClose() {
if rl.IsKeyPressed(rl.KeyS) {
rimg := rl.LoadImageFromTexture(texture)
f, err := os.Create("image_saved.png")
if err != nil {
rl.TraceLog(rl.LogError, err.Error())
}
err = png.Encode(f, rimg.ToImage())
if err != nil {
rl.TraceLog(rl.LogError, err.Error())
}
f.Close()
}
rl.BeginDrawing()
rl.ClearBackground(rl.RayWhite)
rl.DrawText("PRESS S TO SAVE IMAGE FROM TEXTURE", 20, 20, 12, rl.LightGray)
rl.DrawTexture(texture, screenWidth/2-texture.Width/2, screenHeight/2-texture.Height/2, rl.White)
rl.DrawText("this IS a texture loaded from an image.Image!", 285, 370, 10, rl.Gray)
rl.EndDrawing()
}
rl.UnloadTexture(texture)
rl.CloseWindow()
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.7 KiB

View file

@ -0,0 +1,37 @@
package main
import (
"github.com/gen2brain/raylib-go/raylib"
)
func main() {
screenWidth := int32(800)
screenHeight := int32(450)
rl.InitWindow(screenWidth, screenHeight, "raylib [textures] example - image loading")
// NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
image := rl.LoadImage("raylib_logo.png") // Loaded in CPU memory (RAM)
texture := rl.LoadTextureFromImage(image) // Image converted to texture, GPU memory (VRAM)
rl.UnloadImage(image) // Once image has been converted to texture and uploaded to VRAM, it can be unloaded from RAM
rl.SetTargetFPS(60)
for !rl.WindowShouldClose() {
rl.BeginDrawing()
rl.ClearBackground(rl.RayWhite)
rl.DrawTexture(texture, screenWidth/2-texture.Width/2, screenHeight/2-texture.Height/2, rl.White)
rl.DrawText("this IS a texture loaded from an image!", 300, 370, 10, rl.Gray)
rl.EndDrawing()
}
rl.UnloadTexture(texture)
rl.CloseWindow()
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.7 KiB

View file

@ -0,0 +1,135 @@
package main
import (
rl "github.com/gen2brain/raylib-go/raylib"
)
const numProcesses = 8
// Process
const (
None = iota
ColorGrayscale
ColorTint
ColorInvert
ColorContrast
ColorBrightness
FlipVertical
FlipHorizontal
)
var processText = []string{
"NO PROCESSING",
"COLOR GRAYSCALE",
"COLOR TINT",
"COLOR INVERT",
"COLOR CONTRAST",
"COLOR BRIGHTNESS",
"FLIP VERTICAL",
"FLIP HORIZONTAL",
}
func main() {
screenWidth := int32(800)
screenHeight := int32(450)
rl.InitWindow(screenWidth, screenHeight, "raylib [textures] example - image processing")
image := rl.LoadImage("parrots.png") // Loaded in CPU memory (RAM)
rl.ImageFormat(image, rl.UncompressedR8g8b8a8) // Format image to RGBA 32bit (required for texture update)
texture := rl.LoadTextureFromImage(image) // Image converted to texture, GPU memory (VRAM)
currentProcess := None
textureReload := false
selectRecs := make([]rl.Rectangle, numProcesses)
for i := 0; i < numProcesses; i++ {
selectRecs[i] = rl.NewRectangle(40, 50+32*float32(i), 150, 30)
}
rl.SetTargetFPS(60)
for !rl.WindowShouldClose() {
if rl.IsKeyPressed(rl.KeyDown) {
currentProcess++
if currentProcess > 7 {
currentProcess = 0
}
textureReload = true
} else if rl.IsKeyPressed(rl.KeyUp) {
currentProcess--
if currentProcess < 0 {
currentProcess = 7
}
textureReload = true
}
if textureReload {
rl.UnloadImage(image) // Unload current image data
image = rl.LoadImage("parrots.png") // Re-load image data
// NOTE: Image processing is a costly CPU process to be done every frame,
// If image processing is required in a frame-basis, it should be done
// with a texture and by shaders
switch currentProcess {
case ColorGrayscale:
rl.ImageColorGrayscale(image)
break
case ColorTint:
rl.ImageColorTint(image, rl.Green)
break
case ColorInvert:
rl.ImageColorInvert(image)
break
case ColorContrast:
rl.ImageColorContrast(image, -40)
break
case ColorBrightness:
rl.ImageColorBrightness(image, -80)
break
case FlipVertical:
rl.ImageFlipVertical(image)
break
case FlipHorizontal:
rl.ImageFlipHorizontal(image)
break
default:
break
}
pixels := rl.LoadImageColors(image) // Get pixel data from image (RGBA 32bit)
rl.UpdateTexture(texture, pixels) // Update texture with new image data
textureReload = false
}
rl.BeginDrawing()
rl.ClearBackground(rl.RayWhite)
rl.DrawText("IMAGE PROCESSING:", 40, 30, 10, rl.DarkGray)
// Draw rectangles
for i := 0; i < numProcesses; i++ {
if i == currentProcess {
rl.DrawRectangleRec(selectRecs[i], rl.SkyBlue)
rl.DrawRectangleLines(int32(selectRecs[i].X), int32(selectRecs[i].Y), int32(selectRecs[i].Width), int32(selectRecs[i].Height), rl.Blue)
rl.DrawText(processText[i], int32(selectRecs[i].X+selectRecs[i].Width/2)-rl.MeasureText(processText[i], 10)/2, int32(selectRecs[i].Y)+11, 10, rl.DarkBlue)
} else {
rl.DrawRectangleRec(selectRecs[i], rl.LightGray)
rl.DrawRectangleLines(int32(selectRecs[i].X), int32(selectRecs[i].Y), int32(selectRecs[i].Width), int32(selectRecs[i].Height), rl.Gray)
rl.DrawText(processText[i], int32(selectRecs[i].X+selectRecs[i].Width/2)-rl.MeasureText(processText[i], 10)/2, int32(selectRecs[i].Y)+11, 10, rl.DarkGray)
}
}
rl.DrawTexture(texture, screenWidth-texture.Width-60, screenHeight/2-texture.Height/2, rl.White)
rl.DrawRectangleLines(screenWidth-texture.Width-60, screenHeight/2-texture.Height/2, texture.Width, texture.Height, rl.Black)
rl.EndDrawing()
}
rl.UnloadTexture(texture)
rl.CloseWindow()
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 288 KiB

Binary file not shown.

View file

@ -0,0 +1,61 @@
package main
import (
rl "github.com/gen2brain/raylib-go/raylib"
)
func main() {
screenWidth := int32(800)
screenHeight := int32(450)
rl.InitWindow(screenWidth, screenHeight, "raylib [textures] example - image text drawing")
// TTF Font loading with custom generation parameters
font := rl.LoadFontEx("fonts/KAISG.ttf", 64, nil)
parrots := rl.LoadImage("parrots.png") // Load image in CPU memory (RAM)
// Draw over image using custom font
rl.ImageDrawTextEx(parrots, rl.NewVector2(20, 20), font, "[Parrots font drawing]", float32(font.BaseSize), 0, rl.White)
texture := rl.LoadTextureFromImage(parrots) // Image converted to texture, uploaded to GPU memory (VRAM)
rl.UnloadImage(parrots) // Once image has been converted to texture and uploaded to VRAM, it can be unloaded from RAM
position := rl.NewVector2(float32(screenWidth)/2-float32(texture.Width)/2, float32(screenHeight)/2-float32(texture.Height)/2-20)
showFont := false
rl.SetTargetFPS(60)
for !rl.WindowShouldClose() {
if rl.IsKeyDown(rl.KeySpace) {
showFont = true
} else {
showFont = false
}
rl.BeginDrawing()
rl.ClearBackground(rl.RayWhite)
if !showFont {
// Draw texture with text already drawn inside
rl.DrawTextureV(texture, position, rl.White)
// Draw text directly using sprite font
rl.DrawTextEx(font, "[Parrots font drawing]", rl.NewVector2(position.X+20, position.Y+20+280), float32(font.BaseSize), 0, rl.White)
} else {
rl.DrawTexture(font.Texture, screenWidth/2-font.Texture.Width/2, 50, rl.Black)
}
rl.DrawText("PRESS SPACE to SEE USED SPRITEFONT ", 290, 420, 10, rl.DarkGray)
rl.EndDrawing()
}
rl.UnloadTexture(texture)
rl.UnloadFont(font)
rl.CloseWindow()
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 288 KiB

View file

@ -0,0 +1,31 @@
package main
import (
"github.com/gen2brain/raylib-go/raylib"
)
func main() {
screenWidth := int32(800)
screenHeight := int32(450)
rl.InitWindow(screenWidth, screenHeight, "raylib [textures] example - texture loading and drawing")
// NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
texture := rl.LoadTexture("raylib_logo.png")
rl.SetTargetFPS(60)
for !rl.WindowShouldClose() {
rl.BeginDrawing()
rl.ClearBackground(rl.RayWhite)
rl.DrawTexture(texture, screenWidth/2-texture.Width/2, screenHeight/2-texture.Height/2, rl.White)
rl.DrawText("this IS a texture!", 360, 370, 10, rl.Gray)
rl.EndDrawing()
}
rl.UnloadTexture(texture)
rl.CloseWindow()
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.7 KiB

View file

@ -0,0 +1,123 @@
package main
import (
"github.com/gen2brain/raylib-go/raylib"
)
const (
maxParticles = 200
)
type particle struct {
Position rl.Vector2
Color rl.Color
Alpha float32
Size float32
Rotation float32
Active bool
}
func main() {
screenWidth := int32(800)
screenHeight := int32(450)
//rl.SetConfigFlags(rl.FlagVsyncHint)
rl.InitWindow(screenWidth, screenHeight, "raylib [textures] example - particles blending")
// Particles pool, reuse them!
mouseTail := make([]particle, maxParticles)
// Initialize particles
for i := 0; i < maxParticles; i++ {
mouseTail[i].Position = rl.NewVector2(0, 0)
mouseTail[i].Color = rl.NewColor(byte(rl.GetRandomValue(0, 255)), byte(rl.GetRandomValue(0, 255)), byte(rl.GetRandomValue(0, 255)), 255)
mouseTail[i].Alpha = 1.0
mouseTail[i].Size = float32(rl.GetRandomValue(1, 30)) / 20.0
mouseTail[i].Rotation = float32(rl.GetRandomValue(0, 360))
mouseTail[i].Active = false
}
gravity := float32(3.0)
smoke := rl.LoadTexture("smoke.png")
blending := rl.BlendAlpha
rl.SetTargetFPS(60)
for !rl.WindowShouldClose() {
// Update
// Activate one particle every frame and Update active particles
// NOTE: Particles initial position should be mouse position when activated
// NOTE: Particles fall down with gravity and rotation... and disappear after 2 seconds (alpha = 0)
// NOTE: When a particle disappears, active = false and it can be reused.
for i := 0; i < maxParticles; i++ {
if !mouseTail[i].Active {
mouseTail[i].Active = true
mouseTail[i].Alpha = 1.0
mouseTail[i].Position = rl.GetMousePosition()
i = maxParticles
}
}
for i := 0; i < maxParticles; i++ {
if mouseTail[i].Active {
mouseTail[i].Position.Y += gravity
mouseTail[i].Alpha -= 0.01
if mouseTail[i].Alpha <= 0.0 {
mouseTail[i].Active = false
}
mouseTail[i].Rotation += 5.0
}
}
if rl.IsKeyPressed(rl.KeySpace) {
if blending == rl.BlendAlpha {
blending = rl.BlendAdditive
} else {
blending = rl.BlendAlpha
}
}
// Draw
rl.BeginDrawing()
rl.ClearBackground(rl.DarkGray)
rl.BeginBlendMode(blending)
// Draw active particles
for i := 0; i < maxParticles; i++ {
if mouseTail[i].Active {
rl.DrawTexturePro(
smoke,
rl.NewRectangle(0, 0, float32(smoke.Width), float32(smoke.Height)),
rl.NewRectangle(mouseTail[i].Position.X, mouseTail[i].Position.Y, float32(smoke.Width)*mouseTail[i].Size, float32(smoke.Height)*mouseTail[i].Size),
rl.NewVector2(float32(smoke.Width)*mouseTail[i].Size/2, float32(smoke.Height)*mouseTail[i].Size/2),
mouseTail[i].Rotation,
rl.Fade(mouseTail[i].Color, mouseTail[i].Alpha),
)
}
}
rl.EndBlendMode()
rl.DrawText("PRESS SPACE to CHANGE BLENDING MODE", 180, 20, 20, rl.Black)
if blending == rl.BlendAlpha {
rl.DrawText("ALPHA BLENDING", 290, screenHeight-40, 20, rl.Black)
} else {
rl.DrawText("ADDITIVE BLENDING", 280, screenHeight-40, 20, rl.RayWhite)
}
rl.EndDrawing()
}
rl.UnloadTexture(smoke)
rl.CloseWindow()
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

View file

@ -0,0 +1,64 @@
package main
import (
"github.com/gen2brain/raylib-go/raylib"
)
func main() {
screenWidth := int32(800)
screenHeight := int32(450)
rl.InitWindow(screenWidth, screenHeight, "raylib [textures] example - texture from raw data")
// NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
// Load RAW image data (384x512, 32bit RGBA, no file header)
fudesumiRaw := rl.LoadImageRaw("texture_formats/fudesumi.raw", 384, 512, rl.UncompressedR8g8b8a8, 0)
fudesumi := rl.LoadTextureFromImage(fudesumiRaw) // Upload CPU (RAM) image to GPU (VRAM)
rl.UnloadImage(fudesumiRaw) // Unload CPU (RAM) image data
// Generate a checked texture by code (1024x1024 pixels)
width := 1024
height := 1024
// Dynamic memory allocation to store pixels data (Color type)
pixels := make([]rl.Color, width*height)
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
if ((x/32+y/32)/1)%2 == 0 {
pixels[y*height+x] = rl.Orange
} else {
pixels[y*height+x] = rl.Gold
}
}
}
// LoadImageEx was removed from raylib
// Load pixels data into an image structure and create texture
// checkedIm := rl.LoadImageEx(pixels, int32(width), int32(height))
// checked := rl.LoadTextureFromImage(checkedIm)
// rl.UnloadImage(checkedIm) // Unload CPU (RAM) image data
rl.SetTargetFPS(60)
for !rl.WindowShouldClose() {
rl.BeginDrawing()
rl.ClearBackground(rl.RayWhite)
//rl.DrawTexture(checked, screenWidth/2-checked.Width/2, screenHeight/2-checked.Height/2, rl.Fade(rl.White, 0.5))
rl.DrawTexture(fudesumi, 430, -30, rl.White)
rl.DrawText("CHECKED TEXTURE ", 84, 100, 30, rl.Brown)
rl.DrawText("GENERATED by CODE", 72, 164, 30, rl.Brown)
rl.DrawText("and RAW IMAGE LOADING", 46, 226, 30, rl.Brown)
rl.EndDrawing()
}
rl.UnloadTexture(fudesumi) // Texture unloading
//rl.UnloadTexture(checked) // Texture unloading
rl.CloseWindow()
}

View file

@ -0,0 +1,87 @@
package main
import (
"fmt"
"github.com/gen2brain/raylib-go/raylib"
)
const (
maxFrameSpeed = 15
minFrameSpeed = 1
)
func main() {
screenWidth := int32(800)
screenHeight := int32(450)
rl.InitWindow(screenWidth, screenHeight, "raylib [textures] example - texture loading and drawing")
// NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
scarfy := rl.LoadTexture("scarfy.png") // Texture loading
position := rl.NewVector2(350.0, 280.0)
frameRec := rl.NewRectangle(0, 0, float32(scarfy.Width/6), float32(scarfy.Height))
currentFrame := float32(0)
framesCounter := 0
framesSpeed := 8 // Number of spritesheet frames shown by second
rl.SetTargetFPS(60)
for !rl.WindowShouldClose() {
framesCounter++
if framesCounter >= (60 / framesSpeed) {
framesCounter = 0
currentFrame++
if currentFrame > 5 {
currentFrame = 0
}
frameRec.X = currentFrame * float32(scarfy.Width) / 6
}
if rl.IsKeyPressed(rl.KeyRight) {
framesSpeed++
} else if rl.IsKeyPressed(rl.KeyLeft) {
framesSpeed--
}
if framesSpeed > maxFrameSpeed {
framesSpeed = maxFrameSpeed
} else if framesSpeed < minFrameSpeed {
framesSpeed = minFrameSpeed
}
rl.BeginDrawing()
rl.ClearBackground(rl.RayWhite)
rl.DrawTexture(scarfy, 15, 40, rl.White)
rl.DrawRectangleLines(15, 40, scarfy.Width, scarfy.Height, rl.Lime)
rl.DrawRectangleLines(15+int32(frameRec.X), 40+int32(frameRec.Y), int32(frameRec.Width), int32(frameRec.Height), rl.Red)
rl.DrawText("FRAME SPEED: ", 165, 210, 10, rl.DarkGray)
rl.DrawText(fmt.Sprintf("%02d FPS", framesSpeed), 575, 210, 10, rl.DarkGray)
rl.DrawText("PRESS RIGHT/LEFT KEYS to CHANGE SPEED!", 290, 240, 10, rl.DarkGray)
for i := 0; i < maxFrameSpeed; i++ {
if i < framesSpeed {
rl.DrawRectangle(int32(250+21*i), 205, 20, 20, rl.Red)
}
rl.DrawRectangleLines(int32(250+21*i), 205, 20, 20, rl.Maroon)
}
rl.DrawTextureRec(scarfy, frameRec, position, rl.White) // Draw part of the texture
rl.DrawText("(c) Scarfy sprite by Eiden Marsal", screenWidth-200, screenHeight-20, 10, rl.Gray)
rl.EndDrawing()
}
rl.UnloadTexture(scarfy)
rl.CloseWindow()
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 32 KiB

View file

@ -0,0 +1,59 @@
package main
import (
"github.com/gen2brain/raylib-go/raylib"
)
func main() {
screenWidth := int32(800)
screenHeight := int32(450)
rl.InitWindow(screenWidth, screenHeight, "raylib [textures] examples - texture source and destination rectangles")
// NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
scarfy := rl.LoadTexture("scarfy.png") // Texture loading
frameWidth := float32(scarfy.Width) / 7
frameHeight := float32(scarfy.Height)
// NOTE: Source rectangle (part of the texture to use for drawing)
sourceRec := rl.NewRectangle(0, 0, frameWidth, frameHeight)
// NOTE: Destination rectangle (screen rectangle where drawing part of texture)
destRec := rl.NewRectangle(float32(screenWidth)/2, float32(screenHeight)/2, frameWidth*2, frameHeight*2)
// NOTE: Origin of the texture (rotation/scale point), it's relative to destination rectangle size
origin := rl.NewVector2(float32(frameWidth), float32(frameHeight))
rotation := float32(0)
rl.SetTargetFPS(60)
for !rl.WindowShouldClose() {
// Update
rotation++
// Draw
rl.BeginDrawing()
rl.ClearBackground(rl.RayWhite)
// NOTE: Using DrawTexturePro() we can easily rotate and scale the part of the texture we draw
// sourceRec defines the part of the texture we use for drawing
// destRec defines the rectangle where our texture part will fit (scaling it to fit)
// origin defines the point of the texture used as reference for rotation and scaling
// rotation defines the texture rotation (using origin as rotation point)
rl.DrawTexturePro(scarfy, sourceRec, destRec, origin, rotation, rl.White)
rl.DrawLine(int32(destRec.X), 0, int32(destRec.X), screenHeight, rl.Gray)
rl.DrawLine(0, int32(destRec.Y), screenWidth, int32(destRec.Y), rl.Gray)
rl.DrawText("(c) Scarfy sprite by Eiden Marsal", screenWidth-200, screenHeight-20, 10, rl.Gray)
rl.EndDrawing()
}
rl.UnloadTexture(scarfy)
rl.CloseWindow()
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 32 KiB

View file

@ -0,0 +1,39 @@
package main
import (
rl "github.com/gen2brain/raylib-go/raylib"
)
func main() {
screenWidth := int32(800)
screenHeight := int32(450)
rl.InitWindow(screenWidth, screenHeight, "raylib [textures] example - texture to image")
image := rl.LoadImage("raylib_logo.png") // Load image data into CPU memory (RAM)
texture := rl.LoadTextureFromImage(image) // Image converted to texture, GPU memory (RAM -> VRAM)
rl.UnloadImage(image) // Unload image data from CPU memory (RAM)
image = rl.LoadImageFromTexture(texture) // Retrieve image data from GPU memory (VRAM -> RAM)
rl.UnloadTexture(texture) // Unload texture from GPU memory (VRAM)
texture = rl.LoadTextureFromImage(image) // Recreate texture from retrieved image data (RAM -> VRAM)
rl.UnloadImage(image) // Unload retrieved image data from CPU memory (RAM)
rl.SetTargetFPS(60)
for !rl.WindowShouldClose() {
rl.BeginDrawing()
rl.ClearBackground(rl.RayWhite)
rl.DrawTexture(texture, screenWidth/2-texture.Width/2, screenHeight/2-texture.Height/2, rl.White)
rl.DrawText("this IS a texture loaded from an image!", 300, 370, 10, rl.Gray)
rl.EndDrawing()
}
rl.UnloadTexture(texture)
rl.CloseWindow()
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.7 KiB