diff --git a/R/core.R b/R/core.R index e20d7b2..889366a 100644 --- a/R/core.R +++ b/R/core.R @@ -230,7 +230,6 @@ get_screen_width <- function() { get_screen_height <- function() { .Call(.C_GetScreenHeight_R) } - #' @family Windows-related functions #' @title Get number of connected monitors #' @@ -708,6 +707,7 @@ get_world_to_screen_ex <- function(position, camera, width, height) { #' @title Get the screen space position for a 2d camera world space position #' @param position Vector2. #' @param camera Camera2D +#' @return Unnamed integer vector of length 2 containing x and y coords #' #' @export get_world_to_screen_2d <- function(position, camera) { @@ -718,6 +718,7 @@ get_world_to_screen_2d <- function(position, camera) { #' @title Get the world space position for a 2d camera screen space position #' @param position Vector2. #' @param camera Camera2D +#' @return Unnamed integer vector of length 2 containing x and y coords #' #' @export get_screen_to_world_2d <- function(position, camera) { diff --git a/R/example_core_2d_camera_platformer.R b/R/example_core_2d_camera_platformer.R new file mode 100644 index 0000000..5ed8b46 --- /dev/null +++ b/R/example_core_2d_camera_platformer.R @@ -0,0 +1,330 @@ +#' @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, collision detection functionality is implemented with basic mathematical +#' and R-base functions. +#' +#' Example contributed for `raylib` library by arvyy (@arvyy) and reviewed by +#' Ramon Santamaria (@raysan5) +#' +#' Copyright (c) 2019 arvyy (@arvyy) +#' +#' 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) +#' } +#' +#' +#' # '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[[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[1] < width) camera$offset["x"] <- width - (max[1] - width/2) +#' if (max[2] < height) camera$offset["y"] <- height - (max[2] - height/2) +#' if (min[1] > 0) camera$offset["x"] <- width/2 - min[1] +#' if (min[2] > 0) camera$offset["y"] <- height/2 - min[2] +#' +#' 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 <- 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[1]) +#' camera$target["x"] <- player$position["x"] +#' if (player$position["y"] < bbox_world_min[2]) +#' camera$target["y"] <- player$position["y"] +#' if (player$position["x"] > bbox_world_max[1]) +#' camera$target["x"] <- bbox_world_min[1] + (player$position["x"] - bbox_world_max[1]) +#' if (player$position["y"] > bbox_world_max[2]) +#' camera$target["y"] <- bbox_world_min[2] + (player$position["y"] - bbox_world_max[2]) +#' +#' 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() +#' +#' } +#' } +#' +#' ################### +#' # LAUNCH THE GAME # +#' ################### +#' +#' play_game() +NULL diff --git a/R/example_core_basic_screen_manager.R b/R/example_core_basic_screen_manager.R new file mode 100644 index 0000000..acb461a --- /dev/null +++ b/R/example_core_basic_screen_manager.R @@ -0,0 +1,119 @@ +#' @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() +#' +#' } +#' } +#' +#' ################### +#' # LAUNCH THE GAME # +#' ################### +#' +#' play_game() +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/core_2d_camera.Rd b/man/core_2d_camera.Rd index d00a073..0efd683 100644 --- a/man/core_2d_camera.Rd +++ b/man/core_2d_camera.Rd @@ -142,12 +142,15 @@ 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}}, -\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 new file mode 100644 index 0000000..812d383 --- /dev/null +++ b/man/core_2d_platformer.Rd @@ -0,0 +1,346 @@ +% 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, 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 # +###################### + +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[[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[1] < width) camera$offset["x"] <- width - (max[1] - width/2) + if (max[2] < height) camera$offset["y"] <- height - (max[2] - height/2) + if (min[1] > 0) camera$offset["x"] <- width/2 - min[1] + if (min[2] > 0) camera$offset["y"] <- height/2 - min[2] + + 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 <- 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[1]) + camera$target["x"] <- player$position["x"] + if (player$position["y"] < bbox_world_min[2]) + camera$target["y"] <- player$position["y"] + if (player$position["x"] > bbox_world_max[1]) + camera$target["x"] <- bbox_world_min[1] + (player$position["x"] - bbox_world_max[1]) + if (player$position["y"] > bbox_world_max[2]) + camera$target["y"] <- bbox_world_min[2] + (player$position["y"] - bbox_world_max[2]) + + 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() + + } +} + +################### +# LAUNCH 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}}, +\code{\link{core_window_flags}} +} +\concept{Core examples} diff --git a/man/core_3d_camera.Rd b/man/core_3d_camera.Rd index 4eab861..dabe1e2 100644 --- a/man/core_3d_camera.Rd +++ b/man/core_3d_camera.Rd @@ -68,11 +68,14 @@ 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}}, -\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 d0c22a3..425d0b6 100644 --- a/man/core_3d_camera_first_person.Rd +++ b/man/core_3d_camera_first_person.Rd @@ -106,11 +106,14 @@ 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}}, -\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 5f38d64..1f4190e 100644 --- a/man/core_3d_camera_free.Rd +++ b/man/core_3d_camera_free.Rd @@ -87,11 +87,14 @@ 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}}, -\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 new file mode 100644 index 0000000..28db7f9 --- /dev/null +++ b/man/core_basic_screen_manager.Rd @@ -0,0 +1,135 @@ +% 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() + + } +} + +################### +# 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_window}}, +\code{\link{core_drop_files}}, +\code{\link{core_input_keys}}, +\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 c9cffd9..f9fb110 100644 --- a/man/core_basic_window.Rd +++ b/man/core_basic_window.Rd @@ -34,11 +34,14 @@ 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}} +\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 48583fa..9b948ed 100644 --- a/man/core_drop_files.Rd +++ b/man/core_drop_files.Rd @@ -70,11 +70,14 @@ 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}} +\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 2e31c86..7c3abc6 100644 --- a/man/core_input_keys.Rd +++ b/man/core_input_keys.Rd @@ -50,11 +50,14 @@ 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}} +\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 862c6b6..d7c3667 100644 --- a/man/core_input_mouse.Rd +++ b/man/core_input_mouse.Rd @@ -60,11 +60,14 @@ 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}} +\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_screen_to_world_2d.Rd b/man/get_screen_to_world_2d.Rd index 9ea8ba9..0c4b54e 100644 --- a/man/get_screen_to_world_2d.Rd +++ b/man/get_screen_to_world_2d.Rd @@ -14,6 +14,9 @@ GetScreenToWorld2d(position, camera) \item{camera}{Camera2D} } +\value{ +Unnamed integer vector of length 2 containing x and y coords +} \description{ Get the world space position for a 2d camera screen space position } diff --git a/man/get_world_to_screen_2d.Rd b/man/get_world_to_screen_2d.Rd index 120f185..5785944 100644 --- a/man/get_world_to_screen_2d.Rd +++ b/man/get_world_to_screen_2d.Rd @@ -14,6 +14,9 @@ GetWorldToScreen2d(position, camera) \item{camera}{Camera2D} } +\value{ +Unnamed integer vector of length 2 containing x and y coords +} \description{ Get the screen space position for a 2d camera world space position }