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

View file

@ -0,0 +1,125 @@
package main
import (
"github.com/gen2brain/raylib-go/raylib"
)
var (
maxBuildings int = 100
)
func main() {
screenWidth := int32(800)
screenHeight := int32(450)
rl.InitWindow(screenWidth, screenHeight, "raylib [core] example - 2d camera")
player := rl.NewRectangle(400, 280, 40, 40)
buildings := make([]rl.Rectangle, maxBuildings)
buildColors := make([]rl.Color, maxBuildings)
spacing := float32(0)
for i := 0; i < maxBuildings; i++ {
r := rl.Rectangle{}
r.Width = float32(rl.GetRandomValue(50, 200))
r.Height = float32(rl.GetRandomValue(100, 800))
r.Y = float32(screenHeight) - 130 - r.Height
r.X = -6000 + spacing
spacing += r.Width
c := rl.NewColor(byte(rl.GetRandomValue(200, 240)), byte(rl.GetRandomValue(200, 240)), byte(rl.GetRandomValue(200, 250)), byte(255))
buildings[i] = r
buildColors[i] = c
}
camera := rl.Camera2D{}
camera.Target = rl.NewVector2(float32(player.X+20), float32(player.Y+20))
camera.Offset = rl.NewVector2(float32(screenWidth/2), float32(screenHeight/2))
camera.Rotation = 0.0
camera.Zoom = 1.0
rl.SetTargetFPS(60)
for !rl.WindowShouldClose() {
if rl.IsKeyDown(rl.KeyRight) {
player.X += 2 // Player movement
} else if rl.IsKeyDown(rl.KeyLeft) {
player.X -= 2 // Player movement
}
// Camera target follows player
camera.Target = rl.NewVector2(float32(player.X+20), float32(player.Y+20))
// Camera rotation controls
if rl.IsKeyDown(rl.KeyA) {
camera.Rotation--
} else if rl.IsKeyDown(rl.KeyS) {
camera.Rotation++
}
// Limit camera rotation to 80 degrees (-40 to 40)
if camera.Rotation > 40 {
camera.Rotation = 40
} else if camera.Rotation < -40 {
camera.Rotation = -40
}
// Camera zoom controls
camera.Zoom += float32(rl.GetMouseWheelMove()) * 0.05
if camera.Zoom > 3.0 {
camera.Zoom = 3.0
} else if camera.Zoom < 0.1 {
camera.Zoom = 0.1
}
// Camera reset (zoom and rotation)
if rl.IsKeyPressed(rl.KeyR) {
camera.Zoom = 1.0
camera.Rotation = 0.0
}
rl.BeginDrawing()
rl.ClearBackground(rl.RayWhite)
rl.BeginMode2D(camera)
rl.DrawRectangle(-6000, 320, 13000, 8000, rl.DarkGray)
for i := 0; i < maxBuildings; i++ {
rl.DrawRectangleRec(buildings[i], buildColors[i])
}
rl.DrawRectangleRec(player, rl.Red)
rl.DrawRectangle(int32(camera.Target.X), -500, 1, screenHeight*4, rl.Green)
rl.DrawRectangle(-500, int32(camera.Target.Y), screenWidth*4, 1, rl.Green)
rl.EndMode2D()
rl.DrawText("SCREEN AREA", 640, 10, 20, rl.Red)
rl.DrawRectangle(0, 0, screenWidth, 5, rl.Red)
rl.DrawRectangle(0, 5, 5, screenHeight-10, rl.Red)
rl.DrawRectangle(screenWidth-5, 5, 5, screenHeight-10, rl.Red)
rl.DrawRectangle(0, screenHeight-5, screenWidth, 5, rl.Red)
rl.DrawRectangle(10, 10, 250, 113, rl.Fade(rl.SkyBlue, 0.5))
rl.DrawRectangleLines(10, 10, 250, 113, rl.Blue)
rl.DrawText("Free 2d camera controls:", 20, 20, 10, rl.Black)
rl.DrawText("- Right/Left to move Offset", 40, 40, 10, rl.DarkGray)
rl.DrawText("- Mouse Wheel to Zoom in-out", 40, 60, 10, rl.DarkGray)
rl.DrawText("- A / S to Rotate", 40, 80, 10, rl.DarkGray)
rl.DrawText("- R to reset Zoom and Rotation", 40, 100, 10, rl.DarkGray)
rl.EndDrawing()
}
rl.CloseWindow()
}

View file

