From 109c8c5f17435e9c0efbf2fd55a9c828b055ef9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CTimfon=E2=80=9D?= <“timothy_fong@brown.edu”> Date: Tue, 25 Jul 2023 21:38:27 +0700 Subject: [PATCH 1/2] added core example smooth pixel perfect camera --- .../core/core_smooth pixel-perfect camera.py | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 examples/core/core_smooth pixel-perfect camera.py diff --git a/examples/core/core_smooth pixel-perfect camera.py b/examples/core/core_smooth pixel-perfect camera.py new file mode 100644 index 0000000..aec4e91 --- /dev/null +++ b/examples/core/core_smooth pixel-perfect camera.py @@ -0,0 +1,90 @@ +from pyray import * +import math +#Initialization + +screenWidth = 800 +screenHeight = 450 + +virtualScreenWidth = 160 +virtualScreenHeight = 90 + +virtualRatio = screenWidth/ virtualScreenWidth + +init_window(screenWidth, screenHeight, "raylib [core] example - smooth pixel-perfect camera") + +worldSpaceCamera = Camera2D([0]) # Game world camera +worldSpaceCamera.zoom = 1.0 + +screenSpaceCamera = Camera2D([0]) # Smoothing camera +screenSpaceCamera.zoom = 1.0 + +target = load_render_texture(virtualScreenWidth, virtualScreenHeight); # This is where we'll draw all our objects. + +rec01 = Rectangle(70.0, 35.0, 20.0, 20.0) +rec02 = Rectangle(90.0, 55.0, 30.0, 10.0) +rec03 = Rectangle(80.0, 65.0, 15.0, 25.0) + +# The target's height is flipped (in the source Rectangle), due to OpenGL reasons +sourceRec = Rectangle(0.0, 0.0, target.texture.width, -target.texture.height) +destRec = Rectangle(-virtualRatio, -virtualRatio, screenWidth + (virtualRatio*2), screenHeight + (virtualRatio*2)) + +origin = Vector2(0.0, 0.0) + +rotation = 0.0 + +cameraX = 0.0 +cameraY = 0.0 + +set_target_fps(60) + +# Main game loop +while not window_should_close(): # Detect window close button or ESC key + + # Update + + rotation += 60.0 *get_frame_time(); # Rotate the rectangles, 60 degrees per second + + # Make the camera move to demonstrate the effect + cameraX = (math.sin(get_time())*50.0) - 10.0 + cameraY = math.cos(get_time())*30.0 + + # Set the camera's target to the values computed above + screenSpaceCamera.target = Vector2(cameraX, cameraY) + + # Round worldSpace coordinates, keep decimals into screenSpace coordinates + worldSpaceCamera.target.x = screenSpaceCamera.target.x + screenSpaceCamera.target.x -= worldSpaceCamera.target.x + screenSpaceCamera.target.x *= virtualRatio + + worldSpaceCamera.target.y = screenSpaceCamera.target.y + screenSpaceCamera.target.y -= worldSpaceCamera.target.y + screenSpaceCamera.target.y *= virtualRatio + + begin_texture_mode(target) + clear_background(RAYWHITE) + + begin_mode_2d(worldSpaceCamera) + draw_rectangle_pro(rec01, origin, rotation, BLACK) + draw_rectangle_pro(rec02, origin, -rotation, RED) + draw_rectangle_pro(rec03, origin, rotation + 45.0, BLUE) + end_mode_2d() + end_texture_mode() + + begin_drawing() + clear_background(RED) + + begin_mode_2d(screenSpaceCamera) + draw_texture_pro(target.texture, sourceRec, destRec, origin, 0.0, WHITE) + end_mode_2d() + + draw_text(text_format("Screen resolution: %ix%i", screenWidth, screenHeight), 10, 10, 20, DARKBLUE) + draw_text(text_format("World resolution: %ix%i", virtualScreenWidth, virtualScreenHeight), 10, 40, 20, DARKGREEN) + draw_fps(get_screen_width() - 95, 10) + end_drawing() + + +# De-Initialization + +unload_render_texture(target) # Unload render texture + +close_window(); # Close window and OpenGL context \ No newline at end of file From 9df3448f1c3c5103877de8c700e0f7ae2144f470 Mon Sep 17 00:00:00 2001 From: Tim Fong 2 Date: Wed, 9 Aug 2023 19:16:32 +0700 Subject: [PATCH 2/2] added core_window_flags --- examples/core/core_window_flags.py | 156 +++++++++++++++++++++++++++++ 1 file changed, 156 insertions(+) create mode 100644 examples/core/core_window_flags.py diff --git a/examples/core/core_window_flags.py b/examples/core/core_window_flags.py new file mode 100644 index 0000000..89e5321 --- /dev/null +++ b/examples/core/core_window_flags.py @@ -0,0 +1,156 @@ +from pyray import * +import pyray + +# Initialization +# --------------------------------------------------------- +screen_width = 800 +screen_height = 450 + +init_window(screen_width, screen_height, b"raylib [core] example - window flags") + +ball_position = Vector2(get_screen_width() / 2.0, get_screen_height() / 2.0) +ball_speed = Vector2(5.0, 4.0) +ball_radius = 20.0 + +frames_counter = 0 + +# Main game loop +while not window_should_close(): # Detect window close button or ESC key + # Update + # ----------------------------------------------------- + if is_key_pressed(pyray.KEY_F): + toggle_fullscreen() + + if is_key_pressed(pyray.KEY_R): + if is_window_state(pyray.FLAG_WINDOW_RESIZABLE): + clear_window_state(pyray.FLAG_WINDOW_RESIZABLE) + else: + set_window_state(pyray.FLAG_WINDOW_RESIZABLE) + + if is_key_pressed(pyray.KEY_D): + if is_window_state(pyray.FLAG_WINDOW_UNDECORATED): + clear_window_state(pyray.FLAG_WINDOW_UNDECORATED) + else: + set_window_state(pyray.FLAG_WINDOW_UNDECORATED) + + if is_key_pressed(pyray.KEY_H): + if not is_window_state(pyray.FLAG_WINDOW_HIDDEN): + set_window_state(pyray.FLAG_WINDOW_HIDDEN) + frames_counter = 0 + + if is_window_state(pyray.FLAG_WINDOW_HIDDEN): + frames_counter += 1 + if frames_counter >= 240: + clear_window_state(pyray.FLAG_WINDOW_HIDDEN) # Show window after 3 seconds + + if is_key_pressed(pyray.KEY_N): + if not is_window_state(pyray.FLAG_WINDOW_MINIMIZED): + minimize_window() + frames_counter = 0 + + if is_window_state(pyray.FLAG_WINDOW_MINIMIZED): + frames_counter += 1 + if frames_counter >= 240: + restore_window() # Restore window after 3 seconds + + if is_key_pressed(pyray.KEY_M): + if is_window_state(pyray.FLAG_WINDOW_RESIZABLE): + if is_window_state(pyray.FLAG_WINDOW_MAXIMIZED): + restore_window() + else: + maximize_window() + + if is_key_pressed(pyray.KEY_U): + if is_window_state(pyray.FLAG_WINDOW_UNFOCUSED): + clear_window_state(pyray.FLAG_WINDOW_UNFOCUSED) + else: + set_window_state(pyray.FLAG_WINDOW_UNFOCUSED) + + if is_key_pressed(pyray.KEY_T): + if is_window_state(pyray.FLAG_WINDOW_TOPMOST): + clear_window_state(pyray.FLAG_WINDOW_TOPMOST) + else: + set_window_state(pyray.FLAG_WINDOW_TOPMOST) + + if is_key_pressed(pyray.KEY_A): + if is_window_state(pyray.FLAG_WINDOW_ALWAYS_RUN): + clear_window_state(pyray.FLAG_WINDOW_ALWAYS_RUN) + else: + set_window_state(pyray.FLAG_WINDOW_ALWAYS_RUN) + + if is_key_pressed(pyray.KEY_V): + if is_window_state(pyray.FLAG_VSYNC_HINT): + clear_window_state(pyray.FLAG_VSYNC_HINT) + else: + set_window_state(pyray.FLAG_VSYNC_HINT) + + # Bouncing ball logic + ball_position.x += ball_speed.x + ball_position.y += ball_speed.y + if ball_position.x >= (get_screen_width() - ball_radius) or ball_position.x <= ball_radius: + ball_speed.x *= -1.0 + if ball_position.y >= (get_screen_height() - ball_radius) or ball_position.y <= ball_radius: + ball_speed.y *= -1.0 + + # Draw + # ----------------------------------------------------- + begin_drawing() + + if is_window_state(pyray.FLAG_WINDOW_TRANSPARENT): + clear_background(BLANK) + else: + clear_background(RAYWHITE) + + draw_circle_v(ball_position, ball_radius, MAROON) + draw_rectangle_lines_ex(Rectangle(0, 0, get_screen_width(), get_screen_height()), 4, RAYWHITE) + + draw_circle_v(get_mouse_position(), 10, DARKBLUE) + + draw_fps(10, 10) + + draw_text(f"Screen Size: [{get_screen_width()}, {get_screen_height()}]", 10, 40, 10, GREEN) + + # Draw window state info + draw_text("Following flags can be set after window creation:", 10, 60, 10, GRAY) + flag_texts = [ + ("FLAG_FULLSCREEN_MODE", pyray.FLAG_FULLSCREEN_MODE), + ("FLAG_WINDOW_RESIZABLE", pyray.FLAG_WINDOW_RESIZABLE), + ("FLAG_WINDOW_UNDECORATED", pyray.FLAG_WINDOW_UNDECORATED), + ("FLAG_WINDOW_HIDDEN", pyray.FLAG_WINDOW_HIDDEN), + ("FLAG_WINDOW_MINIMIZED", pyray.FLAG_WINDOW_MINIMIZED), + ("FLAG_WINDOW_MAXIMIZED", pyray.FLAG_WINDOW_MAXIMIZED), + ("FLAG_WINDOW_UNFOCUSED", pyray.FLAG_WINDOW_UNFOCUSED), + ("FLAG_WINDOW_TOPMOST", pyray.FLAG_WINDOW_TOPMOST), + ("FLAG_WINDOW_ALWAYS_RUN", pyray.FLAG_WINDOW_ALWAYS_RUN), + ("FLAG_VSYNC_HINT", pyray.FLAG_VSYNC_HINT), + ] + y_offset = 80 + for text, flag in flag_texts: + if is_window_state(flag): + draw_text(f"[{text[5:]}] {text}: on", 10, y_offset, 10, LIME) + else: + draw_text(f"[{text[5:]}] {text}: off", 10, y_offset, 10, MAROON) + y_offset += 20 + + draw_text("Following flags can only be set before window creation:", 10, 300, 10, GRAY) + if is_window_state(pyray.FLAG_WINDOW_HIGHDPI): + draw_text("FLAG_WINDOW_HIGHDPI: on", 10, 320, 10, LIME) + else: + draw_text("FLAG_WINDOW_HIGHDPI: off", 10, 320, 10, MAROON) + if is_window_state(pyray.FLAG_WINDOW_TRANSPARENT): + draw_text("FLAG_WINDOW_TRANSPARENT: on", 10, 340, 10, LIME) + else: + draw_text("FLAG_WINDOW_TRANSPARENT: off", 10, 340, 10, MAROON) + if is_window_state(pyray.FLAG_MSAA_4X_HINT): + draw_text("FLAG_MSAA_4X_HINT: on", 10, 360, 10, LIME) + else: + draw_text("FLAG_MSAA_4X_HINT: off", 10, 360, 10, MAROON) + + end_drawing() + # ----------------------------------------------------- + +# De-Initialization +# --------------------------------------------------------- +close_window() # Close window and OpenGL context +# --------------------------------------------------------- +