From c37b16a5a7527eb9915f7de415a29103d0f4414a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D7=93=D7=95=D7=A8=20=D7=A9=D7=A4=D7=99=D7=A8=D7=90?= Date: Sat, 24 Sep 2022 15:35:02 +0300 Subject: [PATCH] making textures_bunnymark_more_pythonic.py stand for the Python API conventions. and some more fixes --- examples/shapes/shapes_following_eyes.py | 9 +- .../textures_bunnymark_more_pythonic.py | 200 +++++++++--------- 2 files changed, 101 insertions(+), 108 deletions(-) diff --git a/examples/shapes/shapes_following_eyes.py b/examples/shapes/shapes_following_eyes.py index 7ed2138..374b513 100644 --- a/examples/shapes/shapes_following_eyes.py +++ b/examples/shapes/shapes_following_eyes.py @@ -7,13 +7,6 @@ raylib [shapes] example - Following Eyes # Import # ------------------------------------------------------------------------------------ from pyray import * -from raylib.colors import ( - RAYWHITE, - BROWN, - BLACK, - LIGHTGRAY, - DARKGREEN, -) from math import ( atan2, cos, @@ -43,7 +36,7 @@ def main(): angle = 0.0 dx, dy, dxx, dyy = 0.0, 0.0, 0.0, 0.0 - set_target_fps(60) + set_target_fps(60) # Set our game to run at 60 frames-per-second # ---------------------------------------------------------------------------------- # Main game loop diff --git a/examples/textures/textures_bunnymark_more_pythonic.py b/examples/textures/textures_bunnymark_more_pythonic.py index 19768af..6655a6b 100644 --- a/examples/textures/textures_bunnymark_more_pythonic.py +++ b/examples/textures/textures_bunnymark_more_pythonic.py @@ -1,110 +1,110 @@ -# Dont use C data structures when we can avoid it. Makes Pypy slightly faster. +""" -from raylib import * -import random +raylib [textures] example - Bunnymark -MAX_BUNNIES = 500000 +""" +# Import +# ------------------------------------------------------------------------------------ +from pyray import * +# ------------------------------------------------------------------------------------ + +# Definitions +# ------------------------------------------------------------------------------------ +MAX_BUNNIES = 50000 # This is the maximum amount of elements (quads) per batch # NOTE: This value is defined in [rlgl] module and can be changed there -MAX_BATCH_ELEMENTS = 8192 - +MAX_BATCH_ELEMENTS = 8192 class Bunny: def __init__(self): - self.position_x = 0.0 - self.position_y = 0.0 - self.speed_x = 0.0 - self.speed_y = 0.0 - self.color_r = 0 - self.color_g = 0 - self.color_b = 0 - self.color_a = 0 + self.position = Vector2(0, 0) + self.speed = Vector2(0, 0) + self.color = Color(0, 0, 0, 255) +# ------------------------------------------------------------------------------------ + +# ------------------------------------------------------------------------------------ +# Program main entry point +# ------------------------------------------------------------------------------------ +def main(): + # Initialization + # ---------------------------------------------------------------------------------- + SCREEN_WIDTH = 800 + SCREEN_HEIGHT = 450 + + init_window(SCREEN_WIDTH, SCREEN_HEIGHT, "raylib [textures] example - bunnymark") + + # Load bunny texture + tex_bunny = load_texture("resources/wabbit_alpha.png") + + bunnies = [] + for i in range(0, MAX_BUNNIES): + bunnies.append(Bunny()) + + bunnies_count = 0 # Bunnies counter + + set_target_fps(60) # Set our game to run at 60 frames-per-second + # ---------------------------------------------------------------------------------- + + # Main game loop + while not window_should_close(): # Detect window close button or ESC key + # Update + # ---------------------------------------------------------------------------------- + if is_mouse_button_down(MouseButton.MOUSE_BUTTON_LEFT): + # Create more bunnies + for i in range(0, 100): + if bunnies_count < MAX_BUNNIES: + bunnies[bunnies_count].position = get_mouse_position() + bunnies[bunnies_count].speed.x = get_random_value(-250, 250) / 60.0 + bunnies[bunnies_count].speed.y = get_random_value(-250, 250) / 60.0 + bunnies[bunnies_count].color = Color(get_random_value(50, 240), get_random_value(80, 240), get_random_value(100, 240), 255) + bunnies_count += 1 + + # Update bunnies + for i in range(0, bunnies_count): + bunnies[i].position.x += bunnies[i].speed.x + bunnies[i].position.y += bunnies[i].speed.y + + if bunnies[i].position.x + tex_bunny.width / 2 > SCREEN_WIDTH or bunnies[i].position.x + tex_bunny.width / 2 < 0: + bunnies[i].speed.x *= -1 + if bunnies[i].position.y + tex_bunny.height / 2 > SCREEN_HEIGHT or bunnies[i].position.y + tex_bunny.height / 2 - 40 < 0: + bunnies[i].speed.y *= -1 + # ---------------------------------------------------------------------------------- + + # Draw + # ---------------------------------------------------------------------------------- + begin_drawing() + + clear_background(RAYWHITE) + + for i in range(0, bunnies_count): + # NOTE: When internal batch buffer limit is reached (MAX_BATCH_ELEMENTS), + # a draw call is launched and buffer starts being filled again; + # before issuing a draw call, updated vertex data from internal CPU buffer is send to GPU... + # Process of sending data is costly, and it could happen that GPU data has not been completely + # processed for drawing while new data is tried to be sent (updating current in-use buffers) + # it could generate a stall and consequently a frame drop, limiting the number of drawn bunnies + draw_texture(tex_bunny, int(bunnies[i].position.x), int(bunnies[i].position.y), bunnies[i].color) + + draw_rectangle(0, 0, SCREEN_WIDTH, 40, BLACK) + text = f"bunnies {bunnies_count}" + draw_text(text.encode('utf-8'), 120, 10, 20, GREEN) + text = f"batched draw calls: {1 + int(bunnies_count / MAX_BATCH_ELEMENTS)}" + draw_text(text.encode('utf-8'), 320, 10, 20, MAROON) + + draw_fps(10, 10) + + end_drawing() + # ---------------------------------------------------------------------------------- + + # De-Initialization + # ---------------------------------------------------------------------------------- + unload_texture(tex_bunny) # Unload bunny texture + + close_window() # Close window and OpenGL context + # ---------------------------------------------------------------------------------- -# // Initialization -# //-------------------------------------------------------------------------------------- -screenWidth = 1920; -screenHeight = 1080; - -InitWindow(screenWidth, screenHeight, b"raylib [textures] example - bunnymark") - -# // Load bunny texture -texBunny = LoadTexture(b"resources/wabbit_alpha.png") - -bunnies = [] -for i in range(0, MAX_BUNNIES): - bunnies.append(Bunny()) - -bunniesCount = 0; # Bunnies counter - -SetTargetFPS(60); # Set our game to run at 60 frames-per-second -#//-------------------------------------------------------------------------------------- - -#// Main game loop -while not WindowShouldClose(): #// Detect window close button or ESC key - #// Update - #//---------------------------------------------------------------------------------- - if IsMouseButtonDown(MOUSE_BUTTON_LEFT): - #// Create more bunnies - for i in range(0, 100): - if bunniesCount < MAX_BUNNIES: - bunnies[bunniesCount].position_x = GetMousePosition().x - bunnies[bunniesCount].position_y = GetMousePosition().y - bunnies[bunniesCount].speed_x = random.randint(-250, 250)/60.0 - bunnies[bunniesCount].speed_y = random.randint(-250, 250)/60.0 - bunnies[bunniesCount].color_r = random.randint(50,240) - bunnies[bunniesCount].color_g = random.randint(80, 240) - bunnies[bunniesCount].color_b = random.randint(100, 240) - bunnies[bunniesCount].color_a = 255 - bunniesCount+=1 - - - # // Update bunnies - for i in range(0, bunniesCount): - bunnies[i].position_x += bunnies[i].speed_x - bunnies[i].position_y += bunnies[i].speed_y - - if ((bunnies[i].position_x + texBunny.width/2) > GetScreenWidth()) or ((bunnies[i].position_x + texBunny.width/2) < 0): - bunnies[i].speed_x *= -1 - if ((bunnies[i].position_y + texBunny.height/2) > GetScreenHeight()) or ((bunnies[i].position_y + texBunny.height/2 - 40) < 0): - bunnies[i].speed_y *= -1 - - # //---------------------------------------------------------------------------------- - # - # // Draw - # //---------------------------------------------------------------------------------- - BeginDrawing() - - ClearBackground(RAYWHITE) - - for i in range(0, bunniesCount): - # // NOTE: When internal batch buffer limit is reached (MAX_BATCH_ELEMENTS), - # // a draw call is launched and buffer starts being filled again; - # // before issuing a draw call, updated vertex data from internal CPU buffer is send to GPU... - # // Process of sending data is costly and it could happen that GPU data has not been completely - # // processed for drawing while new data is tried to be sent (updating current in-use buffers) - # // it could generates a stall and consequently a frame drop, limiting the number of drawn bunnies - DrawTexture(texBunny, int(bunnies[i].position_x), int(bunnies[i].position_y), (bunnies[i].color_r,bunnies[i].color_g,bunnies[i].color_b,bunnies[i].color_a)) - - DrawRectangle(0, 0, screenWidth, 40, BLACK) - text = f"bunnies {bunniesCount}" - DrawText(text.encode('utf-8'), 120, 10, 20, GREEN) - text = f"batched draw calls: { 1 + int(bunniesCount/MAX_BATCH_ELEMENTS)}" - DrawText(text.encode('utf-8'), 320, 10, 20, MAROON) - - DrawFPS(10, 10) - - EndDrawing() - #//---------------------------------------------------------------------------------- - - -#// De-Initialization -#//-------------------------------------------------------------------------------------- - - -UnloadTexture(texBunny); #Unload bunny texture - -CloseWindow() # Close window and OpenGL context -#//-------------------------------------------------------------------------------------- - +# Execute the main function +if __name__ == '__main__': + main()