@ -0,0 +1,216 @@
package main
import (
"os"
rl "github.com/gen2brain/raylib-go/raylib"
)
const colorCount = 23
func main() {
const screenWidth, screenHeight int32 = 800, 450
rl.InitWindow(screenWidth, screenHeight, "raypaint")
// Colors to choose from
var colors = [colorCount]rl.Color{
rl.RayWhite, rl.Yellow, rl.Gold, rl.Orange, rl.Pink, rl.Red, rl.Maroon, rl.Green, rl.Lime, rl.DarkGreen,
rl.SkyBlue, rl.Blue, rl.DarkBlue, rl.Purple, rl.Violet, rl.DarkPurple, rl.Beige, rl.Brown, rl.DarkBrown,
rl.LightGray, rl.Gray, rl.DarkGray, rl.Black,
}
// Define colorsRecs data (for every rectangle)
var colorRecs = [colorCount]rl.Rectangle{}
for i := 0; i < colorCount; i++ {
colorRecs[i].X = float32(10 + 30*i + 2*i)
colorRecs[i].Y = 10
colorRecs[i].Width = 30
colorRecs[i].Height = 30
}
colorSelected := 0
colorSelectedPrev := colorSelected
colorMouseHover := 0
brushSize := 20
var btnSaveRec = rl.Rectangle{750, 10, 40, 30}
btnSaveMouseHover := false
showSaveMessage := false
saveMessageCounter := 0
checkSaveHover := func() rl.Color {
if btnSaveMouseHover {
return rl.Red
}
return rl.Black
}
// Create a RenderTexture2D to use as a canvas
var target rl.RenderTexture2D = rl.LoadRenderTexture(screenWidth, screenHeight)
// Clear render texture before entering the game loop
rl.BeginTextureMode(target)
rl.ClearBackground(colors[0])
rl.EndTextureMode()
rl.SetTargetFPS(120)
// Main game loop
for !rl.WindowShouldClose() {
mousePos := rl.GetMousePosition()
// Move between colors with keys
if rl.IsKeyPressed(rl.KeyRight) {
colorSelected++
} else if rl.IsKeyPressed(rl.KeyLeft) {
colorSelected--
}
if colorSelected >= colorCount {
colorSelected = colorCount - 1
} else if colorSelected < 0 {
colorSelected = 0
}
// Choose color with mouse
for i := 0; i < colorCount; i++ {
if rl.CheckCollisionPointRec(mousePos, colorRecs[i]) {
colorMouseHover = i
break
} else {
colorMouseHover = -1
}
}
if colorMouseHover >= 0 && rl.IsMouseButtonPressed(rl.MouseLeftButton) {
colorSelected = colorMouseHover
colorSelectedPrev = colorSelected
}
// Change brush size
brushSize += int(rl.GetMouseWheelMove() * 5)
if brushSize < 2 {
brushSize = 2
}
if brushSize > 50 {
brushSize = 50
}
if rl.IsKeyPressed(rl.KeyC) {
// Clear render texture to clear color
rl.BeginTextureMode(target)
rl.ClearBackground(colors[0])
rl.EndTextureMode()
}
if rl.IsMouseButtonDown(rl.MouseLeftButton) || rl.GetGestureDetected() == rl.GestureDrag {
// Clear render texture to clear color
rl.BeginTextureMode(target)
if mousePos.Y > 50 {
rl.DrawCircle(int32(mousePos.X), int32(mousePos.Y), float32(brushSize), colors[colorSelected])
}
rl.EndTextureMode()
}
if rl.IsMouseButtonDown(rl.MouseRightButton) {
colorSelected = 0
// Erase circle from render texture
rl.BeginTextureMode(target)
if mousePos.Y > 50 {
rl.DrawCircle(int32(mousePos.X), int32(mousePos.Y), float32(brushSize), colors[0])
}
rl.EndTextureMode()
} else {
colorSelected = colorSelectedPrev
}
if rl.CheckCollisionPointRec(mousePos, btnSaveRec) {
btnSaveMouseHover = true
} else {
btnSaveMouseHover = false
}
if btnSaveMouseHover && rl.IsMouseButtonReleased(rl.MouseLeftButton) || rl.IsKeyPressed(rl.KeyS) {
image := rl.LoadImageFromTexture(target.Texture)
rl.ImageFlipVertical(*&image)
rl.ExportImage(*image, "export.png")
rl.UnloadImage(image)
showSaveMessage = true
}
if showSaveMessage {
// On saving, show a full screen message for 2 seconds
saveMessageCounter++
if saveMessageCounter > 240 {
showSaveMessage = false
saveMessageCounter = 0
}
}
// Draw
//----------------------------------------------------------------------------------
rl.BeginDrawing()
rl.ClearBackground(rl.RayWhite)
// NOTE: Render texture must be y-flipped due to default OpenGL coordinates (left-bottom)
rl.DrawTextureRec(target.Texture, rl.Rectangle{0, 0, float32(target.Texture.Width), float32(-target.Texture.Height)}, rl.Vector2{0, 0}, rl.White)
if mousePos.Y > 50 {
if rl.IsMouseButtonDown(rl.MouseRightButton) {
rl.DrawCircleLines(int32(mousePos.X), int32(mousePos.Y), float32(brushSize), rl.Gray)
} else {
rl.DrawCircle(rl.GetMouseX(), rl.GetMouseY(), float32(brushSize), colors[colorSelected])
}
}
// Draw top panel
rl.DrawRectangle(0, 0, int32(rl.GetScreenWidth()), 50, rl.RayWhite)
rl.DrawLine(0, 50, int32(rl.GetScreenWidth()), 50, rl.LightGray)
// Draw color selection rectangles
for i := 0; i < colorCount; i++ {
rl.DrawRectangleRec(colorRecs[i], colors[i])
}
rl.DrawRectangleLines(10, 10, 30, 30, rl.LightGray)
if colorMouseHover >= 0 {
rl.DrawRectangleRec(colorRecs[colorMouseHover], rl.Fade(rl.White, 0.0))
}
rl.DrawRectangleLinesEx(rl.Rectangle{
colorRecs[colorSelected].X - 2, colorRecs[colorSelected].Y - 2, colorRecs[colorSelected].Width + 4, colorRecs[colorSelected].Height + 4,
}, 2, rl.Black)
// Draw save image button
rl.DrawRectangleLinesEx(btnSaveRec, 2, checkSaveHover())
rl.DrawText("SAVE!", 755, 20, 10, checkSaveHover())
if showSaveMessage {
rl.DrawRectangle(0, 0, int32(rl.GetScreenWidth()), int32(rl.GetScreenHeight()), rl.Fade(rl.RayWhite, 0.8))
rl.DrawRectangle(0, 150, int32(rl.GetScreenWidth()), 80, rl.Black)
rl.DrawText("IMAGE SAVED: export.png", 150, 180, 20, rl.RayWhite)
}
rl.EndDrawing()
}
// De-Initialization
//--------------------------------------------------------------------------------------
rl.UnloadRenderTexture(target)
rl.CloseWindow()
os.Exit(0)
}

View file

