Merge pull request #423 from Hultan/master
3 more examples under texture
This commit is contained in:
commit
42a2d9159c
7 changed files with 530 additions and 0 deletions
302
examples/textures/draw_tiled/main.go
Normal file
302
examples/textures/draw_tiled/main.go
Normal file
|
@ -0,0 +1,302 @@
|
|||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [textures] example - Draw part of the texture tiled
|
||||
*
|
||||
* Example originally created with raylib 3.0, last time updated with raylib 4.2
|
||||
*
|
||||
* Example contributed by Vlad Adrian (@demizdor) and reviewed by Ramon Santamaria (@raysan5)
|
||||
*
|
||||
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
|
||||
* BSD-like license that allows static linking with closed source software
|
||||
*
|
||||
* Copyright (c) 2020-2024 Vlad Adrian (@demizdor) and Ramon Santamaria (@raysan5)
|
||||
*
|
||||
********************************************************************************************/
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
rl "github.com/gen2brain/raylib-go/raylib"
|
||||
)
|
||||
|
||||
const (
|
||||
screenWidth = 800
|
||||
screenHeight = 450
|
||||
optWidth = 220 // Max width for the options container
|
||||
marginSize = 8 // Size for the margins
|
||||
colorSize = 16 // Size of the color select buttons
|
||||
)
|
||||
|
||||
func main() {
|
||||
rl.SetConfigFlags(rl.FlagWindowResizable) // Make the window resizable
|
||||
rl.InitWindow(screenWidth, screenHeight, "raylib [textures] example - Draw part of a texture tiled")
|
||||
|
||||
// NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
|
||||
texPattern := rl.LoadTexture("patterns.png")
|
||||
rl.SetTextureFilter(texPattern, rl.TextureFilterNearestMipLinear) // Makes the texture smoother when upscaled
|
||||
|
||||
// Coordinates for all patterns inside the texture
|
||||
recPattern := []rl.Rectangle{
|
||||
{3, 3, 66, 66},
|
||||
{75, 3, 100, 100},
|
||||
{3, 75, 66, 66},
|
||||
{7, 156, 50, 50},
|
||||
{85, 106, 90, 45},
|
||||
{75, 154, 100, 60},
|
||||
}
|
||||
|
||||
// Setup colors
|
||||
colors := []rl.Color{rl.Black, rl.Maroon, rl.Orange, rl.Blue, rl.Purple,
|
||||
rl.Beige, rl.Lime, rl.Red, rl.DarkGray, rl.SkyBlue}
|
||||
var maxColors = len(colors)
|
||||
colorRec := make([]rl.Rectangle, maxColors)
|
||||
|
||||
// Calculate rectangle for each color
|
||||
var x, y float32
|
||||
for i := 0; i < maxColors; i++ {
|
||||
colorRec[i].X = 2.0 + marginSize + x
|
||||
colorRec[i].Y = 22.0 + 256.0 + marginSize + y
|
||||
colorRec[i].Width = colorSize * 2.0
|
||||
colorRec[i].Height = colorSize
|
||||
|
||||
if i == (maxColors/2 - 1) {
|
||||
x = 0
|
||||
y += colorSize + marginSize
|
||||
} else {
|
||||
x += colorSize*2 + marginSize
|
||||
}
|
||||
}
|
||||
|
||||
activePattern := 0
|
||||
activeCol := 0
|
||||
scale := float32(1.0)
|
||||
rotation := float32(0.0)
|
||||
|
||||
rl.SetTargetFPS(60)
|
||||
|
||||
// Main game loop
|
||||
for !rl.WindowShouldClose() { // Detect window close button or ESC key
|
||||
// Handle mouse
|
||||
if rl.IsMouseButtonPressed(rl.MouseButtonLeft) {
|
||||
mouse := rl.GetMousePosition()
|
||||
|
||||
// Check which pattern was clicked and set it as the active pattern
|
||||
for i := 0; i < len(recPattern); i++ {
|
||||
r := rl.Rectangle{
|
||||
X: 2 + marginSize + recPattern[i].X,
|
||||
Y: 40 + marginSize + recPattern[i].Y,
|
||||
Width: recPattern[i].Width,
|
||||
Height: recPattern[i].Height,
|
||||
}
|
||||
if rl.CheckCollisionPointRec(mouse, r) {
|
||||
activePattern = i
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// Check to see which color was clicked and set it as the active color
|
||||
for i := 0; i < maxColors; i++ {
|
||||
if rl.CheckCollisionPointRec(mouse, colorRec[i]) {
|
||||
activeCol = i
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Handle keys
|
||||
|
||||
// Change scale
|
||||
if rl.IsKeyPressed(rl.KeyUp) {
|
||||
scale += 0.25
|
||||
}
|
||||
if rl.IsKeyPressed(rl.KeyDown) {
|
||||
scale -= 0.25
|
||||
}
|
||||
scale = clamp(scale, 0.25, 10)
|
||||
|
||||
// Change rotation
|
||||
if rl.IsKeyPressed(rl.KeyLeft) {
|
||||
rotation -= 25.0
|
||||
}
|
||||
if rl.IsKeyPressed(rl.KeyRight) {
|
||||
rotation += 25.0
|
||||
}
|
||||
|
||||
// Reset
|
||||
if rl.IsKeyPressed(rl.KeySpace) {
|
||||
rotation = 0.0
|
||||
scale = 1.0
|
||||
}
|
||||
|
||||
// Draw
|
||||
rl.BeginDrawing()
|
||||
rl.ClearBackground(rl.RayWhite)
|
||||
|
||||
// Draw the tiled area
|
||||
src := rl.Rectangle{
|
||||
X: optWidth + marginSize,
|
||||
Y: marginSize,
|
||||
Width: float32(rl.GetScreenWidth()) - optWidth - 2.0*marginSize,
|
||||
Height: float32(rl.GetScreenHeight()) - 2.0*marginSize,
|
||||
}
|
||||
DrawTextureTiled(texPattern, recPattern[activePattern], src, rl.Vector2{}, rotation, scale, colors[activeCol])
|
||||
|
||||
// Draw options
|
||||
rl.DrawRectangle(marginSize, marginSize, optWidth-marginSize, int32(rl.GetScreenHeight())-2*marginSize,
|
||||
rl.ColorAlpha(rl.LightGray, 0.5))
|
||||
|
||||
rl.DrawText("Select Pattern", 2+marginSize, 30+marginSize, 10, rl.Black)
|
||||
rl.DrawTexture(texPattern, 2+marginSize, 40+marginSize, rl.Black)
|
||||
rl.DrawRectangle(int32(2+marginSize+recPattern[activePattern].X),
|
||||
int32(40+marginSize+recPattern[activePattern].Y),
|
||||
int32(recPattern[activePattern].Width),
|
||||
int32(recPattern[activePattern].Height), rl.ColorAlpha(rl.DarkBlue, 0.3))
|
||||
|
||||
rl.DrawText("Select Color", 2+marginSize, 10+256+marginSize, 10, rl.Black)
|
||||
for i := 0; i < maxColors; i++ {
|
||||
rl.DrawRectangleRec(colorRec[i], colors[i])
|
||||
if activeCol == i {
|
||||
rl.DrawRectangleLinesEx(colorRec[i], 3, rl.ColorAlpha(rl.White, 0.5))
|
||||
}
|
||||
}
|
||||
|
||||
rl.DrawText("Scale (UP/DOWN to change)", 2+marginSize, 80+256+marginSize, 10, rl.Black)
|
||||
rl.DrawText(fmt.Sprintf("%.2fx", scale), 2+marginSize, 92+256+marginSize, 20, rl.Black)
|
||||
|
||||
rl.DrawText("Rotation (LEFT/RIGHT to change)", 2+marginSize, 122+256+marginSize, 10, rl.Black)
|
||||
rl.DrawText(fmt.Sprintf("%.0f degrees", rotation), 2+marginSize, 134+256+marginSize, 20, rl.Black)
|
||||
|
||||
rl.DrawText("Press [SPACE] to reset", 2+marginSize, 164+256+marginSize, 10, rl.DarkBlue)
|
||||
|
||||
// Draw FPS
|
||||
rl.DrawText(fmt.Sprintf("%d FPS", rl.GetFPS()), 2+marginSize, 2+marginSize, 20, rl.Black)
|
||||
rl.EndDrawing()
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
rl.UnloadTexture(texPattern) // Unload texture
|
||||
|
||||
rl.CloseWindow() // Close window and OpenGL context
|
||||
}
|
||||
|
||||
// DrawTextureTiled draws a part of a texture (defined by a rectangle) with rotation and scale tiled into dest.
|
||||
func DrawTextureTiled(texture rl.Texture2D, source, dest rl.Rectangle, origin rl.Vector2, rotation, scale float32,
|
||||
tint rl.Color) {
|
||||
|
||||
if (texture.ID <= 0) || (scale <= 0.0) { // Want see an infinite loop?!...just delete this line!
|
||||
return
|
||||
}
|
||||
if (source.Width == 0) || (source.Height == 0) {
|
||||
return
|
||||
}
|
||||
|
||||
tileWidth := source.Width * scale
|
||||
tileHeight := source.Height * scale
|
||||
if (dest.Width < tileWidth) && (dest.Height < tileHeight) {
|
||||
// Can fit only one tile
|
||||
src := rl.Rectangle{
|
||||
X: source.X,
|
||||
Y: source.Y,
|
||||
Width: dest.Width / tileWidth * source.Width,
|
||||
Height: dest.Height / tileHeight * source.Height,
|
||||
}
|
||||
dst := rl.Rectangle{X: dest.X, Y: dest.Y, Width: dest.Width, Height: dest.Height}
|
||||
rl.DrawTexturePro(texture, src, dst, origin, rotation, tint)
|
||||
} else if dest.Width <= tileWidth {
|
||||
// Tiled vertically (one column)
|
||||
var dy float32
|
||||
for ; dy+tileHeight < dest.Height; dy += tileHeight {
|
||||
src := rl.Rectangle{
|
||||
X: source.X,
|
||||
Y: source.Y,
|
||||
Width: dest.Width / tileWidth * source.Width,
|
||||
Height: source.Height,
|
||||
}
|
||||
dst := rl.Rectangle{X: dest.X, Y: dest.Y + dy, Width: dest.Width, Height: tileHeight}
|
||||
rl.DrawTexturePro(texture, src, dst, origin, rotation, tint)
|
||||
}
|
||||
|
||||
// Fit last tile
|
||||
if dy < dest.Height {
|
||||
src := rl.Rectangle{X: source.X, Y: source.Y,
|
||||
Width: (dest.Width / tileWidth) * source.Width,
|
||||
Height: ((dest.Height - dy) / tileHeight) * source.Height,
|
||||
}
|
||||
dst := rl.Rectangle{X: dest.X, Y: dest.Y + dy, Width: dest.Width, Height: dest.Height - dy}
|
||||
rl.DrawTexturePro(texture, src, dst, origin, rotation, tint)
|
||||
}
|
||||
} else if dest.Height <= tileHeight {
|
||||
// Tiled horizontally (one row)
|
||||
var dx float32
|
||||
for ; dx+tileWidth < dest.Width; dx += tileWidth {
|
||||
src := rl.Rectangle{
|
||||
X: source.X, Y: source.Y, Width: source.Width,
|
||||
Height: (dest.Height / tileHeight) * source.Height,
|
||||
}
|
||||
dst := rl.Rectangle{X: dest.X + dx, Y: dest.Y, Width: tileWidth, Height: dest.Height}
|
||||
rl.DrawTexturePro(texture, src, dst, origin, rotation, tint)
|
||||
}
|
||||
|
||||
// Fit last tile
|
||||
if dx < dest.Width {
|
||||
src := rl.Rectangle{
|
||||
X: source.X, Y: source.Y, Width: ((dest.Width - dx) / tileWidth) * source.Width,
|
||||
Height: (dest.Height / tileHeight) * source.Height,
|
||||
}
|
||||
dst := rl.Rectangle{X: dest.X + dx, Y: dest.Y, Width: dest.Width - dx, Height: dest.Height}
|
||||
rl.DrawTexturePro(texture, src,
|
||||
dst, origin, rotation, tint)
|
||||
}
|
||||
} else {
|
||||
// Tiled both horizontally and vertically (rows and columns)
|
||||
var dx float32
|
||||
for ; dx+tileWidth < dest.Width; dx += tileWidth {
|
||||
var dy float32
|
||||
for ; dy+tileHeight < dest.Height; dy += tileHeight {
|
||||
dst := rl.Rectangle{X: dest.X + dx, Y: dest.Y + dy, Width: tileWidth, Height: tileHeight}
|
||||
rl.DrawTexturePro(texture, source, dst, origin, rotation, tint)
|
||||
}
|
||||
|
||||
if dy < dest.Height {
|
||||
src := rl.Rectangle{
|
||||
X: source.X, Y: source.Y,
|
||||
Width: source.Width, Height: ((dest.Height - dy) / tileHeight) * source.Height,
|
||||
}
|
||||
dst := rl.Rectangle{
|
||||
X: dest.X + dx, Y: dest.Y + dy,
|
||||
Width: tileWidth, Height: dest.Height - dy,
|
||||
}
|
||||
rl.DrawTexturePro(texture, src, dst, origin, rotation, tint)
|
||||
}
|
||||
}
|
||||
|
||||
// Fit last column of tiles
|
||||
if dx < dest.Width {
|
||||
var dy float32
|
||||
for ; dy+tileHeight < dest.Height; dy += tileHeight {
|
||||
src := rl.Rectangle{
|
||||
X: source.X, Y: source.Y,
|
||||
Width: ((dest.Width - dx) / tileWidth) * source.Width, Height: source.Height,
|
||||
}
|
||||
dst := rl.Rectangle{X: dest.X + dx, Y: dest.Y + dy, Width: dest.Width - dx, Height: tileHeight}
|
||||
rl.DrawTexturePro(texture, src, dst, origin, rotation, tint)
|
||||
}
|
||||
|
||||
// Draw final tile in the bottom right corner
|
||||
if dy < dest.Height {
|
||||
src := rl.Rectangle{
|
||||
X: source.X, Y: source.Y,
|
||||
Width: ((dest.Width - dx) / tileWidth) * source.Width,
|
||||
Height: ((dest.Height - dy) / tileHeight) * source.Height,
|
||||
}
|
||||
dst := rl.Rectangle{X: dest.X + dx, Y: dest.Y + dy, Width: dest.Width - dx, Height: dest.Height - dy}
|
||||
rl.DrawTexturePro(texture, src, dst, origin, rotation, tint)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func clamp(value, minValue, maxValue float32) float32 {
|
||||
return min(maxValue, max(value, minValue))
|
||||
}
|
BIN
examples/textures/draw_tiled/patterns.png
Normal file
BIN
examples/textures/draw_tiled/patterns.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 7.2 KiB |
134
examples/textures/npatch_drawing/main.go
Normal file
134
examples/textures/npatch_drawing/main.go
Normal file
|
@ -0,0 +1,134 @@
|
|||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [textures] example - N-patch drawing
|
||||
*
|
||||
* NOTE: Images are loaded in CPU memory (RAM); textures are loaded in GPU memory (VRAM)
|
||||
*
|
||||
* Example originally created with raylib 2.0, last time updated with raylib 2.5
|
||||
*
|
||||
* Example contributed by Jorge A. Gomes (@overdev) and reviewed by Ramon Santamaria (@raysan5)
|
||||
*
|
||||
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
|
||||
* BSD-like license that allows static linking with closed source software
|
||||
*
|
||||
* Copyright (c) 2018-2024 Jorge A. Gomes (@overdev) and Ramon Santamaria (@raysan5)
|
||||
*
|
||||
********************************************************************************************/
|
||||
package main
|
||||
|
||||
import rl "github.com/gen2brain/raylib-go/raylib"
|
||||
|
||||
const (
|
||||
screenWidth = 800
|
||||
screenHeight = 450
|
||||
)
|
||||
|
||||
func main() {
|
||||
rl.InitWindow(screenWidth, screenHeight, "raylib [textures] example - N-patch drawing")
|
||||
|
||||
// NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
|
||||
nPatchTexture := rl.LoadTexture("ninepatch_button.png")
|
||||
|
||||
var mousePosition rl.Vector2
|
||||
var origin rl.Vector2
|
||||
|
||||
// Position and size of the n-patches
|
||||
dstRec1 := rl.Rectangle{X: 480.0, Y: 160.0, Width: 32.0, Height: 32.0}
|
||||
dstRec2 := rl.Rectangle{X: 160.0, Y: 160.0, Width: 32.0, Height: 32.0}
|
||||
dstRecH := rl.Rectangle{X: 160.0, Y: 93.0, Width: 32.0, Height: 32.0}
|
||||
dstRecV := rl.Rectangle{X: 92.0, Y: 160.0, Width: 32.0, Height: 32.0}
|
||||
|
||||
// A 9-patch (NPatchNinePatch) changes its sizes in both axis
|
||||
ninePatchInfo1 := rl.NPatchInfo{
|
||||
Source: rl.Rectangle{Width: 64.0, Height: 64.0},
|
||||
Left: 12,
|
||||
Top: 40,
|
||||
Right: 12,
|
||||
Bottom: 12,
|
||||
Layout: rl.NPatchNinePatch,
|
||||
}
|
||||
ninePatchInfo2 := rl.NPatchInfo{
|
||||
Source: rl.Rectangle{Y: 128.0, Width: 64.0, Height: 64.0},
|
||||
Left: 16,
|
||||
Top: 16,
|
||||
Right: 16,
|
||||
Bottom: 16,
|
||||
Layout: rl.NPatchNinePatch,
|
||||
}
|
||||
|
||||
// A horizontal 3-patch (NPatchThreePatchHorizontal) changes its sizes along the x-axis only
|
||||
h3PatchInfo := rl.NPatchInfo{
|
||||
Source: rl.Rectangle{Y: 64.0, Width: 64.0, Height: 64.0},
|
||||
Left: 8,
|
||||
Top: 8,
|
||||
Right: 8,
|
||||
Bottom: 8,
|
||||
Layout: rl.NPatchThreePatchHorizontal,
|
||||
}
|
||||
|
||||
// A vertical 3-patch (NPatchThreePatchVertical) changes its sizes along the y-axis only
|
||||
v3PatchInfo := rl.NPatchInfo{
|
||||
Source: rl.Rectangle{Y: 192.0, Width: 64.0, Height: 64.0},
|
||||
Left: 6,
|
||||
Top: 6,
|
||||
Right: 6,
|
||||
Bottom: 6,
|
||||
Layout: rl.NPatchThreePatchVertical,
|
||||
}
|
||||
|
||||
rl.SetTargetFPS(60)
|
||||
|
||||
// Main game loop
|
||||
for !rl.WindowShouldClose() { // Detect window close button or ESC key
|
||||
mousePosition = rl.GetMousePosition()
|
||||
|
||||
// Resize the n-patches based on mouse position
|
||||
dstRec1.Width = mousePosition.X - dstRec1.X
|
||||
dstRec1.Height = mousePosition.Y - dstRec1.Y
|
||||
dstRec2.Width = mousePosition.X - dstRec2.X
|
||||
dstRec2.Height = mousePosition.Y - dstRec2.Y
|
||||
dstRecH.Width = mousePosition.X - dstRecH.X
|
||||
dstRecV.Height = mousePosition.Y - dstRecV.Y
|
||||
|
||||
// Set a minimum Width and/or Height
|
||||
dstRec1.Width = clamp(dstRec1.Width, 1, 300)
|
||||
dstRec1.Height = clamp(dstRec1.Height, 1, screenHeight)
|
||||
dstRec2.Width = clamp(dstRec2.Width, 1, 300)
|
||||
dstRec2.Height = clamp(dstRec2.Height, 1, screenHeight)
|
||||
dstRecH.Width = clamp(dstRecH.Width, 1, screenWidth)
|
||||
dstRecV.Height = clamp(dstRecV.Height, 1, screenHeight)
|
||||
|
||||
// Draw
|
||||
rl.BeginDrawing()
|
||||
rl.ClearBackground(rl.RayWhite)
|
||||
|
||||
// Draw the n-patches
|
||||
rl.DrawTextureNPatch(nPatchTexture, ninePatchInfo2, dstRec2, origin, 0.0, rl.White)
|
||||
rl.DrawTextureNPatch(nPatchTexture, ninePatchInfo1, dstRec1, origin, 0.0, rl.White)
|
||||
rl.DrawTextureNPatch(nPatchTexture, h3PatchInfo, dstRecH, origin, 0.0, rl.White)
|
||||
rl.DrawTextureNPatch(nPatchTexture, v3PatchInfo, dstRecV, origin, 0.0, rl.White)
|
||||
|
||||
// Draw the source texture
|
||||
rl.DrawRectangleLines(5, 88, 74, 266, rl.Blue)
|
||||
rl.DrawTexture(nPatchTexture, 10, 93, rl.White)
|
||||
rl.DrawText("TEXTURE", 15, 360, 10, rl.DarkGray)
|
||||
|
||||
rl.DrawText("Move the mouse to stretch or shrink the n-patches", 10, 20, 20, rl.DarkGray)
|
||||
|
||||
rl.EndDrawing()
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
rl.UnloadTexture(nPatchTexture) // Texture unloading
|
||||
rl.CloseWindow() // Close window and OpenGL context
|
||||
}
|
||||
|
||||
func clamp(value, min, max float32) float32 {
|
||||
if value < min {
|
||||
return min
|
||||
}
|
||||
if value > max {
|
||||
return max
|
||||
}
|
||||
return value
|
||||
}
|
BIN
examples/textures/npatch_drawing/ninepatch_button.png
Normal file
BIN
examples/textures/npatch_drawing/ninepatch_button.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 5.8 KiB |
BIN
examples/textures/sprite_button/button.png
Normal file
BIN
examples/textures/sprite_button/button.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 23 KiB |
BIN
examples/textures/sprite_button/buttonfx.wav
Normal file
BIN
examples/textures/sprite_button/buttonfx.wav
Normal file
Binary file not shown.
94
examples/textures/sprite_button/main.go
Normal file
94
examples/textures/sprite_button/main.go
Normal file
|
@ -0,0 +1,94 @@
|
|||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [textures] example - sprite button
|
||||
*
|
||||
* Example originally created with raylib 2.5, last time updated with raylib 2.5
|
||||
*
|
||||
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
|
||||
* BSD-like license that allows static linking with closed source software
|
||||
*
|
||||
* Copyright (c) 2019-2024 Ramon Santamaria (@raysan5)
|
||||
*
|
||||
********************************************************************************************/
|
||||
package main
|
||||
|
||||
import rl "github.com/gen2brain/raylib-go/raylib"
|
||||
|
||||
const (
|
||||
screenWidth = 800
|
||||
screenHeight = 450
|
||||
numFrames = 3 // Number of frames (rectangles) for the button sprite texture
|
||||
)
|
||||
|
||||
func main() {
|
||||
rl.InitWindow(screenWidth, screenHeight, "raylib [textures] example - sprite button")
|
||||
|
||||
rl.InitAudioDevice() // Initialize audio device
|
||||
|
||||
fxButton := rl.LoadSound("buttonfx.wav") // Load button sound
|
||||
button := rl.LoadTexture("button.png") // Load button texture
|
||||
|
||||
// Define frame rectangle for drawing
|
||||
frameHeight := float32(button.Height) / numFrames
|
||||
sourceRec := rl.Rectangle{Width: float32(button.Width), Height: frameHeight}
|
||||
|
||||
// Define button bounds on screen
|
||||
btnBounds := rl.Rectangle{
|
||||
X: float32(screenWidth/2.0 - button.Width/2.0),
|
||||
Y: float32(screenHeight/2.0 - button.Height/numFrames/2.0),
|
||||
Width: float32(button.Width),
|
||||
Height: frameHeight,
|
||||
}
|
||||
|
||||
btnState := 0 // Button state: 0-NORMAL, 1-MOUSE_HOVER, 2-PRESSED
|
||||
btnAction := false // Button action should be activated
|
||||
|
||||
rl.SetTargetFPS(60)
|
||||
|
||||
// Main game loop
|
||||
for !rl.WindowShouldClose() { // Detect window close button or ESC key
|
||||
mousePoint := rl.GetMousePosition()
|
||||
btnAction = false
|
||||
|
||||
// Check button state
|
||||
if rl.CheckCollisionPointRec(mousePoint, btnBounds) {
|
||||
if rl.IsMouseButtonDown(rl.MouseButtonLeft) {
|
||||
btnState = 2
|
||||
} else {
|
||||
btnState = 1
|
||||
}
|
||||
|
||||
if rl.IsMouseButtonReleased(rl.MouseButtonLeft) {
|
||||
btnAction = true
|
||||
}
|
||||
} else {
|
||||
|
||||
btnState = 0
|
||||
}
|
||||
|
||||
if btnAction {
|
||||
rl.PlaySound(fxButton)
|
||||
|
||||
// TODO: Any desired action
|
||||
}
|
||||
|
||||
// Calculate button frame rectangle to draw depending on button state
|
||||
sourceRec.Y = float32(btnState) * frameHeight
|
||||
|
||||
// Draw
|
||||
rl.BeginDrawing()
|
||||
rl.ClearBackground(rl.RayWhite)
|
||||
|
||||
rl.DrawTextureRec(button, sourceRec, rl.Vector2{X: btnBounds.X, Y: btnBounds.Y}, rl.White) // Draw button frame
|
||||
|
||||
rl.EndDrawing()
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
rl.UnloadTexture(button) // Unload button texture
|
||||
rl.UnloadSound(fxButton) // Unload sound
|
||||
|
||||
rl.CloseAudioDevice() // Close audio device
|
||||
|
||||
rl.CloseWindow() // Close window and OpenGL context
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue