From 19f1c8d3bc3766cba7f1774107bd83a24d97ad66 Mon Sep 17 00:00:00 2001 From: kosin Date: Wed, 11 May 2022 22:49:46 +0200 Subject: [PATCH 1/5] platformer example init --- R/example_core_2d_camera_platformer.R | 306 ++++++++++++++++++++++++++ 1 file changed, 306 insertions(+) create mode 100644 R/example_core_2d_camera_platformer.R diff --git a/R/example_core_2d_camera_platformer.R b/R/example_core_2d_camera_platformer.R new file mode 100644 index 0000000..68a4e07 --- /dev/null +++ b/R/example_core_2d_camera_platformer.R @@ -0,0 +1,306 @@ +###################### +# Pre-initialization # +###################### + +library(raylib.R) + +G <- 400 +player_jump_spd <- 350 +player_hor_spd <- 200 + +# 'player' object constructor +Player <- function(x, y, speed, can_jump) { + list(position = Vector2(x, y), + speed = speed, + can_jump = can_jump) +} + + +# 'environment item' object constructor +EnvItem <- function(x, y, width, height, blocking, color, alpha = 255) { + list(rect = Rectangle(x, y, width, height), + blocking = blocking, + color = Color(color, alpha)) +} + +# 'player update' function +update_player <- function(player, env_items, env_items_length, delta) { + + if (is_key_down(keyboard_key$KEY_LEFT)) + player$position["x"] <- player$position["x"] - player_hor_spd * delta + + if (is_key_down(keyboard_key$KEY_RIGHT)) + player$position["x"] <- player$position["x"] + player_hor_spd * delta + + if (is_key_down(keyboard_key$KEY_SPACE) && isTRUE(player$can_jump)) { + player$speed <- player$speed - player_jump_spd + player$can_jump <- FALSE + + } + + hit_obstacle <- FALSE + + for (i in 1:env_items_length) { + if (env_items[[i]]$blocking && + (env_items[[i]]$rect["x"] <= player$position["x"]) && + (env_items[[i]]$rect["x"] + env_items[[i]]$rect["width"] >= player$position["x"]) && + (env_items[[i]]$rect["y"] >= player$position["y"]) && + (env_items[[i]]$rect["y"] < player$position["y"] + player$speed * delta)) { + hit_obstacle <- TRUE + player$speed <- 0 + player$position["y"] <- env_items[[i]]$rect["y"] + + } + } + + if (!hit_obstacle) { + player$position["y"] <- player$position["y"] + player$speed * delta + player$speed <- player$speed + G * delta + player$can_jump <- FALSE + + } else + player$can_jump <- TRUE + + return(player) +} + +# camera update functions +update_camera_center <- function(camera, player, width, height) { + camera$offset <- Vector2(width/2, height/2) + camera$target <- player$position + return(camera) +} + +# update_camera_center_inside_map <- +# function(camera, player, env_items, env_items_length, delta, width, height) { +# camera$target <- player$position +# camera$offset <- Vector2(width/2, height/2) +# +# min_x <- min_y <- 1000 +# max_x <- max_y <- -1000 +# +# +# } +# +# void UpdateCameraCenterInsideMap(Camera2D *camera, Player *player, EnvItem *envItems, int envItemsLength, float delta, int width, int height) +# { +# camera->target = player->position; +# camera->offset = (Vector2){ width/2.0f, height/2.0f }; +# float minX = 1000, minY = 1000, maxX = -1000, maxY = -1000; +# +# for (int i = 0; i < envItemsLength; i++) +# { +# EnvItem *ei = envItems + i; +# minX = fminf(ei->rect.x, minX); +# maxX = fmaxf(ei->rect.x + ei->rect.width, maxX); +# minY = fminf(ei->rect.y, minY); +# maxY = fmaxf(ei->rect.y + ei->rect.height, maxY); +# } +# +# Vector2 max = GetWorldToScreen2D((Vector2){ maxX, maxY }, *camera); +# Vector2 min = GetWorldToScreen2D((Vector2){ minX, minY }, *camera); +# +# if (max.x < width) camera->offset.x = width - (max.x - width/2); +# if (max.y < height) camera->offset.y = height - (max.y - height/2); +# if (min.x > 0) camera->offset.x = width/2 - min.x; +# if (min.y > 0) camera->offset.y = height/2 - min.y; +# } + + + + +# void UpdateCameraCenterInsideMap(Camera2D *camera, Player *player, EnvItem *envItems, int envItemsLength, float delta, int width, int height); +# void UpdateCameraCenterSmoothFollow(Camera2D *camera, Player *player, EnvItem *envItems, int envItemsLength, float delta, int width, int height); +# void UpdateCameraEvenOutOnLanding(Camera2D *camera, Player *player, EnvItem *envItems, int envItemsLength, float delta, int width, int height); +# void UpdateCameraPlayerBoundsPush(Camera2D *camera, Player *player, EnvItem *envItems, int envItemsLength, float delta, int width, int height); + +################## +# Initialization # +################## + +play_game <- function() { + + screen_width <- 800 + screen_height <- 450 + + init_window(screen_width, screen_height, "raylib [core] example - 2d camera") + + player <- Player(x = 400, y = 280, speed = 0, can_jump = FALSE) + + env_items <- list( + EnvItem(0, 0, 1000, 400, FALSE, raylib_color$LIGHTGRAY), + EnvItem(0, 400, 1000, 200, TRUE, raylib_color$GRAY), + EnvItem(300, 200, 400, 10, TRUE, raylib_color$GRAY), + EnvItem(250, 300, 100, 10, TRUE, raylib_color$GRAY), + EnvItem(650, 300, 100, 10, TRUE, raylib_color$GRAY) + ) + + env_items_length <- length(env_items) + + camera <- Camera2D(target = player$position, + offset = Vector2(screen_width/2, screen_height/2), + rotation = 0, + zoom = 1) + + # // Store pointers to the multiple update camera functions + # void (*cameraUpdaters[])(Camera2D*, Player*, EnvItem*, int, float, int, int) = { + # UpdateCameraCenter, + # UpdateCameraCenterInsideMap, + # UpdateCameraCenterSmoothFollow, + # UpdateCameraEvenOutOnLanding, + # UpdateCameraPlayerBoundsPush + # }; + + # int cameraOption = 0; + # int cameraUpdatersLength = sizeof(cameraUpdaters)/sizeof(cameraUpdaters[0]); + + # char *cameraDescriptions[] = { + # "Follow player center", + # "Follow player center, but clamp to map edges", + # "Follow player center; smoothed", + # "Follow player center horizontally; updateplayer center vertically after landing", + # "Player push camera on getting too close to screen edge" + # }; + + set_target_fps(60) + + ################## + # Main game loop # + ################## + + while(!window_should_close()) { + + # delta_time <- get_frame_time() + delta_time <- 1/60 + + player <- update_player(player, env_items, env_items_length, delta_time) + + camera$zoom <- camera$zoom + get_mouse_wheel_move()*0.05 + + if (camera$zoom > 3) + camera$zoom = 3 + else if (camera$zoom < 0.25) + camera$zoom = 0.25 + + if (is_key_pressed(keyboard_key$KEY_R)) { + camera$zoom <- 1 + player$position <- Vector2(400, 280) + } + + # if (IsKeyPressed(KEY_C)) cameraOption = (cameraOption + 1)%cameraUpdatersLength; + # + # // Call update camera function by its pointer + # cameraUpdaters[cameraOption](&camera, &player, envItems, envItemsLength, deltaTime, screenWidth, screenHeight); + # //---------------------------------------------------------------------------------- + + camera <- update_camera_center(camera, player, screen_width, screen_height) + + begin_drawing() + + clear_background(raylib_color$LIGHTGRAY) + + begin_mode_2d(camera) + + # for (int i = 0; i < envItemsLength; i++) DrawRectangleRec(envItems[i].rect, envItems[i].color); + + invisible(lapply(env_items, \(x) { draw_rectangle_rec(x$rect, x$color) })) + + player_rect <- Rectangle(player$position["x"] - 20, player$position["y"] - 40, 40, 40) + draw_rectangle_rec(player_rect, raylib_color$RED) + + end_mode_2d() + + DrawText("Controls:", 20, 20, 10, raylib_color$BLACK); + DrawText("- Right/Left to move", 40, 40, 10, raylib_color$DARKGRAY); + DrawText("- Space to jump", 40, 60, 10, raylib_color$DARKGRAY); + DrawText("- Mouse Wheel to Zoom in-out, R to reset zoom", 40, 80, 10, raylib_color$DARKGRAY); + # DrawText("- C to change camera mode", 40, 100, 10, raylib_color$DARKGRAY); + # DrawText("Current camera mode:", 20, 120, 10, "BLACK"); + # DrawText(cameraDescriptions[cameraOption], 40, 140, 10, DARKGRAY); + + end_drawing() + + } + + ##################### + # De-initialization # + ##################### + + close_window() + +} + +play_game() + +# void UpdateCameraCenterSmoothFollow(Camera2D *camera, Player *player, EnvItem *envItems, int envItemsLength, float delta, int width, int height) +# { +# static float minSpeed = 30; +# static float minEffectLength = 10; +# static float fractionSpeed = 0.8f; +# +# camera->offset = (Vector2){ width/2.0f, height/2.0f }; +# Vector2 diff = Vector2Subtract(player->position, camera->target); +# float length = Vector2Length(diff); +# +# if (length > minEffectLength) +# { +# float speed = fmaxf(fractionSpeed*length, minSpeed); +# camera->target = Vector2Add(camera->target, Vector2Scale(diff, speed*delta/length)); +# } +# } +# +# void UpdateCameraEvenOutOnLanding(Camera2D *camera, Player *player, EnvItem *envItems, int envItemsLength, float delta, int width, int height) +# { +# static float evenOutSpeed = 700; +# static int eveningOut = false; +# static float evenOutTarget; +# +# camera->offset = (Vector2){ width/2.0f, height/2.0f }; +# camera->target.x = player->position.x; +# +# if (eveningOut) +# { +# if (evenOutTarget > camera->target.y) +# { +# camera->target.y += evenOutSpeed*delta; +# +# if (camera->target.y > evenOutTarget) +# { +# camera->target.y = evenOutTarget; +# eveningOut = 0; +# } +# } +# else +# { +# camera->target.y -= evenOutSpeed*delta; +# +# if (camera->target.y < evenOutTarget) +# { +# camera->target.y = evenOutTarget; +# eveningOut = 0; +# } +# } +# } +# else +# { +# if (player->canJump && (player->speed == 0) && (player->position.y != camera->target.y)) +# { +# eveningOut = 1; +# evenOutTarget = player->position.y; +# } +# } +# } +# +# void UpdateCameraPlayerBoundsPush(Camera2D *camera, Player *player, EnvItem *envItems, int envItemsLength, float delta, int width, int height) +# { +# static Vector2 bbox = { 0.2f, 0.2f }; +# +# Vector2 bboxWorldMin = GetScreenToWorld2D((Vector2){ (1 - bbox.x)*0.5f*width, (1 - bbox.y)*0.5f*height }, *camera); +# Vector2 bboxWorldMax = GetScreenToWorld2D((Vector2){ (1 + bbox.x)*0.5f*width, (1 + bbox.y)*0.5f*height }, *camera); +# camera->offset = (Vector2){ (1 - bbox.x)*0.5f * width, (1 - bbox.y)*0.5f*height }; +# +# if (player->position.x < bboxWorldMin.x) camera->target.x = player->position.x; +# if (player->position.y < bboxWorldMin.y) camera->target.y = player->position.y; +# if (player->position.x > bboxWorldMax.x) camera->target.x = bboxWorldMin.x + (player->position.x - bboxWorldMax.x); +# if (player->position.y > bboxWorldMax.y) camera->target.y = bboxWorldMin.y + (player->position.y - bboxWorldMax.y); +# } \ No newline at end of file From 91a32c681d96e33895db81229b6cfd55b60cf12d Mon Sep 17 00:00:00 2001 From: kosin Date: Thu, 12 May 2022 21:59:46 +0200 Subject: [PATCH 2/5] example 2d camera platformer: 3 cam updaters works --- R/example_core_2d_camera_platformer.R | 281 +++++++++++++------------- 1 file changed, 144 insertions(+), 137 deletions(-) diff --git a/R/example_core_2d_camera_platformer.R b/R/example_core_2d_camera_platformer.R index 68a4e07..72a7533 100644 --- a/R/example_core_2d_camera_platformer.R +++ b/R/example_core_2d_camera_platformer.R @@ -65,54 +65,121 @@ update_player <- function(player, env_items, env_items_length, delta) { } # camera update functions -update_camera_center <- function(camera, player, width, height) { +update_camera_center <- function( + camera, player, env_items, env_items_length, delta, width, height) { camera$offset <- Vector2(width/2, height/2) camera$target <- player$position + return(camera) } -# update_camera_center_inside_map <- -# function(camera, player, env_items, env_items_length, delta, width, height) { +# update_camera_center_inside_map <- function( +# camera, player, env_items, env_items_length, delta, width, height) { # camera$target <- player$position # camera$offset <- Vector2(width/2, height/2) -# -# min_x <- min_y <- 1000 -# max_x <- max_y <- -1000 -# -# -# } # -# void UpdateCameraCenterInsideMap(Camera2D *camera, Player *player, EnvItem *envItems, int envItemsLength, float delta, int width, int height) -# { -# camera->target = player->position; -# camera->offset = (Vector2){ width/2.0f, height/2.0f }; -# float minX = 1000, minY = 1000, maxX = -1000, maxY = -1000; +# min_x <- min_y <- 1000 +# max_x <- max_y <- -1000 # -# for (int i = 0; i < envItemsLength; i++) -# { -# EnvItem *ei = envItems + i; -# minX = fminf(ei->rect.x, minX); -# maxX = fmaxf(ei->rect.x + ei->rect.width, maxX); -# minY = fminf(ei->rect.y, minY); -# maxY = fmaxf(ei->rect.y + ei->rect.height, maxY); +# for (i in 1:env_items_length) { +# min_x <- min(env_items[[i]]$rect["x"], min_x) +# max_x <- max(env_items[[i]]$rect["x"] + env_items$rect["width"], max_x) +# min_y <- min(env_items[[i]]$rect["y"], min_y) +# max_y <- max(env_items[[i]]$rect["y"] + env_items$rect["height"], max_y) # } # -# Vector2 max = GetWorldToScreen2D((Vector2){ maxX, maxY }, *camera); -# Vector2 min = GetWorldToScreen2D((Vector2){ minX, minY }, *camera); +# max = .Call(.C_GetWorldToScreen2D_R, Vector2(max_x, max_y), camera) +# min = .Call(.C_GetWorldToScreen2D_R, Vector2(min_x, min_y), camera) # -# if (max.x < width) camera->offset.x = width - (max.x - width/2); -# if (max.y < height) camera->offset.y = height - (max.y - height/2); -# if (min.x > 0) camera->offset.x = width/2 - min.x; -# if (min.y > 0) camera->offset.y = height/2 - min.y; -# } - +# if (max["x"] < width) camera$offset["x"] <- width - (max["x"] - width/2) +# if (max["y"] < height) camera$offset["y"] <- heigh - (max["y"] - height/2) +# if (min["x"] > 0) camera$offset["x"] <- width/2 - min["x"] +# if (min["y"] > 0) camera$offset["y"] <- height/2 - min["y"] +# +# return(camera) +# } +update_camera_center_smooth_follow <- function( + camera, player, env_items, env_items_length, delta, width, height) { + + min_speed <- 30 + min_effect_length <- 10 + fraction_speed <- 0.8 + + camera$offset <- Vector2(width/2, height/2) + + diff <- player$position - camera$target + veclength <- sqrt(sum(sapply(diff, \(x) x^2))) + + if (veclength > min_effect_length) { + speed <- max(fraction_speed * veclength, min_speed) + camera$target <- camera$target + diff*(speed * delta / veclength) + } + return(camera) +} +update_camera_even_out_on_landing <- function( + camera, player, env_items, env_items_length, delta, width, height) { + + if (!exists(".update_camera_even_out_env")) { + .update_camera_even_out_env <- new.env() + .update_camera_even_out_env[["evening_out"]] <- FALSE + .update_camera_even_out_env[["even_out_target"]] <- NULL + } + + even_out_speed <- 700 + + camera$offset <- Vector2(width/2, height/2) + camera$target["x"] <- player$position["x"] + + if (isTRUE(.update_camera_even_out_env[["evening_out"]])) { + if (.update_camera_even_out_env[["even_out_target"]] > camera$target["y"]) { + camera$target["y"] <- camera$target["y"] + even_out_speed * delta + if (camera$target["y"] > .update_camera_even_out_env[["even_out_target"]]) { + camera$target["y"] <- .update_camera_even_out_env[["even_out_target"]] + .update_camera_even_out_env[["evening_out"]] <- FALSE + } + } else { + camera$target["y"] <- camera$target["y"] - even_out_speed*delta + if (camera$target["y"] < .update_camera_even_out_env[["even_out_target"]]) { + camera$target["y"] <- .update_camera_even_out_env[["even_out_target"]] + .update_camera_even_out_env[["evening_out"]] <- FALSE + } + } + } else if (isTRUE(player$can_jump) && player$speed == 0 && player$position["y"] != camera$target["y"]) { + .update_camera_even_out_env[["evening_out"]] <- TRUE + .update_camera_even_out_env[["even_out_target"]] <- player$position["y"] + } + + return(camera) +} -# void UpdateCameraCenterInsideMap(Camera2D *camera, Player *player, EnvItem *envItems, int envItemsLength, float delta, int width, int height); -# void UpdateCameraCenterSmoothFollow(Camera2D *camera, Player *player, EnvItem *envItems, int envItemsLength, float delta, int width, int height); -# void UpdateCameraEvenOutOnLanding(Camera2D *camera, Player *player, EnvItem *envItems, int envItemsLength, float delta, int width, int height); -# void UpdateCameraPlayerBoundsPush(Camera2D *camera, Player *player, EnvItem *envItems, int envItemsLength, float delta, int width, int height); +# update_camera_player_bound_push <- function( +# camera, player, env_items, env_items_length, delta, width, height) { +# +# bbox <- Vector2(0.2, 0.2) +# +# bbox_world_min <- .Call(.C_GetScreenToWorld2D, +# Vector2((1 - bbox["x"]) * 0.5 * width, (1 - bbox["y"] * 0.5 * height)), +# camera) +# bbox_world_max <- .Call(.C_GetScreenToWorld2D, +# Vector2((1 + bbox["x"]) * 0.5 * width, (1 + bbox["y"] * 0.5 * height)), +# camera) +# +# camera$offset <- Vector2((1 - bbox["x"]) * 0.5 * width, (1 - bbox["y"]) * 0.5 * height) +# +# if (player$position["x"] < bbox_world_min["x"]) +# camera$target["x"] <- player$position["x"] +# if (player$position["y"] < bbox_world_min["y"]) +# camera$target["y"] <- player$position["y"] +# if (player$position["x"] > bbox_world_max["x"]) +# camera$target["x"] <- bbox_world_min["x"] + (player$position["x"] - bbox_world_min["x"]) +# if (player$position["y"] > bbox_world_max["y"]) +# camera$target["y"] <- bbox_world_min["y"] + (player$position["y"] - bbox_world_min["y"]) +# +# return(camera) +# +# } ################## # Initialization # @@ -142,25 +209,31 @@ play_game <- function() { rotation = 0, zoom = 1) - # // Store pointers to the multiple update camera functions - # void (*cameraUpdaters[])(Camera2D*, Player*, EnvItem*, int, float, int, int) = { - # UpdateCameraCenter, - # UpdateCameraCenterInsideMap, - # UpdateCameraCenterSmoothFollow, - # UpdateCameraEvenOutOnLanding, - # UpdateCameraPlayerBoundsPush - # }; - - # int cameraOption = 0; - # int cameraUpdatersLength = sizeof(cameraUpdaters)/sizeof(cameraUpdaters[0]); - - # char *cameraDescriptions[] = { - # "Follow player center", - # "Follow player center, but clamp to map edges", - # "Follow player center; smoothed", - # "Follow player center horizontally; updateplayer center vertically after landing", - # "Player push camera on getting too close to screen edge" - # }; + # set up camera updaters + camera_updaters <- list( + update = list( + descr = "Follow player center", + func = update_camera_center + ), + # update_inside_map = list( + # descr = "Follow player center, but clamp to map edges", + # func = update_camera_center_inside_map + # ), + update_smooth_follow = list( + descr = "Follow player center;\nsmoothed", + func = update_camera_center_smooth_follow + ), + update_even_out_on_landing = list( + descr = "Follow player center horizontally;\nupdate player center vertically after landing", + func = update_camera_even_out_on_landing + )#, + # update_player_bound_push = list( + # descr = "Player push camera on getting too close to screen edge", + # func = update_camera_player_bound_push + # ) + ) + + camera_option = 1 set_target_fps(60) @@ -170,6 +243,7 @@ play_game <- function() { while(!window_should_close()) { + # to implement # delta_time <- get_frame_time() delta_time <- 1/60 @@ -187,13 +261,21 @@ play_game <- function() { player$position <- Vector2(400, 280) } - # if (IsKeyPressed(KEY_C)) cameraOption = (cameraOption + 1)%cameraUpdatersLength; - # - # // Call update camera function by its pointer - # cameraUpdaters[cameraOption](&camera, &player, envItems, envItemsLength, deltaTime, screenWidth, screenHeight); - # //---------------------------------------------------------------------------------- + if (is_key_pressed(keyboard_key$KEY_C)) { + camera_option <- camera_option %% length(camera_updaters) + 1 + } - camera <- update_camera_center(camera, player, screen_width, screen_height) + # update the camera! + camera <- do.call(what = camera_updaters[[camera_option]]$func, + args = list( + camera = camera, + player = player, + env_items = env_items, + env_items_length = env_items_length, + delta = delta_time, + width = screen_width, + height = screen_height + )) begin_drawing() @@ -201,8 +283,6 @@ play_game <- function() { begin_mode_2d(camera) - # for (int i = 0; i < envItemsLength; i++) DrawRectangleRec(envItems[i].rect, envItems[i].color); - invisible(lapply(env_items, \(x) { draw_rectangle_rec(x$rect, x$color) })) player_rect <- Rectangle(player$position["x"] - 20, player$position["y"] - 40, 40, 40) @@ -214,9 +294,9 @@ play_game <- function() { DrawText("- Right/Left to move", 40, 40, 10, raylib_color$DARKGRAY); DrawText("- Space to jump", 40, 60, 10, raylib_color$DARKGRAY); DrawText("- Mouse Wheel to Zoom in-out, R to reset zoom", 40, 80, 10, raylib_color$DARKGRAY); - # DrawText("- C to change camera mode", 40, 100, 10, raylib_color$DARKGRAY); - # DrawText("Current camera mode:", 20, 120, 10, "BLACK"); - # DrawText(cameraDescriptions[cameraOption], 40, 140, 10, DARKGRAY); + DrawText("- C to change camera mode", 40, 100, 10, raylib_color$DARKGRAY); + DrawText("Current camera mode:", 20, 120, 10, raylib_color$BLACK); + DrawText(camera_updaters[[camera_option]]$descr, 40, 140, 10, raylib_color$DARKGRAY); end_drawing() @@ -230,77 +310,4 @@ play_game <- function() { } -play_game() - -# void UpdateCameraCenterSmoothFollow(Camera2D *camera, Player *player, EnvItem *envItems, int envItemsLength, float delta, int width, int height) -# { -# static float minSpeed = 30; -# static float minEffectLength = 10; -# static float fractionSpeed = 0.8f; -# -# camera->offset = (Vector2){ width/2.0f, height/2.0f }; -# Vector2 diff = Vector2Subtract(player->position, camera->target); -# float length = Vector2Length(diff); -# -# if (length > minEffectLength) -# { -# float speed = fmaxf(fractionSpeed*length, minSpeed); -# camera->target = Vector2Add(camera->target, Vector2Scale(diff, speed*delta/length)); -# } -# } -# -# void UpdateCameraEvenOutOnLanding(Camera2D *camera, Player *player, EnvItem *envItems, int envItemsLength, float delta, int width, int height) -# { -# static float evenOutSpeed = 700; -# static int eveningOut = false; -# static float evenOutTarget; -# -# camera->offset = (Vector2){ width/2.0f, height/2.0f }; -# camera->target.x = player->position.x; -# -# if (eveningOut) -# { -# if (evenOutTarget > camera->target.y) -# { -# camera->target.y += evenOutSpeed*delta; -# -# if (camera->target.y > evenOutTarget) -# { -# camera->target.y = evenOutTarget; -# eveningOut = 0; -# } -# } -# else -# { -# camera->target.y -= evenOutSpeed*delta; -# -# if (camera->target.y < evenOutTarget) -# { -# camera->target.y = evenOutTarget; -# eveningOut = 0; -# } -# } -# } -# else -# { -# if (player->canJump && (player->speed == 0) && (player->position.y != camera->target.y)) -# { -# eveningOut = 1; -# evenOutTarget = player->position.y; -# } -# } -# } -# -# void UpdateCameraPlayerBoundsPush(Camera2D *camera, Player *player, EnvItem *envItems, int envItemsLength, float delta, int width, int height) -# { -# static Vector2 bbox = { 0.2f, 0.2f }; -# -# Vector2 bboxWorldMin = GetScreenToWorld2D((Vector2){ (1 - bbox.x)*0.5f*width, (1 - bbox.y)*0.5f*height }, *camera); -# Vector2 bboxWorldMax = GetScreenToWorld2D((Vector2){ (1 + bbox.x)*0.5f*width, (1 + bbox.y)*0.5f*height }, *camera); -# camera->offset = (Vector2){ (1 - bbox.x)*0.5f * width, (1 - bbox.y)*0.5f*height }; -# -# if (player->position.x < bboxWorldMin.x) camera->target.x = player->position.x; -# if (player->position.y < bboxWorldMin.y) camera->target.y = player->position.y; -# if (player->position.x > bboxWorldMax.x) camera->target.x = bboxWorldMin.x + (player->position.x - bboxWorldMax.x); -# if (player->position.y > bboxWorldMax.y) camera->target.y = bboxWorldMin.y + (player->position.y - bboxWorldMax.y); -# } \ No newline at end of file +play_game() \ No newline at end of file From fa765d5839b7e6905ae8d2d8ad8b2c51fb7b9250 Mon Sep 17 00:00:00 2001 From: kosin Date: Fri, 13 May 2022 19:37:48 +0200 Subject: [PATCH 3/5] vectorized `for` loop; `update_camera_even_out_on_landing` fix --- R/example_core_2d_camera_platformer.R | 96 ++++++++++++++------------- 1 file changed, 51 insertions(+), 45 deletions(-) diff --git a/R/example_core_2d_camera_platformer.R b/R/example_core_2d_camera_platformer.R index 72a7533..04fb57f 100644 --- a/R/example_core_2d_camera_platformer.R +++ b/R/example_core_2d_camera_platformer.R @@ -40,17 +40,21 @@ update_player <- function(player, env_items, env_items_length, delta) { hit_obstacle <- FALSE - for (i in 1:env_items_length) { - if (env_items[[i]]$blocking && - (env_items[[i]]$rect["x"] <= player$position["x"]) && - (env_items[[i]]$rect["x"] + env_items[[i]]$rect["width"] >= player$position["x"]) && - (env_items[[i]]$rect["y"] >= player$position["y"]) && - (env_items[[i]]$rect["y"] < player$position["y"] + player$speed * delta)) { - hit_obstacle <- TRUE - player$speed <- 0 - player$position["y"] <- env_items[[i]]$rect["y"] - - } + collisions <- sapply( + env_items, \(env_item) { + env_item$blocking && + env_item$rect["x"] <= player$position["x"] && + (env_item$rect["x"] + env_item$rect["width"] >= player$position["x"]) && + env_item$rect["y"] >= player$position["y"] && + (env_item$rect["y"] < player$position["y"] + player$speed * delta) + }) + + coll_i <- which(collisions) + + if (sum(coll_i) > 0) { + hit_obstacle <- TRUE + player$speed <- 0 + player$position["y"] <- env_items[[coll_i]]$rect["y"] } if (!hit_obstacle) { @@ -73,31 +77,33 @@ update_camera_center <- function( return(camera) } -# update_camera_center_inside_map <- function( -# camera, player, env_items, env_items_length, delta, width, height) { -# camera$target <- player$position -# camera$offset <- Vector2(width/2, height/2) -# -# min_x <- min_y <- 1000 -# max_x <- max_y <- -1000 -# -# for (i in 1:env_items_length) { -# min_x <- min(env_items[[i]]$rect["x"], min_x) -# max_x <- max(env_items[[i]]$rect["x"] + env_items$rect["width"], max_x) -# min_y <- min(env_items[[i]]$rect["y"], min_y) -# max_y <- max(env_items[[i]]$rect["y"] + env_items$rect["height"], max_y) -# } -# -# max = .Call(.C_GetWorldToScreen2D_R, Vector2(max_x, max_y), camera) -# min = .Call(.C_GetWorldToScreen2D_R, Vector2(min_x, min_y), camera) -# -# if (max["x"] < width) camera$offset["x"] <- width - (max["x"] - width/2) -# if (max["y"] < height) camera$offset["y"] <- heigh - (max["y"] - height/2) -# if (min["x"] > 0) camera$offset["x"] <- width/2 - min["x"] -# if (min["y"] > 0) camera$offset["y"] <- height/2 - min["y"] -# -# return(camera) -# } +update_camera_center_inside_map <- function( + camera, player, env_items, env_items_length, delta, width, height) { + camera$target <- player$position + camera$offset <- Vector2(width/2, height/2) + + min_x <- min_y <- 1000 + max_x <- max_y <- -1000 + + for (i in 1:env_items_length) { + min_x <- min(env_items[[i]]$rect["x"], min_x) + max_x <- max(env_items[[i]]$rect["x"] + env_items$rect["width"], max_x) + min_y <- min(env_items[[i]]$rect["y"], min_y) + max_y <- max(env_items[[i]]$rect["y"] + env_items$rect["height"], max_y) + + + max = .Call(.C_GetWorldToScreen2D_R, Vector2(max_x, max_y), camera, PACKAGE = "raylib.R") + min = .Call(.C_GetWorldToScreen2D_R, Vector2(min_x, min_y), camera, PACKAGE = "raylib.R") + + if (max["x"] < width) camera$offset["x"] <- width - (max["x"] - width/2) + if (max["y"] < height) camera$offset["y"] <- heigh - (max["y"] - height/2) + if (min["x"] > 0) camera$offset["x"] <- width/2 - min["x"] + if (min["y"] > 0) camera$offset["y"] <- height/2 - min["y"] + + } + + return(camera) + } update_camera_center_smooth_follow <- function( camera, player, env_items, env_items_length, delta, width, height) { @@ -121,18 +127,13 @@ update_camera_center_smooth_follow <- function( update_camera_even_out_on_landing <- function( camera, player, env_items, env_items_length, delta, width, height) { - if (!exists(".update_camera_even_out_env")) { - .update_camera_even_out_env <- new.env() - .update_camera_even_out_env[["evening_out"]] <- FALSE - .update_camera_even_out_env[["even_out_target"]] <- NULL - } - even_out_speed <- 700 camera$offset <- Vector2(width/2, height/2) camera$target["x"] <- player$position["x"] if (isTRUE(.update_camera_even_out_env[["evening_out"]])) { + if (.update_camera_even_out_env[["even_out_target"]] > camera$target["y"]) { camera$target["y"] <- camera$target["y"] + even_out_speed * delta if (camera$target["y"] > .update_camera_even_out_env[["even_out_target"]]) { @@ -154,15 +155,20 @@ update_camera_even_out_on_landing <- function( return(camera) } +# environement needed for `update_even_out_on_landing` camera updater +.update_camera_even_out_env <- new.env() +.update_camera_even_out_env[["evening_out"]] <- FALSE +.update_camera_even_out_env[["even_out_target"]] <- NULL + # update_camera_player_bound_push <- function( # camera, player, env_items, env_items_length, delta, width, height) { # # bbox <- Vector2(0.2, 0.2) # -# bbox_world_min <- .Call(.C_GetScreenToWorld2D, +# bbox_world_min <- .Call(.C_GetScreenToWorld2D_R, # Vector2((1 - bbox["x"]) * 0.5 * width, (1 - bbox["y"] * 0.5 * height)), # camera) -# bbox_world_max <- .Call(.C_GetScreenToWorld2D, +# bbox_world_max <- .Call(.C_GetScreenToWorld2D_R, # Vector2((1 + bbox["x"]) * 0.5 * width, (1 + bbox["y"] * 0.5 * height)), # camera) # @@ -310,4 +316,4 @@ play_game <- function() { } -play_game() \ No newline at end of file +play_game() From b6e2526e10299e05ba1908ca13fe962ee05774ed Mon Sep 17 00:00:00 2001 From: kosin Date: Fri, 13 May 2022 21:33:01 +0200 Subject: [PATCH 4/5] basic_screen_manager example added --- NAMESPACE | 4 + R/example_core_2d_camera_platformer.R | 645 +++++++++++++------------- R/example_core_basic_screen_manager.R | 117 +++++ man/Camera2D.Rd | 3 + man/Camera3D.Rd | 12 + man/Color.Rd | 6 + man/Image.Rd | 3 + man/Ray.Rd | 31 ++ man/Rectangle.Rd | 3 + man/RenderTexture.Rd | 49 ++ man/Shader.Rd | 35 ++ man/Texture.Rd | 9 + man/Vector2.Rd | 3 + man/Vector3.Rd | 3 + man/Vector4.Rd | 6 + man/blend_mode.Rd | 25 + man/core_2d_camera.Rd | 2 + man/core_2d_platformer.Rd | 341 ++++++++++++++ man/core_3d_camera.Rd | 2 + man/core_3d_camera_first_person.Rd | 2 + man/core_3d_camera_free.Rd | 2 + man/core_basic_screen_manager.Rd | 132 ++++++ man/core_basic_window.Rd | 2 + man/core_drop_files.Rd | 2 + man/core_input_keys.Rd | 2 + man/core_input_mouse.Rd | 2 + man/cubemap_layout.Rd | 16 + man/font_type.Rd | 16 + man/material_map_index.Rd | 16 + man/npatch_layout.Rd | 16 + man/pixel_format.Rd | 16 + man/shader_attribute_data_type.Rd | 16 + man/shader_location_index.Rd | 16 + man/shader_uniform_data_type.Rd | 16 + man/texture_filter.Rd | 17 + man/texture_wrap.Rd | 16 + 36 files changed, 1285 insertions(+), 319 deletions(-) create mode 100644 R/example_core_basic_screen_manager.R create mode 100644 man/Ray.Rd create mode 100644 man/RenderTexture.Rd create mode 100644 man/Shader.Rd create mode 100644 man/blend_mode.Rd create mode 100644 man/core_2d_platformer.Rd create mode 100644 man/core_basic_screen_manager.Rd create mode 100644 man/cubemap_layout.Rd create mode 100644 man/font_type.Rd create mode 100644 man/material_map_index.Rd create mode 100644 man/npatch_layout.Rd create mode 100644 man/pixel_format.Rd create mode 100644 man/shader_attribute_data_type.Rd create mode 100644 man/shader_location_index.Rd create mode 100644 man/shader_uniform_data_type.Rd create mode 100644 man/texture_filter.Rd create mode 100644 man/texture_wrap.Rd diff --git a/NAMESPACE b/NAMESPACE index d34b3ea..978fd62 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -100,7 +100,10 @@ export(IsWindowState) export(MaximizeWindow) export(MinimizeWindow) export(Quaternion) +export(Ray) export(Rectangle) +export(RenderTexture) +export(RenderTexture2D) export(RestoreWindow) export(SetCameraAltControl) export(SetCameraMode) @@ -116,6 +119,7 @@ export(SetShapesTexture) export(SetTargetFps) export(SetWindowIcon) export(SetWindowState) +export(Shader) export(Texture) export(Texture2D) export(TextureCubemap) diff --git a/R/example_core_2d_camera_platformer.R b/R/example_core_2d_camera_platformer.R index 04fb57f..5f7c6be 100644 --- a/R/example_core_2d_camera_platformer.R +++ b/R/example_core_2d_camera_platformer.R @@ -1,319 +1,326 @@ -###################### -# Pre-initialization # -###################### - -library(raylib.R) - -G <- 400 -player_jump_spd <- 350 -player_hor_spd <- 200 - -# 'player' object constructor -Player <- function(x, y, speed, can_jump) { - list(position = Vector2(x, y), - speed = speed, - can_jump = can_jump) -} - - -# 'environment item' object constructor -EnvItem <- function(x, y, width, height, blocking, color, alpha = 255) { - list(rect = Rectangle(x, y, width, height), - blocking = blocking, - color = Color(color, alpha)) -} - -# 'player update' function -update_player <- function(player, env_items, env_items_length, delta) { - - if (is_key_down(keyboard_key$KEY_LEFT)) - player$position["x"] <- player$position["x"] - player_hor_spd * delta - - if (is_key_down(keyboard_key$KEY_RIGHT)) - player$position["x"] <- player$position["x"] + player_hor_spd * delta - - if (is_key_down(keyboard_key$KEY_SPACE) && isTRUE(player$can_jump)) { - player$speed <- player$speed - player_jump_spd - player$can_jump <- FALSE - - } - - hit_obstacle <- FALSE - - collisions <- sapply( - env_items, \(env_item) { - env_item$blocking && - env_item$rect["x"] <= player$position["x"] && - (env_item$rect["x"] + env_item$rect["width"] >= player$position["x"]) && - env_item$rect["y"] >= player$position["y"] && - (env_item$rect["y"] < player$position["y"] + player$speed * delta) - }) - - coll_i <- which(collisions) - - if (sum(coll_i) > 0) { - hit_obstacle <- TRUE - player$speed <- 0 - player$position["y"] <- env_items[[coll_i]]$rect["y"] - } - - if (!hit_obstacle) { - player$position["y"] <- player$position["y"] + player$speed * delta - player$speed <- player$speed + G * delta - player$can_jump <- FALSE - - } else - player$can_jump <- TRUE - - return(player) -} - -# camera update functions -update_camera_center <- function( - camera, player, env_items, env_items_length, delta, width, height) { - camera$offset <- Vector2(width/2, height/2) - camera$target <- player$position - - return(camera) -} - -update_camera_center_inside_map <- function( - camera, player, env_items, env_items_length, delta, width, height) { - camera$target <- player$position - camera$offset <- Vector2(width/2, height/2) - - min_x <- min_y <- 1000 - max_x <- max_y <- -1000 - - for (i in 1:env_items_length) { - min_x <- min(env_items[[i]]$rect["x"], min_x) - max_x <- max(env_items[[i]]$rect["x"] + env_items$rect["width"], max_x) - min_y <- min(env_items[[i]]$rect["y"], min_y) - max_y <- max(env_items[[i]]$rect["y"] + env_items$rect["height"], max_y) - - - max = .Call(.C_GetWorldToScreen2D_R, Vector2(max_x, max_y), camera, PACKAGE = "raylib.R") - min = .Call(.C_GetWorldToScreen2D_R, Vector2(min_x, min_y), camera, PACKAGE = "raylib.R") - - if (max["x"] < width) camera$offset["x"] <- width - (max["x"] - width/2) - if (max["y"] < height) camera$offset["y"] <- heigh - (max["y"] - height/2) - if (min["x"] > 0) camera$offset["x"] <- width/2 - min["x"] - if (min["y"] > 0) camera$offset["y"] <- height/2 - min["y"] - - } - - return(camera) - } - -update_camera_center_smooth_follow <- function( - camera, player, env_items, env_items_length, delta, width, height) { - - min_speed <- 30 - min_effect_length <- 10 - fraction_speed <- 0.8 - - camera$offset <- Vector2(width/2, height/2) - - diff <- player$position - camera$target - veclength <- sqrt(sum(sapply(diff, \(x) x^2))) - - if (veclength > min_effect_length) { - speed <- max(fraction_speed * veclength, min_speed) - camera$target <- camera$target + diff*(speed * delta / veclength) - } - return(camera) -} - -update_camera_even_out_on_landing <- function( - camera, player, env_items, env_items_length, delta, width, height) { - - even_out_speed <- 700 - - camera$offset <- Vector2(width/2, height/2) - camera$target["x"] <- player$position["x"] - - if (isTRUE(.update_camera_even_out_env[["evening_out"]])) { - - if (.update_camera_even_out_env[["even_out_target"]] > camera$target["y"]) { - camera$target["y"] <- camera$target["y"] + even_out_speed * delta - if (camera$target["y"] > .update_camera_even_out_env[["even_out_target"]]) { - camera$target["y"] <- .update_camera_even_out_env[["even_out_target"]] - .update_camera_even_out_env[["evening_out"]] <- FALSE - } - } else { - camera$target["y"] <- camera$target["y"] - even_out_speed*delta - if (camera$target["y"] < .update_camera_even_out_env[["even_out_target"]]) { - camera$target["y"] <- .update_camera_even_out_env[["even_out_target"]] - .update_camera_even_out_env[["evening_out"]] <- FALSE - } - } - } else if (isTRUE(player$can_jump) && player$speed == 0 && player$position["y"] != camera$target["y"]) { - .update_camera_even_out_env[["evening_out"]] <- TRUE - .update_camera_even_out_env[["even_out_target"]] <- player$position["y"] - } - - return(camera) -} - -# environement needed for `update_even_out_on_landing` camera updater -.update_camera_even_out_env <- new.env() -.update_camera_even_out_env[["evening_out"]] <- FALSE -.update_camera_even_out_env[["even_out_target"]] <- NULL - -# update_camera_player_bound_push <- function( -# camera, player, env_items, env_items_length, delta, width, height) { -# -# bbox <- Vector2(0.2, 0.2) -# -# bbox_world_min <- .Call(.C_GetScreenToWorld2D_R, -# Vector2((1 - bbox["x"]) * 0.5 * width, (1 - bbox["y"] * 0.5 * height)), -# camera) -# bbox_world_max <- .Call(.C_GetScreenToWorld2D_R, -# Vector2((1 + bbox["x"]) * 0.5 * width, (1 + bbox["y"] * 0.5 * height)), -# camera) -# -# camera$offset <- Vector2((1 - bbox["x"]) * 0.5 * width, (1 - bbox["y"]) * 0.5 * height) -# -# if (player$position["x"] < bbox_world_min["x"]) -# camera$target["x"] <- player$position["x"] -# if (player$position["y"] < bbox_world_min["y"]) -# camera$target["y"] <- player$position["y"] -# if (player$position["x"] > bbox_world_max["x"]) -# camera$target["x"] <- bbox_world_min["x"] + (player$position["x"] - bbox_world_min["x"]) -# if (player$position["y"] > bbox_world_max["y"]) -# camera$target["y"] <- bbox_world_min["y"] + (player$position["y"] - bbox_world_min["y"]) -# -# return(camera) -# -# } - -################## -# Initialization # -################## - -play_game <- function() { - - screen_width <- 800 - screen_height <- 450 - - init_window(screen_width, screen_height, "raylib [core] example - 2d camera") - - player <- Player(x = 400, y = 280, speed = 0, can_jump = FALSE) - - env_items <- list( - EnvItem(0, 0, 1000, 400, FALSE, raylib_color$LIGHTGRAY), - EnvItem(0, 400, 1000, 200, TRUE, raylib_color$GRAY), - EnvItem(300, 200, 400, 10, TRUE, raylib_color$GRAY), - EnvItem(250, 300, 100, 10, TRUE, raylib_color$GRAY), - EnvItem(650, 300, 100, 10, TRUE, raylib_color$GRAY) - ) - - env_items_length <- length(env_items) - - camera <- Camera2D(target = player$position, - offset = Vector2(screen_width/2, screen_height/2), - rotation = 0, - zoom = 1) - - # set up camera updaters - camera_updaters <- list( - update = list( - descr = "Follow player center", - func = update_camera_center - ), - # update_inside_map = list( - # descr = "Follow player center, but clamp to map edges", - # func = update_camera_center_inside_map - # ), - update_smooth_follow = list( - descr = "Follow player center;\nsmoothed", - func = update_camera_center_smooth_follow - ), - update_even_out_on_landing = list( - descr = "Follow player center horizontally;\nupdate player center vertically after landing", - func = update_camera_even_out_on_landing - )#, - # update_player_bound_push = list( - # descr = "Player push camera on getting too close to screen edge", - # func = update_camera_player_bound_push - # ) - ) - - camera_option = 1 - - set_target_fps(60) - - ################## - # Main game loop # - ################## - - while(!window_should_close()) { - - # to implement - # delta_time <- get_frame_time() - delta_time <- 1/60 - - player <- update_player(player, env_items, env_items_length, delta_time) - - camera$zoom <- camera$zoom + get_mouse_wheel_move()*0.05 - - if (camera$zoom > 3) - camera$zoom = 3 - else if (camera$zoom < 0.25) - camera$zoom = 0.25 - - if (is_key_pressed(keyboard_key$KEY_R)) { - camera$zoom <- 1 - player$position <- Vector2(400, 280) - } - - if (is_key_pressed(keyboard_key$KEY_C)) { - camera_option <- camera_option %% length(camera_updaters) + 1 - } - - # update the camera! - camera <- do.call(what = camera_updaters[[camera_option]]$func, - args = list( - camera = camera, - player = player, - env_items = env_items, - env_items_length = env_items_length, - delta = delta_time, - width = screen_width, - height = screen_height - )) - - begin_drawing() - - clear_background(raylib_color$LIGHTGRAY) - - begin_mode_2d(camera) - - invisible(lapply(env_items, \(x) { draw_rectangle_rec(x$rect, x$color) })) - - player_rect <- Rectangle(player$position["x"] - 20, player$position["y"] - 40, 40, 40) - draw_rectangle_rec(player_rect, raylib_color$RED) - - end_mode_2d() - - DrawText("Controls:", 20, 20, 10, raylib_color$BLACK); - DrawText("- Right/Left to move", 40, 40, 10, raylib_color$DARKGRAY); - DrawText("- Space to jump", 40, 60, 10, raylib_color$DARKGRAY); - DrawText("- Mouse Wheel to Zoom in-out, R to reset zoom", 40, 80, 10, raylib_color$DARKGRAY); - DrawText("- C to change camera mode", 40, 100, 10, raylib_color$DARKGRAY); - DrawText("Current camera mode:", 20, 120, 10, raylib_color$BLACK); - DrawText(camera_updaters[[camera_option]]$descr, 40, 140, 10, raylib_color$DARKGRAY); - - end_drawing() - - } - - ##################### - # De-initialization # - ##################### - - close_window() - -} - -play_game() +#' @name core_2d_platformer +#' @rdname core_2d_platformer +#' @family Core examples +#' @title 2D camera platformer +#' +#' @description +#' This example shows multiple ways to update 2D camera with custom functions. +#' Also, basic collision functionality is implemented with basic mathematical +#' and R-base functions. +#' +#' @examplesIf interactive() +#' +#'###################### +#'# Pre-initialization # +#'###################### +#' +#' G <- 400 +#' player_jump_spd <- 350 +#' player_hor_spd <- 200 +#' +#' # 'player' object constructor +#' Player <- function(x, y, speed, can_jump) { +#' list(position = Vector2(x, y), +#' speed = speed, +#' can_jump = can_jump) +#' } +#' +#' +#' # 'environment item' object constructor +#' EnvItem <- function(x, y, width, height, blocking, color, alpha = 255) { +#' list(rect = Rectangle(x, y, width, height), +#' blocking = blocking, +#' color = Color(color, alpha)) +#' } +#' +#' # 'player update' function +#' update_player <- function(player, env_items, env_items_length, delta) { +#' +#' if (is_key_down(keyboard_key$KEY_LEFT)) +#' player$position["x"] <- player$position["x"] - player_hor_spd * delta +#' +#' if (is_key_down(keyboard_key$KEY_RIGHT)) +#' player$position["x"] <- player$position["x"] + player_hor_spd * delta +#' +#' if (is_key_down(keyboard_key$KEY_SPACE) && isTRUE(player$can_jump)) { +#' player$speed <- player$speed - player_jump_spd +#' player$can_jump <- FALSE +#' +#' } +#' +#' hit_obstacle <- FALSE +#' +#' collisions <- sapply( +#' env_items, \(env_item) { +#' env_item$blocking && +#' env_item$rect["x"] <= player$position["x"] && +#' (env_item$rect["x"] + env_item$rect["width"] >= player$position["x"]) && +#' env_item$rect["y"] >= player$position["y"] && +#' (env_item$rect["y"] < player$position["y"] + player$speed * delta) +#' }) +#' +#' coll_i <- which(collisions) +#' +#' if (sum(coll_i) > 0) { +#' hit_obstacle <- TRUE +#' player$speed <- 0 +#' player$position["y"] <- env_items[[coll_i]]$rect["y"] +#' } +#' +#' if (!hit_obstacle) { +#' player$position["y"] <- player$position["y"] + player$speed * delta +#' player$speed <- player$speed + G * delta +#' player$can_jump <- FALSE +#' +#' } else +#' player$can_jump <- TRUE +#' +#' return(player) +#' } +#' +#' # camera update functions +#' update_camera_center <- function( +#' camera, player, env_items, env_items_length, delta, width, height) { +#' camera$offset <- Vector2(width/2, height/2) +#' camera$target <- player$position +#' +#' return(camera) +#' } +#' +#' # update_camera_center_inside_map <- function( +#' # camera, player, env_items, env_items_length, delta, width, height) { +#' # camera$target <- player$position +#' # camera$offset <- Vector2(width/2, height/2) +#' # +#' # min_x <- min_y <- 1000 +#' # max_x <- max_y <- -1000 +#' # +#' # for (i in 1:env_items_length) { +#' # min_x <- min(env_items[[i]]$rect["x"], min_x) +#' # max_x <- max(env_items[[i]]$rect["x"] + env_items$rect["width"], max_x) +#' # min_y <- min(env_items[[i]]$rect["y"], min_y) +#' # max_y <- max(env_items[[i]]$rect["y"] + env_items$rect["height"], max_y) +#' # +#' # +#' # max = .Call(.C_GetWorldToScreen2D_R, Vector2(max_x, max_y), camera, PACKAGE = "raylib.R") +#' # min = .Call(.C_GetWorldToScreen2D_R, Vector2(min_x, min_y), camera, PACKAGE = "raylib.R") +#' # +#' # if (max["x"] < width) camera$offset["x"] <- width - (max["x"] - width/2) +#' # if (max["y"] < height) camera$offset["y"] <- heigh - (max["y"] - height/2) +#' # if (min["x"] > 0) camera$offset["x"] <- width/2 - min["x"] +#' # if (min["y"] > 0) camera$offset["y"] <- height/2 - min["y"] +#' # +#' # } +#' # +#' # return(camera) +#' # } +#' +#' update_camera_center_smooth_follow <- function( +#' camera, player, env_items, env_items_length, delta, width, height) { +#' +#' min_speed <- 30 +#' min_effect_length <- 10 +#' fraction_speed <- 0.8 +#' +#' camera$offset <- Vector2(width/2, height/2) +#' +#' diff <- player$position - camera$target +#' veclength <- sqrt(sum(sapply(diff, \(x) x^2))) +#' +#' if (veclength > min_effect_length) { +#' speed <- max(fraction_speed * veclength, min_speed) +#' camera$target <- camera$target + diff*(speed * delta / veclength) +#' } +#' return(camera) +#' } +#' +#' update_camera_even_out_on_landing <- function( +#' camera, player, env_items, env_items_length, delta, width, height) { +#' +#' even_out_speed <- 700 +#' +#' camera$offset <- Vector2(width/2, height/2) +#' camera$target["x"] <- player$position["x"] +#' +#' if (isTRUE(.update_camera_even_out_env[["evening_out"]])) { +#' +#' if (.update_camera_even_out_env[["even_out_target"]] > camera$target["y"]) { +#' camera$target["y"] <- camera$target["y"] + even_out_speed * delta +#' if (camera$target["y"] > .update_camera_even_out_env[["even_out_target"]]) { +#' camera$target["y"] <- .update_camera_even_out_env[["even_out_target"]] +#' .update_camera_even_out_env[["evening_out"]] <- FALSE +#' } +#' } else { +#' camera$target["y"] <- camera$target["y"] - even_out_speed*delta +#' if (camera$target["y"] < .update_camera_even_out_env[["even_out_target"]]) { +#' camera$target["y"] <- .update_camera_even_out_env[["even_out_target"]] +#' .update_camera_even_out_env[["evening_out"]] <- FALSE +#' } +#' } +#' } else if (isTRUE(player$can_jump) && player$speed == 0 && player$position["y"] != camera$target["y"]) { +#' .update_camera_even_out_env[["evening_out"]] <- TRUE +#' .update_camera_even_out_env[["even_out_target"]] <- player$position["y"] +#' } +#' +#' return(camera) +#' } +#' +#' # environement needed for `update_even_out_on_landing` camera updater +#' .update_camera_even_out_env <- new.env() +#' .update_camera_even_out_env[["evening_out"]] <- FALSE +#' .update_camera_even_out_env[["even_out_target"]] <- NULL +#' +#' # update_camera_player_bound_push <- function( +#' # camera, player, env_items, env_items_length, delta, width, height) { +#' # +#' # bbox <- Vector2(0.2, 0.2) +#' # +#' # bbox_world_min <- .Call(.C_GetScreenToWorld2D_R, +#' # Vector2((1 - bbox["x"]) * 0.5 * width, (1 - bbox["y"] * 0.5 * height)), +#' # camera) +#' # bbox_world_max <- .Call(.C_GetScreenToWorld2D_R, +#' # Vector2((1 + bbox["x"]) * 0.5 * width, (1 + bbox["y"] * 0.5 * height)), +#' # camera) +#' # +#' # camera$offset <- Vector2((1 - bbox["x"]) * 0.5 * width, (1 - bbox["y"]) * 0.5 * height) +#' # +#' # if (player$position["x"] < bbox_world_min["x"]) +#' # camera$target["x"] <- player$position["x"] +#' # if (player$position["y"] < bbox_world_min["y"]) +#' # camera$target["y"] <- player$position["y"] +#' # if (player$position["x"] > bbox_world_max["x"]) +#' # camera$target["x"] <- bbox_world_min["x"] + (player$position["x"] - bbox_world_min["x"]) +#' # if (player$position["y"] > bbox_world_max["y"]) +#' # camera$target["y"] <- bbox_world_min["y"] + (player$position["y"] - bbox_world_min["y"]) +#' # +#' # return(camera) +#' # +#' # } +#' +#' ################## +#' # Initialization # +#' ################## +#' +#' play_game <- function() { +#' +#' screen_width <- 800 +#' screen_height <- 450 +#' +#' init_window(screen_width, screen_height, "raylib [core] example - 2d camera") +#' on.exit(close_window()) +#' +#' player <- Player(x = 400, y = 280, speed = 0, can_jump = FALSE) +#' +#' env_items <- list( +#' EnvItem(0, 0, 1000, 400, FALSE, raylib_color$LIGHTGRAY), +#' EnvItem(0, 400, 1000, 200, TRUE, raylib_color$GRAY), +#' EnvItem(300, 200, 400, 10, TRUE, raylib_color$GRAY), +#' EnvItem(250, 300, 100, 10, TRUE, raylib_color$GRAY), +#' EnvItem(650, 300, 100, 10, TRUE, raylib_color$GRAY) +#' ) +#' +#' env_items_length <- length(env_items) +#' +#' camera <- Camera2D(target = player$position, +#' offset = Vector2(screen_width/2, screen_height/2), +#' rotation = 0, +#' zoom = 1) +#' +#' # set up camera updaters +#' camera_updaters <- list( +#' update = list( +#' descr = "Follow player center", +#' func = update_camera_center +#' ), +#' # update_inside_map = list( +#' # descr = "Follow player center, but clamp to map edges", +#' # func = update_camera_center_inside_map +#' # ), +#' update_smooth_follow = list( +#' descr = "Follow player center;\nsmoothed", +#' func = update_camera_center_smooth_follow +#' ), +#' update_even_out_on_landing = list( +#' descr = "Follow player center horizontally;\nupdate player center vertically after landing", +#' func = update_camera_even_out_on_landing +#' )#, +#' # update_player_bound_push = list( +#' # descr = "Player push camera on getting too close to screen edge", +#' # func = update_camera_player_bound_push +#' # ) +#' ) +#' +#' camera_option = 1 +#' +#' set_target_fps(60) +#' +#' ################## +#' # Main game loop # +#' ################## +#' +#' while(!window_should_close()) { +#' +#' # to implement +#' # delta_time <- get_frame_time() +#' delta_time <- 1/60 +#' +#' player <- update_player(player, env_items, env_items_length, delta_time) +#' +#' camera$zoom <- camera$zoom + get_mouse_wheel_move()*0.05 +#' +#' if (camera$zoom > 3) +#' camera$zoom = 3 +#' else if (camera$zoom < 0.25) +#' camera$zoom = 0.25 +#' +#' if (is_key_pressed(keyboard_key$KEY_R)) { +#' camera$zoom <- 1 +#' player$position <- Vector2(400, 280) +#' } +#' +#' if (is_key_pressed(keyboard_key$KEY_C)) { +#' camera_option <- camera_option %% length(camera_updaters) + 1 +#' } +#' +#' # update the camera! +#' camera <- do.call(what = camera_updaters[[camera_option]]$func, +#' args = list( +#' camera = camera, +#' player = player, +#' env_items = env_items, +#' env_items_length = env_items_length, +#' delta = delta_time, +#' width = screen_width, +#' height = screen_height +#' )) +#' +#' begin_drawing() +#' +#' clear_background(raylib_color$LIGHTGRAY) +#' +#' begin_mode_2d(camera) +#' +#' invisible(lapply(env_items, \(x) { draw_rectangle_rec(x$rect, x$color) })) +#' +#' player_rect <- Rectangle(player$position["x"] - 20, player$position["y"] - 40, 40, 40) +#' draw_rectangle_rec(player_rect, raylib_color$RED) +#' +#' end_mode_2d() +#' +#' DrawText("Controls:", 20, 20, 10, raylib_color$BLACK); +#' DrawText("- Right/Left to move", 40, 40, 10, raylib_color$DARKGRAY); +#' DrawText("- Space to jump", 40, 60, 10, raylib_color$DARKGRAY); +#' DrawText("- Mouse Wheel to Zoom in-out, R to reset zoom", 40, 80, 10, raylib_color$DARKGRAY); +#' DrawText("- C to change camera mode", 40, 100, 10, raylib_color$DARKGRAY); +#' DrawText("Current camera mode:", 20, 120, 10, raylib_color$BLACK); +#' DrawText(camera_updaters[[camera_option]]$descr, 40, 140, 10, raylib_color$DARKGRAY); +#' +#' end_drawing() +#' +#' } +#' } +#' +#' # run the game +#' +#' play_game() +NULL \ No newline at end of file diff --git a/R/example_core_basic_screen_manager.R b/R/example_core_basic_screen_manager.R new file mode 100644 index 0000000..6469582 --- /dev/null +++ b/R/example_core_basic_screen_manager.R @@ -0,0 +1,117 @@ +#' @name core_basic_screen_manager +#' @rdname core_basic_screen_manager +#' @family Core examples +#' @title Basic Screen Manager +#' +#' @description +#' Example showing possibility to menage different screen types in main +#' game loop +#' +#' @examplesIf interactive() +#' +#' play_game <- function() { +#' +#' ################## +#' # Initialization # +#' ################## +#' +#' screen_width <- 800 +#' screen_height <- 450 +#' +#' init_window(screen_width, screen_height, "raylib [core] example - basic screen manager") +#' +#' # deinitialization of the window +#' on.exit(close_window()) +#' +#' current_screen <- "LOGO" +#' +#' frames_counter <- 0 +#' +#' set_target_fps(60) +#' +#' ################## +#' # Main game loop # +#' ################## +#' +#' while(!window_should_close()) { +#' +#' # menage screen switches during this part +#' switch(current_screen, +#' +#' LOGO = { +#' +#' frames_counter <- frames_counter + 1 # count frames +#' +#' # Wait for 2 seconds (120 frames) before jumping to TITLE screen +#' if (frames_counter > 120) +#' current_screen <- "TITLE" +#' }, +#' +#' TITLE = { +#' +#' if (is_key_pressed(keyboard_key$KEY_ENTER)) +#' current_screen <- "GAMEPLAY" +#' +#' }, +#' +#' GAMEPLAY = { +#' +#' if (is_key_pressed(keyboard_key$KEY_ENTER)) +#' current_screen <- "ENDING" +#' +#' }, +#' +#' ENDING = { +#' +#' if (is_key_pressed(keyboard_key$KEY_ENTER)) +#' current_screen <- "TITLE" +#' +#' }) +#' +#' begin_drawing() +#' +#' clear_background(raylib_color$RAYWHITE) +#' +#' # draw differently depending on the current screen +#' switch (current_screen, +#' +#' LOGO = { +#' +#' draw_text("LOGO SCREEN", 20, 20, 40, raylib_color$LIGHTGRAY) +#' draw_text("WAIT for 2 SECONDS...", 290, 220, 20, raylib_color$GRAY) +#' +#' }, +#' +#' TITLE = { +#' +#' draw_rectangle(0, 0, screen_width, screen_height, raylib_color$GREEN) +#' draw_text("TITLE SCREEN", 20, 20, 40, raylib_color$DARKGREEN) +#' draw_text("PRESS ENTER to JUMP to GAMEPLAY SCREEN", 120, 220, 20, raylib_color$DARKGREEN) +#' +#' }, +#' +#' GAMEPLAY = { +#' +#' draw_rectangle(0, 0, screen_width, screen_height, raylib_color$PURPLE) +#' draw_text("GAMEPLAY SCREEN", 20, 20, 40, raylib_color$MAROON) +#' draw_text("PRESS ENTER to JUMP to ENDING SCREEN", 120, 220, 20, raylib_color$MAROON) +#' +#' }, +#' +#' ENDING = { +#' +#' draw_rectangle(0, 0, screen_width, screen_height, raylib_color$BLUE) +#' draw_text("ENDNG SCREEN", 20, 20, 40, raylib_color$DARKBLUE) +#' draw_text("PRESS ENTER to JUMP to ENDING SCREEN", 120, 220, 20, raylib_color$DARKBLUE) +#' +#' }) +#' +#' end_drawing() +#' +#' } +#' } +#' +#' # run the game +#' +#' play_game() +NULL \ No newline at end of file diff --git a/man/Camera2D.Rd b/man/Camera2D.Rd index fc39887..9c18591 100644 --- a/man/Camera2D.Rd +++ b/man/Camera2D.Rd @@ -23,7 +23,10 @@ Other Raylib objects: \code{\link{Camera3D}()}, \code{\link{Color}()}, \code{\link{Image}()}, +\code{\link{Ray}()}, \code{\link{Rectangle}()}, +\code{\link{RenderTexture}()}, +\code{\link{Shader}()}, \code{\link{Texture}()}, \code{\link{Vector2}()}, \code{\link{Vector3}()}, diff --git a/man/Camera3D.Rd b/man/Camera3D.Rd index 41db211..1d6d725 100644 --- a/man/Camera3D.Rd +++ b/man/Camera3D.Rd @@ -41,7 +41,10 @@ Other Raylib objects: \code{\link{Camera2D}()}, \code{\link{Color}()}, \code{\link{Image}()}, +\code{\link{Ray}()}, \code{\link{Rectangle}()}, +\code{\link{RenderTexture}()}, +\code{\link{Shader}()}, \code{\link{Texture}()}, \code{\link{Vector2}()}, \code{\link{Vector3}()}, @@ -51,7 +54,10 @@ Other Raylib objects: \code{\link{Camera2D}()}, \code{\link{Color}()}, \code{\link{Image}()}, +\code{\link{Ray}()}, \code{\link{Rectangle}()}, +\code{\link{RenderTexture}()}, +\code{\link{Shader}()}, \code{\link{Texture}()}, \code{\link{Vector2}()}, \code{\link{Vector3}()}, @@ -61,7 +67,10 @@ Other Raylib objects: \code{\link{Camera2D}()}, \code{\link{Color}()}, \code{\link{Image}()}, +\code{\link{Ray}()}, \code{\link{Rectangle}()}, +\code{\link{RenderTexture}()}, +\code{\link{Shader}()}, \code{\link{Texture}()}, \code{\link{Vector2}()}, \code{\link{Vector3}()}, @@ -71,7 +80,10 @@ Other Raylib objects: \code{\link{Camera2D}()}, \code{\link{Color}()}, \code{\link{Image}()}, +\code{\link{Ray}()}, \code{\link{Rectangle}()}, +\code{\link{RenderTexture}()}, +\code{\link{Shader}()}, \code{\link{Texture}()}, \code{\link{Vector2}()}, \code{\link{Vector3}()}, diff --git a/man/Color.Rd b/man/Color.Rd index f275442..e880680 100644 --- a/man/Color.Rd +++ b/man/Color.Rd @@ -27,7 +27,10 @@ Other Raylib objects: \code{\link{Camera2D}()}, \code{\link{Camera3D}()}, \code{\link{Image}()}, +\code{\link{Ray}()}, \code{\link{Rectangle}()}, +\code{\link{RenderTexture}()}, +\code{\link{Shader}()}, \code{\link{Texture}()}, \code{\link{Vector2}()}, \code{\link{Vector3}()}, @@ -37,7 +40,10 @@ Other Raylib objects: \code{\link{Camera2D}()}, \code{\link{Camera3D}()}, \code{\link{Image}()}, +\code{\link{Ray}()}, \code{\link{Rectangle}()}, +\code{\link{RenderTexture}()}, +\code{\link{Shader}()}, \code{\link{Texture}()}, \code{\link{Vector2}()}, \code{\link{Vector3}()}, diff --git a/man/Image.Rd b/man/Image.Rd index 2c729ea..8c2cd00 100644 --- a/man/Image.Rd +++ b/man/Image.Rd @@ -17,7 +17,10 @@ Other Raylib objects: \code{\link{Camera2D}()}, \code{\link{Camera3D}()}, \code{\link{Color}()}, +\code{\link{Ray}()}, \code{\link{Rectangle}()}, +\code{\link{RenderTexture}()}, +\code{\link{Shader}()}, \code{\link{Texture}()}, \code{\link{Vector2}()}, \code{\link{Vector3}()}, diff --git a/man/Ray.Rd b/man/Ray.Rd new file mode 100644 index 0000000..331658c --- /dev/null +++ b/man/Ray.Rd @@ -0,0 +1,31 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/types.R +\name{Ray} +\alias{Ray} +\title{Ray, ray for raycasting} +\usage{ +Ray(position, direction) +} +\arguments{ +\item{position}{Vector3. Ray position (origin)} + +\item{direction}{Vector3. Ray direction (origin)} +} +\description{ +Ray, ray for raycasting +} +\seealso{ +Other Raylib objects: +\code{\link{Camera2D}()}, +\code{\link{Camera3D}()}, +\code{\link{Color}()}, +\code{\link{Image}()}, +\code{\link{Rectangle}()}, +\code{\link{RenderTexture}()}, +\code{\link{Shader}()}, +\code{\link{Texture}()}, +\code{\link{Vector2}()}, +\code{\link{Vector3}()}, +\code{\link{Vector4}()} +} +\concept{Raylib objects} diff --git a/man/Rectangle.Rd b/man/Rectangle.Rd index 53521ba..01da4fc 100644 --- a/man/Rectangle.Rd +++ b/man/Rectangle.Rd @@ -26,6 +26,9 @@ Other Raylib objects: \code{\link{Camera3D}()}, \code{\link{Color}()}, \code{\link{Image}()}, +\code{\link{Ray}()}, +\code{\link{RenderTexture}()}, +\code{\link{Shader}()}, \code{\link{Texture}()}, \code{\link{Vector2}()}, \code{\link{Vector3}()}, diff --git a/man/RenderTexture.Rd b/man/RenderTexture.Rd new file mode 100644 index 0000000..4427722 --- /dev/null +++ b/man/RenderTexture.Rd @@ -0,0 +1,49 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/types.R +\name{RenderTexture} +\alias{RenderTexture} +\alias{RenderTexture2D} +\title{RenderTexture, fbo for texture rendering} +\usage{ +RenderTexture(id, texture, depth) + +RenderTexture2D(id, texture, depth) +} +\arguments{ +\item{id}{Unsigned integer. OpenGL framebuffer object id} + +\item{texture}{Texture. Color buffer attachment texture} + +\item{depth}{Texture. Depth buffer attachment texture} +} +\description{ +RenderTexture, fbo for texture rendering +} +\seealso{ +Other Raylib objects: +\code{\link{Camera2D}()}, +\code{\link{Camera3D}()}, +\code{\link{Color}()}, +\code{\link{Image}()}, +\code{\link{Ray}()}, +\code{\link{Rectangle}()}, +\code{\link{Shader}()}, +\code{\link{Texture}()}, +\code{\link{Vector2}()}, +\code{\link{Vector3}()}, +\code{\link{Vector4}()} + +Other Raylib objects: +\code{\link{Camera2D}()}, +\code{\link{Camera3D}()}, +\code{\link{Color}()}, +\code{\link{Image}()}, +\code{\link{Ray}()}, +\code{\link{Rectangle}()}, +\code{\link{Shader}()}, +\code{\link{Texture}()}, +\code{\link{Vector2}()}, +\code{\link{Vector3}()}, +\code{\link{Vector4}()} +} +\concept{Raylib objects} diff --git a/man/Shader.Rd b/man/Shader.Rd new file mode 100644 index 0000000..1913436 --- /dev/null +++ b/man/Shader.Rd @@ -0,0 +1,35 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/types.R +\name{Shader} +\alias{Shader} +\title{Shader, Load a shader from files or code} +\usage{ +Shader(vs_filename, fs_filename, vs_code, fs_code) +} +\arguments{ +\item{vs_filename}{Path for vertex shader code.} + +\item{fs_filename}{Path for fragment shader code.} + +\item{vs_code}{Vertex shader code.} + +\item{fs_code}{Fragment shader code.} +} +\description{ +Shader, Load a shader from files or code +} +\seealso{ +Other Raylib objects: +\code{\link{Camera2D}()}, +\code{\link{Camera3D}()}, +\code{\link{Color}()}, +\code{\link{Image}()}, +\code{\link{Ray}()}, +\code{\link{Rectangle}()}, +\code{\link{RenderTexture}()}, +\code{\link{Texture}()}, +\code{\link{Vector2}()}, +\code{\link{Vector3}()}, +\code{\link{Vector4}()} +} +\concept{Raylib objects} diff --git a/man/Texture.Rd b/man/Texture.Rd index 7e0f95b..957b62b 100644 --- a/man/Texture.Rd +++ b/man/Texture.Rd @@ -34,7 +34,10 @@ Other Raylib objects: \code{\link{Camera3D}()}, \code{\link{Color}()}, \code{\link{Image}()}, +\code{\link{Ray}()}, \code{\link{Rectangle}()}, +\code{\link{RenderTexture}()}, +\code{\link{Shader}()}, \code{\link{Vector2}()}, \code{\link{Vector3}()}, \code{\link{Vector4}()} @@ -44,7 +47,10 @@ Other Raylib objects: \code{\link{Camera3D}()}, \code{\link{Color}()}, \code{\link{Image}()}, +\code{\link{Ray}()}, \code{\link{Rectangle}()}, +\code{\link{RenderTexture}()}, +\code{\link{Shader}()}, \code{\link{Vector2}()}, \code{\link{Vector3}()}, \code{\link{Vector4}()} @@ -54,7 +60,10 @@ Other Raylib objects: \code{\link{Camera3D}()}, \code{\link{Color}()}, \code{\link{Image}()}, +\code{\link{Ray}()}, \code{\link{Rectangle}()}, +\code{\link{RenderTexture}()}, +\code{\link{Shader}()}, \code{\link{Vector2}()}, \code{\link{Vector3}()}, \code{\link{Vector4}()} diff --git a/man/Vector2.Rd b/man/Vector2.Rd index e17bf96..6f0e076 100644 --- a/man/Vector2.Rd +++ b/man/Vector2.Rd @@ -21,7 +21,10 @@ Other Raylib objects: \code{\link{Camera3D}()}, \code{\link{Color}()}, \code{\link{Image}()}, +\code{\link{Ray}()}, \code{\link{Rectangle}()}, +\code{\link{RenderTexture}()}, +\code{\link{Shader}()}, \code{\link{Texture}()}, \code{\link{Vector3}()}, \code{\link{Vector4}()} diff --git a/man/Vector3.Rd b/man/Vector3.Rd index b9b9193..dc906f6 100644 --- a/man/Vector3.Rd +++ b/man/Vector3.Rd @@ -23,7 +23,10 @@ Other Raylib objects: \code{\link{Camera3D}()}, \code{\link{Color}()}, \code{\link{Image}()}, +\code{\link{Ray}()}, \code{\link{Rectangle}()}, +\code{\link{RenderTexture}()}, +\code{\link{Shader}()}, \code{\link{Texture}()}, \code{\link{Vector2}()}, \code{\link{Vector4}()} diff --git a/man/Vector4.Rd b/man/Vector4.Rd index e28ba4e..63acbde 100644 --- a/man/Vector4.Rd +++ b/man/Vector4.Rd @@ -28,7 +28,10 @@ Other Raylib objects: \code{\link{Camera3D}()}, \code{\link{Color}()}, \code{\link{Image}()}, +\code{\link{Ray}()}, \code{\link{Rectangle}()}, +\code{\link{RenderTexture}()}, +\code{\link{Shader}()}, \code{\link{Texture}()}, \code{\link{Vector2}()}, \code{\link{Vector3}()} @@ -38,7 +41,10 @@ Other Raylib objects: \code{\link{Camera3D}()}, \code{\link{Color}()}, \code{\link{Image}()}, +\code{\link{Ray}()}, \code{\link{Rectangle}()}, +\code{\link{RenderTexture}()}, +\code{\link{Shader}()}, \code{\link{Texture}()}, \code{\link{Vector2}()}, \code{\link{Vector3}()} diff --git a/man/blend_mode.Rd b/man/blend_mode.Rd new file mode 100644 index 0000000..5d830d7 --- /dev/null +++ b/man/blend_mode.Rd @@ -0,0 +1,25 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/enums.R +\docType{data} +\name{blend_mode} +\alias{blend_mode} +\title{Color blending modes (pre-defined)} +\format{ +An object of class ‘list’ of length 7. +\describe{ +\item{BLEND_ALPHA}{Blend textures considering alpha (default)} +\item{BLEND_ADDITIVE}{Blend textures adding colors} +\item{BLEND_MULTIPLIED}{Blend textures multiplying colors} +\item{BLEND_ADD_COLORS}{Blend textures adding colors (alternative)} +\item{BLEND_SUBTRACT_COLORS}{Blend textures subtracting colors (alternative)} +\item{BLEND_ALPHA_PREMUL}{Blend premultiplied textures considering alpha} +\item{BLEND_CUSTOM}{Blend textures using custom src/dst factors (use rlSetBlendMode())} +} +} +\usage{ +blend_mode +} +\description{ +Color blending modes (pre-defined) +} +\keyword{datasets} diff --git a/man/core_2d_camera.Rd b/man/core_2d_camera.Rd index d00a073..e66fc01 100644 --- a/man/core_2d_camera.Rd +++ b/man/core_2d_camera.Rd @@ -142,9 +142,11 @@ close_window() # Close window and OpenGL context } \seealso{ Other Core examples: +\code{\link{core_2d_platformer}}, \code{\link{core_3d_camera_first_person}}, \code{\link{core_3d_camera_free}}, \code{\link{core_3d_camera}}, +\code{\link{core_basic_screen_manager}}, \code{\link{core_basic_window}}, \code{\link{core_drop_files}}, \code{\link{core_input_keys}}, diff --git a/man/core_2d_platformer.Rd b/man/core_2d_platformer.Rd new file mode 100644 index 0000000..432dc4b --- /dev/null +++ b/man/core_2d_platformer.Rd @@ -0,0 +1,341 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/example_core_2d_camera_platformer.R +\name{core_2d_platformer} +\alias{core_2d_platformer} +\title{2D camera platformer} +\description{ +This example shows multiple ways to update 2D camera with custom functions. +Also, basic collision functionality is implemented with basic mathematical +and R-base functions. +} +\examples{ +\dontshow{if (interactive()) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} + +###################### +# Pre-initialization # +###################### + +G <- 400 +player_jump_spd <- 350 +player_hor_spd <- 200 + +# 'player' object constructor +Player <- function(x, y, speed, can_jump) { + list(position = Vector2(x, y), + speed = speed, + can_jump = can_jump) +} + + +# 'environment item' object constructor +EnvItem <- function(x, y, width, height, blocking, color, alpha = 255) { + list(rect = Rectangle(x, y, width, height), + blocking = blocking, + color = Color(color, alpha)) +} + +# 'player update' function +update_player <- function(player, env_items, env_items_length, delta) { + + if (is_key_down(keyboard_key$KEY_LEFT)) + player$position["x"] <- player$position["x"] - player_hor_spd * delta + + if (is_key_down(keyboard_key$KEY_RIGHT)) + player$position["x"] <- player$position["x"] + player_hor_spd * delta + + if (is_key_down(keyboard_key$KEY_SPACE) && isTRUE(player$can_jump)) { + player$speed <- player$speed - player_jump_spd + player$can_jump <- FALSE + + } + + hit_obstacle <- FALSE + + collisions <- sapply( + env_items, \(env_item) { + env_item$blocking && + env_item$rect["x"] <= player$position["x"] && + (env_item$rect["x"] + env_item$rect["width"] >= player$position["x"]) && + env_item$rect["y"] >= player$position["y"] && + (env_item$rect["y"] < player$position["y"] + player$speed * delta) + }) + + coll_i <- which(collisions) + + if (sum(coll_i) > 0) { + hit_obstacle <- TRUE + player$speed <- 0 + player$position["y"] <- env_items[[coll_i]]$rect["y"] + } + + if (!hit_obstacle) { + player$position["y"] <- player$position["y"] + player$speed * delta + player$speed <- player$speed + G * delta + player$can_jump <- FALSE + + } else + player$can_jump <- TRUE + + return(player) +} + +# camera update functions +update_camera_center <- function( + camera, player, env_items, env_items_length, delta, width, height) { + camera$offset <- Vector2(width/2, height/2) + camera$target <- player$position + + return(camera) +} + +# update_camera_center_inside_map <- function( +# camera, player, env_items, env_items_length, delta, width, height) { +# camera$target <- player$position +# camera$offset <- Vector2(width/2, height/2) +# +# min_x <- min_y <- 1000 +# max_x <- max_y <- -1000 +# +# for (i in 1:env_items_length) { +# min_x <- min(env_items[[i]]$rect["x"], min_x) +# max_x <- max(env_items[[i]]$rect["x"] + env_items$rect["width"], max_x) +# min_y <- min(env_items[[i]]$rect["y"], min_y) +# max_y <- max(env_items[[i]]$rect["y"] + env_items$rect["height"], max_y) +# +# +# max = .Call(.C_GetWorldToScreen2D_R, Vector2(max_x, max_y), camera, PACKAGE = "raylib.R") +# min = .Call(.C_GetWorldToScreen2D_R, Vector2(min_x, min_y), camera, PACKAGE = "raylib.R") +# +# if (max["x"] < width) camera$offset["x"] <- width - (max["x"] - width/2) +# if (max["y"] < height) camera$offset["y"] <- heigh - (max["y"] - height/2) +# if (min["x"] > 0) camera$offset["x"] <- width/2 - min["x"] +# if (min["y"] > 0) camera$offset["y"] <- height/2 - min["y"] +# +# } +# +# return(camera) +# } + +update_camera_center_smooth_follow <- function( + camera, player, env_items, env_items_length, delta, width, height) { + + min_speed <- 30 + min_effect_length <- 10 + fraction_speed <- 0.8 + + camera$offset <- Vector2(width/2, height/2) + + diff <- player$position - camera$target + veclength <- sqrt(sum(sapply(diff, \(x) x^2))) + + if (veclength > min_effect_length) { + speed <- max(fraction_speed * veclength, min_speed) + camera$target <- camera$target + diff*(speed * delta / veclength) + } + return(camera) +} + +update_camera_even_out_on_landing <- function( + camera, player, env_items, env_items_length, delta, width, height) { + + even_out_speed <- 700 + + camera$offset <- Vector2(width/2, height/2) + camera$target["x"] <- player$position["x"] + + if (isTRUE(.update_camera_even_out_env[["evening_out"]])) { + + if (.update_camera_even_out_env[["even_out_target"]] > camera$target["y"]) { + camera$target["y"] <- camera$target["y"] + even_out_speed * delta + if (camera$target["y"] > .update_camera_even_out_env[["even_out_target"]]) { + camera$target["y"] <- .update_camera_even_out_env[["even_out_target"]] + .update_camera_even_out_env[["evening_out"]] <- FALSE + } + } else { + camera$target["y"] <- camera$target["y"] - even_out_speed*delta + if (camera$target["y"] < .update_camera_even_out_env[["even_out_target"]]) { + camera$target["y"] <- .update_camera_even_out_env[["even_out_target"]] + .update_camera_even_out_env[["evening_out"]] <- FALSE + } + } + } else if (isTRUE(player$can_jump) && player$speed == 0 && player$position["y"] != camera$target["y"]) { + .update_camera_even_out_env[["evening_out"]] <- TRUE + .update_camera_even_out_env[["even_out_target"]] <- player$position["y"] + } + + return(camera) +} + +# environement needed for `update_even_out_on_landing` camera updater +.update_camera_even_out_env <- new.env() +.update_camera_even_out_env[["evening_out"]] <- FALSE +.update_camera_even_out_env[["even_out_target"]] <- NULL + +# update_camera_player_bound_push <- function( + # camera, player, env_items, env_items_length, delta, width, height) { +# +# bbox <- Vector2(0.2, 0.2) +# +# bbox_world_min <- .Call(.C_GetScreenToWorld2D_R, +# Vector2((1 - bbox["x"]) * 0.5 * width, (1 - bbox["y"] * 0.5 * height)), +# camera) +# bbox_world_max <- .Call(.C_GetScreenToWorld2D_R, +# Vector2((1 + bbox["x"]) * 0.5 * width, (1 + bbox["y"] * 0.5 * height)), +# camera) +# +# camera$offset <- Vector2((1 - bbox["x"]) * 0.5 * width, (1 - bbox["y"]) * 0.5 * height) +# +# if (player$position["x"] < bbox_world_min["x"]) +# camera$target["x"] <- player$position["x"] +# if (player$position["y"] < bbox_world_min["y"]) +# camera$target["y"] <- player$position["y"] +# if (player$position["x"] > bbox_world_max["x"]) +# camera$target["x"] <- bbox_world_min["x"] + (player$position["x"] - bbox_world_min["x"]) +# if (player$position["y"] > bbox_world_max["y"]) +# camera$target["y"] <- bbox_world_min["y"] + (player$position["y"] - bbox_world_min["y"]) +# +# return(camera) +# +# } + +################## +# Initialization # +################## + +play_game <- function() { + + screen_width <- 800 + screen_height <- 450 + + init_window(screen_width, screen_height, "raylib [core] example - 2d camera") + on.exit(close_window()) + + player <- Player(x = 400, y = 280, speed = 0, can_jump = FALSE) + + env_items <- list( + EnvItem(0, 0, 1000, 400, FALSE, raylib_color$LIGHTGRAY), + EnvItem(0, 400, 1000, 200, TRUE, raylib_color$GRAY), + EnvItem(300, 200, 400, 10, TRUE, raylib_color$GRAY), + EnvItem(250, 300, 100, 10, TRUE, raylib_color$GRAY), + EnvItem(650, 300, 100, 10, TRUE, raylib_color$GRAY) + ) + + env_items_length <- length(env_items) + + camera <- Camera2D(target = player$position, + offset = Vector2(screen_width/2, screen_height/2), + rotation = 0, + zoom = 1) + + # set up camera updaters + camera_updaters <- list( + update = list( + descr = "Follow player center", + func = update_camera_center + ), + # update_inside_map = list( + # descr = "Follow player center, but clamp to map edges", + # func = update_camera_center_inside_map + # ), + update_smooth_follow = list( + descr = "Follow player center;\nsmoothed", + func = update_camera_center_smooth_follow + ), + update_even_out_on_landing = list( + descr = "Follow player center horizontally;\nupdate player center vertically after landing", + func = update_camera_even_out_on_landing + )#, + # update_player_bound_push = list( + # descr = "Player push camera on getting too close to screen edge", + # func = update_camera_player_bound_push + # ) + ) + + camera_option = 1 + + set_target_fps(60) + + ################## + # Main game loop # + ################## + + while(!window_should_close()) { + + # to implement + # delta_time <- get_frame_time() + delta_time <- 1/60 + + player <- update_player(player, env_items, env_items_length, delta_time) + + camera$zoom <- camera$zoom + get_mouse_wheel_move()*0.05 + + if (camera$zoom > 3) + camera$zoom = 3 + else if (camera$zoom < 0.25) + camera$zoom = 0.25 + + if (is_key_pressed(keyboard_key$KEY_R)) { + camera$zoom <- 1 + player$position <- Vector2(400, 280) + } + + if (is_key_pressed(keyboard_key$KEY_C)) { + camera_option <- camera_option \%\% length(camera_updaters) + 1 + } + + # update the camera! + camera <- do.call(what = camera_updaters[[camera_option]]$func, + args = list( + camera = camera, + player = player, + env_items = env_items, + env_items_length = env_items_length, + delta = delta_time, + width = screen_width, + height = screen_height + )) + + begin_drawing() + + clear_background(raylib_color$LIGHTGRAY) + + begin_mode_2d(camera) + + invisible(lapply(env_items, \(x) { draw_rectangle_rec(x$rect, x$color) })) + + player_rect <- Rectangle(player$position["x"] - 20, player$position["y"] - 40, 40, 40) + draw_rectangle_rec(player_rect, raylib_color$RED) + + end_mode_2d() + + DrawText("Controls:", 20, 20, 10, raylib_color$BLACK); + DrawText("- Right/Left to move", 40, 40, 10, raylib_color$DARKGRAY); + DrawText("- Space to jump", 40, 60, 10, raylib_color$DARKGRAY); + DrawText("- Mouse Wheel to Zoom in-out, R to reset zoom", 40, 80, 10, raylib_color$DARKGRAY); + DrawText("- C to change camera mode", 40, 100, 10, raylib_color$DARKGRAY); + DrawText("Current camera mode:", 20, 120, 10, raylib_color$BLACK); + DrawText(camera_updaters[[camera_option]]$descr, 40, 140, 10, raylib_color$DARKGRAY); + + end_drawing() + + } +} + +# run the game + +play_game() +\dontshow{\}) # examplesIf} +} +\seealso{ +Other Core examples: +\code{\link{core_2d_camera}}, +\code{\link{core_3d_camera_first_person}}, +\code{\link{core_3d_camera_free}}, +\code{\link{core_3d_camera}}, +\code{\link{core_basic_screen_manager}}, +\code{\link{core_basic_window}}, +\code{\link{core_drop_files}}, +\code{\link{core_input_keys}}, +\code{\link{core_input_mouse}} +} +\concept{Core examples} diff --git a/man/core_3d_camera.Rd b/man/core_3d_camera.Rd index 4eab861..529629f 100644 --- a/man/core_3d_camera.Rd +++ b/man/core_3d_camera.Rd @@ -68,8 +68,10 @@ close_window() # Close window and OpenGL context \seealso{ Other Core examples: \code{\link{core_2d_camera}}, +\code{\link{core_2d_platformer}}, \code{\link{core_3d_camera_first_person}}, \code{\link{core_3d_camera_free}}, +\code{\link{core_basic_screen_manager}}, \code{\link{core_basic_window}}, \code{\link{core_drop_files}}, \code{\link{core_input_keys}}, diff --git a/man/core_3d_camera_first_person.Rd b/man/core_3d_camera_first_person.Rd index d0c22a3..9f1df2b 100644 --- a/man/core_3d_camera_first_person.Rd +++ b/man/core_3d_camera_first_person.Rd @@ -106,8 +106,10 @@ close_window() # Close window and OpenGL context \seealso{ Other Core examples: \code{\link{core_2d_camera}}, +\code{\link{core_2d_platformer}}, \code{\link{core_3d_camera_free}}, \code{\link{core_3d_camera}}, +\code{\link{core_basic_screen_manager}}, \code{\link{core_basic_window}}, \code{\link{core_drop_files}}, \code{\link{core_input_keys}}, diff --git a/man/core_3d_camera_free.Rd b/man/core_3d_camera_free.Rd index 5f38d64..27cc022 100644 --- a/man/core_3d_camera_free.Rd +++ b/man/core_3d_camera_free.Rd @@ -87,8 +87,10 @@ close_window() # Close window and OpenGL context \seealso{ Other Core examples: \code{\link{core_2d_camera}}, +\code{\link{core_2d_platformer}}, \code{\link{core_3d_camera_first_person}}, \code{\link{core_3d_camera}}, +\code{\link{core_basic_screen_manager}}, \code{\link{core_basic_window}}, \code{\link{core_drop_files}}, \code{\link{core_input_keys}}, diff --git a/man/core_basic_screen_manager.Rd b/man/core_basic_screen_manager.Rd new file mode 100644 index 0000000..0a3a629 --- /dev/null +++ b/man/core_basic_screen_manager.Rd @@ -0,0 +1,132 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/example_core_basic_screen_manager.R +\name{core_basic_screen_manager} +\alias{core_basic_screen_manager} +\title{Basic Screen Manager} +\description{ +Example showing possibility to menage different screen types in main +game loop +} +\examples{ +\dontshow{if (interactive()) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} + +play_game <- function() { + + ################## + # Initialization # + ################## + + screen_width <- 800 + screen_height <- 450 + + init_window(screen_width, screen_height, "raylib [core] example - basic screen manager") + + # deinitialization of the window + on.exit(close_window()) + + current_screen <- "LOGO" + + frames_counter <- 0 + + set_target_fps(60) + + ################## + # Main game loop # + ################## + + while(!window_should_close()) { + + # menage screen switches during this part + switch(current_screen, + + LOGO = { + + frames_counter <- frames_counter + 1 # count frames + + # Wait for 2 seconds (120 frames) before jumping to TITLE screen + if (frames_counter > 120) + current_screen <- "TITLE" + }, + + TITLE = { + + if (is_key_pressed(keyboard_key$KEY_ENTER)) + current_screen <- "GAMEPLAY" + + }, + + GAMEPLAY = { + + if (is_key_pressed(keyboard_key$KEY_ENTER)) + current_screen <- "ENDING" + + }, + + ENDING = { + + if (is_key_pressed(keyboard_key$KEY_ENTER)) + current_screen <- "TITLE" + + }) + + begin_drawing() + + clear_background(raylib_color$RAYWHITE) + + # draw differently depending on the current screen + switch (current_screen, + + LOGO = { + + draw_text("LOGO SCREEN", 20, 20, 40, raylib_color$LIGHTGRAY) + draw_text("WAIT for 2 SECONDS...", 290, 220, 20, raylib_color$GRAY) + + }, + + TITLE = { + + draw_rectangle(0, 0, screen_width, screen_height, raylib_color$GREEN) + draw_text("TITLE SCREEN", 20, 20, 40, raylib_color$DARKGREEN) + draw_text("PRESS ENTER to JUMP to GAMEPLAY SCREEN", 120, 220, 20, raylib_color$DARKGREEN) + + }, + + GAMEPLAY = { + + draw_rectangle(0, 0, screen_width, screen_height, raylib_color$PURPLE) + draw_text("GAMEPLAY SCREEN", 20, 20, 40, raylib_color$MAROON) + draw_text("PRESS ENTER to JUMP to ENDING SCREEN", 120, 220, 20, raylib_color$MAROON) + + }, + + ENDING = { + + draw_rectangle(0, 0, screen_width, screen_height, raylib_color$BLUE) + draw_text("ENDNG SCREEN", 20, 20, 40, raylib_color$DARKBLUE) + draw_text("PRESS ENTER to JUMP to ENDING SCREEN", 120, 220, 20, raylib_color$DARKBLUE) + + }) + + end_drawing() + + } +} + +# run the game + +play_game() +\dontshow{\}) # examplesIf} +} +\seealso{ +Other Core examples: +\code{\link{core_2d_camera}}, +\code{\link{core_2d_platformer}}, +\code{\link{core_3d_camera_first_person}}, +\code{\link{core_3d_camera_free}}, +\code{\link{core_3d_camera}}, +\code{\link{core_basic_window}}, +\code{\link{core_drop_files}}, +\code{\link{core_input_keys}}, +\code{\link{core_input_mouse}} +} +\concept{Core examples} diff --git a/man/core_basic_window.Rd b/man/core_basic_window.Rd index c9cffd9..e67fbae 100644 --- a/man/core_basic_window.Rd +++ b/man/core_basic_window.Rd @@ -34,9 +34,11 @@ close_window() \seealso{ Other Core examples: \code{\link{core_2d_camera}}, +\code{\link{core_2d_platformer}}, \code{\link{core_3d_camera_first_person}}, \code{\link{core_3d_camera_free}}, \code{\link{core_3d_camera}}, +\code{\link{core_basic_screen_manager}}, \code{\link{core_drop_files}}, \code{\link{core_input_keys}}, \code{\link{core_input_mouse}} diff --git a/man/core_drop_files.Rd b/man/core_drop_files.Rd index 48583fa..d759a7b 100644 --- a/man/core_drop_files.Rd +++ b/man/core_drop_files.Rd @@ -70,9 +70,11 @@ close_window() # Close window and OpenGL context \seealso{ Other Core examples: \code{\link{core_2d_camera}}, +\code{\link{core_2d_platformer}}, \code{\link{core_3d_camera_first_person}}, \code{\link{core_3d_camera_free}}, \code{\link{core_3d_camera}}, +\code{\link{core_basic_screen_manager}}, \code{\link{core_basic_window}}, \code{\link{core_input_keys}}, \code{\link{core_input_mouse}} diff --git a/man/core_input_keys.Rd b/man/core_input_keys.Rd index 2e31c86..fa11262 100644 --- a/man/core_input_keys.Rd +++ b/man/core_input_keys.Rd @@ -50,9 +50,11 @@ close_window() \seealso{ Other Core examples: \code{\link{core_2d_camera}}, +\code{\link{core_2d_platformer}}, \code{\link{core_3d_camera_first_person}}, \code{\link{core_3d_camera_free}}, \code{\link{core_3d_camera}}, +\code{\link{core_basic_screen_manager}}, \code{\link{core_basic_window}}, \code{\link{core_drop_files}}, \code{\link{core_input_mouse}} diff --git a/man/core_input_mouse.Rd b/man/core_input_mouse.Rd index 862c6b6..eea1f98 100644 --- a/man/core_input_mouse.Rd +++ b/man/core_input_mouse.Rd @@ -60,9 +60,11 @@ close_window() # Close window and OpenGL context \seealso{ Other Core examples: \code{\link{core_2d_camera}}, +\code{\link{core_2d_platformer}}, \code{\link{core_3d_camera_first_person}}, \code{\link{core_3d_camera_free}}, \code{\link{core_3d_camera}}, +\code{\link{core_basic_screen_manager}}, \code{\link{core_basic_window}}, \code{\link{core_drop_files}}, \code{\link{core_input_keys}} diff --git a/man/cubemap_layout.Rd b/man/cubemap_layout.Rd new file mode 100644 index 0000000..75ab660 --- /dev/null +++ b/man/cubemap_layout.Rd @@ -0,0 +1,16 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/enums.R +\docType{data} +\name{cubemap_layout} +\alias{cubemap_layout} +\title{Cubemap layouts} +\format{ +An object of class \code{list} of length 6. +} +\usage{ +cubemap_layout +} +\description{ +Cubemap layouts +} +\keyword{datasets} diff --git a/man/font_type.Rd b/man/font_type.Rd new file mode 100644 index 0000000..2aa37b7 --- /dev/null +++ b/man/font_type.Rd @@ -0,0 +1,16 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/enums.R +\docType{data} +\name{font_type} +\alias{font_type} +\title{Font type, defines generation method} +\format{ +An object of class \code{list} of length 3. +} +\usage{ +font_type +} +\description{ +Font type, defines generation method +} +\keyword{datasets} diff --git a/man/material_map_index.Rd b/man/material_map_index.Rd new file mode 100644 index 0000000..e54f143 --- /dev/null +++ b/man/material_map_index.Rd @@ -0,0 +1,16 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/enums.R +\docType{data} +\name{material_map_index} +\alias{material_map_index} +\title{Material map index} +\format{ +An object of class \code{list} of length 13. +} +\usage{ +material_map_index +} +\description{ +Material map index +} +\keyword{datasets} diff --git a/man/npatch_layout.Rd b/man/npatch_layout.Rd new file mode 100644 index 0000000..742b215 --- /dev/null +++ b/man/npatch_layout.Rd @@ -0,0 +1,16 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/enums.R +\docType{data} +\name{npatch_layout} +\alias{npatch_layout} +\title{N-patch layout} +\format{ +An object of class \code{list} of length 3. +} +\usage{ +npatch_layout +} +\description{ +N-patch layout +} +\keyword{datasets} diff --git a/man/pixel_format.Rd b/man/pixel_format.Rd new file mode 100644 index 0000000..ebf7a20 --- /dev/null +++ b/man/pixel_format.Rd @@ -0,0 +1,16 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/enums.R +\docType{data} +\name{pixel_format} +\alias{pixel_format} +\title{Pixel formats} +\format{ +An object of class \code{list} of length 21. +} +\usage{ +pixel_format +} +\description{ +Note: Support depends on OpenGL version and platform +} +\keyword{datasets} diff --git a/man/shader_attribute_data_type.Rd b/man/shader_attribute_data_type.Rd new file mode 100644 index 0000000..baaf3f4 --- /dev/null +++ b/man/shader_attribute_data_type.Rd @@ -0,0 +1,16 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/enums.R +\docType{data} +\name{shader_attribute_data_type} +\alias{shader_attribute_data_type} +\title{Shader attribute data type} +\format{ +An object of class \code{list} of length 4. +} +\usage{ +shader_attribute_data_type +} +\description{ +Shader attribute data type +} +\keyword{datasets} diff --git a/man/shader_location_index.Rd b/man/shader_location_index.Rd new file mode 100644 index 0000000..06425dc --- /dev/null +++ b/man/shader_location_index.Rd @@ -0,0 +1,16 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/enums.R +\docType{data} +\name{shader_location_index} +\alias{shader_location_index} +\title{Shader location index} +\format{ +An object of class \code{list} of length 26. +} +\usage{ +shader_location_index +} +\description{ +Shader location index +} +\keyword{datasets} diff --git a/man/shader_uniform_data_type.Rd b/man/shader_uniform_data_type.Rd new file mode 100644 index 0000000..b38198f --- /dev/null +++ b/man/shader_uniform_data_type.Rd @@ -0,0 +1,16 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/enums.R +\docType{data} +\name{shader_uniform_data_type} +\alias{shader_uniform_data_type} +\title{Shader uniform data type} +\format{ +An object of class \code{list} of length 9. +} +\usage{ +shader_uniform_data_type +} +\description{ +Shader uniform data type +} +\keyword{datasets} diff --git a/man/texture_filter.Rd b/man/texture_filter.Rd new file mode 100644 index 0000000..86cc7a3 --- /dev/null +++ b/man/texture_filter.Rd @@ -0,0 +1,17 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/enums.R +\docType{data} +\name{texture_filter} +\alias{texture_filter} +\title{Texture parameters: filter mode} +\format{ +An object of class \code{list} of length 6. +} +\usage{ +texture_filter +} +\description{ +Note 1: Filtering considers mipmaps if available in the texture +Note 2: Filter is accordingly set for minification and magnification +} +\keyword{datasets} diff --git a/man/texture_wrap.Rd b/man/texture_wrap.Rd new file mode 100644 index 0000000..f697cdc --- /dev/null +++ b/man/texture_wrap.Rd @@ -0,0 +1,16 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/enums.R +\docType{data} +\name{texture_wrap} +\alias{texture_wrap} +\title{Texture parameters: wrap mode} +\format{ +An object of class \code{list} of length 4. +} +\usage{ +texture_wrap +} +\description{ +Texture parameters: wrap mode +} +\keyword{datasets} From 89189fdbf95b83f19fd4d231454f9b8227ae5040 Mon Sep 17 00:00:00 2001 From: kosin Date: Sun, 15 May 2022 09:47:26 +0200 Subject: [PATCH 5/5] few core functions, core_window_flags addition --- NAMESPACE | 5 + R/core.R | 50 ++- R/example_core_2d_camera_platformer.R | 586 +++++++++++++------------- R/example_core_basic_screen_manager.R | 214 +++++----- R/example_core_window_flags.R | 223 ++++++++++ man/clear_window_state.Rd | 1 + man/close_window.Rd | 1 + man/core_2d_camera.Rd | 3 +- man/core_2d_platformer.Rd | 525 +++++++++++------------ man/core_3d_camera.Rd | 3 +- man/core_3d_camera_first_person.Rd | 3 +- man/core_3d_camera_free.Rd | 3 +- man/core_basic_screen_manager.Rd | 127 +++--- man/core_basic_window.Rd | 3 +- man/core_drop_files.Rd | 3 +- man/core_input_keys.Rd | 3 +- man/core_input_mouse.Rd | 3 +- man/core_window_flags.Rd | 238 +++++++++++ man/get_frame_time.Rd | 16 + man/get_screen_height.Rd | 12 + man/get_screen_to_world_2d.Rd | 24 ++ man/get_screen_width.Rd | 33 ++ man/get_world_to_screen_2d.Rd | 24 ++ man/init_window.Rd | 1 + man/is_window_focused.Rd | 1 + man/is_window_fullscreen.Rd | 1 + man/is_window_hidden.Rd | 1 + man/is_window_maximized.Rd | 1 + man/is_window_minimized.Rd | 1 + man/is_window_ready.Rd | 1 + man/is_window_resized.Rd | 1 + man/is_window_state.Rd | 1 + man/maximize_window.Rd | 1 + man/minimize_window.Rd | 1 + man/restore_window.Rd | 1 + man/set_target_fps.Rd | 4 + man/set_window_icon.Rd | 1 + man/set_window_state.Rd | 1 + man/toggle_fullscreen.Rd | 1 + man/window_should_close.Rd | 1 + 40 files changed, 1395 insertions(+), 728 deletions(-) create mode 100644 R/example_core_window_flags.R create mode 100644 man/core_window_flags.Rd create mode 100644 man/get_frame_time.Rd create mode 100644 man/get_screen_height.Rd create mode 100644 man/get_screen_to_world_2d.Rd create mode 100644 man/get_screen_width.Rd create mode 100644 man/get_world_to_screen_2d.Rd diff --git a/NAMESPACE b/NAMESPACE index 978fd62..41c1e79 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -194,12 +194,17 @@ export(fade) export(get_char_pressed) export(get_collision_rec) export(get_dropped_files) +export(get_frame_time) export(get_key_pressed) export(get_mouse_delta) export(get_mouse_position) export(get_mouse_wheel_move) export(get_mouse_x) export(get_mouse_y) +export(get_screen_height) +export(get_screen_to_world_2d) +export(get_screen_width) +export(get_world_to_screen_2d) export(init_window) export(is_file_dropped) export(is_key_down) diff --git a/R/core.R b/R/core.R index 47bb33e..b93cd05 100644 --- a/R/core.R +++ b/R/core.R @@ -162,6 +162,21 @@ set_window_icon <- function(image) { invisible() } +#' @family Windows-related functions +#' @title Get current screen width +#' @export +get_screen_width <- function() { + .Call(.C_GetScreenWidth_R) +} + +#' @family Windows-related function +#' @title Get current screen height +#' @export +get_screen_height <- function() { + .Call(.C_GetScreenHeight_R) +} + + ##--------------------------## ## Drawing-related functions ##--------------------------## @@ -247,7 +262,13 @@ set_target_fps <- function(fps) { .Call(.C_SetTargetFPS_R, fps) invisible() } - +#' @family Timing-related functions +#' @title Get frame time +#' +#' @export +get_frame_time <- function() { + .Call(.C_GetFrameTime_R) +} ##-----------------------------------## ## File management functions ##-----------------------------------## @@ -468,3 +489,30 @@ set_mouse_cursor <- function(cursor) { .Call(.C_SetMouseCursor_R, cursor) invisible() } + +##-----------------------------------## +## Screen-space-related functions +##-----------------------------------## +#' @family Screen-space-related functions +#' @title Get the screen space position for a 2d camera world space position +#' @param position Vector2 +#' @param camera Camera2D +#' @return Vector2 +#' +#' @export +get_world_to_screen_2d <- function(position, camera) { + out <- .Call(.C_GetWorldToScreen2D_R, position, camera) + Vector2(out[1], out[2]) +} + +#' @family Screen-space-related functions +#' @title Get the world space position for a 2d camera screen space position +#' @param position Vector2 +#' @param camera Camera2D +#' @return Vector2 +#' +#' @export +get_screen_to_world_2d <- function(position, camera) { + out <- .Call(.C_GetScreenToWorld2D_R, position, camera) + Vector2(out[1], out[2]) +} diff --git a/R/example_core_2d_camera_platformer.R b/R/example_core_2d_camera_platformer.R index 5f7c6be..e78ca20 100644 --- a/R/example_core_2d_camera_platformer.R +++ b/R/example_core_2d_camera_platformer.R @@ -2,325 +2,329 @@ #' @rdname core_2d_platformer #' @family Core examples #' @title 2D camera platformer -#' -#' @description +#' +#' @description #' This example shows multiple ways to update 2D camera with custom functions. -#' Also, basic collision functionality is implemented with basic mathematical +#' Also, collision detection functionality is implemented with basic mathematical #' and R-base functions. #' -#' @examplesIf interactive() +#' Example contributed for `raylib` library by arvyy (@arvyy) and reviewed by +#' Ramon Santamaria (@raysan5) +#' +#' Copyright (c) 2019 arvyy (@arvyy) #' -#'###################### -#'# Pre-initialization # -#'###################### +#' Example modified for `raylib.R` +#' +#' @examplesIf interactive() +#' ###################### +#' # Pre-initialization # +#' ###################### #' #' G <- 400 #' player_jump_spd <- 350 #' player_hor_spd <- 200 -#' +#' #' # 'player' object constructor #' Player <- function(x, y, speed, can_jump) { -#' list(position = Vector2(x, y), -#' speed = speed, -#' can_jump = can_jump) +#' list(position = Vector2(x, y), +#' speed = speed, +#' can_jump = can_jump) #' } -#' -#' +#' +#' #' # 'environment item' object constructor #' EnvItem <- function(x, y, width, height, blocking, color, alpha = 255) { -#' list(rect = Rectangle(x, y, width, height), -#' blocking = blocking, -#' color = Color(color, alpha)) +#' list(rect = Rectangle(x, y, width, height), +#' blocking = blocking, +#' color = Color(color, alpha)) #' } -#' +#' #' # 'player update' function #' update_player <- function(player, env_items, env_items_length, delta) { -#' -#' if (is_key_down(keyboard_key$KEY_LEFT)) -#' player$position["x"] <- player$position["x"] - player_hor_spd * delta -#' -#' if (is_key_down(keyboard_key$KEY_RIGHT)) -#' player$position["x"] <- player$position["x"] + player_hor_spd * delta -#' -#' if (is_key_down(keyboard_key$KEY_SPACE) && isTRUE(player$can_jump)) { -#' player$speed <- player$speed - player_jump_spd -#' player$can_jump <- FALSE -#' -#' } -#' -#' hit_obstacle <- FALSE -#' -#' collisions <- sapply( -#' env_items, \(env_item) { -#' env_item$blocking && -#' env_item$rect["x"] <= player$position["x"] && -#' (env_item$rect["x"] + env_item$rect["width"] >= player$position["x"]) && -#' env_item$rect["y"] >= player$position["y"] && -#' (env_item$rect["y"] < player$position["y"] + player$speed * delta) -#' }) -#' -#' coll_i <- which(collisions) -#' -#' if (sum(coll_i) > 0) { -#' hit_obstacle <- TRUE -#' player$speed <- 0 -#' player$position["y"] <- env_items[[coll_i]]$rect["y"] -#' } -#' -#' if (!hit_obstacle) { -#' player$position["y"] <- player$position["y"] + player$speed * delta -#' player$speed <- player$speed + G * delta -#' player$can_jump <- FALSE -#' -#' } else -#' player$can_jump <- TRUE -#' -#' return(player) +#' +#' if (is_key_down(keyboard_key$KEY_LEFT)) +#' player$position["x"] <- player$position["x"] - player_hor_spd * delta +#' +#' if (is_key_down(keyboard_key$KEY_RIGHT)) +#' player$position["x"] <- player$position["x"] + player_hor_spd * delta +#' +#' if (is_key_down(keyboard_key$KEY_SPACE) && isTRUE(player$can_jump)) { +#' player$speed <- player$speed - player_jump_spd +#' player$can_jump <- FALSE +#' +#' } +#' +#' hit_obstacle <- FALSE +#' +#' collisions <- sapply( +#' env_items, \(env_item) { +#' env_item$blocking && +#' env_item$rect["x"] <= player$position["x"] && +#' (env_item$rect["x"] + env_item$rect["width"] >= player$position["x"]) && +#' env_item$rect["y"] >= player$position["y"] && +#' (env_item$rect["y"] < player$position["y"] + player$speed * delta) +#' }) +#' +#' coll_i <- which(collisions) +#' +#' if (sum(coll_i) > 0) { +#' hit_obstacle <- TRUE +#' player$speed <- 0 +#' player$position["y"] <- env_items[[coll_i]]$rect["y"] +#' } +#' +#' if (!hit_obstacle) { +#' player$position["y"] <- player$position["y"] + player$speed * delta +#' player$speed <- player$speed + G * delta +#' player$can_jump <- FALSE +#' +#' } else +#' player$can_jump <- TRUE +#' +#' return(player) #' } -#' +#' #' # camera update functions -#' update_camera_center <- function( -#' camera, player, env_items, env_items_length, delta, width, height) { -#' camera$offset <- Vector2(width/2, height/2) -#' camera$target <- player$position -#' -#' return(camera) +#' update_camera_center <- function(camera, player, env_items, env_items_length, delta, width, height) { +#' +#' camera$offset <- Vector2(width/2, height/2) +#' camera$target <- player$position +#' +#' return(camera) #' } -#' -#' # update_camera_center_inside_map <- function( -#' # camera, player, env_items, env_items_length, delta, width, height) { -#' # camera$target <- player$position -#' # camera$offset <- Vector2(width/2, height/2) -#' # -#' # min_x <- min_y <- 1000 -#' # max_x <- max_y <- -1000 -#' # -#' # for (i in 1:env_items_length) { -#' # min_x <- min(env_items[[i]]$rect["x"], min_x) -#' # max_x <- max(env_items[[i]]$rect["x"] + env_items$rect["width"], max_x) -#' # min_y <- min(env_items[[i]]$rect["y"], min_y) -#' # max_y <- max(env_items[[i]]$rect["y"] + env_items$rect["height"], max_y) -#' # -#' # -#' # max = .Call(.C_GetWorldToScreen2D_R, Vector2(max_x, max_y), camera, PACKAGE = "raylib.R") -#' # min = .Call(.C_GetWorldToScreen2D_R, Vector2(min_x, min_y), camera, PACKAGE = "raylib.R") -#' # -#' # if (max["x"] < width) camera$offset["x"] <- width - (max["x"] - width/2) -#' # if (max["y"] < height) camera$offset["y"] <- heigh - (max["y"] - height/2) -#' # if (min["x"] > 0) camera$offset["x"] <- width/2 - min["x"] -#' # if (min["y"] > 0) camera$offset["y"] <- height/2 - min["y"] -#' # -#' # } -#' # -#' # return(camera) -#' # } -#' -#' update_camera_center_smooth_follow <- function( -#' camera, player, env_items, env_items_length, delta, width, height) { -#' -#' min_speed <- 30 -#' min_effect_length <- 10 -#' fraction_speed <- 0.8 -#' -#' camera$offset <- Vector2(width/2, height/2) -#' -#' diff <- player$position - camera$target -#' veclength <- sqrt(sum(sapply(diff, \(x) x^2))) -#' -#' if (veclength > min_effect_length) { -#' speed <- max(fraction_speed * veclength, min_speed) -#' camera$target <- camera$target + diff*(speed * delta / veclength) -#' } -#' return(camera) +#' +#' update_camera_center_inside_map <- function(camera, player, env_items, env_items_length, delta, width, height) { +#' +#' camera$target <- player$position +#' camera$offset <- Vector2(width/2, height/2) +#' +#' min_x <- min_y <- 1000 +#' max_x <- max_y <- -1000 +#' +#' for (i in 1:env_items_length) { +#' min_x <- min(env_items[[i]]$rect["x"], min_x) +#' max_x <- max(env_items[[i]]$rect["x"] + env_items[[i]]$rect["width"], max_x) +#' min_y <- min(env_items[[i]]$rect["y"], min_y) +#' max_y <- max(env_items[[i]]$rect["y"] + env_items[[i]]$rect["height"], max_y) +#' } +#' +#' max <- get_world_to_screen_2d(Vector2(max_x, max_y), camera) +#' min <- get_world_to_screen_2d(Vector2(min_x, min_y), camera) +#' +#' if (max["x"] < width) camera$offset["x"] <- width - (max["x"] - width/2) +#' if (max["y"] < height) camera$offset["y"] <- height - (max["y"] - height/2) +#' if (min["x"] > 0) camera$offset["x"] <- width/2 - min["x"] +#' if (min["y"] > 0) camera$offset["y"] <- height/2 - min["y"] +#' +#' return(camera) #' } -#' -#' update_camera_even_out_on_landing <- function( -#' camera, player, env_items, env_items_length, delta, width, height) { -#' -#' even_out_speed <- 700 -#' -#' camera$offset <- Vector2(width/2, height/2) -#' camera$target["x"] <- player$position["x"] -#' -#' if (isTRUE(.update_camera_even_out_env[["evening_out"]])) { -#' -#' if (.update_camera_even_out_env[["even_out_target"]] > camera$target["y"]) { -#' camera$target["y"] <- camera$target["y"] + even_out_speed * delta -#' if (camera$target["y"] > .update_camera_even_out_env[["even_out_target"]]) { -#' camera$target["y"] <- .update_camera_even_out_env[["even_out_target"]] -#' .update_camera_even_out_env[["evening_out"]] <- FALSE -#' } -#' } else { -#' camera$target["y"] <- camera$target["y"] - even_out_speed*delta -#' if (camera$target["y"] < .update_camera_even_out_env[["even_out_target"]]) { -#' camera$target["y"] <- .update_camera_even_out_env[["even_out_target"]] -#' .update_camera_even_out_env[["evening_out"]] <- FALSE -#' } -#' } -#' } else if (isTRUE(player$can_jump) && player$speed == 0 && player$position["y"] != camera$target["y"]) { -#' .update_camera_even_out_env[["evening_out"]] <- TRUE -#' .update_camera_even_out_env[["even_out_target"]] <- player$position["y"] -#' } -#' -#' return(camera) +#' +#' update_camera_center_smooth_follow <- function(camera, player, env_items, env_items_length, delta, width, height) { +#' +#' min_speed <- 30 +#' min_effect_length <- 10 +#' fraction_speed <- 0.8 +#' +#' camera$offset <- Vector2(width/2, height/2) +#' +#' diff <- player$position - camera$target +#' veclength <- sqrt(sum(sapply(diff, \(x) x^2))) +#' +#' if (veclength > min_effect_length) { +#' speed <- max(fraction_speed * veclength, min_speed) +#' camera$target <- camera$target + diff*(speed * delta / veclength) +#' } +#' return(camera) +#' } +#' +#' update_camera_even_out_on_landing <- function(camera, player, env_items, env_items_length, delta, width, height) { +#' +#' even_out_speed <- 700 +#' +#' camera$offset <- Vector2(width/2, height/2) +#' camera$target["x"] <- player$position["x"] +#' +#' if (isTRUE(.update_camera_even_out_env[["evening_out"]])) { +#' +#' if (.update_camera_even_out_env[["even_out_target"]] > camera$target["y"]) { +#' camera$target["y"] <- camera$target["y"] + even_out_speed * delta +#' if (camera$target["y"] > .update_camera_even_out_env[["even_out_target"]]) { +#' camera$target["y"] <- .update_camera_even_out_env[["even_out_target"]] +#' .update_camera_even_out_env[["evening_out"]] <- FALSE +#' } +#' } else { +#' camera$target["y"] <- camera$target["y"] - even_out_speed*delta +#' if (camera$target["y"] < .update_camera_even_out_env[["even_out_target"]]) { +#' camera$target["y"] <- .update_camera_even_out_env[["even_out_target"]] +#' .update_camera_even_out_env[["evening_out"]] <- FALSE +#' } +#' } +#' } else if (isTRUE(player$can_jump) && player$speed == 0 && player$position["y"] != camera$target["y"]) { +#' .update_camera_even_out_env[["evening_out"]] <- TRUE +#' .update_camera_even_out_env[["even_out_target"]] <- player$position["y"] +#' } +#' +#' return(camera) #' } -#' +#' #' # environement needed for `update_even_out_on_landing` camera updater #' .update_camera_even_out_env <- new.env() #' .update_camera_even_out_env[["evening_out"]] <- FALSE #' .update_camera_even_out_env[["even_out_target"]] <- NULL -#' -#' # update_camera_player_bound_push <- function( -#' # camera, player, env_items, env_items_length, delta, width, height) { -#' # -#' # bbox <- Vector2(0.2, 0.2) -#' # -#' # bbox_world_min <- .Call(.C_GetScreenToWorld2D_R, -#' # Vector2((1 - bbox["x"]) * 0.5 * width, (1 - bbox["y"] * 0.5 * height)), -#' # camera) -#' # bbox_world_max <- .Call(.C_GetScreenToWorld2D_R, -#' # Vector2((1 + bbox["x"]) * 0.5 * width, (1 + bbox["y"] * 0.5 * height)), -#' # camera) -#' # -#' # camera$offset <- Vector2((1 - bbox["x"]) * 0.5 * width, (1 - bbox["y"]) * 0.5 * height) -#' # -#' # if (player$position["x"] < bbox_world_min["x"]) -#' # camera$target["x"] <- player$position["x"] -#' # if (player$position["y"] < bbox_world_min["y"]) -#' # camera$target["y"] <- player$position["y"] -#' # if (player$position["x"] > bbox_world_max["x"]) -#' # camera$target["x"] <- bbox_world_min["x"] + (player$position["x"] - bbox_world_min["x"]) -#' # if (player$position["y"] > bbox_world_max["y"]) -#' # camera$target["y"] <- bbox_world_min["y"] + (player$position["y"] - bbox_world_min["y"]) -#' # -#' # return(camera) -#' # -#' # } -#' +#' +#' update_camera_player_bound_push <- function(camera, player, env_items, env_items_length, delta, width, height) { +#' +#' bbox <- Vector2(0.2, 0.2) +#' +#' bbox_world_min <- get_screen_to_world_2d( +#' Vector2((1 - bbox["x"]) * 0.5 * width, (1 - bbox["y"] * 0.5 * height)), +#' camera) +#' +#' bbox_world_max <- get_screen_to_world_2d( +#' Vector2((1 + bbox["x"]) * 0.5 * width, (1 + bbox["y"] * 0.5 * height)), +#' camera) +#' +#' camera$offset <- Vector2((1 - bbox["x"]) * 0.5 * width, (1 - bbox["y"]) * 0.5 * height) +#' +#' if (player$position["x"] < bbox_world_min["x"]) +#' camera$target["x"] <- player$position["x"] +#' if (player$position["y"] < bbox_world_min["y"]) +#' camera$target["y"] <- player$position["y"] +#' if (player$position["x"] > bbox_world_max["x"]) +#' camera$target["x"] <- bbox_world_min["x"] + (player$position["x"] - bbox_world_max["x"]) +#' if (player$position["y"] > bbox_world_max["y"]) +#' camera$target["y"] <- bbox_world_min["y"] + (player$position["y"] - bbox_world_max["y"]) +#' +#' return(camera) +#' +#' } +#' #' ################## #' # Initialization # #' ################## -#' +#' #' play_game <- function() { -#' -#' screen_width <- 800 -#' screen_height <- 450 -#' -#' init_window(screen_width, screen_height, "raylib [core] example - 2d camera") -#' on.exit(close_window()) -#' -#' player <- Player(x = 400, y = 280, speed = 0, can_jump = FALSE) -#' -#' env_items <- list( -#' EnvItem(0, 0, 1000, 400, FALSE, raylib_color$LIGHTGRAY), -#' EnvItem(0, 400, 1000, 200, TRUE, raylib_color$GRAY), -#' EnvItem(300, 200, 400, 10, TRUE, raylib_color$GRAY), -#' EnvItem(250, 300, 100, 10, TRUE, raylib_color$GRAY), -#' EnvItem(650, 300, 100, 10, TRUE, raylib_color$GRAY) -#' ) -#' -#' env_items_length <- length(env_items) -#' -#' camera <- Camera2D(target = player$position, -#' offset = Vector2(screen_width/2, screen_height/2), -#' rotation = 0, -#' zoom = 1) -#' -#' # set up camera updaters -#' camera_updaters <- list( -#' update = list( -#' descr = "Follow player center", -#' func = update_camera_center -#' ), -#' # update_inside_map = list( -#' # descr = "Follow player center, but clamp to map edges", -#' # func = update_camera_center_inside_map -#' # ), -#' update_smooth_follow = list( -#' descr = "Follow player center;\nsmoothed", -#' func = update_camera_center_smooth_follow -#' ), -#' update_even_out_on_landing = list( -#' descr = "Follow player center horizontally;\nupdate player center vertically after landing", -#' func = update_camera_even_out_on_landing -#' )#, -#' # update_player_bound_push = list( -#' # descr = "Player push camera on getting too close to screen edge", -#' # func = update_camera_player_bound_push -#' # ) -#' ) -#' -#' camera_option = 1 -#' -#' set_target_fps(60) -#' -#' ################## -#' # Main game loop # -#' ################## -#' -#' while(!window_should_close()) { -#' -#' # to implement -#' # delta_time <- get_frame_time() -#' delta_time <- 1/60 -#' -#' player <- update_player(player, env_items, env_items_length, delta_time) -#' -#' camera$zoom <- camera$zoom + get_mouse_wheel_move()*0.05 -#' -#' if (camera$zoom > 3) -#' camera$zoom = 3 -#' else if (camera$zoom < 0.25) -#' camera$zoom = 0.25 -#' -#' if (is_key_pressed(keyboard_key$KEY_R)) { -#' camera$zoom <- 1 -#' player$position <- Vector2(400, 280) -#' } -#' -#' if (is_key_pressed(keyboard_key$KEY_C)) { -#' camera_option <- camera_option %% length(camera_updaters) + 1 -#' } -#' -#' # update the camera! -#' camera <- do.call(what = camera_updaters[[camera_option]]$func, -#' args = list( -#' camera = camera, -#' player = player, -#' env_items = env_items, -#' env_items_length = env_items_length, -#' delta = delta_time, -#' width = screen_width, -#' height = screen_height -#' )) -#' -#' begin_drawing() -#' -#' clear_background(raylib_color$LIGHTGRAY) -#' -#' begin_mode_2d(camera) -#' -#' invisible(lapply(env_items, \(x) { draw_rectangle_rec(x$rect, x$color) })) -#' -#' player_rect <- Rectangle(player$position["x"] - 20, player$position["y"] - 40, 40, 40) -#' draw_rectangle_rec(player_rect, raylib_color$RED) -#' -#' end_mode_2d() -#' -#' DrawText("Controls:", 20, 20, 10, raylib_color$BLACK); -#' DrawText("- Right/Left to move", 40, 40, 10, raylib_color$DARKGRAY); -#' DrawText("- Space to jump", 40, 60, 10, raylib_color$DARKGRAY); -#' DrawText("- Mouse Wheel to Zoom in-out, R to reset zoom", 40, 80, 10, raylib_color$DARKGRAY); -#' DrawText("- C to change camera mode", 40, 100, 10, raylib_color$DARKGRAY); -#' DrawText("Current camera mode:", 20, 120, 10, raylib_color$BLACK); -#' DrawText(camera_updaters[[camera_option]]$descr, 40, 140, 10, raylib_color$DARKGRAY); -#' -#' end_drawing() -#' -#' } +#' +#' screen_width <- 800 +#' screen_height <- 450 +#' +#' init_window(screen_width, screen_height, "raylib [core] example - 2d camera") +#' on.exit(close_window()) +#' +#' player <- Player(x = 400, y = 280, speed = 0, can_jump = FALSE) +#' +#' env_items <- list( +#' EnvItem(0, 0, 1000, 400, FALSE, raylib_color$LIGHTGRAY), +#' EnvItem(0, 400, 1000, 200, TRUE, raylib_color$GRAY), +#' EnvItem(300, 200, 400, 10, TRUE, raylib_color$GRAY), +#' EnvItem(250, 300, 100, 10, TRUE, raylib_color$GRAY), +#' EnvItem(650, 300, 100, 10, TRUE, raylib_color$GRAY) +#' ) +#' +#' env_items_length <- length(env_items) +#' +#' camera <- Camera2D(target = player$position, +#' offset = Vector2(screen_width/2, screen_height/2), +#' rotation = 0, +#' zoom = 1) +#' +#' # set up camera updaters +#' camera_updaters <- list( +#' update = list( +#' descr = "Follow player center", +#' func = update_camera_center +#' ), +#' update_inside_map = list( +#' descr = "Follow player center, but clamp to map edges", +#' func = update_camera_center_inside_map +#' ), +#' update_smooth_follow = list( +#' descr = "Follow player center;\nsmoothed", +#' func = update_camera_center_smooth_follow +#' ), +#' update_even_out_on_landing = list( +#' descr = "Follow player center horizontally;\nupdate player center vertically after landing", +#' func = update_camera_even_out_on_landing +#' ), +#' update_player_bound_push = list( +#' descr = "Player push camera on getting too close to screen edge", +#' func = update_camera_player_bound_push +#' ) +#' ) +#' +#' camera_option = 1 +#' +#' set_target_fps(60) +#' +#' ################## +#' # Main game loop # +#' ################## +#' +#' while(!window_should_close()) { +#' +#' # to implement +#' delta_time <- get_frame_time() +#' # delta_time <- 1/60 +#' +#' player <- update_player(player, env_items, env_items_length, delta_time) +#' +#' camera$zoom <- camera$zoom + get_mouse_wheel_move()*0.05 +#' +#' if (camera$zoom > 3) +#' camera$zoom = 3 +#' else if (camera$zoom < 0.25) +#' camera$zoom = 0.25 +#' +#' if (is_key_pressed(keyboard_key$KEY_R)) { +#' camera$zoom <- 1 +#' player$position <- Vector2(400, 280) +#' } +#' +#' if (is_key_pressed(keyboard_key$KEY_C)) { +#' camera_option <- camera_option %% length(camera_updaters) + 1 +#' } +#' +#' # update the camera! +#' camera <- do.call(what = camera_updaters[[camera_option]]$func, +#' args = list( +#' camera = camera, +#' player = player, +#' env_items = env_items, +#' env_items_length = env_items_length, +#' delta = delta_time, +#' width = screen_width, +#' height = screen_height +#' )) +#' +#' begin_drawing() +#' +#' clear_background(raylib_color$LIGHTGRAY) +#' +#' begin_mode_2d(camera) +#' +#' invisible(lapply(env_items, \(x) { draw_rectangle_rec(x$rect, x$color) })) +#' +#' player_rect <- Rectangle(player$position["x"] - 20, player$position["y"] - 40, 40, 40) +#' draw_rectangle_rec(player_rect, raylib_color$RED) +#' +#' end_mode_2d() +#' +#' DrawText("Controls:", 20, 20, 10, raylib_color$BLACK); +#' DrawText("- Right/Left to move", 40, 40, 10, raylib_color$DARKGRAY); +#' DrawText("- Space to jump", 40, 60, 10, raylib_color$DARKGRAY); +#' DrawText("- Mouse Wheel to Zoom in-out, R to reset zoom", 40, 80, 10, raylib_color$DARKGRAY); +#' DrawText("- C to change camera mode", 40, 100, 10, raylib_color$DARKGRAY); +#' DrawText("Current camera mode:", 20, 120, 10, raylib_color$BLACK); +#' DrawText(camera_updaters[[camera_option]]$descr, 40, 140, 10, raylib_color$DARKGRAY); +#' +#' end_drawing() +#' +#' } #' } -#' -#' # run the game -#' +#' +#' ################### +#' # LAUNCH THE GAME # +#' ################### +#' #' play_game() -NULL \ No newline at end of file +NULL diff --git a/R/example_core_basic_screen_manager.R b/R/example_core_basic_screen_manager.R index 6469582..acb461a 100644 --- a/R/example_core_basic_screen_manager.R +++ b/R/example_core_basic_screen_manager.R @@ -2,116 +2,118 @@ #' @rdname core_basic_screen_manager #' @family Core examples #' @title Basic Screen Manager -#' -#' @description +#' +#' @description #' Example showing possibility to menage different screen types in main #' game loop #' #' @examplesIf interactive() -#' +#' #' play_game <- function() { -#' -#' ################## -#' # Initialization # -#' ################## -#' -#' screen_width <- 800 -#' screen_height <- 450 -#' -#' init_window(screen_width, screen_height, "raylib [core] example - basic screen manager") -#' -#' # deinitialization of the window -#' on.exit(close_window()) -#' -#' current_screen <- "LOGO" -#' -#' frames_counter <- 0 -#' -#' set_target_fps(60) -#' -#' ################## -#' # Main game loop # -#' ################## -#' -#' while(!window_should_close()) { -#' -#' # menage screen switches during this part -#' switch(current_screen, -#' -#' LOGO = { -#' -#' frames_counter <- frames_counter + 1 # count frames -#' -#' # Wait for 2 seconds (120 frames) before jumping to TITLE screen -#' if (frames_counter > 120) -#' current_screen <- "TITLE" -#' }, -#' -#' TITLE = { -#' -#' if (is_key_pressed(keyboard_key$KEY_ENTER)) -#' current_screen <- "GAMEPLAY" -#' -#' }, -#' -#' GAMEPLAY = { -#' -#' if (is_key_pressed(keyboard_key$KEY_ENTER)) -#' current_screen <- "ENDING" -#' -#' }, -#' -#' ENDING = { -#' -#' if (is_key_pressed(keyboard_key$KEY_ENTER)) -#' current_screen <- "TITLE" -#' -#' }) -#' -#' begin_drawing() -#' -#' clear_background(raylib_color$RAYWHITE) -#' -#' # draw differently depending on the current screen -#' switch (current_screen, -#' -#' LOGO = { -#' -#' draw_text("LOGO SCREEN", 20, 20, 40, raylib_color$LIGHTGRAY) -#' draw_text("WAIT for 2 SECONDS...", 290, 220, 20, raylib_color$GRAY) -#' -#' }, -#' -#' TITLE = { -#' -#' draw_rectangle(0, 0, screen_width, screen_height, raylib_color$GREEN) -#' draw_text("TITLE SCREEN", 20, 20, 40, raylib_color$DARKGREEN) -#' draw_text("PRESS ENTER to JUMP to GAMEPLAY SCREEN", 120, 220, 20, raylib_color$DARKGREEN) -#' -#' }, -#' -#' GAMEPLAY = { -#' -#' draw_rectangle(0, 0, screen_width, screen_height, raylib_color$PURPLE) -#' draw_text("GAMEPLAY SCREEN", 20, 20, 40, raylib_color$MAROON) -#' draw_text("PRESS ENTER to JUMP to ENDING SCREEN", 120, 220, 20, raylib_color$MAROON) -#' -#' }, -#' -#' ENDING = { -#' -#' draw_rectangle(0, 0, screen_width, screen_height, raylib_color$BLUE) -#' draw_text("ENDNG SCREEN", 20, 20, 40, raylib_color$DARKBLUE) -#' draw_text("PRESS ENTER to JUMP to ENDING SCREEN", 120, 220, 20, raylib_color$DARKBLUE) -#' -#' }) -#' -#' end_drawing() -#' -#' } +#' +#' ################## +#' # Initialization # +#' ################## +#' +#' screen_width <- 800 +#' screen_height <- 450 +#' +#' init_window(screen_width, screen_height, "raylib [core] example - basic screen manager") +#' +#' # deinitialization of the window +#' on.exit(close_window()) +#' +#' current_screen <- "LOGO" +#' +#' frames_counter <- 0 +#' +#' set_target_fps(60) +#' +#' ################## +#' # Main game loop # +#' ################## +#' +#' while(!window_should_close()) { +#' +#' # menage screen switches during this part +#' switch(current_screen, +#' +#' LOGO = { +#' +#' frames_counter <- frames_counter + 1 # count frames +#' +#' # Wait for 2 seconds (120 frames) before jumping to TITLE screen +#' if (frames_counter > 120) +#' current_screen <- "TITLE" +#' }, +#' +#' TITLE = { +#' +#' if (is_key_pressed(keyboard_key$KEY_ENTER)) +#' current_screen <- "GAMEPLAY" +#' +#' }, +#' +#' GAMEPLAY = { +#' +#' if (is_key_pressed(keyboard_key$KEY_ENTER)) +#' current_screen <- "ENDING" +#' +#' }, +#' +#' ENDING = { +#' +#' if (is_key_pressed(keyboard_key$KEY_ENTER)) +#' current_screen <- "TITLE" +#' +#' }) +#' +#' begin_drawing() +#' +#' clear_background(raylib_color$RAYWHITE) +#' +#' # draw differently depending on the current screen +#' switch (current_screen, +#' +#' LOGO = { +#' +#' draw_text("LOGO SCREEN", 20, 20, 40, raylib_color$LIGHTGRAY) +#' draw_text("WAIT for 2 SECONDS...", 290, 220, 20, raylib_color$GRAY) +#' +#' }, +#' +#' TITLE = { +#' +#' draw_rectangle(0, 0, screen_width, screen_height, raylib_color$GREEN) +#' draw_text("TITLE SCREEN", 20, 20, 40, raylib_color$DARKGREEN) +#' draw_text("PRESS ENTER to JUMP to GAMEPLAY SCREEN", 120, 220, 20, raylib_color$DARKGREEN) +#' +#' }, +#' +#' GAMEPLAY = { +#' +#' draw_rectangle(0, 0, screen_width, screen_height, raylib_color$PURPLE) +#' draw_text("GAMEPLAY SCREEN", 20, 20, 40, raylib_color$MAROON) +#' draw_text("PRESS ENTER to JUMP to ENDING SCREEN", 120, 220, 20, raylib_color$MAROON) +#' +#' }, +#' +#' ENDING = { +#' +#' draw_rectangle(0, 0, screen_width, screen_height, raylib_color$BLUE) +#' draw_text("ENDNG SCREEN", 20, 20, 40, raylib_color$DARKBLUE) +#' draw_text("PRESS ENTER to JUMP to ENDING SCREEN", 120, 220, 20, raylib_color$DARKBLUE) +#' +#' }) +#' +#' end_drawing() +#' +#' } #' } -#' -#' # run the game -#' +#' +#' ################### +#' # LAUNCH THE GAME # +#' ################### +#' #' play_game() -NULL \ No newline at end of file +NULL diff --git a/R/example_core_window_flags.R b/R/example_core_window_flags.R new file mode 100644 index 0000000..b935e68 --- /dev/null +++ b/R/example_core_window_flags.R @@ -0,0 +1,223 @@ +#' @name core_window_flags +#' @rdname core_window_flags +#' @family Core examples +#' @title Window Flags example +#' +#' @description +#' This example shows the window flags that can be set to manipulate the way +#' in which the window is launch and run. +#' +#' @examplesIf interactive() +#' +#' play_game <- function() { +#' +#' ################## +#' # Initialization # +#' ################## +#' +#' screen_width <- 800 +#' screen_height <- 450 +#' +#' # Possible window flags: +#' # ------------------------------ +#' # config_flags$FLAG_VSYNC_HINT +#' # config_flags$FLAG_FULLSCREEN_MODE -> not working properly (wrong scaling) +#' # config_flags$FLAG_WINDOW_RESIZABLE +#' # config_flags$FLAG_WINDOW_UNDECORATED +#' # config_flags$FLAG_WINDOW_TRANSPARENT +#' # config_flags$FLAG_WINDOW_HIDDEN +#' # config_flags$FLAG_WINDOW_MINIMIZED -> not supported of window creation +#' # config_flags$FLAG_WINDOW_MAXIMIZED -> not supported on window creation +#' # config_flags$FLAG_WINDOW_UNFOCUSED +#' # config_flags$FLAG_WINDOW_TOPMOST +#' # config_flags$FLAG_WINDOW_HIGHDPI -> errors after minimize-resize, fb size is recalculated +#' # config_flags$FLAG_WINDOW_ALWAYS_RUN +#' # config_flags$FLAG_MSAA_4X_HINT +#' +#' # Set configuration flags for window creation +#' # set_config_flags(config_flags$FLAG_VSYNC_HINT | config_flags$FLAG_MSAA_4X_HINT | config_flags$FLAG_WINDOW_HIGHDPI) +#' +#' init_window(screen_width, screen_height, "raylib [core] example - window flags") +#' # set up the automatic de-initialization +#' on.exit(close_window()) +#' +#' ball_position <- Vector2(get_screen_width() / 2, get_screen_height() / 2) +#' ball_speed <- Vector2(5, 4) +#' ball_radius <- 20 +#' +#' frames_counter <- 0 +#' +#' set_target_fps(60) +#' +#' ################## +#' # Main game loop # +#' ################## +#' +#' while(!window_should_close()) { +#' +#' # window state setters #### +#' if (is_key_pressed(keyboard_key$KEY_F)) +#' toggle_fullscreen() +#' +#' if (is_key_pressed(keyboard_key$KEY_R)) { +#' if (is_window_state(config_flags$FLAG_WINDOW_RESIZABLE)) +#' clear_window_state(config_flags$FLAG_WINDOW_RESIZABLE) +#' else +#' set_window_state(config_flags$FLAG_WINDOW_RESIZABLE) +#' } +#' +#' if (is_key_pressed(keyboard_key$KEY_D)) { +#' if (is_window_state(config_flags$FLAG_WINDOW_UNDECORATED)) +#' clear_window_state(config_flags$FLAG_WINDOW_UNDECORATED) +#' else +#' set_window_state(config_flags$FLAG_WINDOW_UNDECORATED) +#' } +#' +#' if (is_key_pressed(keyboard_key$KEY_H)) { +#' if (!is_window_state(config_flags$FLAG_WINDOW_HIDDEN)) +#' set_window_state(config_flags$FLAG_WINDOW_HIDDEN) +#' frames_counter <- 0 +#' } +#' +#' if (is_window_state(config_flags$FLAG_WINDOW_HIDDEN)) { +#' frames_counter <- frames_counter + 1 +#' if (frames_counter >= 240) # show window after ~ 3 seconds +#' clear_window_state(config_flags$FLAG_WINDOW_HIDDEN) +#' } +#' +#' if (is_key_pressed(keyboard_key$KEY_N)) { +#' if (!is_window_state(config_flags$FLAG_WINDOW_MINIMIZED)) +#' set_window_state(config_flags$FLAG_WINDOW_MINIMIZED) +#' frames_counter <- 0 +#' } +#' +#' if (is_window_state(config_flags$FLAG_WINDOW_MINIMIZED)) { +#' frames_counter <- frames_counter + 1 +#' if (frames_counter >= 240) # show window after ~ 3 seconds +#' clear_window_state(config_flags$FLAG_WINDOW_MINIMIZED) +#' } +#' +#' if (is_key_pressed(keyboard_key$KEY_M)) { +#' # requires FLAG_WINDOW_RESIZABLE enabmled; +#' if (is_window_state(config_flags$FLAG_WINDOW_MAXIMIZED)) +#' restore_window() +#' else maximize_window() +#' } +#' +#' if (is_key_pressed(keyboard_key$KEY_U)) { +#' if (is_window_state(config_flags$FLAG_WINDOW_UNFOCUSED)) +#' clear_window_state(config_flags$FLAG_WINDOW_UNFOCUSED) +#' else set_window_state(config_flags$FLAG_WINDOW_UNFOCUSED) +#' } +#' +#' if (is_key_pressed(keyboard_key$KEY_T)) { +#' if (is_window_state(config_flags$FLAG_WINDOW_TOPMOST)) +#' clear_window_state(config_flags$FLAG_WINDOW_TOPMOST) +#' else set_window_state(config_flags$FLAG_WINDOW_TOPMOST) +#' } +#' +#' if (is_key_pressed(keyboard_key$KEY_A)) { +#' if (is_window_state(config_flags$FLAG_WINDOW_ALWAYS_RUN)) +#' clear_window_state(config_flags$FLAG_WINDOW_ALWAYS_RUN) +#' else set_window_state(config_flags$FLAG_WINDOW_ALWAYS_RUN) +#' } +#' +#' if (is_key_pressed(keyboard_key$KEY_V)) { +#' if (is_window_state(config_flags$FLAG_VSYNC_HINT)) +#' clear_window_state(config_flags$FLAG_VSYNC_HINT) +#' else set_window_state(config_flags$FLAG_VSYNC_HINT) +#' } +#' +#' # bouncing ball logic #### +#' ball_position["x"] <- ball_position["x"] + ball_speed["x"] +#' ball_position["y"] <- ball_position["y"] + ball_speed["y"] +#' if ((ball_position["x"]) >= (get_screen_width() - ball_radius) || +#' (ball_position["x"] <= ball_radius)) +#' ball_speed["x"] <- ball_speed["x"] * -1 +#' if ((ball_position["y"]) >= (get_screen_height() - ball_radius) || +#' (ball_position["y"] <= ball_radius)) +#' ball_speed["y"] <- ball_speed["y"] * -1 +#' +#' # Draw #### +#' begin_drawing() +#' +#' if (is_window_state(config_flags$FLAG_WINDOW_TRANSPARENT)) +#' clear_background(raylib_color$BLANK) +#' else +#' clear_background(raylib_color$RAYWHITE) +#' +#' draw_circle_v(ball_position, ball_radius, raylib_color$MAROON) +#' draw_rectangle_lines_ex(Rectangle(0, 0, get_screen_width(), get_screen_height()), 4, raylib_color$RAYWHITE) +#' +#' draw_circle_v(get_mouse_position(), 10, raylib_color$DARKBLUE) +#' draw_fps(10, 10) +#' draw_text(sprintf("Screen Size: [%i, %i]", get_screen_width(), get_screen_height()), 10, 40, 10, raylib_color$GREEN) +#' +#' # window state info +#' draw_text("Following flags can be set after window creation:", 10, 60, 10, raylib_color$GRAY) +#' if (is_window_state(config_flags$FLAG_FULLSCREEN_MODE)) +#' draw_text("[F] FLAG_FULLSCREEN_MODE: on", 10, 80, 10, raylib_color$LIME) +#' else +#' draw_text("[F] FLAG_FULLSCREEN_MODE: off", 10, 80, 10, raylib_color$MAROON) +#' if (is_window_state(config_flags$FLAG_WINDOW_RESIZABLE)) +#' draw_text("[R] FLAG_WINDOW_RESIZABLE: on", 10, 100, 10, raylib_color$LIME) +#' else +#' draw_text("[R] FLAG_WINDOW_RESIZABLE: off", 10, 100, 10, raylib_color$MAROON) +#' if (is_window_state(config_flags$FLAG_WINDOW_UNDECORATED)) +#' draw_text("[D] FLAG_WINDOW_UNDECORATED: on", 10, 120, 10, raylib_color$LIME) +#' else +#' draw_text("[D] FLAG_WINDOW_UNDECORATED: off", 10, 120, 10, raylib_color$MAROON) +#' if (is_window_state(config_flags$FLAG_WINDOW_HIDDEN)) +#' draw_text("[H] FLAG_WINDOW_HIDDEN: on", 10, 140, 10, raylib_color$LIME) +#' else +#' draw_text("[H] FLAG_WINDOW_HIDDEN: off", 10, 140, 10, raylib_color$MAROON) +#' if (is_window_state(config_flags$FLAG_WINDOW_MINIMIZED)) +#' draw_text("[N] FLAG_WINDOW_MINIMIZED: on", 10, 160, 10, raylib_color$LIME) +#' else +#' draw_text("[N] FLAG_WINDOW_MINIMIZED: off", 10, 160, 10, raylib_color$MAROON) +#' if (is_window_state(config_flags$FLAG_WINDOW_MAXIMIZED)) +#' draw_text("[M] FLAG_WINDOW_MAXIMIZED: on", 10, 180, 10, raylib_color$LIME) +#' else +#' draw_text("[M] FLAG_WINDOW_MAXIMIZED: off", 10, 180, 10, raylib_color$MAROON) +#' if (is_window_state(config_flags$FLAG_WINDOW_UNFOCUSED)) +#' draw_text("[G] FLAG_WINDOW_UNFOCUSED: on", 10, 200, 10, raylib_color$LIME) +#' else +#' draw_text("[G] FLAG_WINDOW_UNFOCUSED: off", 10, 200, 10, raylib_color$MAROON) +#' if (is_window_state(config_flags$FLAG_WINDOW_TOPMOST)) +#' draw_text("[T] FLAG_WINDOW_TOPMOST: on", 10, 220, 10, raylib_color$LIME) +#' else +#' draw_text("[T] FLAG_WINDOW_TOPMOST: off", 10, 220, 10, raylib_color$MAROON) +#' if (is_window_state(config_flags$FLAG_WINDOW_ALWAYS_RUN)) +#' draw_text("[A] FLAG_WINDOW_ALWAYS_RUN: on", 10, 240, 10, raylib_color$LIME) +#' else +#' draw_text("[A] FLAG_WINDOW_ALWAYS_RUN: off", 10, 240, 10, raylib_color$MAROON) +#' if (is_window_state(config_flags$FLAG_VSYNC_HINT)) +#' draw_text("[V] FLAG_VSYNC_HINT: on", 10, 260, 10, raylib_color$LIME) +#' else +#' draw_text("[V] FLAG_VSYNC_HINT: off", 10, 260, 10, raylib_color$MAROON) +#' +#' draw_text("Following flags can only be set before window creation:", 10, 300, 10, raylib_color$GRAY) +#' if (is_window_state(config_flags$FLAG_WINDOW_HIGHDPI)) +#' draw_text("FLAG_WINDOW_HIGHDPI: on", 10, 320, 10, raylib_color$LIME) +#' else +#' draw_text("FLAG_WINDOW_HIGHDPI: off", 10, 320, 10, raylib_color$MAROON) +#' if (is_window_state(config_flags$FLAG_WINDOW_TRANSPARENT)) +#' draw_text("FLAG_WINDOW_TRANSPARENT: on", 10, 340, 10, raylib_color$LIME) +#' else +#' draw_text("FLAG_WINDOW_TRANSPARENT: off", 10, 340, 10, raylib_color$MAROON) +#' if (is_window_state(config_flags$FLAG_MSAA_4X_HINT)) +#' draw_text("FLAG_MSAA_4X_HINT: on", 10, 360, 10, raylib_color$LIME) +#' else +#' draw_text("FLAG_MSAA_4X_HINT: off", 10, 360, 10, raylib_color$MAROON) +#' +#' end_drawing() +#' } +#' } +#' +#' ################### +#' # LAUNCH THE GAME # +#' ################### +#' +#' play_game() +#' +NULL diff --git a/man/clear_window_state.Rd b/man/clear_window_state.Rd index 93d7d6e..db599d9 100644 --- a/man/clear_window_state.Rd +++ b/man/clear_window_state.Rd @@ -18,6 +18,7 @@ Clear window configuration state flags \seealso{ Other Windows-related functions: \code{\link{close_window}()}, +\code{\link{get_screen_width}()}, \code{\link{init_window}()}, \code{\link{is_window_focused}()}, \code{\link{is_window_fullscreen}()}, diff --git a/man/close_window.Rd b/man/close_window.Rd index 670b016..11f6355 100644 --- a/man/close_window.Rd +++ b/man/close_window.Rd @@ -15,6 +15,7 @@ Close window and unload OpenGL context \seealso{ Other Windows-related functions: \code{\link{clear_window_state}()}, +\code{\link{get_screen_width}()}, \code{\link{init_window}()}, \code{\link{is_window_focused}()}, \code{\link{is_window_fullscreen}()}, diff --git a/man/core_2d_camera.Rd b/man/core_2d_camera.Rd index e66fc01..0efd683 100644 --- a/man/core_2d_camera.Rd +++ b/man/core_2d_camera.Rd @@ -150,6 +150,7 @@ Other Core examples: \code{\link{core_basic_window}}, \code{\link{core_drop_files}}, \code{\link{core_input_keys}}, -\code{\link{core_input_mouse}} +\code{\link{core_input_mouse}}, +\code{\link{core_window_flags}} } \concept{Core examples} diff --git a/man/core_2d_platformer.Rd b/man/core_2d_platformer.Rd index 432dc4b..89e8d8d 100644 --- a/man/core_2d_platformer.Rd +++ b/man/core_2d_platformer.Rd @@ -5,12 +5,18 @@ \title{2D camera platformer} \description{ This example shows multiple ways to update 2D camera with custom functions. -Also, basic collision functionality is implemented with basic mathematical +Also, collision detection functionality is implemented with basic mathematical and R-base functions. + +Example contributed for \code{raylib} library by arvyy (@arvyy) and reviewed by +Ramon Santamaria (@raysan5) + +Copyright (c) 2019 arvyy (@arvyy) + +Example modified for \code{raylib.R} } \examples{ \dontshow{if (interactive()) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} - ###################### # Pre-initialization # ###################### @@ -21,149 +27,145 @@ player_hor_spd <- 200 # 'player' object constructor Player <- function(x, y, speed, can_jump) { - list(position = Vector2(x, y), - speed = speed, - can_jump = can_jump) + list(position = Vector2(x, y), + speed = speed, + can_jump = can_jump) } # 'environment item' object constructor EnvItem <- function(x, y, width, height, blocking, color, alpha = 255) { - list(rect = Rectangle(x, y, width, height), - blocking = blocking, - color = Color(color, alpha)) + list(rect = Rectangle(x, y, width, height), + blocking = blocking, + color = Color(color, alpha)) } # 'player update' function update_player <- function(player, env_items, env_items_length, delta) { - if (is_key_down(keyboard_key$KEY_LEFT)) - player$position["x"] <- player$position["x"] - player_hor_spd * delta + if (is_key_down(keyboard_key$KEY_LEFT)) + player$position["x"] <- player$position["x"] - player_hor_spd * delta - if (is_key_down(keyboard_key$KEY_RIGHT)) - player$position["x"] <- player$position["x"] + player_hor_spd * delta + if (is_key_down(keyboard_key$KEY_RIGHT)) + player$position["x"] <- player$position["x"] + player_hor_spd * delta - if (is_key_down(keyboard_key$KEY_SPACE) && isTRUE(player$can_jump)) { - player$speed <- player$speed - player_jump_spd - player$can_jump <- FALSE + if (is_key_down(keyboard_key$KEY_SPACE) && isTRUE(player$can_jump)) { + player$speed <- player$speed - player_jump_spd + player$can_jump <- FALSE - } + } - hit_obstacle <- FALSE + hit_obstacle <- FALSE - collisions <- sapply( - env_items, \(env_item) { - env_item$blocking && - env_item$rect["x"] <= player$position["x"] && - (env_item$rect["x"] + env_item$rect["width"] >= player$position["x"]) && - env_item$rect["y"] >= player$position["y"] && - (env_item$rect["y"] < player$position["y"] + player$speed * delta) - }) + collisions <- sapply( + env_items, \(env_item) { + env_item$blocking && + env_item$rect["x"] <= player$position["x"] && + (env_item$rect["x"] + env_item$rect["width"] >= player$position["x"]) && + env_item$rect["y"] >= player$position["y"] && + (env_item$rect["y"] < player$position["y"] + player$speed * delta) + }) - coll_i <- which(collisions) + coll_i <- which(collisions) - if (sum(coll_i) > 0) { - hit_obstacle <- TRUE - player$speed <- 0 - player$position["y"] <- env_items[[coll_i]]$rect["y"] - } + if (sum(coll_i) > 0) { + hit_obstacle <- TRUE + player$speed <- 0 + player$position["y"] <- env_items[[coll_i]]$rect["y"] + } - if (!hit_obstacle) { - player$position["y"] <- player$position["y"] + player$speed * delta - player$speed <- player$speed + G * delta - player$can_jump <- FALSE + if (!hit_obstacle) { + player$position["y"] <- player$position["y"] + player$speed * delta + player$speed <- player$speed + G * delta + player$can_jump <- FALSE - } else - player$can_jump <- TRUE + } else + player$can_jump <- TRUE - return(player) + return(player) } # camera update functions -update_camera_center <- function( - camera, player, env_items, env_items_length, delta, width, height) { - camera$offset <- Vector2(width/2, height/2) - camera$target <- player$position +update_camera_center <- function(camera, player, env_items, env_items_length, delta, width, height) { - return(camera) + camera$offset <- Vector2(width/2, height/2) + camera$target <- player$position + + return(camera) } -# update_camera_center_inside_map <- function( -# camera, player, env_items, env_items_length, delta, width, height) { -# camera$target <- player$position -# camera$offset <- Vector2(width/2, height/2) -# -# min_x <- min_y <- 1000 -# max_x <- max_y <- -1000 -# -# for (i in 1:env_items_length) { -# min_x <- min(env_items[[i]]$rect["x"], min_x) -# max_x <- max(env_items[[i]]$rect["x"] + env_items$rect["width"], max_x) -# min_y <- min(env_items[[i]]$rect["y"], min_y) -# max_y <- max(env_items[[i]]$rect["y"] + env_items$rect["height"], max_y) -# -# -# max = .Call(.C_GetWorldToScreen2D_R, Vector2(max_x, max_y), camera, PACKAGE = "raylib.R") -# min = .Call(.C_GetWorldToScreen2D_R, Vector2(min_x, min_y), camera, PACKAGE = "raylib.R") -# -# if (max["x"] < width) camera$offset["x"] <- width - (max["x"] - width/2) -# if (max["y"] < height) camera$offset["y"] <- heigh - (max["y"] - height/2) -# if (min["x"] > 0) camera$offset["x"] <- width/2 - min["x"] -# if (min["y"] > 0) camera$offset["y"] <- height/2 - min["y"] -# -# } -# -# return(camera) -# } - -update_camera_center_smooth_follow <- function( - camera, player, env_items, env_items_length, delta, width, height) { - - min_speed <- 30 - min_effect_length <- 10 - fraction_speed <- 0.8 - - camera$offset <- Vector2(width/2, height/2) - - diff <- player$position - camera$target - veclength <- sqrt(sum(sapply(diff, \(x) x^2))) - - if (veclength > min_effect_length) { - speed <- max(fraction_speed * veclength, min_speed) - camera$target <- camera$target + diff*(speed * delta / veclength) - } - return(camera) +update_camera_center_inside_map <- function(camera, player, env_items, env_items_length, delta, width, height) { + + camera$target <- player$position + camera$offset <- Vector2(width/2, height/2) + + min_x <- min_y <- 1000 + max_x <- max_y <- -1000 + + for (i in 1:env_items_length) { + min_x <- min(env_items[[i]]$rect["x"], min_x) + max_x <- max(env_items[[i]]$rect["x"] + env_items[[i]]$rect["width"], max_x) + min_y <- min(env_items[[i]]$rect["y"], min_y) + max_y <- max(env_items[[i]]$rect["y"] + env_items[[i]]$rect["height"], max_y) + } + + max <- get_world_to_screen_2d(Vector2(max_x, max_y), camera) + min <- get_world_to_screen_2d(Vector2(min_x, min_y), camera) + + if (max["x"] < width) camera$offset["x"] <- width - (max["x"] - width/2) + if (max["y"] < height) camera$offset["y"] <- height - (max["y"] - height/2) + if (min["x"] > 0) camera$offset["x"] <- width/2 - min["x"] + if (min["y"] > 0) camera$offset["y"] <- height/2 - min["y"] + + return(camera) } -update_camera_even_out_on_landing <- function( - camera, player, env_items, env_items_length, delta, width, height) { - - even_out_speed <- 700 - - camera$offset <- Vector2(width/2, height/2) - camera$target["x"] <- player$position["x"] - - if (isTRUE(.update_camera_even_out_env[["evening_out"]])) { - - if (.update_camera_even_out_env[["even_out_target"]] > camera$target["y"]) { - camera$target["y"] <- camera$target["y"] + even_out_speed * delta - if (camera$target["y"] > .update_camera_even_out_env[["even_out_target"]]) { - camera$target["y"] <- .update_camera_even_out_env[["even_out_target"]] - .update_camera_even_out_env[["evening_out"]] <- FALSE - } - } else { - camera$target["y"] <- camera$target["y"] - even_out_speed*delta - if (camera$target["y"] < .update_camera_even_out_env[["even_out_target"]]) { - camera$target["y"] <- .update_camera_even_out_env[["even_out_target"]] - .update_camera_even_out_env[["evening_out"]] <- FALSE - } - } - } else if (isTRUE(player$can_jump) && player$speed == 0 && player$position["y"] != camera$target["y"]) { - .update_camera_even_out_env[["evening_out"]] <- TRUE - .update_camera_even_out_env[["even_out_target"]] <- player$position["y"] - } - - return(camera) +update_camera_center_smooth_follow <- function(camera, player, env_items, env_items_length, delta, width, height) { + + min_speed <- 30 + min_effect_length <- 10 + fraction_speed <- 0.8 + + camera$offset <- Vector2(width/2, height/2) + + diff <- player$position - camera$target + veclength <- sqrt(sum(sapply(diff, \(x) x^2))) + + if (veclength > min_effect_length) { + speed <- max(fraction_speed * veclength, min_speed) + camera$target <- camera$target + diff*(speed * delta / veclength) + } + return(camera) +} + +update_camera_even_out_on_landing <- function(camera, player, env_items, env_items_length, delta, width, height) { + + even_out_speed <- 700 + + camera$offset <- Vector2(width/2, height/2) + camera$target["x"] <- player$position["x"] + + if (isTRUE(.update_camera_even_out_env[["evening_out"]])) { + + if (.update_camera_even_out_env[["even_out_target"]] > camera$target["y"]) { + camera$target["y"] <- camera$target["y"] + even_out_speed * delta + if (camera$target["y"] > .update_camera_even_out_env[["even_out_target"]]) { + camera$target["y"] <- .update_camera_even_out_env[["even_out_target"]] + .update_camera_even_out_env[["evening_out"]] <- FALSE + } + } else { + camera$target["y"] <- camera$target["y"] - even_out_speed*delta + if (camera$target["y"] < .update_camera_even_out_env[["even_out_target"]]) { + camera$target["y"] <- .update_camera_even_out_env[["even_out_target"]] + .update_camera_even_out_env[["evening_out"]] <- FALSE + } + } + } else if (isTRUE(player$can_jump) && player$speed == 0 && player$position["y"] != camera$target["y"]) { + .update_camera_even_out_env[["evening_out"]] <- TRUE + .update_camera_even_out_env[["even_out_target"]] <- player$position["y"] + } + + return(camera) } # environement needed for `update_even_out_on_landing` camera updater @@ -171,32 +173,32 @@ update_camera_even_out_on_landing <- function( .update_camera_even_out_env[["evening_out"]] <- FALSE .update_camera_even_out_env[["even_out_target"]] <- NULL -# update_camera_player_bound_push <- function( - # camera, player, env_items, env_items_length, delta, width, height) { -# -# bbox <- Vector2(0.2, 0.2) -# -# bbox_world_min <- .Call(.C_GetScreenToWorld2D_R, -# Vector2((1 - bbox["x"]) * 0.5 * width, (1 - bbox["y"] * 0.5 * height)), -# camera) -# bbox_world_max <- .Call(.C_GetScreenToWorld2D_R, -# Vector2((1 + bbox["x"]) * 0.5 * width, (1 + bbox["y"] * 0.5 * height)), -# camera) -# -# camera$offset <- Vector2((1 - bbox["x"]) * 0.5 * width, (1 - bbox["y"]) * 0.5 * height) -# -# if (player$position["x"] < bbox_world_min["x"]) -# camera$target["x"] <- player$position["x"] -# if (player$position["y"] < bbox_world_min["y"]) -# camera$target["y"] <- player$position["y"] -# if (player$position["x"] > bbox_world_max["x"]) -# camera$target["x"] <- bbox_world_min["x"] + (player$position["x"] - bbox_world_min["x"]) -# if (player$position["y"] > bbox_world_max["y"]) -# camera$target["y"] <- bbox_world_min["y"] + (player$position["y"] - bbox_world_min["y"]) -# -# return(camera) -# -# } +update_camera_player_bound_push <- function(camera, player, env_items, env_items_length, delta, width, height) { + + bbox <- Vector2(0.2, 0.2) + + bbox_world_min <- get_screen_to_world_2d( + Vector2((1 - bbox["x"]) * 0.5 * width, (1 - bbox["y"] * 0.5 * height)), + camera) + + bbox_world_max <- get_screen_to_world_2d( + Vector2((1 + bbox["x"]) * 0.5 * width, (1 + bbox["y"] * 0.5 * height)), + camera) + + camera$offset <- Vector2((1 - bbox["x"]) * 0.5 * width, (1 - bbox["y"]) * 0.5 * height) + + if (player$position["x"] < bbox_world_min["x"]) + camera$target["x"] <- player$position["x"] + if (player$position["y"] < bbox_world_min["y"]) + camera$target["y"] <- player$position["y"] + if (player$position["x"] > bbox_world_max["x"]) + camera$target["x"] <- bbox_world_min["x"] + (player$position["x"] - bbox_world_max["x"]) + if (player$position["y"] > bbox_world_max["y"]) + camera$target["y"] <- bbox_world_min["y"] + (player$position["y"] - bbox_world_max["y"]) + + return(camera) + +} ################## # Initialization # @@ -204,124 +206,126 @@ update_camera_even_out_on_landing <- function( play_game <- function() { - screen_width <- 800 - screen_height <- 450 - - init_window(screen_width, screen_height, "raylib [core] example - 2d camera") - on.exit(close_window()) - - player <- Player(x = 400, y = 280, speed = 0, can_jump = FALSE) - - env_items <- list( - EnvItem(0, 0, 1000, 400, FALSE, raylib_color$LIGHTGRAY), - EnvItem(0, 400, 1000, 200, TRUE, raylib_color$GRAY), - EnvItem(300, 200, 400, 10, TRUE, raylib_color$GRAY), - EnvItem(250, 300, 100, 10, TRUE, raylib_color$GRAY), - EnvItem(650, 300, 100, 10, TRUE, raylib_color$GRAY) - ) - - env_items_length <- length(env_items) - - camera <- Camera2D(target = player$position, - offset = Vector2(screen_width/2, screen_height/2), - rotation = 0, - zoom = 1) - - # set up camera updaters - camera_updaters <- list( - update = list( - descr = "Follow player center", - func = update_camera_center - ), - # update_inside_map = list( - # descr = "Follow player center, but clamp to map edges", - # func = update_camera_center_inside_map - # ), - update_smooth_follow = list( - descr = "Follow player center;\nsmoothed", - func = update_camera_center_smooth_follow - ), - update_even_out_on_landing = list( - descr = "Follow player center horizontally;\nupdate player center vertically after landing", - func = update_camera_even_out_on_landing - )#, - # update_player_bound_push = list( - # descr = "Player push camera on getting too close to screen edge", - # func = update_camera_player_bound_push - # ) - ) - - camera_option = 1 - - set_target_fps(60) - - ################## - # Main game loop # - ################## - - while(!window_should_close()) { - - # to implement - # delta_time <- get_frame_time() - delta_time <- 1/60 - - player <- update_player(player, env_items, env_items_length, delta_time) - - camera$zoom <- camera$zoom + get_mouse_wheel_move()*0.05 - - if (camera$zoom > 3) - camera$zoom = 3 - else if (camera$zoom < 0.25) - camera$zoom = 0.25 - - if (is_key_pressed(keyboard_key$KEY_R)) { - camera$zoom <- 1 - player$position <- Vector2(400, 280) - } - - if (is_key_pressed(keyboard_key$KEY_C)) { - camera_option <- camera_option \%\% length(camera_updaters) + 1 - } - - # update the camera! - camera <- do.call(what = camera_updaters[[camera_option]]$func, - args = list( - camera = camera, - player = player, - env_items = env_items, - env_items_length = env_items_length, - delta = delta_time, - width = screen_width, - height = screen_height - )) - - begin_drawing() - - clear_background(raylib_color$LIGHTGRAY) - - begin_mode_2d(camera) - - invisible(lapply(env_items, \(x) { draw_rectangle_rec(x$rect, x$color) })) - - player_rect <- Rectangle(player$position["x"] - 20, player$position["y"] - 40, 40, 40) - draw_rectangle_rec(player_rect, raylib_color$RED) - - end_mode_2d() - - DrawText("Controls:", 20, 20, 10, raylib_color$BLACK); - DrawText("- Right/Left to move", 40, 40, 10, raylib_color$DARKGRAY); - DrawText("- Space to jump", 40, 60, 10, raylib_color$DARKGRAY); - DrawText("- Mouse Wheel to Zoom in-out, R to reset zoom", 40, 80, 10, raylib_color$DARKGRAY); - DrawText("- C to change camera mode", 40, 100, 10, raylib_color$DARKGRAY); - DrawText("Current camera mode:", 20, 120, 10, raylib_color$BLACK); - DrawText(camera_updaters[[camera_option]]$descr, 40, 140, 10, raylib_color$DARKGRAY); - - end_drawing() - - } + screen_width <- 800 + screen_height <- 450 + + init_window(screen_width, screen_height, "raylib [core] example - 2d camera") + on.exit(close_window()) + + player <- Player(x = 400, y = 280, speed = 0, can_jump = FALSE) + + env_items <- list( + EnvItem(0, 0, 1000, 400, FALSE, raylib_color$LIGHTGRAY), + EnvItem(0, 400, 1000, 200, TRUE, raylib_color$GRAY), + EnvItem(300, 200, 400, 10, TRUE, raylib_color$GRAY), + EnvItem(250, 300, 100, 10, TRUE, raylib_color$GRAY), + EnvItem(650, 300, 100, 10, TRUE, raylib_color$GRAY) + ) + + env_items_length <- length(env_items) + + camera <- Camera2D(target = player$position, + offset = Vector2(screen_width/2, screen_height/2), + rotation = 0, + zoom = 1) + + # set up camera updaters + camera_updaters <- list( + update = list( + descr = "Follow player center", + func = update_camera_center + ), + update_inside_map = list( + descr = "Follow player center, but clamp to map edges", + func = update_camera_center_inside_map + ), + update_smooth_follow = list( + descr = "Follow player center;\nsmoothed", + func = update_camera_center_smooth_follow + ), + update_even_out_on_landing = list( + descr = "Follow player center horizontally;\nupdate player center vertically after landing", + func = update_camera_even_out_on_landing + ), + update_player_bound_push = list( + descr = "Player push camera on getting too close to screen edge", + func = update_camera_player_bound_push + ) + ) + + camera_option = 1 + + set_target_fps(60) + + ################## + # Main game loop # + ################## + + while(!window_should_close()) { + + # to implement + delta_time <- get_frame_time() + # delta_time <- 1/60 + + player <- update_player(player, env_items, env_items_length, delta_time) + + camera$zoom <- camera$zoom + get_mouse_wheel_move()*0.05 + + if (camera$zoom > 3) + camera$zoom = 3 + else if (camera$zoom < 0.25) + camera$zoom = 0.25 + + if (is_key_pressed(keyboard_key$KEY_R)) { + camera$zoom <- 1 + player$position <- Vector2(400, 280) + } + + if (is_key_pressed(keyboard_key$KEY_C)) { + camera_option <- camera_option \%\% length(camera_updaters) + 1 + } + + # update the camera! + camera <- do.call(what = camera_updaters[[camera_option]]$func, + args = list( + camera = camera, + player = player, + env_items = env_items, + env_items_length = env_items_length, + delta = delta_time, + width = screen_width, + height = screen_height + )) + + begin_drawing() + + clear_background(raylib_color$LIGHTGRAY) + + begin_mode_2d(camera) + + invisible(lapply(env_items, \(x) { draw_rectangle_rec(x$rect, x$color) })) + + player_rect <- Rectangle(player$position["x"] - 20, player$position["y"] - 40, 40, 40) + draw_rectangle_rec(player_rect, raylib_color$RED) + + end_mode_2d() + + DrawText("Controls:", 20, 20, 10, raylib_color$BLACK); + DrawText("- Right/Left to move", 40, 40, 10, raylib_color$DARKGRAY); + DrawText("- Space to jump", 40, 60, 10, raylib_color$DARKGRAY); + DrawText("- Mouse Wheel to Zoom in-out, R to reset zoom", 40, 80, 10, raylib_color$DARKGRAY); + DrawText("- C to change camera mode", 40, 100, 10, raylib_color$DARKGRAY); + DrawText("Current camera mode:", 20, 120, 10, raylib_color$BLACK); + DrawText(camera_updaters[[camera_option]]$descr, 40, 140, 10, raylib_color$DARKGRAY); + + end_drawing() + + } } -# run the game +################### +# LAUNCH THE GAME # +################### play_game() \dontshow{\}) # examplesIf} @@ -336,6 +340,7 @@ Other Core examples: \code{\link{core_basic_window}}, \code{\link{core_drop_files}}, \code{\link{core_input_keys}}, -\code{\link{core_input_mouse}} +\code{\link{core_input_mouse}}, +\code{\link{core_window_flags}} } \concept{Core examples} diff --git a/man/core_3d_camera.Rd b/man/core_3d_camera.Rd index 529629f..dabe1e2 100644 --- a/man/core_3d_camera.Rd +++ b/man/core_3d_camera.Rd @@ -75,6 +75,7 @@ Other Core examples: \code{\link{core_basic_window}}, \code{\link{core_drop_files}}, \code{\link{core_input_keys}}, -\code{\link{core_input_mouse}} +\code{\link{core_input_mouse}}, +\code{\link{core_window_flags}} } \concept{Core examples} diff --git a/man/core_3d_camera_first_person.Rd b/man/core_3d_camera_first_person.Rd index 9f1df2b..425d0b6 100644 --- a/man/core_3d_camera_first_person.Rd +++ b/man/core_3d_camera_first_person.Rd @@ -113,6 +113,7 @@ Other Core examples: \code{\link{core_basic_window}}, \code{\link{core_drop_files}}, \code{\link{core_input_keys}}, -\code{\link{core_input_mouse}} +\code{\link{core_input_mouse}}, +\code{\link{core_window_flags}} } \concept{Core examples} diff --git a/man/core_3d_camera_free.Rd b/man/core_3d_camera_free.Rd index 27cc022..1f4190e 100644 --- a/man/core_3d_camera_free.Rd +++ b/man/core_3d_camera_free.Rd @@ -94,6 +94,7 @@ Other Core examples: \code{\link{core_basic_window}}, \code{\link{core_drop_files}}, \code{\link{core_input_keys}}, -\code{\link{core_input_mouse}} +\code{\link{core_input_mouse}}, +\code{\link{core_window_flags}} } \concept{Core examples} diff --git a/man/core_basic_screen_manager.Rd b/man/core_basic_screen_manager.Rd index 0a3a629..28db7f9 100644 --- a/man/core_basic_screen_manager.Rd +++ b/man/core_basic_screen_manager.Rd @@ -12,107 +12,109 @@ game loop play_game <- function() { - ################## - # Initialization # - ################## + ################## + # Initialization # + ################## - screen_width <- 800 - screen_height <- 450 + screen_width <- 800 + screen_height <- 450 - init_window(screen_width, screen_height, "raylib [core] example - basic screen manager") + init_window(screen_width, screen_height, "raylib [core] example - basic screen manager") - # deinitialization of the window - on.exit(close_window()) + # deinitialization of the window + on.exit(close_window()) - current_screen <- "LOGO" + current_screen <- "LOGO" - frames_counter <- 0 + frames_counter <- 0 - set_target_fps(60) + set_target_fps(60) - ################## - # Main game loop # - ################## + ################## + # Main game loop # + ################## - while(!window_should_close()) { + while(!window_should_close()) { - # menage screen switches during this part - switch(current_screen, + # menage screen switches during this part + switch(current_screen, - LOGO = { + LOGO = { - frames_counter <- frames_counter + 1 # count frames + frames_counter <- frames_counter + 1 # count frames - # Wait for 2 seconds (120 frames) before jumping to TITLE screen - if (frames_counter > 120) - current_screen <- "TITLE" - }, + # Wait for 2 seconds (120 frames) before jumping to TITLE screen + if (frames_counter > 120) + current_screen <- "TITLE" + }, - TITLE = { + TITLE = { - if (is_key_pressed(keyboard_key$KEY_ENTER)) - current_screen <- "GAMEPLAY" + if (is_key_pressed(keyboard_key$KEY_ENTER)) + current_screen <- "GAMEPLAY" - }, + }, - GAMEPLAY = { + GAMEPLAY = { - if (is_key_pressed(keyboard_key$KEY_ENTER)) - current_screen <- "ENDING" + if (is_key_pressed(keyboard_key$KEY_ENTER)) + current_screen <- "ENDING" - }, + }, - ENDING = { + ENDING = { - if (is_key_pressed(keyboard_key$KEY_ENTER)) - current_screen <- "TITLE" + if (is_key_pressed(keyboard_key$KEY_ENTER)) + current_screen <- "TITLE" - }) + }) - begin_drawing() + begin_drawing() - clear_background(raylib_color$RAYWHITE) + clear_background(raylib_color$RAYWHITE) - # draw differently depending on the current screen - switch (current_screen, + # draw differently depending on the current screen + switch (current_screen, - LOGO = { + LOGO = { - draw_text("LOGO SCREEN", 20, 20, 40, raylib_color$LIGHTGRAY) - draw_text("WAIT for 2 SECONDS...", 290, 220, 20, raylib_color$GRAY) + draw_text("LOGO SCREEN", 20, 20, 40, raylib_color$LIGHTGRAY) + draw_text("WAIT for 2 SECONDS...", 290, 220, 20, raylib_color$GRAY) - }, + }, - TITLE = { + TITLE = { - draw_rectangle(0, 0, screen_width, screen_height, raylib_color$GREEN) - draw_text("TITLE SCREEN", 20, 20, 40, raylib_color$DARKGREEN) - draw_text("PRESS ENTER to JUMP to GAMEPLAY SCREEN", 120, 220, 20, raylib_color$DARKGREEN) + draw_rectangle(0, 0, screen_width, screen_height, raylib_color$GREEN) + draw_text("TITLE SCREEN", 20, 20, 40, raylib_color$DARKGREEN) + draw_text("PRESS ENTER to JUMP to GAMEPLAY SCREEN", 120, 220, 20, raylib_color$DARKGREEN) - }, + }, - GAMEPLAY = { + GAMEPLAY = { - draw_rectangle(0, 0, screen_width, screen_height, raylib_color$PURPLE) - draw_text("GAMEPLAY SCREEN", 20, 20, 40, raylib_color$MAROON) - draw_text("PRESS ENTER to JUMP to ENDING SCREEN", 120, 220, 20, raylib_color$MAROON) + draw_rectangle(0, 0, screen_width, screen_height, raylib_color$PURPLE) + draw_text("GAMEPLAY SCREEN", 20, 20, 40, raylib_color$MAROON) + draw_text("PRESS ENTER to JUMP to ENDING SCREEN", 120, 220, 20, raylib_color$MAROON) - }, + }, - ENDING = { + ENDING = { - draw_rectangle(0, 0, screen_width, screen_height, raylib_color$BLUE) - draw_text("ENDNG SCREEN", 20, 20, 40, raylib_color$DARKBLUE) - draw_text("PRESS ENTER to JUMP to ENDING SCREEN", 120, 220, 20, raylib_color$DARKBLUE) + draw_rectangle(0, 0, screen_width, screen_height, raylib_color$BLUE) + draw_text("ENDNG SCREEN", 20, 20, 40, raylib_color$DARKBLUE) + draw_text("PRESS ENTER to JUMP to ENDING SCREEN", 120, 220, 20, raylib_color$DARKBLUE) - }) + }) - end_drawing() + end_drawing() - } + } } -# run the game +################### +# LAUNCH THE GAME # +################### play_game() \dontshow{\}) # examplesIf} @@ -127,6 +129,7 @@ Other Core examples: \code{\link{core_basic_window}}, \code{\link{core_drop_files}}, \code{\link{core_input_keys}}, -\code{\link{core_input_mouse}} +\code{\link{core_input_mouse}}, +\code{\link{core_window_flags}} } \concept{Core examples} diff --git a/man/core_basic_window.Rd b/man/core_basic_window.Rd index e67fbae..f9fb110 100644 --- a/man/core_basic_window.Rd +++ b/man/core_basic_window.Rd @@ -41,6 +41,7 @@ Other Core examples: \code{\link{core_basic_screen_manager}}, \code{\link{core_drop_files}}, \code{\link{core_input_keys}}, -\code{\link{core_input_mouse}} +\code{\link{core_input_mouse}}, +\code{\link{core_window_flags}} } \concept{Core examples} diff --git a/man/core_drop_files.Rd b/man/core_drop_files.Rd index d759a7b..9b948ed 100644 --- a/man/core_drop_files.Rd +++ b/man/core_drop_files.Rd @@ -77,6 +77,7 @@ Other Core examples: \code{\link{core_basic_screen_manager}}, \code{\link{core_basic_window}}, \code{\link{core_input_keys}}, -\code{\link{core_input_mouse}} +\code{\link{core_input_mouse}}, +\code{\link{core_window_flags}} } \concept{Core examples} diff --git a/man/core_input_keys.Rd b/man/core_input_keys.Rd index fa11262..7c3abc6 100644 --- a/man/core_input_keys.Rd +++ b/man/core_input_keys.Rd @@ -57,6 +57,7 @@ Other Core examples: \code{\link{core_basic_screen_manager}}, \code{\link{core_basic_window}}, \code{\link{core_drop_files}}, -\code{\link{core_input_mouse}} +\code{\link{core_input_mouse}}, +\code{\link{core_window_flags}} } \concept{Core examples} diff --git a/man/core_input_mouse.Rd b/man/core_input_mouse.Rd index eea1f98..d7c3667 100644 --- a/man/core_input_mouse.Rd +++ b/man/core_input_mouse.Rd @@ -67,6 +67,7 @@ Other Core examples: \code{\link{core_basic_screen_manager}}, \code{\link{core_basic_window}}, \code{\link{core_drop_files}}, -\code{\link{core_input_keys}} +\code{\link{core_input_keys}}, +\code{\link{core_window_flags}} } \concept{Core examples} diff --git a/man/core_window_flags.Rd b/man/core_window_flags.Rd new file mode 100644 index 0000000..a3bd377 --- /dev/null +++ b/man/core_window_flags.Rd @@ -0,0 +1,238 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/example_core_window_flags.R +\name{core_window_flags} +\alias{core_window_flags} +\title{Window Flags example} +\description{ +This example shows the window flags that can be set to manipulate the way +in which the window is launch and run. +} +\examples{ +\dontshow{if (interactive()) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} + +play_game <- function() { + + ################## + # Initialization # + ################## + + screen_width <- 800 + screen_height <- 450 + + # Possible window flags: + # ------------------------------ + # config_flags$FLAG_VSYNC_HINT + # config_flags$FLAG_FULLSCREEN_MODE -> not working properly (wrong scaling) + # config_flags$FLAG_WINDOW_RESIZABLE + # config_flags$FLAG_WINDOW_UNDECORATED + # config_flags$FLAG_WINDOW_TRANSPARENT + # config_flags$FLAG_WINDOW_HIDDEN + # config_flags$FLAG_WINDOW_MINIMIZED -> not supported of window creation + # config_flags$FLAG_WINDOW_MAXIMIZED -> not supported on window creation + # config_flags$FLAG_WINDOW_UNFOCUSED + # config_flags$FLAG_WINDOW_TOPMOST + # config_flags$FLAG_WINDOW_HIGHDPI -> errors after minimize-resize, fb size is recalculated + # config_flags$FLAG_WINDOW_ALWAYS_RUN + # config_flags$FLAG_MSAA_4X_HINT + + # Set configuration flags for window creation + # set_config_flags(config_flags$FLAG_VSYNC_HINT | config_flags$FLAG_MSAA_4X_HINT | config_flags$FLAG_WINDOW_HIGHDPI) + + init_window(screen_width, screen_height, "raylib [core] example - window flags") + # set up the automatic de-initialization + on.exit(close_window()) + + ball_position <- Vector2(get_screen_width() / 2, get_screen_height() / 2) + ball_speed <- Vector2(5, 4) + ball_radius <- 20 + + frames_counter <- 0 + + set_target_fps(60) + + ################## + # Main game loop # + ################## + + while(!window_should_close()) { + + # window state setters #### + if (is_key_pressed(keyboard_key$KEY_F)) + toggle_fullscreen() + + if (is_key_pressed(keyboard_key$KEY_R)) { + if (is_window_state(config_flags$FLAG_WINDOW_RESIZABLE)) + clear_window_state(config_flags$FLAG_WINDOW_RESIZABLE) + else + set_window_state(config_flags$FLAG_WINDOW_RESIZABLE) + } + + if (is_key_pressed(keyboard_key$KEY_D)) { + if (is_window_state(config_flags$FLAG_WINDOW_UNDECORATED)) + clear_window_state(config_flags$FLAG_WINDOW_UNDECORATED) + else + set_window_state(config_flags$FLAG_WINDOW_UNDECORATED) + } + + if (is_key_pressed(keyboard_key$KEY_H)) { + if (!is_window_state(config_flags$FLAG_WINDOW_HIDDEN)) + set_window_state(config_flags$FLAG_WINDOW_HIDDEN) + frames_counter <- 0 + } + + if (is_window_state(config_flags$FLAG_WINDOW_HIDDEN)) { + frames_counter <- frames_counter + 1 + if (frames_counter >= 240) # show window after ~ 3 seconds + clear_window_state(config_flags$FLAG_WINDOW_HIDDEN) + } + + if (is_key_pressed(keyboard_key$KEY_N)) { + if (!is_window_state(config_flags$FLAG_WINDOW_MINIMIZED)) + set_window_state(config_flags$FLAG_WINDOW_MINIMIZED) + frames_counter <- 0 + } + + if (is_window_state(config_flags$FLAG_WINDOW_MINIMIZED)) { + frames_counter <- frames_counter + 1 + if (frames_counter >= 240) # show window after ~ 3 seconds + clear_window_state(config_flags$FLAG_WINDOW_MINIMIZED) + } + + if (is_key_pressed(keyboard_key$KEY_M)) { + # requires FLAG_WINDOW_RESIZABLE enabmled; + if (is_window_state(config_flags$FLAG_WINDOW_MAXIMIZED)) + restore_window() + else maximize_window() + } + + if (is_key_pressed(keyboard_key$KEY_U)) { + if (is_window_state(config_flags$FLAG_WINDOW_UNFOCUSED)) + clear_window_state(config_flags$FLAG_WINDOW_UNFOCUSED) + else set_window_state(config_flags$FLAG_WINDOW_UNFOCUSED) + } + + if (is_key_pressed(keyboard_key$KEY_T)) { + if (is_window_state(config_flags$FLAG_WINDOW_TOPMOST)) + clear_window_state(config_flags$FLAG_WINDOW_TOPMOST) + else set_window_state(config_flags$FLAG_WINDOW_TOPMOST) + } + + if (is_key_pressed(keyboard_key$KEY_A)) { + if (is_window_state(config_flags$FLAG_WINDOW_ALWAYS_RUN)) + clear_window_state(config_flags$FLAG_WINDOW_ALWAYS_RUN) + else set_window_state(config_flags$FLAG_WINDOW_ALWAYS_RUN) + } + + if (is_key_pressed(keyboard_key$KEY_V)) { + if (is_window_state(config_flags$FLAG_VSYNC_HINT)) + clear_window_state(config_flags$FLAG_VSYNC_HINT) + else set_window_state(config_flags$FLAG_VSYNC_HINT) + } + + # bouncing ball logic #### + ball_position["x"] <- ball_position["x"] + ball_speed["x"] + ball_position["y"] <- ball_position["y"] + ball_speed["y"] + if ((ball_position["x"]) >= (get_screen_width() - ball_radius) || + (ball_position["x"] <= ball_radius)) + ball_speed["x"] <- ball_speed["x"] * -1 + if ((ball_position["y"]) >= (get_screen_height() - ball_radius) || + (ball_position["y"] <= ball_radius)) + ball_speed["y"] <- ball_speed["y"] * -1 + + # Draw #### + begin_drawing() + + if (is_window_state(config_flags$FLAG_WINDOW_TRANSPARENT)) + clear_background(raylib_color$BLANK) + else + clear_background(raylib_color$RAYWHITE) + + draw_circle_v(ball_position, ball_radius, raylib_color$MAROON) + draw_rectangle_lines_ex(Rectangle(0, 0, get_screen_width(), get_screen_height()), 4, raylib_color$RAYWHITE) + + draw_circle_v(get_mouse_position(), 10, raylib_color$DARKBLUE) + draw_fps(10, 10) + draw_text(sprintf("Screen Size: [\%i, \%i]", get_screen_width(), get_screen_height()), 10, 40, 10, raylib_color$GREEN) + + # window state info + draw_text("Following flags can be set after window creation:", 10, 60, 10, raylib_color$GRAY) + if (is_window_state(config_flags$FLAG_FULLSCREEN_MODE)) + draw_text("[F] FLAG_FULLSCREEN_MODE: on", 10, 80, 10, raylib_color$LIME) + else + draw_text("[F] FLAG_FULLSCREEN_MODE: off", 10, 80, 10, raylib_color$MAROON) + if (is_window_state(config_flags$FLAG_WINDOW_RESIZABLE)) + draw_text("[R] FLAG_WINDOW_RESIZABLE: on", 10, 100, 10, raylib_color$LIME) + else + draw_text("[R] FLAG_WINDOW_RESIZABLE: off", 10, 100, 10, raylib_color$MAROON) + if (is_window_state(config_flags$FLAG_WINDOW_UNDECORATED)) + draw_text("[D] FLAG_WINDOW_UNDECORATED: on", 10, 120, 10, raylib_color$LIME) + else + draw_text("[D] FLAG_WINDOW_UNDECORATED: off", 10, 120, 10, raylib_color$MAROON) + if (is_window_state(config_flags$FLAG_WINDOW_HIDDEN)) + draw_text("[H] FLAG_WINDOW_HIDDEN: on", 10, 140, 10, raylib_color$LIME) + else + draw_text("[H] FLAG_WINDOW_HIDDEN: off", 10, 140, 10, raylib_color$MAROON) + if (is_window_state(config_flags$FLAG_WINDOW_MINIMIZED)) + draw_text("[N] FLAG_WINDOW_MINIMIZED: on", 10, 160, 10, raylib_color$LIME) + else + draw_text("[N] FLAG_WINDOW_MINIMIZED: off", 10, 160, 10, raylib_color$MAROON) + if (is_window_state(config_flags$FLAG_WINDOW_MAXIMIZED)) + draw_text("[M] FLAG_WINDOW_MAXIMIZED: on", 10, 180, 10, raylib_color$LIME) + else + draw_text("[M] FLAG_WINDOW_MAXIMIZED: off", 10, 180, 10, raylib_color$MAROON) + if (is_window_state(config_flags$FLAG_WINDOW_UNFOCUSED)) + draw_text("[G] FLAG_WINDOW_UNFOCUSED: on", 10, 200, 10, raylib_color$LIME) + else + draw_text("[G] FLAG_WINDOW_UNFOCUSED: off", 10, 200, 10, raylib_color$MAROON) + if (is_window_state(config_flags$FLAG_WINDOW_TOPMOST)) + draw_text("[T] FLAG_WINDOW_TOPMOST: on", 10, 220, 10, raylib_color$LIME) + else + draw_text("[T] FLAG_WINDOW_TOPMOST: off", 10, 220, 10, raylib_color$MAROON) + if (is_window_state(config_flags$FLAG_WINDOW_ALWAYS_RUN)) + draw_text("[A] FLAG_WINDOW_ALWAYS_RUN: on", 10, 240, 10, raylib_color$LIME) + else + draw_text("[A] FLAG_WINDOW_ALWAYS_RUN: off", 10, 240, 10, raylib_color$MAROON) + if (is_window_state(config_flags$FLAG_VSYNC_HINT)) + draw_text("[V] FLAG_VSYNC_HINT: on", 10, 260, 10, raylib_color$LIME) + else + draw_text("[V] FLAG_VSYNC_HINT: off", 10, 260, 10, raylib_color$MAROON) + + draw_text("Following flags can only be set before window creation:", 10, 300, 10, raylib_color$GRAY) + if (is_window_state(config_flags$FLAG_WINDOW_HIGHDPI)) + draw_text("FLAG_WINDOW_HIGHDPI: on", 10, 320, 10, raylib_color$LIME) + else + draw_text("FLAG_WINDOW_HIGHDPI: off", 10, 320, 10, raylib_color$MAROON) + if (is_window_state(config_flags$FLAG_WINDOW_TRANSPARENT)) + draw_text("FLAG_WINDOW_TRANSPARENT: on", 10, 340, 10, raylib_color$LIME) + else + draw_text("FLAG_WINDOW_TRANSPARENT: off", 10, 340, 10, raylib_color$MAROON) + if (is_window_state(config_flags$FLAG_MSAA_4X_HINT)) + draw_text("FLAG_MSAA_4X_HINT: on", 10, 360, 10, raylib_color$LIME) + else + draw_text("FLAG_MSAA_4X_HINT: off", 10, 360, 10, raylib_color$MAROON) + + end_drawing() + } +} + +################### +# LAUNCH THE GAME # +################### + +play_game() +\dontshow{\}) # examplesIf} +} +\seealso{ +Other Core examples: +\code{\link{core_2d_camera}}, +\code{\link{core_2d_platformer}}, +\code{\link{core_3d_camera_first_person}}, +\code{\link{core_3d_camera_free}}, +\code{\link{core_3d_camera}}, +\code{\link{core_basic_screen_manager}}, +\code{\link{core_basic_window}}, +\code{\link{core_drop_files}}, +\code{\link{core_input_keys}}, +\code{\link{core_input_mouse}} +} +\concept{Core examples} diff --git a/man/get_frame_time.Rd b/man/get_frame_time.Rd new file mode 100644 index 0000000..b9c5b07 --- /dev/null +++ b/man/get_frame_time.Rd @@ -0,0 +1,16 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/core.R +\name{get_frame_time} +\alias{get_frame_time} +\title{Get frame time} +\usage{ +get_frame_time() +} +\description{ +Get frame time +} +\seealso{ +Other Timing-related functions: +\code{\link{set_target_fps}()} +} +\concept{Timing-related functions} diff --git a/man/get_screen_height.Rd b/man/get_screen_height.Rd new file mode 100644 index 0000000..4e396ca --- /dev/null +++ b/man/get_screen_height.Rd @@ -0,0 +1,12 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/core.R +\name{get_screen_height} +\alias{get_screen_height} +\title{Get current screen height} +\usage{ +get_screen_height() +} +\description{ +Get current screen height +} +\concept{Windows-related function} diff --git a/man/get_screen_to_world_2d.Rd b/man/get_screen_to_world_2d.Rd new file mode 100644 index 0000000..225b147 --- /dev/null +++ b/man/get_screen_to_world_2d.Rd @@ -0,0 +1,24 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/core.R +\name{get_screen_to_world_2d} +\alias{get_screen_to_world_2d} +\title{Get the world space position for a 2d camera screen space position} +\usage{ +get_screen_to_world_2d(position, camera) +} +\arguments{ +\item{position}{Vector2} + +\item{camera}{Camera2D} +} +\value{ +Vector2 +} +\description{ +Get the world space position for a 2d camera screen space position +} +\seealso{ +Other Screen-space-related functions: +\code{\link{get_world_to_screen_2d}()} +} +\concept{Screen-space-related functions} diff --git a/man/get_screen_width.Rd b/man/get_screen_width.Rd new file mode 100644 index 0000000..a01dcd4 --- /dev/null +++ b/man/get_screen_width.Rd @@ -0,0 +1,33 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/core.R +\name{get_screen_width} +\alias{get_screen_width} +\title{Get current screen width} +\usage{ +get_screen_width() +} +\description{ +Get current screen width +} +\seealso{ +Other Windows-related functions: +\code{\link{clear_window_state}()}, +\code{\link{close_window}()}, +\code{\link{init_window}()}, +\code{\link{is_window_focused}()}, +\code{\link{is_window_fullscreen}()}, +\code{\link{is_window_hidden}()}, +\code{\link{is_window_maximized}()}, +\code{\link{is_window_minimized}()}, +\code{\link{is_window_ready}()}, +\code{\link{is_window_resized}()}, +\code{\link{is_window_state}()}, +\code{\link{maximize_window}()}, +\code{\link{minimize_window}()}, +\code{\link{restore_window}()}, +\code{\link{set_window_icon}()}, +\code{\link{set_window_state}()}, +\code{\link{toggle_fullscreen}()}, +\code{\link{window_should_close}()} +} +\concept{Windows-related functions} diff --git a/man/get_world_to_screen_2d.Rd b/man/get_world_to_screen_2d.Rd new file mode 100644 index 0000000..29f14c2 --- /dev/null +++ b/man/get_world_to_screen_2d.Rd @@ -0,0 +1,24 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/core.R +\name{get_world_to_screen_2d} +\alias{get_world_to_screen_2d} +\title{Get the screen space position for a 2d camera world space position} +\usage{ +get_world_to_screen_2d(position, camera) +} +\arguments{ +\item{position}{Vector2} + +\item{camera}{Camera2D} +} +\value{ +Vector2 +} +\description{ +Get the screen space position for a 2d camera world space position +} +\seealso{ +Other Screen-space-related functions: +\code{\link{get_screen_to_world_2d}()} +} +\concept{Screen-space-related functions} diff --git a/man/init_window.Rd b/man/init_window.Rd index 783f386..3f5b7b1 100644 --- a/man/init_window.Rd +++ b/man/init_window.Rd @@ -23,6 +23,7 @@ Initialize window and OpenGL context Other Windows-related functions: \code{\link{clear_window_state}()}, \code{\link{close_window}()}, +\code{\link{get_screen_width}()}, \code{\link{is_window_focused}()}, \code{\link{is_window_fullscreen}()}, \code{\link{is_window_hidden}()}, diff --git a/man/is_window_focused.Rd b/man/is_window_focused.Rd index 600d05e..7d99355 100644 --- a/man/is_window_focused.Rd +++ b/man/is_window_focused.Rd @@ -16,6 +16,7 @@ Check if window is currently maximized (only PLATFORM_DESKTOP) Other Windows-related functions: \code{\link{clear_window_state}()}, \code{\link{close_window}()}, +\code{\link{get_screen_width}()}, \code{\link{init_window}()}, \code{\link{is_window_fullscreen}()}, \code{\link{is_window_hidden}()}, diff --git a/man/is_window_fullscreen.Rd b/man/is_window_fullscreen.Rd index 56066ff..30c4141 100644 --- a/man/is_window_fullscreen.Rd +++ b/man/is_window_fullscreen.Rd @@ -16,6 +16,7 @@ Check if window is currently fullscreen Other Windows-related functions: \code{\link{clear_window_state}()}, \code{\link{close_window}()}, +\code{\link{get_screen_width}()}, \code{\link{init_window}()}, \code{\link{is_window_focused}()}, \code{\link{is_window_hidden}()}, diff --git a/man/is_window_hidden.Rd b/man/is_window_hidden.Rd index 8da9c77..955ef6f 100644 --- a/man/is_window_hidden.Rd +++ b/man/is_window_hidden.Rd @@ -16,6 +16,7 @@ Check if window is currently hidden (only PLATFORM_DESKTOP) Other Windows-related functions: \code{\link{clear_window_state}()}, \code{\link{close_window}()}, +\code{\link{get_screen_width}()}, \code{\link{init_window}()}, \code{\link{is_window_focused}()}, \code{\link{is_window_fullscreen}()}, diff --git a/man/is_window_maximized.Rd b/man/is_window_maximized.Rd index 397505c..f57bbd5 100644 --- a/man/is_window_maximized.Rd +++ b/man/is_window_maximized.Rd @@ -16,6 +16,7 @@ Check if window is currently maximized (only PLATFORM_DESKTOP) Other Windows-related functions: \code{\link{clear_window_state}()}, \code{\link{close_window}()}, +\code{\link{get_screen_width}()}, \code{\link{init_window}()}, \code{\link{is_window_focused}()}, \code{\link{is_window_fullscreen}()}, diff --git a/man/is_window_minimized.Rd b/man/is_window_minimized.Rd index 13d3ec2..f199284 100644 --- a/man/is_window_minimized.Rd +++ b/man/is_window_minimized.Rd @@ -16,6 +16,7 @@ Check if window is currently minimized (only PLATFORM_DESKTOP) Other Windows-related functions: \code{\link{clear_window_state}()}, \code{\link{close_window}()}, +\code{\link{get_screen_width}()}, \code{\link{init_window}()}, \code{\link{is_window_focused}()}, \code{\link{is_window_fullscreen}()}, diff --git a/man/is_window_ready.Rd b/man/is_window_ready.Rd index 8d674b8..5a13145 100644 --- a/man/is_window_ready.Rd +++ b/man/is_window_ready.Rd @@ -16,6 +16,7 @@ Check if window has been initialized successfully Other Windows-related functions: \code{\link{clear_window_state}()}, \code{\link{close_window}()}, +\code{\link{get_screen_width}()}, \code{\link{init_window}()}, \code{\link{is_window_focused}()}, \code{\link{is_window_fullscreen}()}, diff --git a/man/is_window_resized.Rd b/man/is_window_resized.Rd index 6e282c5..2bb10fb 100644 --- a/man/is_window_resized.Rd +++ b/man/is_window_resized.Rd @@ -16,6 +16,7 @@ Check if window has been resized last frame Other Windows-related functions: \code{\link{clear_window_state}()}, \code{\link{close_window}()}, +\code{\link{get_screen_width}()}, \code{\link{init_window}()}, \code{\link{is_window_focused}()}, \code{\link{is_window_fullscreen}()}, diff --git a/man/is_window_state.Rd b/man/is_window_state.Rd index ad6dbe6..35acc76 100644 --- a/man/is_window_state.Rd +++ b/man/is_window_state.Rd @@ -19,6 +19,7 @@ Check if one specific window flag is enabled Other Windows-related functions: \code{\link{clear_window_state}()}, \code{\link{close_window}()}, +\code{\link{get_screen_width}()}, \code{\link{init_window}()}, \code{\link{is_window_focused}()}, \code{\link{is_window_fullscreen}()}, diff --git a/man/maximize_window.Rd b/man/maximize_window.Rd index aa84cb2..2923a27 100644 --- a/man/maximize_window.Rd +++ b/man/maximize_window.Rd @@ -16,6 +16,7 @@ Set window state: maximized, if resizable (only PLATFORM_DESKTOP) Other Windows-related functions: \code{\link{clear_window_state}()}, \code{\link{close_window}()}, +\code{\link{get_screen_width}()}, \code{\link{init_window}()}, \code{\link{is_window_focused}()}, \code{\link{is_window_fullscreen}()}, diff --git a/man/minimize_window.Rd b/man/minimize_window.Rd index 83049fd..7e56b37 100644 --- a/man/minimize_window.Rd +++ b/man/minimize_window.Rd @@ -16,6 +16,7 @@ Set window state: minimized, if resizable (only PLATFORM_DESKTOP) Other Windows-related functions: \code{\link{clear_window_state}()}, \code{\link{close_window}()}, +\code{\link{get_screen_width}()}, \code{\link{init_window}()}, \code{\link{is_window_focused}()}, \code{\link{is_window_fullscreen}()}, diff --git a/man/restore_window.Rd b/man/restore_window.Rd index 0778af3..b313044 100644 --- a/man/restore_window.Rd +++ b/man/restore_window.Rd @@ -16,6 +16,7 @@ Set window state: not minimized/maximized (only PLATFORM_DESKTOP) Other Windows-related functions: \code{\link{clear_window_state}()}, \code{\link{close_window}()}, +\code{\link{get_screen_width}()}, \code{\link{init_window}()}, \code{\link{is_window_focused}()}, \code{\link{is_window_fullscreen}()}, diff --git a/man/set_target_fps.Rd b/man/set_target_fps.Rd index ba8010f..8526b53 100644 --- a/man/set_target_fps.Rd +++ b/man/set_target_fps.Rd @@ -15,4 +15,8 @@ SetTargetFps(fps) \description{ Set target FPS (maximum) } +\seealso{ +Other Timing-related functions: +\code{\link{get_frame_time}()} +} \concept{Timing-related functions} diff --git a/man/set_window_icon.Rd b/man/set_window_icon.Rd index c249c87..6302485 100644 --- a/man/set_window_icon.Rd +++ b/man/set_window_icon.Rd @@ -19,6 +19,7 @@ Set window state: not minimized/maximized (only PLATFORM_DESKTOP) Other Windows-related functions: \code{\link{clear_window_state}()}, \code{\link{close_window}()}, +\code{\link{get_screen_width}()}, \code{\link{init_window}()}, \code{\link{is_window_focused}()}, \code{\link{is_window_fullscreen}()}, diff --git a/man/set_window_state.Rd b/man/set_window_state.Rd index 03907ae..295598a 100644 --- a/man/set_window_state.Rd +++ b/man/set_window_state.Rd @@ -19,6 +19,7 @@ Set window configuration state using flags (only PLATFORM_DESKTOP) Other Windows-related functions: \code{\link{clear_window_state}()}, \code{\link{close_window}()}, +\code{\link{get_screen_width}()}, \code{\link{init_window}()}, \code{\link{is_window_focused}()}, \code{\link{is_window_fullscreen}()}, diff --git a/man/toggle_fullscreen.Rd b/man/toggle_fullscreen.Rd index b462621..928e493 100644 --- a/man/toggle_fullscreen.Rd +++ b/man/toggle_fullscreen.Rd @@ -16,6 +16,7 @@ Toggle window state: fullscreen/windowed (only PLATFORM_DESKTOP) Other Windows-related functions: \code{\link{clear_window_state}()}, \code{\link{close_window}()}, +\code{\link{get_screen_width}()}, \code{\link{init_window}()}, \code{\link{is_window_focused}()}, \code{\link{is_window_fullscreen}()}, diff --git a/man/window_should_close.Rd b/man/window_should_close.Rd index 67f9440..45f948b 100644 --- a/man/window_should_close.Rd +++ b/man/window_should_close.Rd @@ -16,6 +16,7 @@ Check if KEY_ESCAPE pressed or Close icon pressed Other Windows-related functions: \code{\link{clear_window_state}()}, \code{\link{close_window}()}, +\code{\link{get_screen_width}()}, \code{\link{init_window}()}, \code{\link{is_window_focused}()}, \code{\link{is_window_fullscreen}()},