add core examples
This commit is contained in:
parent
1a9e976d4c
commit
34665bd091
3 changed files with 310 additions and 0 deletions
128
examples/core/2d_camera_splitscreen/main.go
Normal file
128
examples/core/2d_camera_splitscreen/main.go
Normal file
|
@ -0,0 +1,128 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
rl "github.com/gen2brain/raylib-go/raylib"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
screenW = int32(800)
|
||||||
|
screenH = int32(440)
|
||||||
|
playerSize = float32(40)
|
||||||
|
cam1, cam2 rl.Camera2D
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
|
||||||
|
rl.InitWindow(screenW, screenH, "raylib [core] example - 2d camera split screen")
|
||||||
|
|
||||||
|
player1 := rl.NewRectangle(200, 200, playerSize, playerSize)
|
||||||
|
player2 := rl.NewRectangle(250, 200, playerSize, playerSize)
|
||||||
|
|
||||||
|
cam1.Target = rl.NewVector2(player1.X, player1.Y)
|
||||||
|
cam1.Offset = rl.NewVector2(200, 200)
|
||||||
|
cam1.Rotation = 0
|
||||||
|
cam1.Zoom = 1
|
||||||
|
|
||||||
|
cam2 = cam1
|
||||||
|
cam2.Target = rl.NewVector2(player2.X, player2.Y)
|
||||||
|
|
||||||
|
screenCam1 := rl.LoadRenderTexture(screenW/2, screenH)
|
||||||
|
screenCam2 := rl.LoadRenderTexture(screenW/2, screenH)
|
||||||
|
|
||||||
|
splitScreenRec := rl.NewRectangle(0, 0, float32(screenCam1.Texture.Width), -float32(screenCam1.Texture.Height))
|
||||||
|
|
||||||
|
rl.SetTargetFPS(60)
|
||||||
|
|
||||||
|
for !rl.WindowShouldClose() {
|
||||||
|
|
||||||
|
if rl.IsKeyDown(rl.KeyS) {
|
||||||
|
player1.Y += 3
|
||||||
|
} else if rl.IsKeyDown(rl.KeyW) {
|
||||||
|
player1.Y -= 3
|
||||||
|
}
|
||||||
|
if rl.IsKeyDown(rl.KeyD) {
|
||||||
|
player1.X += 3
|
||||||
|
} else if rl.IsKeyDown(rl.KeyA) {
|
||||||
|
player1.X -= 3
|
||||||
|
}
|
||||||
|
|
||||||
|
if rl.IsKeyDown(rl.KeyUp) {
|
||||||
|
player2.Y -= 3
|
||||||
|
} else if rl.IsKeyDown(rl.KeyDown) {
|
||||||
|
player2.Y += 3
|
||||||
|
}
|
||||||
|
if rl.IsKeyDown(rl.KeyRight) {
|
||||||
|
player2.X += 3
|
||||||
|
} else if rl.IsKeyDown(rl.KeyLeft) {
|
||||||
|
player2.X -= 3
|
||||||
|
}
|
||||||
|
|
||||||
|
cam1.Target = rl.NewVector2(player1.X, player1.Y)
|
||||||
|
cam2.Target = rl.NewVector2(player2.X, player2.Y)
|
||||||
|
|
||||||
|
rl.BeginTextureMode(screenCam1)
|
||||||
|
rl.ClearBackground(rl.RayWhite)
|
||||||
|
rl.BeginMode2D(cam1)
|
||||||
|
|
||||||
|
for i := 0; i < int(screenW/int32(playerSize))+1; i++ {
|
||||||
|
rl.DrawLineV(rl.NewVector2(playerSize*float32(i), 0), rl.NewVector2(playerSize*float32(i), float32(screenH)), rl.LightGray)
|
||||||
|
}
|
||||||
|
for i := 0; i < int(screenH/int32(playerSize))+1; i++ {
|
||||||
|
rl.DrawLineV(rl.NewVector2(0, playerSize*float32(i)), rl.NewVector2(float32(screenW), playerSize*float32(i)), rl.LightGray)
|
||||||
|
}
|
||||||
|
for i := 0; i < int(screenW/int32(playerSize)); i++ {
|
||||||
|
for j := 0; j < int(screenH/int32(playerSize)); j++ {
|
||||||
|
rl.DrawText("["+fmt.Sprint(i)+","+fmt.Sprint(j)+"]", 10+int32(playerSize*float32(i)), 15+int32(playerSize*float32(j)), 10, rl.LightGray)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
rl.DrawRectangleRec(player1, rl.Red)
|
||||||
|
rl.DrawRectangleRec(player2, rl.Blue)
|
||||||
|
rl.EndMode2D()
|
||||||
|
|
||||||
|
rl.DrawRectangle(0, 0, screenW/2, 30, rl.Fade(rl.RayWhite, 0.6))
|
||||||
|
rl.DrawText("PLAYER 1 WASD KEYS", 10, 10, 10, rl.Maroon)
|
||||||
|
rl.EndTextureMode()
|
||||||
|
|
||||||
|
rl.BeginTextureMode(screenCam2)
|
||||||
|
rl.ClearBackground(rl.RayWhite)
|
||||||
|
rl.BeginMode2D(cam2)
|
||||||
|
|
||||||
|
for i := 0; i < int(screenW/int32(playerSize))+1; i++ {
|
||||||
|
rl.DrawLineV(rl.NewVector2(playerSize*float32(i), 0), rl.NewVector2(playerSize*float32(i), float32(screenH)), rl.LightGray)
|
||||||
|
}
|
||||||
|
for i := 0; i < int(screenH/int32(playerSize))+1; i++ {
|
||||||
|
rl.DrawLineV(rl.NewVector2(0, playerSize*float32(i)), rl.NewVector2(float32(screenW), playerSize*float32(i)), rl.LightGray)
|
||||||
|
}
|
||||||
|
for i := 0; i < int(screenW/int32(playerSize)); i++ {
|
||||||
|
for j := 0; j < int(screenH/int32(playerSize)); j++ {
|
||||||
|
rl.DrawText("["+fmt.Sprint(i)+","+fmt.Sprint(j)+"]", 10+int32(playerSize*float32(i)), 15+int32(playerSize*float32(j)), 10, rl.LightGray)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
rl.DrawRectangleRec(player1, rl.Red)
|
||||||
|
rl.DrawRectangleRec(player2, rl.Blue)
|
||||||
|
rl.EndMode2D()
|
||||||
|
|
||||||
|
rl.DrawRectangle(0, 0, screenW/2, 30, rl.Fade(rl.RayWhite, 0.6))
|
||||||
|
rl.DrawText("PLAYER 2 ARROW KEYS", 10, 10, 10, rl.Maroon)
|
||||||
|
rl.EndTextureMode()
|
||||||
|
|
||||||
|
rl.BeginDrawing()
|
||||||
|
rl.ClearBackground(rl.Black)
|
||||||
|
|
||||||
|
rl.DrawTextureRec(screenCam1.Texture, splitScreenRec, rl.NewVector2(0, 0), rl.White)
|
||||||
|
rl.DrawTextureRec(screenCam2.Texture, splitScreenRec, rl.NewVector2(float32(screenW/2), 0), rl.White)
|
||||||
|
rl.DrawRectangle((screenW/2)-2, 0, 4, screenH, rl.LightGray)
|
||||||
|
|
||||||
|
rl.EndDrawing()
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
rl.UnloadRenderTexture(screenCam1)
|
||||||
|
rl.UnloadRenderTexture(screenCam2)
|
||||||
|
|
||||||
|
rl.CloseWindow()
|
||||||
|
}
|
130
examples/core/3d_camera_splitscreen/main.go
Normal file
130
examples/core/3d_camera_splitscreen/main.go
Normal file
|
@ -0,0 +1,130 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
rl "github.com/gen2brain/raylib-go/raylib"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
screenW = int32(800)
|
||||||
|
screenH = int32(450)
|
||||||
|
playerSize = float32(40)
|
||||||
|
cam1, cam2 rl.Camera3D
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
|
||||||
|
rl.InitWindow(screenW, screenH, "raylib [core] example - 3d camera split screen")
|
||||||
|
|
||||||
|
cam1.Fovy = 45
|
||||||
|
cam1.Up.Y = 1
|
||||||
|
cam1.Target.Y = 1
|
||||||
|
cam1.Position.Z = -3
|
||||||
|
cam1.Position.Y = 0.5
|
||||||
|
|
||||||
|
cam2 = cam1
|
||||||
|
cam1.Position.Z = 3
|
||||||
|
|
||||||
|
screenCam1 := rl.LoadRenderTexture(screenW/2, screenH)
|
||||||
|
screenCam2 := rl.LoadRenderTexture(screenW/2, screenH)
|
||||||
|
|
||||||
|
splitScreenRec := rl.NewRectangle(0, 0, float32(screenCam1.Texture.Width), -float32(screenCam1.Texture.Height))
|
||||||
|
|
||||||
|
count := 5
|
||||||
|
spacing := float32(4)
|
||||||
|
|
||||||
|
rl.SetTargetFPS(60)
|
||||||
|
|
||||||
|
for !rl.WindowShouldClose() {
|
||||||
|
|
||||||
|
frameOffset := 10 * rl.GetFrameTime()
|
||||||
|
|
||||||
|
if rl.IsKeyDown(rl.KeyW) {
|
||||||
|
cam1.Position.Z -= frameOffset
|
||||||
|
cam1.Target.Z -= frameOffset
|
||||||
|
} else if rl.IsKeyDown(rl.KeyS) {
|
||||||
|
cam1.Position.Z += frameOffset
|
||||||
|
cam1.Target.Z += frameOffset
|
||||||
|
}
|
||||||
|
if rl.IsKeyDown(rl.KeyD) {
|
||||||
|
cam1.Position.X += frameOffset
|
||||||
|
cam1.Target.X += frameOffset
|
||||||
|
} else if rl.IsKeyDown(rl.KeyA) {
|
||||||
|
cam1.Position.X -= frameOffset
|
||||||
|
cam1.Target.X -= frameOffset
|
||||||
|
}
|
||||||
|
|
||||||
|
if rl.IsKeyDown(rl.KeyUp) {
|
||||||
|
cam2.Position.Z += frameOffset
|
||||||
|
cam2.Target.Z += frameOffset
|
||||||
|
} else if rl.IsKeyDown(rl.KeyDown) {
|
||||||
|
cam2.Position.Z -= frameOffset
|
||||||
|
cam2.Target.Z -= frameOffset
|
||||||
|
}
|
||||||
|
if rl.IsKeyDown(rl.KeyRight) {
|
||||||
|
cam2.Position.X -= frameOffset
|
||||||
|
cam2.Target.X -= frameOffset
|
||||||
|
} else if rl.IsKeyDown(rl.KeyLeft) {
|
||||||
|
cam2.Position.X += frameOffset
|
||||||
|
cam2.Target.X += frameOffset
|
||||||
|
}
|
||||||
|
|
||||||
|
rl.BeginTextureMode(screenCam1)
|
||||||
|
rl.ClearBackground(rl.SkyBlue)
|
||||||
|
rl.BeginMode3D(cam1)
|
||||||
|
|
||||||
|
rl.DrawPlane(rl.Vector3Zero(), rl.NewVector2(50, 50), rl.Beige)
|
||||||
|
|
||||||
|
for x := -float32(count) * spacing; x <= float32(count)*spacing; x += spacing {
|
||||||
|
for z := -float32(count) * spacing; z <= float32(count)*spacing; z += spacing {
|
||||||
|
rl.DrawCube(rl.NewVector3(x-0.5, 1.5, z), 1, 1, 1, rl.Lime)
|
||||||
|
rl.DrawCube(rl.NewVector3(x-0.5, 0.5, z), 0.25, 1, 0.25, rl.Brown)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
rl.DrawCube(cam1.Position, 1, 1, 1, rl.Red)
|
||||||
|
rl.DrawCube(cam2.Position, 1, 1, 1, rl.Blue)
|
||||||
|
|
||||||
|
rl.EndMode3D()
|
||||||
|
|
||||||
|
rl.DrawRectangle(0, 0, screenW/2, 40, rl.Fade(rl.RayWhite, 0.8))
|
||||||
|
rl.DrawText("PLAYER 1 WASD KEYS", 10, 10, 20, rl.Maroon)
|
||||||
|
rl.EndTextureMode()
|
||||||
|
|
||||||
|
rl.BeginTextureMode(screenCam2)
|
||||||
|
rl.ClearBackground(rl.SkyBlue)
|
||||||
|
rl.BeginMode3D(cam2)
|
||||||
|
|
||||||
|
rl.DrawPlane(rl.Vector3Zero(), rl.NewVector2(50, 50), rl.Beige)
|
||||||
|
|
||||||
|
for x := -float32(count) * spacing; x <= float32(count)*spacing; x += spacing {
|
||||||
|
for z := -float32(count) * spacing; z <= float32(count)*spacing; z += spacing {
|
||||||
|
rl.DrawCube(rl.NewVector3(x, 1.5, z), 1, 1, 1, rl.Lime)
|
||||||
|
rl.DrawCube(rl.NewVector3(x, 0.5, z), 0.25, 1, 0.25, rl.Brown)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
rl.DrawCube(cam1.Position, 1, 1, 1, rl.Red)
|
||||||
|
rl.DrawCube(cam2.Position, 1, 1, 1, rl.Blue)
|
||||||
|
|
||||||
|
rl.EndMode3D()
|
||||||
|
|
||||||
|
rl.DrawRectangle(0, 0, screenW/2, 40, rl.Fade(rl.RayWhite, 0.8))
|
||||||
|
rl.DrawText("PLAYER 2 ARROW KEYS", 10, 10, 20, rl.Maroon)
|
||||||
|
rl.EndTextureMode()
|
||||||
|
|
||||||
|
rl.BeginDrawing()
|
||||||
|
rl.ClearBackground(rl.Black)
|
||||||
|
|
||||||
|
rl.DrawTextureRec(screenCam1.Texture, splitScreenRec, rl.NewVector2(0, 0), rl.White)
|
||||||
|
rl.DrawTextureRec(screenCam2.Texture, splitScreenRec, rl.NewVector2(float32(screenW/2), 0), rl.White)
|
||||||
|
rl.DrawRectangle((screenW/2)-2, 0, 4, screenH, rl.LightGray)
|
||||||
|
|
||||||
|
rl.EndDrawing()
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
rl.UnloadRenderTexture(screenCam1)
|
||||||
|
rl.UnloadRenderTexture(screenCam2)
|
||||||
|
|
||||||
|
rl.CloseWindow()
|
||||||
|
}
|
52
examples/core/scissor_test/main.go
Normal file
52
examples/core/scissor_test/main.go
Normal file
|
@ -0,0 +1,52 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
rl "github.com/gen2brain/raylib-go/raylib"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
screenW = int32(800)
|
||||||
|
screenH = int32(450)
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
|
||||||
|
rl.InitWindow(screenW, screenH, "raylib [core] example - scissor test")
|
||||||
|
|
||||||
|
scissorArea := rl.NewRectangle(0, 0, 300, 300)
|
||||||
|
scissorMode := true
|
||||||
|
|
||||||
|
rl.SetTargetFPS(60)
|
||||||
|
|
||||||
|
for !rl.WindowShouldClose() {
|
||||||
|
|
||||||
|
if rl.IsKeyPressed(rl.KeyS) {
|
||||||
|
scissorMode = !scissorMode
|
||||||
|
}
|
||||||
|
|
||||||
|
scissorArea.X = float32(rl.GetMouseX())
|
||||||
|
scissorArea.Y = float32(rl.GetMouseY())
|
||||||
|
|
||||||
|
rl.BeginDrawing()
|
||||||
|
|
||||||
|
rl.ClearBackground(rl.RayWhite)
|
||||||
|
|
||||||
|
if scissorMode {
|
||||||
|
rl.BeginScissorMode(scissorArea.ToInt32().X, scissorArea.ToInt32().Y, scissorArea.ToInt32().Width, scissorArea.ToInt32().Height)
|
||||||
|
}
|
||||||
|
|
||||||
|
rl.DrawRectangle(0, 0, screenW, screenH, rl.Red)
|
||||||
|
rl.DrawText("MOVE MOUSE TO REVEAL TEXT", 190, 200, 20, rl.Black)
|
||||||
|
|
||||||
|
if scissorMode {
|
||||||
|
rl.EndScissorMode()
|
||||||
|
}
|
||||||
|
|
||||||
|
rl.DrawRectangleLinesEx(scissorArea, 1, rl.Black)
|
||||||
|
rl.DrawText("S KEY TO TOGGLE MODE", 10, 10, 20, rl.Black)
|
||||||
|
|
||||||
|
rl.EndDrawing()
|
||||||
|
}
|
||||||
|
|
||||||
|
rl.CloseWindow()
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue