diff --git a/examples/shapes/collision_area/main.go b/examples/shapes/collision_area/main.go new file mode 100644 index 0000000..005c8c6 --- /dev/null +++ b/examples/shapes/collision_area/main.go @@ -0,0 +1,88 @@ +package main + +import ( + "fmt" + + rl "github.com/gen2brain/raylib-go/raylib" +) + +var ( + screenWidth = int32(1280) + screenHeight = int32(720) + + pause = false + collision = false + + boxCollision = rl.Rectangle{} + screenUpperLimit = float32(40) + boxAspeedX = float32(4) +) + +func main() { + + rl.InitWindow(screenWidth, screenHeight, "raylib [shapes] example - collision area") + + rl.SetTargetFPS(60) + + boxA := rl.NewRectangle(10, float32(rl.GetScreenHeight()/2)-50, 200, 100) + boxB := rl.NewRectangle(float32(rl.GetScreenWidth()/2)-30, float32(rl.GetScreenHeight()/2)-30, 60, 60) + + for !rl.WindowShouldClose() { + + mousePos := rl.GetMousePosition() + + if !pause { + boxA.X += boxAspeedX + } + if boxA.X+boxA.Width >= float32(rl.GetScreenWidth()) || boxA.X <= 0 { + boxAspeedX *= -1 + } + + boxB.X = mousePos.X - boxB.Width/2 + boxB.Y = mousePos.Y - boxB.Height/2 + + if boxB.X+boxB.Width >= float32(rl.GetScreenWidth()) { + boxB.X = float32(rl.GetScreenWidth()) - boxB.Width + } else if boxB.X <= 0 { + boxB.X = 0 + } + + if boxB.Y+boxB.Height >= float32(rl.GetScreenHeight()) { + boxB.Y = float32(rl.GetScreenHeight()) - boxB.Height + } else if boxB.X <= screenUpperLimit { + boxB.Y = screenUpperLimit + } + + collision := rl.CheckCollisionRecs(boxA, boxB) + + if collision { + boxCollision = rl.GetCollisionRec(boxA, boxB) + } + + if rl.IsKeyPressed(rl.KeySpace) { + pause = !pause + } + + rl.BeginDrawing() + rl.ClearBackground(rl.RayWhite) + + if collision { + rl.DrawRectangle(0, 0, screenWidth, int32(screenUpperLimit), rl.Red) + rl.DrawRectangleRec(boxCollision, rl.Lime) + rl.DrawText("COLLISION", int32(rl.GetScreenWidth()/2)-(rl.MeasureText("COLLISION", 20)/2), int32(screenUpperLimit/2)-10, 20, rl.Black) + rl.DrawText("Collision Area: "+fmt.Sprint(boxCollision.Width*boxCollision.Height), int32(rl.GetScreenWidth()/2)-100, int32(screenUpperLimit+10), 20, rl.Black) + } else { + rl.DrawRectangle(0, 0, screenWidth, int32(screenUpperLimit), rl.Black) + } + + rl.DrawRectangleRec(boxA, rl.Orange) + rl.DrawRectangleRec(boxB, rl.Blue) + + rl.DrawText("Press SPACE to PAUSE/RESUME", 20, int32(rl.GetScreenHeight())-35, 20, rl.Black) + + rl.EndDrawing() + + } + + rl.CloseWindow() +} diff --git a/examples/shapes/rectangle_scaling/main.go b/examples/shapes/rectangle_scaling/main.go new file mode 100644 index 0000000..d72e790 --- /dev/null +++ b/examples/shapes/rectangle_scaling/main.go @@ -0,0 +1,78 @@ +package main + +import ( + rl "github.com/gen2brain/raylib-go/raylib" +) + +var ( + mouseScaleMarkSize = float32(12) + rec = rl.NewRectangle(100, 100, 200, 80) + mouseScaleReady, MouseScaleMode = false, false +) + +func main() { + screenWidth := int32(1280) + screenHeight := int32(720) + + rl.InitWindow(screenWidth, screenHeight, "raylib [shapes] example - rectangle scaling mouse") + + rl.SetTargetFPS(60) + + rl.SetMousePosition(0, 0) + + for !rl.WindowShouldClose() { + + mousePos := rl.GetMousePosition() + + if rl.CheckCollisionPointRec(mousePos, rl.NewRectangle(rec.X+rec.Width-mouseScaleMarkSize, rec.Y+rec.Height-mouseScaleMarkSize, mouseScaleMarkSize, mouseScaleMarkSize)) { + mouseScaleReady = true + if rl.IsMouseButtonPressed(rl.MouseLeftButton) { + MouseScaleMode = true + } + } else { + mouseScaleReady = false + } + + if MouseScaleMode { + + mouseScaleReady = true + rec.Width = mousePos.X - rec.X + rec.Height = mousePos.Y - rec.Y + + // CHECK MIN MAX REC SIZES + if rec.Width < mouseScaleMarkSize { + rec.Width = rec.Width + } + if rec.Height < mouseScaleMarkSize { + rec.Height = rec.Width + } + if rec.Width > (float32(rl.GetScreenWidth()) - rec.X) { + rec.Width = float32(rl.GetScreenWidth()) - rec.X + } + if rec.Height > (float32(rl.GetScreenHeight()) - rec.Y) { + rec.Height = float32(rl.GetScreenHeight()) - rec.Y + } + if rl.IsMouseButtonReleased(rl.MouseLeftButton) { + MouseScaleMode = false + } + + } + + rl.BeginDrawing() + rl.ClearBackground(rl.RayWhite) + + rl.DrawText("Scale rectangle dragging from bottom-right corner!", 10, 10, 20, rl.Black) + + rl.DrawRectangleRec(rec, rl.Fade(rl.Green, 0.5)) + + if mouseScaleReady { + rl.DrawRectangleLinesEx(rec, 1, rl.Red) + rl.DrawTriangle(rl.NewVector2(rec.X+rec.Width-mouseScaleMarkSize, rec.Y+rec.Height), rl.NewVector2(rec.X+rec.Width, rec.Y+rec.Height), rl.NewVector2(rec.X+rec.Width, rec.Y+rec.Height-mouseScaleMarkSize), rl.Red) + } + + rl.EndDrawing() + + } + + rl.CloseWindow() +}