-
-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathtextures_bunnymark_dynamic.py
More file actions
113 lines (84 loc) · 4.57 KB
/
Copy pathtextures_bunnymark_dynamic.py
File metadata and controls
113 lines (84 loc) · 4.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# /*******************************************************************************************
# *
# * raylib [textures] example - Bunnymark
# *
# * This example has been created using raylib 1.6 (www.raylib.com)
# * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
# *
# * Copyright (c) 2014-2019 Ramon Santamaria (@raysan5)
# *
# ********************************************************************************************/
from raylib.dynamic import ffi, raylib as rl
from raylib.colors import *
MAX_BUNNIES = 500000
# 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
class Bunny:
def __init__(self):
self.position = ffi.new('struct Vector2 *', [0.0, 0.0])
self.speed = ffi.new('struct Vector2 *', [0.0, 0.0])
self.color = ffi.new('struct Color *', [0, 0, 0, 0])
# // Initialization
# //--------------------------------------------------------------------------------------
screenWidth = 1920;
screenHeight = 1080;
rl.InitWindow(screenWidth, screenHeight, b"raylib [textures] example - bunnymark")
# // Load bunny texture
texBunny = rl.LoadTexture(b"resources/wabbit_alpha.png")
bunnies = []
for i in range(0, MAX_BUNNIES):
bunnies.append(Bunny())
bunniesCount = 0; # Bunnies counter
rl.SetTargetFPS(60); # Set our game to run at 60 frames-per-second
#//--------------------------------------------------------------------------------------
#// Main game loop
while not rl.WindowShouldClose(): #// Detect window close button or ESC key
#// Update
#//----------------------------------------------------------------------------------
if rl.IsMouseButtonDown(rl.MOUSE_BUTTON_LEFT):
#// Create more bunnies
for i in range(0, 100):
if bunniesCount < MAX_BUNNIES:
bunnies[bunniesCount].position = rl.GetMousePosition()
bunnies[bunniesCount].speed.x = rl.GetRandomValue(-250, 250)/60.0
bunnies[bunniesCount].speed.y = rl.GetRandomValue(-250, 250)/60.0
bunnies[bunniesCount].color = (rl.GetRandomValue(50, 240),
rl.GetRandomValue(80, 240),
rl.GetRandomValue(100, 240), 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) > rl.GetScreenWidth()) or ((bunnies[i].position.x + texBunny.width/2) < 0):
bunnies[i].speed.x *= -1
if ((bunnies[i].position.y + texBunny.height/2) > rl.GetScreenHeight()) or ((bunnies[i].position.y + texBunny.height/2 - 40) < 0):
bunnies[i].speed.y *= -1
# //----------------------------------------------------------------------------------
#
# // Draw
# //----------------------------------------------------------------------------------
rl.BeginDrawing()
rl.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
rl.DrawTexture(texBunny, int(bunnies[i].position.x), int(bunnies[i].position.y), bunnies[i].color)
rl.DrawRectangle(0, 0, screenWidth, 40, BLACK)
text = f"bunnies {bunniesCount}"
rl.DrawText(text.encode('utf-8'), 120, 10, 20, GREEN)
text = f"batched draw calls: { 1 + int(bunniesCount/MAX_BATCH_ELEMENTS)}"
rl.DrawText(text.encode('utf-8'), 320, 10, 20, MAROON)
rl.DrawFPS(10, 10)
rl.EndDrawing()
#//----------------------------------------------------------------------------------
#// De-Initialization
#//--------------------------------------------------------------------------------------
rl.UnloadTexture(texBunny); #Unload bunny texture
rl.CloseWindow() # Close window and OpenGL context
#//--------------------------------------------------------------------------------------