@ -0,0 +1,69 @@
package main
import (
"github.com/gen2brain/raylib-go/raylib"
)
const (
maxColumns = 20
)
func main() {
rl.InitWindow(800, 450, "raylib [core] example - 3d camera first person")
camera := rl.Camera3D{}
camera.Position = rl.NewVector3(4.0, 2.0, 4.0)
camera.Target = rl.NewVector3(0.0, 1.8, 0.0)
camera.Up = rl.NewVector3(0.0, 1.0, 0.0)
camera.Fovy = 60.0
camera.Projection = rl.CameraPerspective
// Generates some random columns
heights := make([]float32, maxColumns)
positions := make([]rl.Vector3, maxColumns)
colors := make([]rl.Color, maxColumns)
for i := 0; i < maxColumns; i++ {
heights[i] = float32(rl.GetRandomValue(1, 12))
positions[i] = rl.NewVector3(float32(rl.GetRandomValue(-15, 15)), heights[i]/2, float32(rl.GetRandomValue(-15, 15)))
colors[i] = rl.NewColor(uint8(rl.GetRandomValue(20, 255)), uint8(rl.GetRandomValue(10, 55)), 30, 255)
}
rl.SetCameraMode(camera, rl.CameraFirstPerson) // Set a first person camera mode
rl.SetTargetFPS(60)
for !rl.WindowShouldClose() {
rl.UpdateCamera(&camera) // Update camera
rl.BeginDrawing()
rl.ClearBackground(rl.RayWhite)
rl.BeginMode3D(camera)
rl.DrawPlane(rl.NewVector3(0.0, 0.0, 0.0), rl.NewVector2(32.0, 32.0), rl.LightGray) // Draw ground
rl.DrawCube(rl.NewVector3(-16.0, 2.5, 0.0), 1.0, 5.0, 32.0, rl.Blue) // Draw a blue wall
rl.DrawCube(rl.NewVector3(16.0, 2.5, 0.0), 1.0, 5.0, 32.0, rl.Lime) // Draw a green wall
rl.DrawCube(rl.NewVector3(0.0, 2.5, 16.0), 32.0, 5.0, 1.0, rl.Gold) // Draw a yellow wall
// Draw some cubes around
for i := 0; i < maxColumns; i++ {
rl.DrawCube(positions[i], 2.0, heights[i], 2.0, colors[i])
rl.DrawCubeWires(positions[i], 2.0, heights[i], 2.0, rl.Maroon)
}
rl.EndMode3D()
rl.DrawRectangle(10, 10, 220, 70, rl.Fade(rl.SkyBlue, 0.5))
rl.DrawRectangleLines(10, 10, 220, 70, rl.Blue)
rl.DrawText("First person camera default controls:", 20, 20, 10, rl.Black)
rl.DrawText("- Move with keys: W, A, S, D", 40, 40, 10, rl.DarkGray)
rl.DrawText("- Mouse move to look around", 40, 60, 10, rl.DarkGray)
rl.EndDrawing()
}
rl.CloseWindow()
}

View file

@ -0,0 +1,57 @@
package main
import (
"github.com/gen2brain/raylib-go/raylib"
)
func main() {
rl.InitWindow(800, 450, "raylib [core] example - 3d camera free")
camera := rl.Camera3D{}
camera.Position = rl.NewVector3(10.0, 10.0, 10.0)
camera.Target = rl.NewVector3(0.0, 0.0, 0.0)
camera.Up = rl.NewVector3(0.0, 1.0, 0.0)
camera.Fovy = 45.0
camera.Projection = rl.CameraPerspective
cubePosition := rl.NewVector3(0.0, 0.0, 0.0)
rl.SetCameraMode(camera, rl.CameraFree) // Set a free camera mode
rl.SetTargetFPS(60)
for !rl.WindowShouldClose() {
rl.UpdateCamera(&camera) // Update camera
if rl.IsKeyDown(rl.KeyZ) {
camera.Target = rl.NewVector3(0.0, 0.0, 0.0)
}
rl.BeginDrawing()
rl.ClearBackground(rl.RayWhite)
rl.BeginMode3D(camera)
rl.DrawCube(cubePosition, 2.0, 2.0, 2.0, rl.Red)
rl.DrawCubeWires(cubePosition, 2.0, 2.0, 2.0, rl.Maroon)
rl.DrawGrid(10, 1.0)
rl.EndMode3D()
rl.DrawRectangle(10, 10, 320, 133, rl.Fade(rl.SkyBlue, 0.5))
rl.DrawRectangleLines(10, 10, 320, 133, rl.Blue)
rl.DrawText("Free camera default controls:", 20, 20, 10, rl.Black)
rl.DrawText("- Mouse Wheel to Zoom in-out", 40, 40, 10, rl.DarkGray)
rl.DrawText("- Mouse Wheel Pressed to Pan", 40, 60, 10, rl.DarkGray)
rl.DrawText("- Alt + Mouse Wheel Pressed to Rotate", 40, 80, 10, rl.DarkGray)
rl.DrawText("- Alt + Ctrl + Mouse Wheel Pressed for Smooth Zoom", 40, 100, 10, rl.DarkGray)
rl.DrawText("- Z to zoom to (0, 0, 0)", 40, 120, 10, rl.DarkGray)
rl.EndDrawing()
}
rl.CloseWindow()
}

View file

@ -0,0 +1,43 @@
package main
import (
"github.com/gen2brain/raylib-go/raylib"
)
func main() {
rl.InitWindow(800, 450, "raylib [core] example - 3d mode")
camera := rl.Camera3D{}
camera.Position = rl.NewVector3(0.0, 10.0, 10.0)
camera.Target = rl.NewVector3(0.0, 0.0, 0.0)
camera.Up = rl.NewVector3(0.0, 1.0, 0.0)
camera.Fovy = 45.0
camera.Projection = rl.CameraPerspective
cubePosition := rl.NewVector3(0.0, 0.0, 0.0)
rl.SetTargetFPS(60)
for !rl.WindowShouldClose() {
rl.BeginDrawing()
rl.ClearBackground(rl.RayWhite)
rl.BeginMode3D(camera)
rl.DrawCube(cubePosition, 2.0, 2.0, 2.0, rl.Red)
rl.DrawCubeWires(cubePosition, 2.0, 2.0, 2.0, rl.Maroon)
rl.DrawGrid(10, 1.0)
rl.EndMode3D()
rl.DrawText("Welcome to the third dimension!", 10, 40, 20, rl.DarkGray)
rl.DrawFPS(10, 10)
rl.EndDrawing()
}
rl.CloseWindow()
}

View file

@ -0,0 +1,80 @@
package main
import (
rl "github.com/gen2brain/raylib-go/raylib"
)
func main() {
screenWidth := int32(800)
screenHeight := int32(450)
rl.InitWindow(screenWidth, screenHeight, "raylib [core] example - 3d picking")
camera := rl.Camera3D{}
camera.Position = rl.NewVector3(10.0, 10.0, 10.0)
camera.Target = rl.NewVector3(0.0, 0.0, 0.0)
camera.Up = rl.NewVector3(0.0, 1.0, 0.0)
camera.Fovy = 45.0
camera.Projection = rl.CameraPerspective
cubePosition := rl.NewVector3(0.0, 1.0, 0.0)
cubeSize := rl.NewVector3(2.0, 2.0, 2.0)
var ray rl.Ray
var collision rl.RayCollision
rl.SetCameraMode(camera, rl.CameraFree) // Set a free camera mode
rl.SetTargetFPS(60)
for !rl.WindowShouldClose() {
rl.UpdateCamera(&camera) // Update camera
if rl.IsMouseButtonPressed(rl.MouseLeftButton) {
if !collision.Hit {
ray = rl.GetMouseRay(rl.GetMousePosition(), camera)
// Check collision between ray and box
min := rl.NewVector3(cubePosition.X-cubeSize.X/2, cubePosition.Y-cubeSize.Y/2, cubePosition.Z-cubeSize.Z/2)
max := rl.NewVector3(cubePosition.X+cubeSize.X/2, cubePosition.Y+cubeSize.Y/2, cubePosition.Z+cubeSize.Z/2)
collision = rl.GetRayCollisionBox(ray, rl.NewBoundingBox(min, max))
} else {
collision.Hit = false
}
}
rl.BeginDrawing()
rl.ClearBackground(rl.RayWhite)
rl.BeginMode3D(camera)
if collision.Hit {
rl.DrawCube(cubePosition, cubeSize.X, cubeSize.Y, cubeSize.Z, rl.Red)
rl.DrawCubeWires(cubePosition, cubeSize.X, cubeSize.Y, cubeSize.Z, rl.Maroon)
rl.DrawCubeWires(cubePosition, cubeSize.X+0.2, cubeSize.Y+0.2, cubeSize.Z+0.2, rl.Green)
} else {
rl.DrawCube(cubePosition, cubeSize.X, cubeSize.Y, cubeSize.Z, rl.Gray)
rl.DrawCubeWires(cubePosition, cubeSize.X, cubeSize.Y, cubeSize.Z, rl.DarkGray)
}
rl.DrawRay(ray, rl.Maroon)
rl.DrawGrid(10, 1.0)
rl.EndMode3D()
rl.DrawText("Try selecting the box with mouse!", 240, 10, 20, rl.DarkGray)
if collision.Hit {
rl.DrawText("BOX SELECTED", (screenWidth-rl.MeasureText("BOX SELECTED", 30))/2, int32(float32(screenHeight)*0.1), 30, rl.Green)
}
rl.DrawFPS(10, 10)
rl.EndDrawing()
}
rl.CloseWindow()
}

View file

@ -0,0 +1,22 @@
package main
import "github.com/gen2brain/raylib-go/raylib"
func main() {
rl.SetConfigFlags(rl.FlagVsyncHint)
rl.InitWindow(800, 450, "raylib [core] example - basic window")
//rl.SetTargetFPS(60)
for !rl.WindowShouldClose() {
rl.BeginDrawing()
rl.ClearBackground(rl.RayWhite)
rl.DrawText("Congrats! You created your first window!", 190, 200, 20, rl.LightGray)
rl.EndDrawing()
}
rl.CloseWindow()
}

View file

@ -0,0 +1,70 @@
package main
import (
"github.com/gen2brain/raylib-go/raylib"
)
func main() {
rl.InitWindow(800, 450, "raylib [core] example - color selection (collision detection)")
colors := [21]rl.Color{
rl.DarkGray, rl.Maroon, rl.Orange, rl.DarkGreen, rl.DarkBlue, rl.DarkPurple,
rl.DarkBrown, rl.Gray, rl.Red, rl.Gold, rl.Lime, rl.Blue, rl.Violet, rl.Brown,
rl.LightGray, rl.Pink, rl.Yellow, rl.Green, rl.SkyBlue, rl.Purple, rl.Beige,
}
colorsRecs := make([]rl.Rectangle, 21) // Rectangles array
// Fills colorsRecs data (for every rectangle)
for i := 0; i < 21; i++ {
r := rl.Rectangle{}
r.X = float32(20 + 100*(i%7) + 10*(i%7))
r.Y = float32(60 + 100*(i/7) + 10*(i/7))
r.Width = 100
r.Height = 100
colorsRecs[i] = r
}
selected := make([]bool, 21) // Selected rectangles indicator
mousePoint := rl.Vector2{}
rl.SetTargetFPS(60)
for !rl.WindowShouldClose() {
mousePoint = rl.GetMousePosition()
for i := 0; i < 21; i++ { // Iterate along all the rectangles
if rl.CheckCollisionPointRec(mousePoint, colorsRecs[i]) {
colors[i].A = 120
if rl.IsMouseButtonPressed(rl.MouseLeftButton) {
selected[i] = !selected[i]
}
} else {
colors[i].A = 255
}
}
rl.BeginDrawing()
rl.ClearBackground(rl.RayWhite)
for i := 0; i < 21; i++ { // Draw all rectangles
rl.DrawRectangleRec(colorsRecs[i], colors[i])
// Draw four rectangles around selected rectangle
if selected[i] {
rl.DrawRectangle(int32(colorsRecs[i].X), int32(colorsRecs[i].Y), 100, 10, rl.RayWhite) // Square top rectangle
rl.DrawRectangle(int32(colorsRecs[i].X), int32(colorsRecs[i].Y), 10, 100, rl.RayWhite) // Square left rectangle
rl.DrawRectangle(int32(colorsRecs[i].X+90), int32(colorsRecs[i].Y), 10, 100, rl.RayWhite) // Square right rectangle
rl.DrawRectangle(int32(colorsRecs[i].X), int32(colorsRecs[i].Y)+90, 100, 10, rl.RayWhite) // Square bottom rectangle
}
}
rl.EndDrawing()
}
rl.CloseWindow()
}

