mirror of
https://codeberg.org/Kenta/KaylibKit.git
synced 2024-09-19 11:50:22 -06:00
Update few examples. Re-add kCamera2D and kCamera3D with fixed allocator parameters
This commit is contained in:
parent
1702d61296
commit
2419732ff6
10 changed files with 480 additions and 301 deletions
|
@ -6,7 +6,7 @@ plugins {
|
|||
}
|
||||
|
||||
group = "com.prism-architect"
|
||||
version = "1.0.3"
|
||||
version = "1.0.4"
|
||||
|
||||
// Codeberg repository properties providers
|
||||
val tokenProvider: Provider<String> = providers.gradleProperty("KaylibKitToken")
|
||||
|
|
|
@ -8,10 +8,11 @@ import kaylibkit.kShapes.drawRectangle
|
|||
import kaylibkit.kShapes.drawRectangleLines
|
||||
import kaylibkit.kShapes.kRectangle
|
||||
import kaylibkit.kText.drawText
|
||||
import kaylibkit.kTypes.kCamera2D
|
||||
import kaylibkit.kTypes.kColor
|
||||
import kaylibkit.kUtils.fade
|
||||
import kaylibc.*
|
||||
import kaylibkit.kTypes.kCamera2D
|
||||
import kotlinx.cinterop.memScoped
|
||||
import kotlin.random.Random
|
||||
import kotlin.random.nextInt
|
||||
|
||||
|
@ -23,100 +24,132 @@ const val MAX_BUILDINGS = 100
|
|||
// Program main entry point
|
||||
//------------------------------------------------------------------------------------
|
||||
fun main() {
|
||||
setConfigFlags(ConfigFlag.VSYNC_HINT) // Turn VSYNC On
|
||||
initWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "raylib [core] example - 2d camera")
|
||||
memScoped {
|
||||
setConfigFlags(ConfigFlag.VSYNC_HINT) // Turn VSYNC On
|
||||
initWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "raylib [core] example - 2d camera")
|
||||
|
||||
val player = kRectangle(400F, 280F, 40F, 40F)
|
||||
val buildings = Array(MAX_BUILDINGS) { kRectangle() }
|
||||
val buildColor = Array(MAX_BUILDINGS) {
|
||||
kColor(Random.nextInt(200..240).toUByte(), Random.nextInt(200..240).toUByte(), Random.nextInt(200..250).toUByte(), 255U)
|
||||
}
|
||||
var spacing = 0
|
||||
|
||||
buildings.forEach { building ->
|
||||
building.width = Random.nextInt(50..200).toFloat()
|
||||
building.height = Random.nextInt(100..800).toFloat()
|
||||
building.y = SCREEN_HEIGHT - 130F - building.height
|
||||
building.x = -6000F + spacing
|
||||
|
||||
spacing += building.width.toInt()
|
||||
}
|
||||
|
||||
val camera = kCamera2D(kVector2(SCREEN_WIDTH/2F, SCREEN_HEIGHT/2F), kVector2(player.x + 20F, player.y + 20F), 0F, 1F)
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
// Main game loop
|
||||
while (!windowShouldClose) { // Detect window close button or ESC key
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
// Player movement
|
||||
if (isKeyDown(KeyboardKey.RIGHT)) { player.x += 2 }
|
||||
else if (isKeyDown(KeyboardKey.LEFT)) { player.x -= 2 }
|
||||
|
||||
// Camera target follows player
|
||||
// We're unable to modify .target directly due to how cinterop works with K/N as cinterop will translate any nested CStruct as a immutable (val)
|
||||
// if they're not pointing to anything (pointer to), but we can easily modify it by passing immutable CStruct value:
|
||||
camera.target.set(kVector2(player.x + 20F, player.y + 20F))
|
||||
|
||||
// We can also do it a different way by simply modifying the CStruct members
|
||||
|
||||
// Camera rotation controls
|
||||
if (isKeyDown(KeyboardKey.A)) { camera.rotation-- }
|
||||
else if (isKeyDown(KeyboardKey.S)) { camera.rotation++ }
|
||||
|
||||
// Limit camera rotation to 80 degrees (-40 to 40)
|
||||
if (camera.rotation > 40) { camera.rotation = 40F }
|
||||
else if (camera.rotation < -40) { camera.rotation = -40F }
|
||||
|
||||
// Camera zoom controls
|
||||
camera.zoom += getMouseWheelMove() * .05F
|
||||
|
||||
if (camera.zoom > 3F) { camera.zoom = 3F }
|
||||
else if (camera.zoom < .1F) { camera.zoom = .1F }
|
||||
|
||||
// Camera reset (zoom and rotation)
|
||||
if (isKeyPressed(KeyboardKey.R)) {
|
||||
camera.zoom = 1F
|
||||
camera.rotation = 0F
|
||||
val player = kRectangle(400F, 280F, 40F, 40F, allocator = this)
|
||||
val buildings = Array(MAX_BUILDINGS) { kRectangle(allocator = this) }
|
||||
val buildColor = Array(MAX_BUILDINGS) {
|
||||
kColor(
|
||||
Random.nextInt(200..240).toUByte(),
|
||||
Random.nextInt(200..240).toUByte(),
|
||||
Random.nextInt(200..250).toUByte(),
|
||||
255U,
|
||||
allocator = this)
|
||||
}
|
||||
//----------------------------------------------------------------------------------
|
||||
var spacing = 0
|
||||
|
||||
drawing {
|
||||
// Draw
|
||||
buildings.forEach { building ->
|
||||
building.width = Random.nextInt(50..200).toFloat()
|
||||
building.height = Random.nextInt(100..800).toFloat()
|
||||
building.y = SCREEN_HEIGHT - 130F - building.height
|
||||
building.x = -6000F + spacing
|
||||
|
||||
spacing += building.width.toInt()
|
||||
}
|
||||
|
||||
val camera = kCamera2D(kVector2(SCREEN_WIDTH / 2F, SCREEN_HEIGHT / 2F, allocator = this), kVector2(player.x + 20F, player.y + 20F, allocator = this), 0F, 1F, allocator = this)
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
// Main game loop
|
||||
while (!windowShouldClose) { // Detect window close button or ESC key
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
mode2D(camera) {
|
||||
clearBackground(rayWhite)
|
||||
|
||||
drawRectangle(-6000, 320, 13000, 8000, darkGray)
|
||||
|
||||
buildings.forEachIndexed { index, rect -> drawRectangle(rect, buildColor[index]) }
|
||||
|
||||
drawRectangle(player, red)
|
||||
|
||||
drawLine(camera.target.x.toInt(), -SCREEN_HEIGHT*10, camera.target.x.toInt(), SCREEN_HEIGHT*10, green)
|
||||
drawLine(-SCREEN_WIDTH*10, camera.target.y.toInt(), SCREEN_WIDTH*10, camera.target.y.toInt(), green)
|
||||
|
||||
// Player movement
|
||||
if (isKeyDown(KeyboardKey.RIGHT)) {
|
||||
player.x += 2
|
||||
} else if (isKeyDown(KeyboardKey.LEFT)) {
|
||||
player.x -= 2
|
||||
}
|
||||
|
||||
drawText("SCREEN AREA", 640, 10, 20, red)
|
||||
drawRectangle(0, 0, SCREEN_WIDTH, 5, red)
|
||||
drawRectangle(0, 5, 5, SCREEN_HEIGHT - 10, red)
|
||||
drawRectangle(SCREEN_WIDTH - 5, 5, 5, SCREEN_HEIGHT - 10, red)
|
||||
drawRectangle(0, SCREEN_HEIGHT - 5, SCREEN_WIDTH, 5, red)
|
||||
// Camera target follows player
|
||||
// We're unable to modify .target directly due to how cinterop works with K/N as cinterop will translate any nested CStruct as a immutable (val)
|
||||
// if they're not pointing to anything (pointer to), but we can easily modify it by passing immutable CStruct value:
|
||||
camera.target.set(kVector2(player.x + 20F, player.y + 20F, this))
|
||||
|
||||
drawRectangle(10, 10, 250, 113, fade(skyBlue, 0.5F))
|
||||
drawRectangleLines(10, 10, 250, 113, blue)
|
||||
// We can also do it a different way by simply modifying the CStruct members
|
||||
|
||||
drawText("Free 2D camera controls:", 20, 20, 10, black)
|
||||
drawText("- Right/Left to move Offset", 40, 40, 10, darkGreen)
|
||||
drawText("- Mouse Wheel to Zoom in-out", 40, 60, 10, darkGreen)
|
||||
drawText("- A / S to Rotate", 40, 80, 10, darkGreen)
|
||||
drawText("- R to reset Zoom and Rotation", 40, 100, 10, darkGreen)
|
||||
// Camera rotation controls
|
||||
if (isKeyDown(KeyboardKey.A)) {
|
||||
camera.rotation--
|
||||
} else if (isKeyDown(KeyboardKey.S)) {
|
||||
camera.rotation++
|
||||
}
|
||||
|
||||
// Limit camera rotation to 80 degrees (-40 to 40)
|
||||
if (camera.rotation > 40) {
|
||||
camera.rotation = 40F
|
||||
} else if (camera.rotation < -40) {
|
||||
camera.rotation = -40F
|
||||
}
|
||||
|
||||
// Camera zoom controls
|
||||
camera.zoom += getMouseWheelMove() * .05F
|
||||
|
||||
if (camera.zoom > 3F) {
|
||||
camera.zoom = 3F
|
||||
} else if (camera.zoom < .1F) {
|
||||
camera.zoom = .1F
|
||||
}
|
||||
|
||||
// Camera reset (zoom and rotation)
|
||||
if (isKeyPressed(KeyboardKey.R)) {
|
||||
camera.zoom = 1F
|
||||
camera.rotation = 0F
|
||||
}
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
drawing {
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
mode2D(camera) {
|
||||
clearBackground(rayWhite)
|
||||
|
||||
drawRectangle(-6000, 320, 13000, 8000, darkGray)
|
||||
|
||||
buildings.forEachIndexed { index, rect -> drawRectangle(rect, buildColor[index]) }
|
||||
|
||||
drawRectangle(player, red)
|
||||
|
||||
drawLine(
|
||||
camera.target.x.toInt(),
|
||||
-SCREEN_HEIGHT * 10,
|
||||
camera.target.x.toInt(),
|
||||
SCREEN_HEIGHT * 10,
|
||||
green
|
||||
)
|
||||
drawLine(
|
||||
-SCREEN_WIDTH * 10,
|
||||
camera.target.y.toInt(),
|
||||
SCREEN_WIDTH * 10,
|
||||
camera.target.y.toInt(),
|
||||
green
|
||||
)
|
||||
|
||||
}
|
||||
|
||||
drawText("SCREEN AREA", 640, 10, 20, red)
|
||||
drawRectangle(0, 0, SCREEN_WIDTH, 5, red)
|
||||
drawRectangle(0, 5, 5, SCREEN_HEIGHT - 10, red)
|
||||
drawRectangle(SCREEN_WIDTH - 5, 5, 5, SCREEN_HEIGHT - 10, red)
|
||||
drawRectangle(0, SCREEN_HEIGHT - 5, SCREEN_WIDTH, 5, red)
|
||||
|
||||
drawRectangle(10, 10, 250, 113, fade(skyBlue, 0.5F, allocator = this))
|
||||
drawRectangleLines(10, 10, 250, 113, blue)
|
||||
|
||||
drawText("Free 2D camera controls:", 20, 20, 10, black)
|
||||
drawText("- Right/Left to move Offset", 40, 40, 10, darkGreen)
|
||||
drawText("- Mouse Wheel to Zoom in-out", 40, 60, 10, darkGreen)
|
||||
drawText("- A / S to Rotate", 40, 80, 10, darkGreen)
|
||||
drawText("- R to reset Zoom and Rotation", 40, 100, 10, darkGreen)
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
}
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
closeWindow() // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
}
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
closeWindow() // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
}
|
|
@ -9,6 +9,7 @@ import kaylibkit.kText.drawFPS
|
|||
import kaylibkit.kText.drawText
|
||||
import kaylibkit.kTypes.kCamera3D
|
||||
import kaylibc.*
|
||||
import kotlinx.cinterop.memScoped
|
||||
|
||||
const val SCREEN_WIDTH = 800
|
||||
const val SCREEN_HEIGHT = 450
|
||||
|
@ -17,39 +18,49 @@ const val SCREEN_HEIGHT = 450
|
|||
// Program main entry point
|
||||
//------------------------------------------------------------------------------------
|
||||
fun main() {
|
||||
setConfigFlags(ConfigFlag.VSYNC_HINT) // Turn VSYNC On
|
||||
initWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "raylib [core] example - 3d camera mode")
|
||||
memScoped {
|
||||
setConfigFlags(ConfigFlag.VSYNC_HINT) // Turn VSYNC On
|
||||
initWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "raylib [core] example - 3d camera mode")
|
||||
|
||||
val camera = kCamera3D(kVector3(0F, 10F, 10F), kVector3(0F, 0F, 0F), kVector3(0F, 1F, 0F), 45F, CameraProjection.PERSPECTIVE)
|
||||
val cubePosition = kVector3(0F, 0F, 0F)
|
||||
//--------------------------------------------------------------------------------------
|
||||
val camera = kCamera3D(
|
||||
kVector3(0F, 10F, 10F, allocator = this),
|
||||
kVector3(0F, 0F, 0F, allocator = this),
|
||||
kVector3(0F, 1F, 0F, allocator = this),
|
||||
45F,
|
||||
CameraProjection.PERSPECTIVE,
|
||||
allocator = this
|
||||
)
|
||||
val cubePosition = kVector3(0F, 0F, 0F, allocator = this)
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
// Main game loop
|
||||
while (!windowShouldClose) { // Detect window close button or ESC key
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
// TODO: Update your variables here
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
drawing {
|
||||
// Draw
|
||||
// Main game loop
|
||||
while (!windowShouldClose) { // Detect window close button or ESC key
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
// TODO: Update your variables here
|
||||
//----------------------------------------------------------------------------------
|
||||
clearBackground(rayWhite)
|
||||
|
||||
mode3D(camera) {
|
||||
drawCube(cubePosition, 2F, 2F, 2F, red)
|
||||
drawCubeWires(cubePosition, 2F, 2F, 2F, maroon)
|
||||
drawing {
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
clearBackground(rayWhite)
|
||||
|
||||
drawGrid(10, 1F)
|
||||
mode3D(camera) {
|
||||
drawCube(cubePosition, 2F, 2F, 2F, red)
|
||||
drawCubeWires(cubePosition, 2F, 2F, 2F, maroon)
|
||||
|
||||
drawGrid(10, 1F)
|
||||
}
|
||||
|
||||
drawText("Welcome to the third dimension!", 10, 40, 20, darkGray)
|
||||
drawFPS(10, 10)
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
drawText("Welcome to the third dimension!", 10, 40, 20, darkGray)
|
||||
drawFPS(10, 10)
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
closeWindow() // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
}
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
closeWindow() // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
}
|
|
@ -1,7 +1,5 @@
|
|||
import kaylibkit.kCamera.setCameraMode
|
||||
import kaylibkit.kCamera.updateCamera
|
||||
import kaylibkit.kCore.*
|
||||
import kaylibkit.kEnums.CameraMode
|
||||
import kaylibkit.kEnums.CameraProjection
|
||||
import kaylibkit.kEnums.ConfigFlag
|
||||
import kaylibkit.kMath.kVector2
|
||||
|
@ -12,10 +10,16 @@ import kaylibkit.kModels.drawPlane
|
|||
import kaylibkit.kShapes.drawRectangle
|
||||
import kaylibkit.kShapes.drawRectangleLines
|
||||
import kaylibkit.kText.drawText
|
||||
import kaylibkit.kEnums.CameraProjection.*
|
||||
import kaylibkit.kTypes.kCamera3D
|
||||
import kaylibkit.kTypes.kColor
|
||||
import kaylibkit.kUtils.fade
|
||||
import kaylibc.*
|
||||
import kaylibkit.kEnums.CameraMode.*
|
||||
import kaylibkit.kCamera.cameraPitch
|
||||
import kaylibkit.kCamera.cameraYaw
|
||||
import kaylibkit.kEnums.KeyboardKey
|
||||
import kotlinx.cinterop.memScoped
|
||||
import kotlin.random.Random
|
||||
import kotlin.random.nextInt
|
||||
|
||||
|
@ -27,62 +31,168 @@ const val MAX_COLUMNS = 20
|
|||
// Program main entry point
|
||||
//------------------------------------------------------------------------------------
|
||||
fun main() {
|
||||
setConfigFlags(ConfigFlag.VSYNC_HINT) // Turn VSYNC On
|
||||
initWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "raylib [core] example - 3d camera first person")
|
||||
memScoped {
|
||||
setConfigFlags(ConfigFlag.VSYNC_HINT) // Turn VSYNC On
|
||||
initWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "raylib [core] example - 3d camera first person")
|
||||
|
||||
val camera = kCamera3D(kVector3(4F, 2F, 4F), kVector3(0F, 1.8F, 0F), kVector3(0F, 1F, 0F), 60F, CameraProjection.PERSPECTIVE)
|
||||
val camera = kCamera3D(
|
||||
kVector3(4F, 2F, 4F, allocator = this),
|
||||
kVector3(0F, 1.8F, 0F, allocator = this),
|
||||
kVector3(0F, 1F, 0F, allocator = this),
|
||||
60F,
|
||||
CameraProjection.PERSPECTIVE,
|
||||
allocator = this
|
||||
)
|
||||
|
||||
val heights = Array(MAX_COLUMNS) {
|
||||
Random.nextInt(1..12).toFloat()
|
||||
}
|
||||
val position = Array(MAX_COLUMNS) { i ->
|
||||
kVector3(Random.nextInt(-15..15).toFloat(), heights[i]/2F, Random.nextInt(-15..15).toFloat())
|
||||
}
|
||||
val colors = Array(MAX_COLUMNS) {
|
||||
kColor(Random.nextInt(20..255).toUByte(), Random.nextInt(10..55).toUByte(), 30U, 255U)
|
||||
}
|
||||
var cameraMode = FIRST_PERSON
|
||||
|
||||
setCameraMode(camera, CameraMode.FIRST_PERSON)
|
||||
val heights = Array(MAX_COLUMNS) { Random.nextInt(1..12).toFloat() }
|
||||
val position = Array(MAX_COLUMNS) { i ->
|
||||
kVector3(
|
||||
Random.nextInt(-15..15).toFloat(),
|
||||
heights[i] / 2F,
|
||||
Random.nextInt(-15..15).toFloat(),
|
||||
allocator = this
|
||||
)
|
||||
}
|
||||
val colors = Array(MAX_COLUMNS) {
|
||||
kColor(
|
||||
Random.nextInt(20..255).toUByte(),
|
||||
Random.nextInt(10..55).toUByte(),
|
||||
30U,
|
||||
255U,
|
||||
allocator = this
|
||||
)
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------
|
||||
disableCursor()
|
||||
|
||||
// Main game loop
|
||||
while (!windowShouldClose) { // Detect window close button or ESC key
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
updateCamera(camera)
|
||||
//----------------------------------------------------------------------------------
|
||||
// Main game loop
|
||||
while (!windowShouldClose) { // Detect window close button or ESC key
|
||||
|
||||
drawing {
|
||||
// Draw
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
clearBackground(rayWhite)
|
||||
|
||||
mode3D(camera) {
|
||||
drawPlane(kVector3(0F, 0F, 0F), kVector2(32F, 32F), lightGray) // Draw ground
|
||||
drawCube(kVector3(-16F, 2.5F, 0F), 1F, 5F, 32F, blue) // Draw a blue wall
|
||||
drawCube(kVector3(16F, 2.5F, 0F), 1F, 5F, 32F, blue) // Draw a green wall
|
||||
drawCube(kVector3(0F, 2.5F, 16F), 32F, 5F, 1F, blue) // Draw a yellow wall
|
||||
|
||||
// Draw some cubes around
|
||||
heights.indices.forEach { i ->
|
||||
drawCube(position[i], 2F, heights[i], 2F, colors[i])
|
||||
drawCubeWires(position[i], 2F, heights[i], 2F, maroon)
|
||||
// Switch camera mode
|
||||
if (isKeyPressed(KeyboardKey.NUMBER1)) {
|
||||
cameraMode = FREE
|
||||
camera.up.apply {
|
||||
this.x = 0F
|
||||
this.y = 1F
|
||||
this.z = 0F
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
drawRectangle(10, 10, 220, 70, fade(skyBlue, .5F))
|
||||
drawRectangleLines(10, 10, 220, 70, blue)
|
||||
drawText("First person camera default controls:", 20, 20, 10, black);
|
||||
drawText("- Move with keys: W, A, S, D", 40, 40, 10, darkGray);
|
||||
drawText("- Mouse move to look around", 40, 60, 10, darkGray);
|
||||
//----------------------------------------------------------------------------------
|
||||
if (isKeyPressed(KeyboardKey.NUMBER2)) {
|
||||
cameraMode = FIRST_PERSON
|
||||
camera.up.apply {
|
||||
this.x = 0F
|
||||
this.y = 1F
|
||||
this.z = 0F
|
||||
}
|
||||
}
|
||||
|
||||
if (isKeyPressed(KeyboardKey.NUMBER3)) {
|
||||
cameraMode = THIRD_PERSON
|
||||
camera.up.apply {
|
||||
this.x = 0F
|
||||
this.y = 1F
|
||||
this.z = 0F
|
||||
}
|
||||
}
|
||||
|
||||
if (isKeyPressed(KeyboardKey.NUMBER4)) {
|
||||
cameraMode = ORBITAL
|
||||
camera.up.apply {
|
||||
this.x = 0F
|
||||
this.y = 1F
|
||||
this.z = 0F
|
||||
}
|
||||
}
|
||||
|
||||
// Switch camera projection
|
||||
if (isKeyPressed(KeyboardKey.P)) {
|
||||
// Create isometric view
|
||||
cameraMode = THIRD_PERSON
|
||||
// Note: The target distance is related to the render distance in the orthographic projection
|
||||
camera.position.apply {
|
||||
this.x = 0F
|
||||
this.y = 2F
|
||||
this.z = -100F
|
||||
}
|
||||
|
||||
camera.target.apply {
|
||||
this.x = 0F
|
||||
this.y = 2F
|
||||
this.z = 0F
|
||||
}
|
||||
|
||||
camera.up.apply {
|
||||
this.x = 0F
|
||||
this.y = 1F
|
||||
this.z = 0F
|
||||
}
|
||||
|
||||
camera.projection = ORTHOGRAPHIC.value
|
||||
camera.fovy = 20F // near plane width in CAMERA_ORTHOGRAPHIC
|
||||
cameraYaw(camera, -135 * DEG2RAD, true)
|
||||
cameraPitch(camera, -45 * DEG2RAD, true, true, false)
|
||||
} else if (camera.projection == ORTHOGRAPHIC.value) {
|
||||
cameraMode = THIRD_PERSON
|
||||
camera.position.apply {
|
||||
this.x = 0F
|
||||
this.y = 2F
|
||||
this.z = 10F
|
||||
}
|
||||
camera.target.apply {
|
||||
this.x = 0F
|
||||
this.y = 2F
|
||||
this.z = 0F
|
||||
}
|
||||
camera.up.apply {
|
||||
this.x = 0F
|
||||
this.y = 1F
|
||||
this.z = 0F
|
||||
}
|
||||
camera.projection = PERSPECTIVE.value
|
||||
camera.fovy = 60F
|
||||
}
|
||||
// Update camera computes movement internally depending on the camera mode
|
||||
// Some default standard keyboard/mouse inputs are hardcoded to simplify use
|
||||
// For advance camera controls, it's reecommended to compute camera movement manually
|
||||
updateCamera(camera, cameraMode) // Update camera
|
||||
|
||||
drawing {
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
clearBackground(rayWhite)
|
||||
|
||||
mode3D(camera) {
|
||||
drawPlane(kVector3(0F, 0F, 0F, allocator = this), kVector2(32F, 32F, allocator = this), lightGray) // Draw ground
|
||||
drawCube(kVector3(-16F, 2.5F, 0F, allocator = this), 1F, 5F, 32F, blue) // Draw a blue wall
|
||||
drawCube(kVector3(16F, 2.5F, 0F, allocator = this), 1F, 5F, 32F, blue) // Draw a green wall
|
||||
drawCube(kVector3(0F, 2.5F, 16F, allocator = this), 32F, 5F, 1F, blue) // Draw a yellow wall
|
||||
|
||||
// Draw some cubes around
|
||||
heights.indices.forEach { i ->
|
||||
drawCube(position[i], 2F, heights[i], 2F, colors[i])
|
||||
drawCubeWires(position[i], 2F, heights[i], 2F, maroon)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
drawRectangle(10, 10, 220, 70, fade(skyBlue, .5F, allocator = this))
|
||||
drawRectangleLines(10, 10, 220, 70, blue)
|
||||
drawText("First person camera default controls:", 20, 20, 10, black);
|
||||
drawText("- Move with keys: W, A, S, D", 40, 40, 10, darkGray);
|
||||
drawText("- Mouse move to look around", 40, 60, 10, darkGray);
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
}
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
closeWindow() // Close window and OpenGL context
|
||||
//-------------------------------------------------------------------------------------- }
|
||||
}
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
closeWindow() // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
}
|
|
@ -1,4 +1,3 @@
|
|||
import kaylibkit.kCamera.setCameraMode
|
||||
import kaylibkit.kCamera.updateCamera
|
||||
import kaylibkit.kCore.*
|
||||
import kaylibkit.kEnums.CameraMode
|
||||
|
@ -16,6 +15,7 @@ import kaylibkit.kText.drawText
|
|||
import kaylibkit.kTypes.kCamera3D
|
||||
import kaylibkit.kUtils.fade
|
||||
import kaylibc.*
|
||||
import kotlinx.cinterop.memScoped
|
||||
|
||||
const val SCREEN_WIDTH = 800
|
||||
const val SCREEN_HEIGHT = 450
|
||||
|
@ -24,50 +24,60 @@ const val SCREEN_HEIGHT = 450
|
|||
// Program main entry point
|
||||
//------------------------------------------------------------------------------------
|
||||
fun main() {
|
||||
setConfigFlags(ConfigFlag.VSYNC_HINT) // Turn VSYNC On
|
||||
initWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "raylib [core] example - 3d camera free")
|
||||
memScoped {
|
||||
setConfigFlags(ConfigFlag.VSYNC_HINT) // Turn VSYNC On
|
||||
initWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "raylib [core] example - 3d camera free")
|
||||
|
||||
val camera = kCamera3D(kVector3(10F, 10F, 10F), kVector3(), kVector3(0F, 1F, 0F), 45F, CameraProjection.PERSPECTIVE)
|
||||
val cubePosition = kVector3()
|
||||
val camera = kCamera3D(
|
||||
kVector3(10F, 10F, 10F, allocator = this),
|
||||
kVector3(allocator = this),
|
||||
kVector3(0F, 1F, 0F, allocator = this),
|
||||
45F,
|
||||
CameraProjection.PERSPECTIVE,
|
||||
allocator = this
|
||||
)
|
||||
val cubePosition = kVector3(allocator = this)
|
||||
|
||||
setCameraMode(camera, CameraMode.FREE)
|
||||
//--------------------------------------------------------------------------------------
|
||||
disableCursor()
|
||||
|
||||
// Main game loop
|
||||
while (!windowShouldClose) { // Detect window close button or ESC key
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
updateCamera(camera)
|
||||
if (isKeyDown(KeyboardKey.Z)) { camera.target.setZero() }
|
||||
//----------------------------------------------------------------------------------
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
drawing {
|
||||
// Draw
|
||||
// Main game loop
|
||||
while (!windowShouldClose) { // Detect window close button or ESC key
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
clearBackground(rayWhite)
|
||||
|
||||
mode3D(camera) {
|
||||
drawCube(cubePosition, 2F, 2F, 2F, red)
|
||||
drawCubeWires(cubePosition, 2F, 2F, 2F, maroon)
|
||||
|
||||
drawGrid(10, 1F)
|
||||
|
||||
updateCamera(camera, CameraMode.FREE)
|
||||
if (isKeyDown(KeyboardKey.Z)) {
|
||||
camera.target.setZero()
|
||||
}
|
||||
|
||||
drawRectangle(10, 10, 320, 133, fade(skyBlue, .5F))
|
||||
drawRectangleLines(10, 10, 320, 133, blue)
|
||||
|
||||
drawText("Free camera default controls:", 20, 20, 10, black)
|
||||
drawText("- Mouse Wheel to Zoom in-out", 40, 40, 10, darkGray)
|
||||
drawText("- Mouse Wheel Pressed to Pan", 40, 60, 10, darkGray)
|
||||
drawText("- Alt + Mouse Wheel Pressed to Rotate", 40, 80, 10, darkGray)
|
||||
drawText("- Alt + Ctrl + Mouse Wheel Pressed for Smooth Zoom", 40, 100, 10, darkGray)
|
||||
drawText("- Z to zoom to (0, 0, 0)", 40, 120, 10, darkGray)
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
drawing {
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
clearBackground(rayWhite)
|
||||
|
||||
mode3D(camera) {
|
||||
drawCube(cubePosition, 2F, 2F, 2F, red)
|
||||
drawCubeWires(cubePosition, 2F, 2F, 2F, maroon)
|
||||
|
||||
drawGrid(10, 1F)
|
||||
}
|
||||
|
||||
drawRectangle(10, 10, 320, 133, fade(skyBlue, .5F, allocator = this))
|
||||
drawRectangleLines(10, 10, 320, 133, blue)
|
||||
|
||||
drawText("Free camera default controls:", 20, 20, 10, black)
|
||||
drawText("- W & S Key to Zoom in and Zoom out", 40, 40, 10, darkGray)
|
||||
drawText("- A & S to move around", 40, 60, 10, darkGray)
|
||||
drawText("- Q & E to rotate", 40, 80, 10, darkGray)
|
||||
drawText("- Z to zoom to (0, 0, 0)", 40, 120, 10, darkGray)
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
}
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
closeWindow() // Close window and OpenGL context
|
||||
//-------------------------------------------------------------------------------------- }
|
||||
}
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
closeWindow() // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
}
|
|
@ -5,6 +5,7 @@ import kaylibkit.kTextures.drawTexture
|
|||
import kaylibkit.kTextures.loadTexture
|
||||
import kaylibkit.kTextures.unloadTexture
|
||||
import kaylibc.*
|
||||
import kotlinx.cinterop.memScoped
|
||||
|
||||
const val SCREEN_WIDTH = 800
|
||||
const val SCREEN_HEIGHT = 450
|
||||
|
@ -13,36 +14,42 @@ const val SCREEN_HEIGHT = 450
|
|||
// Program main entry point
|
||||
//------------------------------------------------------------------------------------
|
||||
fun main() {
|
||||
setConfigFlags(ConfigFlag.VSYNC_HINT) // Turn VSYNC On
|
||||
initWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "raylib [textures] example - texture loading and drawing")
|
||||
//--------------------------------------------------------------------------------------
|
||||
memScoped {
|
||||
setConfigFlags(ConfigFlag.VSYNC_HINT) // Turn VSYNC On
|
||||
initWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "raylib [textures] example - texture loading and drawing")
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
// NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
|
||||
val texture = loadTexture("resources/raylib_logo.png") // Texture loading
|
||||
// NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
|
||||
val texture = loadTexture("resources/raylib_logo.png", allocator = this) // Texture loading
|
||||
|
||||
// Main game loop
|
||||
while (!windowShouldClose) { // Detect window close button or ESC key
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
// TODO: Update your variables here
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
drawing {
|
||||
|
||||
// Draw
|
||||
// Main game loop
|
||||
while (!windowShouldClose) { // Detect window close button or ESC key
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
clearBackground(rayWhite)
|
||||
|
||||
drawTexture(texture, SCREEN_WIDTH/2 - texture.width/2, SCREEN_HEIGHT/2 - texture.height/2, white)
|
||||
|
||||
drawText("this IS a texture!", 360, 370, 10, gray)
|
||||
// TODO: Update your variables here
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
drawing {
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
clearBackground(rayWhite)
|
||||
|
||||
drawTexture(
|
||||
texture,
|
||||
SCREEN_WIDTH / 2 - texture.width / 2,
|
||||
SCREEN_HEIGHT / 2 - texture.height / 2,
|
||||
white
|
||||
)
|
||||
|
||||
drawText("this IS a texture!", 360, 370, 10, gray)
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
}
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
unloadTexture(texture) // Texture unloading
|
||||
closeWindow() // Close window and OpenGL context
|
||||
//-------------------------------------------------------------------------------------- }
|
||||
}
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
unloadTexture(texture) // Texture unloading
|
||||
|
||||
closeWindow() // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
}
|
|
@ -15,10 +15,14 @@ import kotlin.math.*
|
|||
* @return [Vector2]
|
||||
*/
|
||||
@OptIn(ExperimentalForeignApi::class)
|
||||
inline fun kVector2(x: Float = 0F, y: Float = 0F, allocator: AutofreeScope): Vector2 {
|
||||
return allocator.alloc<Vector2> {
|
||||
this.x = x
|
||||
this.y = y
|
||||
inline fun kVector2(x: Float = 0F, y: Float = 0F, allocator: AutofreeScope?): Vector2 {
|
||||
if (allocator != null) {
|
||||
return allocator.alloc<Vector2> {
|
||||
this.x = x
|
||||
this.y = y
|
||||
}
|
||||
} else {
|
||||
throw IllegalArgumentException("ERROR: No allocator passed to the parameter! Unable to allocate!")
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -15,11 +15,15 @@ import kotlin.math.*
|
|||
* @return [Vector3]
|
||||
*/
|
||||
@OptIn(ExperimentalForeignApi::class)
|
||||
inline fun kVector3(x: Float = 0F, y: Float = 0F, z: Float = 0F, allocator: AutofreeScope): Vector3 {
|
||||
return allocator.alloc<Vector3> {
|
||||
this.x = x
|
||||
this.y = y
|
||||
this.z = z
|
||||
inline fun kVector3(x: Float = 0F, y: Float = 0F, z: Float = 0F, allocator: AutofreeScope?): Vector3 {
|
||||
if (allocator != null) {
|
||||
return allocator.alloc<Vector3> {
|
||||
this.x = x
|
||||
this.y = y
|
||||
this.z = z
|
||||
}
|
||||
} else {
|
||||
throw IllegalArgumentException("ERROR: No allocator passed to the parameter! Unable to allocate!")
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -3,7 +3,6 @@ package kaylibkit.kShapes
|
|||
import kaylibkit.kMath.kVector2
|
||||
import kaylibc.Rectangle
|
||||
import kotlinx.cinterop.AutofreeScope
|
||||
import kotlinx.cinterop.MemScope
|
||||
import kotlinx.cinterop.alloc
|
||||
import kaylibc.Vector2
|
||||
import kotlinx.cinterop.ExperimentalForeignApi
|
||||
|
|
|
@ -1,12 +1,13 @@
|
|||
package kaylibkit.kTypes
|
||||
|
||||
import kaylibkit.kMath.kVector2
|
||||
import kaylibkit.kMath.kVector3
|
||||
import kaylibkit.kMath.set
|
||||
import kaylibkit.kShapes.set
|
||||
import kaylibkit.kImage.set
|
||||
import kaylibkit.kTextures.set
|
||||
import kaylibc.*
|
||||
import kaylibkit.kEnums.CameraProjection
|
||||
import kaylibkit.kMath.kVector3
|
||||
import kotlinx.cinterop.*
|
||||
|
||||
// -- Module: kTypes
|
||||
|
@ -118,34 +119,34 @@ inline fun kFont(baseSize: Int, glyphCount: Int, glyphPadding: Int, texture: Tex
|
|||
}
|
||||
}
|
||||
|
||||
///**
|
||||
// * Constructor function for [Camera3D].
|
||||
// * @return [Camera3D]
|
||||
// */
|
||||
//@OptIn(ExperimentalForeignApi::class)
|
||||
//inline fun kCamera3D(position: Vector3 = kVector3(), target: Vector3 = kVector3(), up: Vector3 = kVector3(), fovy: Float = 0F, projection: kaylibkit.kEnums.CameraProjection, allocator: AutofreeScope = MemScope()) : Camera3D {
|
||||
// return allocator.alloc<Camera3D> {
|
||||
// this.position.set(position)
|
||||
// this.target.set(target)
|
||||
// this.up.set(up)
|
||||
// this.fovy = fovy
|
||||
// this.projection = projection.value
|
||||
// }
|
||||
//}
|
||||
/**
|
||||
* Constructor function for [Camera3D].
|
||||
* @return [Camera3D]
|
||||
*/
|
||||
@OptIn(ExperimentalForeignApi::class)
|
||||
inline fun kCamera3D(position: Vector3 = kVector3(allocator = null), target: Vector3 = kVector3(allocator = null), up: Vector3 = kVector3(allocator = null), fovy: Float = 0F, projection: CameraProjection, allocator: AutofreeScope) : Camera3D {
|
||||
return allocator.alloc<Camera3D> {
|
||||
this.position.set(position)
|
||||
this.target.set(target)
|
||||
this.up.set(up)
|
||||
this.fovy = fovy
|
||||
this.projection = projection.value
|
||||
}
|
||||
}
|
||||
|
||||
///**
|
||||
// * Constructor function for [Camera2D].
|
||||
// * @return [Camera2D]
|
||||
// */
|
||||
//@OptIn(ExperimentalForeignApi::class)
|
||||
//inline fun kCamera2D(offset: Vector2 = kVector2(), target: Vector2 = kVector2(), rotation: Float = 0F, zoom: Float = 0F, allocator: AutofreeScope = MemScope()) : Camera2D {
|
||||
// return allocator.alloc<Camera2D> {
|
||||
// this.offset.set(offset)
|
||||
// this.target.set(target)
|
||||
// this.rotation = rotation
|
||||
// this.zoom = zoom
|
||||
// }
|
||||
//}
|
||||
/**
|
||||
* Constructor function for [Camera2D].
|
||||
* @return [Camera2D]
|
||||
*/
|
||||
@OptIn(ExperimentalForeignApi::class)
|
||||
inline fun kCamera2D(offset: Vector2 = kVector2(allocator = null), target: Vector2 = kVector2(allocator = null), rotation: Float = 0F, zoom: Float = 0F, allocator: AutofreeScope) : Camera2D {
|
||||
return allocator.alloc<Camera2D> {
|
||||
this.offset.set(offset)
|
||||
this.target.set(target)
|
||||
this.rotation = rotation
|
||||
this.zoom = zoom
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor function for [Mesh].
|
||||
|
@ -287,7 +288,7 @@ inline fun kModel(transform: Matrix, meshCount: Int, materialCount: Int, meshes:
|
|||
* @return [ModelAnimation]
|
||||
*/
|
||||
@OptIn(ExperimentalForeignApi::class)
|
||||
inline fun kModelAnimation(boneCount: Int, frameCount: Int, bones: BoneInfo, framePoses: CPointer<CPointerVar<Transform>>, allocator: AutofreeScope = MemScope()) : ModelAnimation {
|
||||
inline fun kModelAnimation(boneCount: Int, frameCount: Int, bones: BoneInfo, framePoses: CPointer<CPointerVar<Transform>>, allocator: AutofreeScope) : ModelAnimation {
|
||||
return allocator.alloc<ModelAnimation> {
|
||||
this.boneCount = boneCount
|
||||
this.frameCount = frameCount
|
||||
|
@ -296,45 +297,45 @@ inline fun kModelAnimation(boneCount: Int, frameCount: Int, bones: BoneInfo, fra
|
|||
}
|
||||
}
|
||||
|
||||
///**
|
||||
// * Constructor function for [Ray].
|
||||
// * @return [Ray]
|
||||
// */
|
||||
//@OptIn(ExperimentalForeignApi::class)
|
||||
//inline fun kRay(position: Vector3 = kVector3(), direction: Vector3 = kVector3(), allocator: AutofreeScope = MemScope()) : Ray {
|
||||
// return allocator.alloc<Ray> {
|
||||
// this.position.set(position)
|
||||
// this.direction.set(direction)
|
||||
// }
|
||||
//}
|
||||
/**
|
||||
* Constructor function for [Ray].
|
||||
* @return [Ray]
|
||||
*/
|
||||
@OptIn(ExperimentalForeignApi::class)
|
||||
inline fun kRay(position: Vector3 = kVector3(allocator = null), direction: Vector3 = kVector3(allocator = null), allocator: AutofreeScope) : Ray {
|
||||
return allocator.alloc<Ray> {
|
||||
this.position.set(position)
|
||||
this.direction.set(direction)
|
||||
}
|
||||
}
|
||||
|
||||
///**
|
||||
// * Constructor function for [RayCollision].
|
||||
// * Important to note that this uses `MemScope()` by default.
|
||||
// * @return [RayCollision]
|
||||
// */
|
||||
//@OptIn(ExperimentalForeignApi::class)
|
||||
//inline fun kRayCollision(hit: Boolean = false, distance: Float = 0F, point: Vector3 = kVector3(), normal: Vector3 = kVector3(), allocator: AutofreeScope = MemScope()) : RayCollision {
|
||||
// return allocator.alloc<RayCollision> {
|
||||
// this.hit = hit
|
||||
// this.distance = distance
|
||||
// this.point.set(point)
|
||||
// this.normal.set(normal)
|
||||
// }
|
||||
//}
|
||||
/**
|
||||
* Constructor function for [RayCollision].
|
||||
* Important to note that this uses `MemScope()` by default.
|
||||
* @return [RayCollision]
|
||||
*/
|
||||
@OptIn(ExperimentalForeignApi::class)
|
||||
inline fun kRayCollision(hit: Boolean = false, distance: Float = 0F, point: Vector3 = kVector3(allocator = null), normal: Vector3 = kVector3(allocator = null), allocator: AutofreeScope) : RayCollision {
|
||||
return allocator.alloc<RayCollision> {
|
||||
this.hit = hit
|
||||
this.distance = distance
|
||||
this.point.set(point)
|
||||
this.normal.set(normal)
|
||||
}
|
||||
}
|
||||
|
||||
///**
|
||||
// * Constructor function for [BoundingBox].
|
||||
// * Important to note that this uses `MemScope()` by default.
|
||||
// * @return [BoundingBox]
|
||||
// */
|
||||
//@OptIn(ExperimentalForeignApi::class)
|
||||
//inline fun kBoundingBox(min: Vector3 = kVector3(), max: Vector3 = kVector3(), allocator: AutofreeScope = MemScope()) : BoundingBox {
|
||||
// return allocator.alloc<BoundingBox> {
|
||||
// this.min.set(min)
|
||||
// this.max.set(max)
|
||||
// }
|
||||
//}
|
||||
/**
|
||||
* Constructor function for [BoundingBox].
|
||||
* Important to note that this uses `MemScope()` by default.
|
||||
* @return [BoundingBox]
|
||||
*/
|
||||
@OptIn(ExperimentalForeignApi::class)
|
||||
inline fun kBoundingBox(min: Vector3 = kVector3(allocator = null), max: Vector3 = kVector3(allocator = null), allocator: AutofreeScope) : BoundingBox {
|
||||
return allocator.alloc<BoundingBox> {
|
||||
this.min.set(min)
|
||||
this.max.set(max)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor function for [Wave].
|
||||
|
|
Loading…
Reference in a new issue