raylib/ports/textures/textures_bunnymark
Licence This example has its own licence, rather than Odin's: raylib/ports/LICENSE
These are ported Raylib examples, and as such, they are subject to Raylib's license.
Copyright (c) 2013-2024 Ramon Santamaria (@raysan5)
The rest of the licence
This software is provided "as-is", without any express or implied warranty. In no event
will the authors be held liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose, including commercial
applications, and to alter it and redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not claim that you
wrote the original software. If you use this software in a product, an acknowledgment
in the product documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be misrepresented
as being the original software.
3. This notice may not be removed or altered from any source distribution.
Code
textures_bunnymark.odin ¶117 linesSource
1/*******************************************************************************************
2*
3* raylib [textures] example - bunnymark
4*
5* Example complexity rating: [★★★☆] 3/4
6*
7* Example originally created with raylib 1.6, last time updated with raylib 2.5
8*
9* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
10* BSD-like license that allows static linking with closed source software
11*
12* Copyright (c) 2014-2025 Ramon Santamaria (@raysan5)
13*
14********************************************************************************************/
15
16package raylib_examples
17
18import rl "vendor:raylib"
19
20//----------------------------------------------------------------------------------
21// Types and Structures Definition
22//----------------------------------------------------------------------------------
23Bunny :: struct {
24 position: rl.Vector2,
25 speed: rl.Vector2,
26 color: rl.Color,
27}
28
29//------------------------------------------------------------------------------------
30// Program main entry point
31//------------------------------------------------------------------------------------
32main :: proc() {
33 // Initialization
34 //--------------------------------------------------------------------------------------
35 SCREEN_WIDTH :: 800
36 SCREEN_HEIGHT :: 450
37
38 MAX_BUNNIES :: 50000 // 50K bunnies limit
39
40 // This is the maximum amount of elements (quads) per batch
41 // NOTE: This value is defined in [rlgl] module and can be changed there
42 MAX_BATCH_ELEMENTS :: 8192
43
44 rl.InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "raylib [textures] example - bunnymark")
45
46 // Load bunny texture
47 tex_bunny := rl.LoadTexture("resources/wabbit_alpha.png")
48
49 bunnies: [dynamic]Bunny // Bunnies array
50 //--------------------------------------------------------------------------------------
51
52 // Main game loop
53 for !rl.WindowShouldClose() { // Detect window close button or ESC key
54 // Update
55 //----------------------------------------------------------------------------------
56 if rl.IsMouseButtonDown(.LEFT) {
57 for i := 0; i < 100; i += 1 {
58 if len(bunnies) < MAX_BUNNIES {
59 bunny: Bunny
60 bunny.position = rl.GetMousePosition()
61 bunny.speed.x = f32(rl.GetRandomValue(-250, 250))/60
62 bunny.speed.y = f32(rl.GetRandomValue(-250, 250))/60
63 bunny.color = {u8(rl.GetRandomValue(50, 240)),
64 u8(rl.GetRandomValue(80, 240)),
65 u8(rl.GetRandomValue(100, 240)), 255}
66
67 append(&bunnies, bunny)
68 }
69 }
70 }
71
72 // Update bunnies
73 for &bunny in bunnies {
74 bunny.position += bunny.speed * rl.GetFrameTime() * 30
75
76 if bunny.position.x + f32(tex_bunny.width)/2 > f32(rl.GetScreenWidth()) ||
77 bunny.position.x + f32(tex_bunny.width)/2 < 0 {
78 bunny.speed.x *= -1
79 }
80 if bunny.position.y + f32(tex_bunny.height/2) > f32(rl.GetScreenHeight()) ||
81 bunny.position.y + f32(tex_bunny.height)/2 - 40 < 0 {
82 bunny.speed.y *= -1
83 }
84 }
85 //----------------------------------------------------------------------------------
86
87 // Draw
88 //----------------------------------------------------------------------------------
89 rl.BeginDrawing()
90
91 rl.ClearBackground(rl.RAYWHITE)
92
93 for bunny in bunnies {
94 rl.DrawTexture(tex_bunny, i32(bunny.position.x), i32(bunny.position.y), bunny.color)
95 }
96
97 rl.DrawRectangle(0, 0, SCREEN_WIDTH, 40, rl.BLACK)
98
99 bunnies_count := len(bunnies)
100 rl.DrawText(rl.TextFormat("bunnies: %i", bunnies_count), 120, 10, 20, rl.GREEN)
101 rl.DrawText(rl.TextFormat("batched draw calls: %i", 1 + bunnies_count/MAX_BATCH_ELEMENTS), 320, 10, 20, rl.MAROON)
102
103 rl.DrawFPS(10, 10)
104
105 rl.EndDrawing()
106 //----------------------------------------------------------------------------------
107 }
108
109 // De-Initialization
110 //--------------------------------------------------------------------------------------
111 delete(bunnies) // Unload bunnies data array
112
113 rl.UnloadTexture(tex_bunny) // Unload bunny texture
114
115 rl.CloseWindow() // Close window and OpenGL context
116 //--------------------------------------------------------------------------------------
117}
Assets 1
Declarations Used 25
- vendor:raylib BeginDrawing · BLACK · ClearBackground · CloseWindow · Color · DrawFPS · DrawRectangle · DrawText · DrawTexture · EndDrawing · GetFrameTime · GetMousePosition · GetRandomValue · GetScreenHeight · GetScreenWidth · GREEN · InitWindow · IsMouseButtonDown · LoadTexture · MAROON · RAYWHITE · TextFormat · UnloadTexture · Vector2 · WindowShouldClose