View file

@ -0,0 +1,51 @@
package main
import (
"github.com/gen2brain/raylib-go/raylib"
)
func main() {
screenWidth := int32(800)
screenHeight := int32(450)
rl.InitWindow(screenWidth, screenHeight, "raylib [core] example - drop files")
rl.SetTargetFPS(60)
var count int
var droppedFiles []string
for !rl.WindowShouldClose() {
if rl.IsFileDropped() {
droppedFiles = rl.LoadDroppedFiles()
count = len(droppedFiles)
}
rl.BeginDrawing()
rl.ClearBackground(rl.RayWhite)
if count == 0 {
rl.DrawText("Drop your files to this window!", 100, 40, 20, rl.DarkGray)
} else {
rl.DrawText("Dropped files:", 100, 40, 20, rl.DarkGray)
for i := 0; i < count; i++ {
if i%2 == 0 {
rl.DrawRectangle(0, int32(85+40*i), screenWidth, 40, rl.Fade(rl.LightGray, 0.5))
} else {
rl.DrawRectangle(0, int32(85+40*i), screenWidth, 40, rl.Fade(rl.LightGray, 0.3))
}
rl.DrawText(droppedFiles[i], 120, int32(100+i*40), 10, rl.Gray)
}
rl.DrawText("Drop new files...", 100, int32(150+count*40), 20, rl.DarkGray)
}
rl.EndDrawing()
}
rl.UnloadDroppedFiles()
rl.CloseWindow()
}

View file

@ -0,0 +1,99 @@
package main
import (
"github.com/gen2brain/raylib-go/raylib"
)
var (
maxGestureStrings int = 20
)
func main() {
screenWidth := int32(800)
screenHeight := int32(450)
rl.InitWindow(screenWidth, screenHeight, "raylib [core] example - gestures detection")
touchPosition := rl.NewVector2(0, 0)
touchArea := rl.NewRectangle(220, 10, float32(screenWidth)-230, float32(screenHeight)-20)
gestureStrings := make([]string, 0)
currentGesture := rl.GestureNone
lastGesture := rl.GestureNone
//rl.SetGesturesEnabled(uint32(rl.GestureHold | rl.GestureDrag)) // Enable only some gestures to be detected
rl.SetTargetFPS(60)
for !rl.WindowShouldClose() {
lastGesture = currentGesture
currentGesture = rl.GetGestureDetected()
touchPosition = rl.GetTouchPosition(0)
if rl.CheckCollisionPointRec(touchPosition, touchArea) && currentGesture != rl.GestureNone {
if currentGesture != lastGesture {
switch currentGesture {
case rl.GestureTap:
gestureStrings = append(gestureStrings, "GESTURE TAP")
case rl.GestureDoubletap:
gestureStrings = append(gestureStrings, "GESTURE DOUBLETAP")
case rl.GestureHold:
gestureStrings = append(gestureStrings, "GESTURE HOLD")
case rl.GestureDrag:
gestureStrings = append(gestureStrings, "GESTURE DRAG")
case rl.GestureSwipeRight:
gestureStrings = append(gestureStrings, "GESTURE SWIPE RIGHT")
case rl.GestureSwipeLeft:
gestureStrings = append(gestureStrings, "GESTURE SWIPE LEFT")
case rl.GestureSwipeUp:
gestureStrings = append(gestureStrings, "GESTURE SWIPE UP")
case rl.GestureSwipeDown:
gestureStrings = append(gestureStrings, "GESTURE SWIPE DOWN")
case rl.GesturePinchIn:
gestureStrings = append(gestureStrings, "GESTURE PINCH IN")
case rl.GesturePinchOut:
gestureStrings = append(gestureStrings, "GESTURE PINCH OUT")
}
if len(gestureStrings) >= maxGestureStrings {
gestureStrings = make([]string, 0)
}
}
}
rl.BeginDrawing()
rl.ClearBackground(rl.RayWhite)
rl.DrawRectangleRec(touchArea, rl.Gray)
rl.DrawRectangle(225, 15, screenWidth-240, screenHeight-30, rl.RayWhite)
rl.DrawText("GESTURES TEST AREA", screenWidth-270, screenHeight-40, 20, rl.Fade(rl.Gray, 0.5))
for i := 0; i < len(gestureStrings); i++ {
if i%2 == 0 {
rl.DrawRectangle(10, int32(30+20*i), 200, 20, rl.Fade(rl.LightGray, 0.5))
} else {
rl.DrawRectangle(10, int32(30+20*i), 200, 20, rl.Fade(rl.LightGray, 0.3))
}
if i < len(gestureStrings)-1 {
rl.DrawText(gestureStrings[i], 35, int32(36+20*i), 10, rl.DarkGray)
} else {
rl.DrawText(gestureStrings[i], 35, int32(36+20*i), 10, rl.Maroon)
}
}
rl.DrawRectangleLines(10, 29, 200, screenHeight-50, rl.Gray)
rl.DrawText("DETECTED GESTURES", 50, 15, 10, rl.Gray)
if currentGesture != rl.GestureNone {
rl.DrawCircleV(touchPosition, 30, rl.Maroon)
}
rl.EndDrawing()
}
rl.CloseWindow()
}

View file

@ -0,0 +1,201 @@
package main
import (
"fmt"
rl "github.com/gen2brain/raylib-go/raylib"
)
const (
xbox360NameID = "Xbox 360 Controller"
ps3NameID = "PLAYSTATION(R)3 Controller"
)
func main() {
rl.SetConfigFlags(rl.FlagMsaa4xHint) // Set MSAA 4X hint before windows creation
rl.InitWindow(800, 450, "raylib [core] example - gamepad input")
texPs3Pad := rl.LoadTexture("ps3.png")
texXboxPad := rl.LoadTexture("xbox.png")
rl.SetTargetFPS(60)
for !rl.WindowShouldClose() {
rl.BeginDrawing()
rl.ClearBackground(rl.RayWhite)
if rl.IsGamepadAvailable(rl.GamepadPlayer1) {
rl.DrawText(fmt.Sprintf("GP1: %s", rl.GetGamepadName(rl.GamepadPlayer1)), 10, 10, 10, rl.Black)
if rl.GetGamepadName(rl.GamepadPlayer1) == xbox360NameID {
rl.DrawTexture(texXboxPad, 0, 0, rl.DarkGray)
// Draw buttons: xbox home
if rl.IsGamepadButtonDown(rl.GamepadPlayer1, rl.GamepadXboxButtonHome) {
rl.DrawCircle(394, 89, 19, rl.Red)
}
// Draw buttons: basic
if rl.IsGamepadButtonDown(rl.GamepadPlayer1, rl.GamepadXboxButtonStart) {
rl.DrawCircle(436, 150, 9, rl.Red)
}
if rl.IsGamepadButtonDown(rl.GamepadPlayer1, rl.GamepadXboxButtonSelect) {
rl.DrawCircle(352, 150, 9, rl.Red)
}
if rl.IsGamepadButtonDown(rl.GamepadPlayer1, rl.GamepadXboxButtonX) {
rl.DrawCircle(501, 151, 15, rl.Blue)
}
if rl.IsGamepadButtonDown(rl.GamepadPlayer1, rl.GamepadXboxButtonA) {
rl.DrawCircle(536, 187, 15, rl.Lime)
}
if rl.IsGamepadButtonDown(rl.GamepadPlayer1, rl.GamepadXboxButtonB) {
rl.DrawCircle(572, 151, 15, rl.Maroon)
}
if rl.IsGamepadButtonDown(rl.GamepadPlayer1, rl.GamepadXboxButtonY) {
rl.DrawCircle(536, 115, 15, rl.Gold)
}
// Draw buttons: d-pad
rl.DrawRectangle(317, 202, 19, 71, rl.Black)
rl.DrawRectangle(293, 228, 69, 19, rl.Black)
if rl.IsGamepadButtonDown(rl.GamepadPlayer1, rl.GamepadXboxButtonUp) {
rl.DrawRectangle(317, 202, 19, 26, rl.Red)
}
if rl.IsGamepadButtonDown(rl.GamepadPlayer1, rl.GamepadXboxButtonDown) {
rl.DrawRectangle(317, 202+45, 19, 26, rl.Red)
}
if rl.IsGamepadButtonDown(rl.GamepadPlayer1, rl.GamepadXboxButtonLeft) {
rl.DrawRectangle(292, 228, 25, 19, rl.Red)
}
if rl.IsGamepadButtonDown(rl.GamepadPlayer1, rl.GamepadXboxButtonRight) {
rl.DrawRectangle(292+44, 228, 26, 19, rl.Red)
}
// Draw buttons: left-right back
if rl.IsGamepadButtonDown(rl.GamepadPlayer1, rl.GamepadXboxButtonLb) {
rl.DrawCircle(259, 61, 20, rl.Red)
}
if rl.IsGamepadButtonDown(rl.GamepadPlayer1, rl.GamepadXboxButtonRb) {
rl.DrawCircle(536, 61, 20, rl.Red)
}
// Draw axis: left joystick
rl.DrawCircle(259, 152, 39, rl.Black)
rl.DrawCircle(259, 152, 34, rl.LightGray)
rl.DrawCircle(int32(259+(rl.GetGamepadAxisMovement(rl.GamepadPlayer1, rl.GamepadXboxAxisLeftX)*20)),
int32(152-(rl.GetGamepadAxisMovement(rl.GamepadPlayer1, rl.GamepadXboxAxisLeftY)*20)), 25, rl.Black)
// Draw axis: right joystick
rl.DrawCircle(461, 237, 38, rl.Black)
rl.DrawCircle(461, 237, 33, rl.LightGray)
rl.DrawCircle(int32(461+(rl.GetGamepadAxisMovement(rl.GamepadPlayer1, rl.GamepadXboxAxisRightX)*20)),
int32(237-(rl.GetGamepadAxisMovement(rl.GamepadPlayer1, rl.GamepadXboxAxisRightY)*20)), 25, rl.Black)
// Draw axis: left-right triggers
rl.DrawRectangle(170, 30, 15, 70, rl.Gray)
rl.DrawRectangle(604, 30, 15, 70, rl.Gray)
rl.DrawRectangle(170, 30, 15, int32(((1.0+rl.GetGamepadAxisMovement(rl.GamepadPlayer1, rl.GamepadXboxAxisLt))/2.0)*70), rl.Red)
rl.DrawRectangle(604, 30, 15, int32(((1.0+rl.GetGamepadAxisMovement(rl.GamepadPlayer1, rl.GamepadXboxAxisRt))/2.0)*70), rl.Red)
} else if rl.GetGamepadName(rl.GamepadPlayer1) == ps3NameID {
rl.DrawTexture(texPs3Pad, 0, 0, rl.DarkGray)
// Draw buttons: ps
if rl.IsGamepadButtonDown(rl.GamepadPlayer1, rl.GamepadPs3ButtonPs) {
rl.DrawCircle(396, 222, 13, rl.Red)
}
// Draw buttons: basic
if rl.IsGamepadButtonDown(rl.GamepadPlayer1, rl.GamepadPs3ButtonSelect) {
rl.DrawRectangle(328, 170, 32, 13, rl.Red)
}
if rl.IsGamepadButtonDown(rl.GamepadPlayer1, rl.GamepadPs3ButtonStart) {
rl.DrawTriangle(rl.NewVector2(436, 168), rl.NewVector2(436, 185), rl.NewVector2(464, 177), rl.Red)
}
if rl.IsGamepadButtonDown(rl.GamepadPlayer1, rl.GamepadPs3ButtonTriangle) {
rl.DrawCircle(557, 144, 13, rl.Lime)
}
if rl.IsGamepadButtonDown(rl.GamepadPlayer1, rl.GamepadPs3ButtonCircle) {
rl.DrawCircle(586, 173, 13, rl.Red)
}
if rl.IsGamepadButtonDown(rl.GamepadPlayer1, rl.GamepadPs3ButtonCross) {
rl.DrawCircle(557, 203, 13, rl.Violet)
}
if rl.IsGamepadButtonDown(rl.GamepadPlayer1, rl.GamepadPs3ButtonSquare) {
rl.DrawCircle(527, 173, 13, rl.Pink)
}
// Draw buttons: d-pad
rl.DrawRectangle(225, 132, 24, 84, rl.Black)
rl.DrawRectangle(195, 161, 84, 25, rl.Black)
if rl.IsGamepadButtonDown(rl.GamepadPlayer1, rl.GamepadPs3ButtonUp) {
rl.DrawRectangle(225, 132, 24, 29, rl.Red)
}
if rl.IsGamepadButtonDown(rl.GamepadPlayer1, rl.GamepadPs3ButtonDown) {
rl.DrawRectangle(225, 132+54, 24, 30, rl.Red)
}
if rl.IsGamepadButtonDown(rl.GamepadPlayer1, rl.GamepadPs3ButtonLeft) {
rl.DrawRectangle(195, 161, 30, 25, rl.Red)
}
if rl.IsGamepadButtonDown(rl.GamepadPlayer1, rl.GamepadPs3ButtonRight) {
rl.DrawRectangle(195+54, 161, 30, 25, rl.Red)
}
// Draw buttons: left-right back buttons
if rl.IsGamepadButtonDown(rl.GamepadPlayer1, rl.GamepadPs3ButtonL1) {
rl.DrawCircle(239, 82, 20, rl.Red)
}
if rl.IsGamepadButtonDown(rl.GamepadPlayer1, rl.GamepadPs3ButtonR1) {
rl.DrawCircle(557, 82, 20, rl.Red)
}
// Draw axis: left joystick
rl.DrawCircle(319, 255, 35, rl.Black)
rl.DrawCircle(319, 255, 31, rl.LightGray)
rl.DrawCircle(int32(319+(rl.GetGamepadAxisMovement(rl.GamepadPlayer1, rl.GamepadPs3AxisLeftX)*20)),
int32(255+(rl.GetGamepadAxisMovement(rl.GamepadPlayer1, rl.GamepadPs3AxisLeftY)*20)), 25, rl.Black)
// Draw axis: right joystick
rl.DrawCircle(475, 255, 35, rl.Black)
rl.DrawCircle(475, 255, 31, rl.LightGray)
rl.DrawCircle(int32(475+(rl.GetGamepadAxisMovement(rl.GamepadPlayer1, rl.GamepadPs3AxisRightX)*20)),
int32(255+(rl.GetGamepadAxisMovement(rl.GamepadPlayer1, rl.GamepadPs3AxisRightY)*20)), 25, rl.Black)
// Draw axis: left-right triggers
rl.DrawRectangle(169, 48, 15, 70, rl.Gray)
rl.DrawRectangle(611, 48, 15, 70, rl.Gray)
rl.DrawRectangle(169, 48, 15, int32(((1.0-rl.GetGamepadAxisMovement(rl.GamepadPlayer1, rl.GamepadPs3AxisL2))/2.0)*70), rl.Red)
rl.DrawRectangle(611, 48, 15, int32(((1.0-rl.GetGamepadAxisMovement(rl.GamepadPlayer1, rl.GamepadPs3AxisR2))/2.0)*70), rl.Red)
} else {
rl.DrawText("- GENERIC GAMEPAD -", 280, 180, 20, rl.Gray)
// TODO: Draw generic gamepad
}
rl.DrawText(fmt.Sprintf("DETECTED AXIS [%d]:", rl.GetGamepadAxisCount(rl.GamepadPlayer1)), 10, 50, 10, rl.Maroon)
for i := int32(0); i < rl.GetGamepadAxisCount(rl.GamepadPlayer1); i++ {
rl.DrawText(fmt.Sprintf("AXIS %d: %.02f", i, rl.GetGamepadAxisMovement(rl.GamepadPlayer1, i)), 20, 70+20*i, 10, rl.DarkGray)
}
if rl.GetGamepadButtonPressed() != -1 {
rl.DrawText(fmt.Sprintf("DETECTED BUTTON: %d", rl.GetGamepadButtonPressed()), 10, 430, 10, rl.Red)
} else {
rl.DrawText("DETECTED BUTTON: NONE", 10, 430, 10, rl.Gray)
}
} else {
rl.DrawText("GP1: NOT DETECTED", 10, 10, 10, rl.Gray)
rl.DrawTexture(texXboxPad, 0, 0, rl.LightGray)
}
rl.EndDrawing()
}
rl.UnloadTexture(texPs3Pad)
rl.UnloadTexture(texXboxPad)
rl.CloseWindow()
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

View file

@ -0,0 +1,41 @@
package main
import (
"github.com/gen2brain/raylib-go/raylib"
)
func main() {
screenWidth := int32(800)
screenHeight := int32(450)
rl.InitWindow(screenWidth, screenHeight, "raylib [core] example - keyboard input")
ballPosition := rl.NewVector2(float32(screenWidth)/2, float32(screenHeight)/2)
rl.SetTargetFPS(60)
for !rl.WindowShouldClose() {
if rl.IsKeyDown(rl.KeyRight) {
ballPosition.X += 0.8
}
if rl.IsKeyDown(rl.KeyLeft) {
ballPosition.X -= 0.8
}
if rl.IsKeyDown(rl.KeyUp) {
ballPosition.Y -= 0.8
}
if rl.IsKeyDown(rl.KeyDown) {
ballPosition.Y += 0.8
}
rl.BeginDrawing()
rl.ClearBackground(rl.RayWhite)
rl.DrawText("move the ball with arrow keys", 10, 10, 20, rl.DarkGray)
rl.DrawCircleV(ballPosition, 50, rl.Maroon)
rl.EndDrawing()
}
rl.CloseWindow()
}

View file

@ -0,0 +1,35 @@
package main
import (
"github.com/gen2brain/raylib-go/raylib"
)
func main() {
rl.InitWindow(800, 450, "raylib [core] example - mouse input")
rl.SetTargetFPS(60)
ballColor := rl.DarkBlue
for !rl.WindowShouldClose() {
ballPosition := rl.GetMousePosition()
if rl.IsMouseButtonPressed(rl.MouseLeftButton) {
ballColor = rl.Maroon
} else if rl.IsMouseButtonPressed(rl.MouseMiddleButton) {
ballColor = rl.Lime
} else if rl.IsMouseButtonPressed(rl.MouseRightButton) {
ballColor = rl.DarkBlue
}
rl.BeginDrawing()
rl.ClearBackground(rl.RayWhite)
rl.DrawCircleV(ballPosition, 40, ballColor)
rl.DrawText("move ball with mouse and click mouse button to change color", 10, 10, 20, rl.DarkGray)
rl.EndDrawing()
}
rl.CloseWindow()
}

View file

@ -0,0 +1,36 @@
package main
import (
"fmt"
rl "github.com/gen2brain/raylib-go/raylib"
)
func main() {
screenWidth := int32(800)
screenHeight := int32(450)
rl.InitWindow(screenWidth, screenHeight, "raylib [core] example - mouse wheel")
boxPositionY := screenHeight/2 - 40
scrollSpeed := int32(4) // Scrolling speed in pixels
rl.SetTargetFPS(60)
for !rl.WindowShouldClose() {
boxPositionY -= int32(rl.GetMouseWheelMove()) * scrollSpeed
rl.BeginDrawing()
rl.ClearBackground(rl.RayWhite)
rl.DrawRectangle(screenWidth/2-40, boxPositionY, 80, 80, rl.Maroon)
rl.DrawText("Use mouse wheel to move the square up and down!", 10, 10, 20, rl.Gray)
rl.DrawText(fmt.Sprintf("Box position Y: %d", boxPositionY), 10, 40, 20, rl.LightGray)
rl.EndDrawing()
}
rl.CloseWindow()
}

View file

@ -0,0 +1,38 @@
package main
import (
"fmt"
"github.com/gen2brain/raylib-go/raylib"
)
func main() {
rl.InitWindow(800, 450, "raylib [core] example - generate random values")
framesCounter := 0 // Variable used to count frames
randValue := rl.GetRandomValue(-8, 5) // Get a random integer number between -8 and 5 (both included)
rl.SetTargetFPS(60)
for !rl.WindowShouldClose() {
framesCounter++
// Every two seconds (120 frames) a new random value is generated
if ((framesCounter / 120) % 2) == 1 {
randValue = rl.GetRandomValue(-8, 5)
framesCounter = 0
}
rl.BeginDrawing()
rl.ClearBackground(rl.RayWhite)
rl.DrawText("Every 2 seconds a new random value is generated:", 130, 100, 20, rl.Maroon)
rl.DrawText(fmt.Sprintf("%d", randValue), 360, 180, 80, rl.LightGray)
rl.EndDrawing()
}
rl.CloseWindow()
}

View file

@ -0,0 +1,52 @@
package main
import (
"github.com/gen2brain/raylib-go/raylib"
)
func main() {
screenWidth := int32(800)
screenHeight := int32(450)
rl.InitWindow(screenWidth, screenHeight, "raylib [core] example - 3d camera free")
camera := rl.Camera{}
camera.Position = rl.NewVector3(10.0, 10.0, 10.0) // Camera position
camera.Target = rl.NewVector3(0.0, 0.0, 0.0) // Camera looking at point
camera.Up = rl.NewVector3(0.0, 1.0, 0.0) // Camera up vector (rotation towards target)
camera.Fovy = 45.0 // Camera field-of-view Y
cubePosition := rl.NewVector3(0.0, 0.0, 0.0)
cubeScreenPosition := rl.Vector2{}
rl.SetCameraMode(camera, rl.CameraFree) // Set a free camera mode
rl.SetTargetFPS(60)
for !rl.WindowShouldClose() {
rl.UpdateCamera(&camera) // Update camera
// Calculate cube screen space position (with a little offset to be in top)
cubeScreenPosition = rl.GetWorldToScreen(rl.NewVector3(cubePosition.X, cubePosition.Y+2.5, cubePosition.Z), camera)
rl.BeginDrawing()
rl.ClearBackground(rl.RayWhite)
rl.BeginMode3D(camera)
rl.DrawCube(cubePosition, 2.0, 2.0, 2.0, rl.Red)
rl.DrawCubeWires(cubePosition, 2.0, 2.0, 2.0, rl.Maroon)
rl.DrawGrid(10, 1.0)
rl.EndMode3D()
rl.DrawText("Enemy: 100 / 100", int32(cubeScreenPosition.X)-rl.MeasureText("Enemy: 100 / 100", 20)/2, int32(cubeScreenPosition.Y), 20, rl.Black)
rl.DrawText("Text is always on top of the cube", (screenWidth-rl.MeasureText("Text is always on top of the cube", 20))/2, 25, 20, rl.Gray)
rl.EndDrawing()
}
rl.CloseWindow()
}