Skip to content

Commit

Permalink
[General] added bunnymark
Browse files Browse the repository at this point in the history
  • Loading branch information
Dacode45 committed Sep 10, 2020
1 parent 09ad1fd commit f302516
Show file tree
Hide file tree
Showing 6 changed files with 217 additions and 69 deletions.
1 change: 1 addition & 0 deletions showcase/src/example/core/core_2d_camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub fn run(rl: &mut RaylibHandle, thread: &RaylibThread) -> crate::SampleOut {
//--------------------------------------------------------------------------------------
let screen_width = 800;
let screen_height = 450;
rl.set_window_size(screen_width, screen_height);
rl.set_window_title(thread, "raylib [core] example - 2d camera");

let mut player = Rectangle::new(400.0, 280.0, 40.0, 40.0);
Expand Down
1 change: 1 addition & 0 deletions showcase/src/example/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ pub mod controls_test_suite;
pub mod core;
pub mod models;
pub mod others;
pub mod textures;
143 changes: 74 additions & 69 deletions showcase/src/example/models/models_material_pbr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,81 +113,86 @@ pub fn run(rl: &mut RaylibHandle, thread: &RaylibThread)-> crate::SampleOut {
.set_shader_value(loc, camera_pos);
//----------------------------------------------------------------------------------

// Draw
//----------------------------------------------------------------------------------
let mut d = rl.begin_drawing(thread);

d.clear_background(Color::RAYWHITE);

{
let mut d = d.begin_mode3D(&camera);

d.draw_model(&mut model, Vector3::zero(), 1.0, Color::WHITE);

d.draw_grid(10, 1.0);
// Draw
//----------------------------------------------------------------------------------
let mut d = rl.begin_drawing(thread);

d.clear_background(Color::RAYWHITE);

{
let mut d = d.begin_mode3D(&camera);

d.draw_model(&mut model, Vector3::zero(), 1.0, Color::WHITE);

d.draw_grid(10, 1.0);
}

d.draw_fps(10, 10);
}

d.draw_fps(10, 10);

//----------------------------------------------------------------------------------
//---------------------------------------------------------------------------------
if rl.is_key_pressed(raylib::consts::KeyboardKey::KEY_ESCAPE) {
// De-Initialization
//--------------------------------------------------------------------------------------
// Shaders and textures must be unloaded by user,
// they could be in use by other models
use raylib::consts::MaterialMapType::*;
unsafe {
rl.unload_texture(
thread,
model.materials()[0].maps()[MAP_ALBEDO as usize]
.texture()
.clone(),
);
rl.unload_texture(
thread,
model.materials()[0].maps()[MAP_NORMAL as usize]
.texture()
.clone(),
);
rl.unload_texture(
thread,
model.materials()[0].maps()[MAP_METALNESS as usize]
.texture()
.clone(),
);
rl.unload_texture(
thread,
model.materials()[0].maps()[MAP_ROUGHNESS as usize]
.texture()
.clone(),
);
rl.unload_texture(
thread,
model.materials()[0].maps()[MAP_OCCLUSION as usize]
.texture()
.clone(),
);
rl.unload_texture(
thread,
model.materials()[0].maps()[MAP_IRRADIANCE as usize]
.texture()
.clone(),
);
rl.unload_texture(
thread,
model.materials()[0].maps()[MAP_PREFILTER as usize]
.texture()
.clone(),
);
rl.unload_texture(
thread,
model.materials()[0].maps()[MAP_BRDF as usize]
.texture()
.clone(),
);
}
}
});

// De-Initialization
//--------------------------------------------------------------------------------------

// // Shaders and textures must be unloaded by user,
// // they could be in use by other models
// use raylib::consts::MaterialMapType::*;
// unsafe {
// rl.unload_texture(
// thread,
// model.materials()[0].maps()[MAP_ALBEDO as usize]
// .texture()
// .clone(),
// );
// rl.unload_texture(
// thread,
// model.materials()[0].maps()[MAP_NORMAL as usize]
// .texture()
// .clone(),
// );
// rl.unload_texture(
// thread,
// model.materials()[0].maps()[MAP_METALNESS as usize]
// .texture()
// .clone(),
// );
// rl.unload_texture(
// thread,
// model.materials()[0].maps()[MAP_ROUGHNESS as usize]
// .texture()
// .clone(),
// );
// rl.unload_texture(
// thread,
// model.materials()[0].maps()[MAP_OCCLUSION as usize]
// .texture()
// .clone(),
// );
// rl.unload_texture(
// thread,
// model.materials()[0].maps()[MAP_IRRADIANCE as usize]
// .texture()
// .clone(),
// );
// rl.unload_texture(
// thread,
// model.materials()[0].maps()[MAP_PREFILTER as usize]
// .texture()
// .clone(),
// );
// rl.unload_texture(
// thread,
// model.materials()[0].maps()[MAP_BRDF as usize]
// .texture()
// .clone(),
// );
// }


//--------------------------------------------------------------------------------------
}
Expand Down
1 change: 1 addition & 0 deletions showcase/src/example/textures/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod textures_bunnymark;
136 changes: 136 additions & 0 deletions showcase/src/example/textures/textures_bunnymark.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/*******************************************************************************************
*
* 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)
*
********************************************************************************************/

use raylib::prelude::*;

// 50K bunnies limit
const MAX_BUNNIES: usize = 50000;

// This is the maximum amount of elements (quads) per batch
// NOTE: This value is defined in [rlgl] module and can be changed there
const MAX_BATCH_ELEMENTS: usize = 8192;

#[derive(Default, Clone)]
struct Bunny {
position: Vector2,
speed: Vector2,
color: Color,
}

pub fn run(rl: &mut RaylibHandle, thread: &RaylibThread) -> crate::SampleOut {
// Initialization
//--------------------------------------------------------------------------------------
let screen_width = 800;
let screen_height = 450;

rl.set_window_size(screen_width, screen_height);
rl.set_window_title(thread, "raylib [textures] example - bunnymark");

// Load bunny texture
let tex_bunny = rl
.load_texture(thread, "original/textures/resources/wabbit_alpha.png")
.expect("texture missing: are you in the right directory?");
let mut bunnies = vec![Bunny::default(); MAX_BUNNIES];

let mut bunniesCount = 0; // Bunnies counter

rl.set_target_fps(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------

// Main game loop
return Box::new(move |rl: &mut RaylibHandle, thread: &RaylibThread| -> () {
use raylib::consts::MouseButton::*;
// Update
//----------------------------------------------------------------------------------
if rl.is_mouse_button_down(MOUSE_LEFT_BUTTON) {
// Create more bunnies
for _ in 0..100 {
if bunniesCount < MAX_BUNNIES {
bunnies[bunniesCount].position = rl.get_mouse_position();
bunnies[bunniesCount].speed.x =
get_random_value::<i32>(-250, 250) as f32 / 60.0;
bunnies[bunniesCount].speed.y =
get_random_value::<i32>(-250, 250) as f32 / 60.0;
bunnies[bunniesCount].color = Color::new(
get_random_value::<i32>(50, 240) as u8,
get_random_value::<i32>(80, 240) as u8,
get_random_value::<i32>(100, 240) as u8,
255,
);
bunniesCount += 1;
}
}
}

// Update bunnies
for i in 0..bunniesCount {
bunnies[i].position.x += bunnies[i].speed.x;
bunnies[i].position.y += bunnies[i].speed.y;

if (bunnies[i].position.x + tex_bunny.width as f32 / 2.0) > rl.get_screen_width() as f32
|| (bunnies[i].position.x + tex_bunny.width as f32 / 2.0) < 0.0
{
bunnies[i].speed.x *= -1.0;
}
if (bunnies[i].position.y + tex_bunny.height as f32 / 2.0)
> rl.get_screen_height() as f32
|| (bunnies[i].position.y + tex_bunny.height as f32 / 2.0 - 40.0) < 0.0
{
bunnies[i].speed.y *= -1.0;
}
}
//----------------------------------------------------------------------------------

// Draw
//----------------------------------------------------------------------------------
let mut d = rl.begin_drawing(thread);

d.clear_background(Color::RAYWHITE);

for i in 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
d.draw_texture(
&tex_bunny,
bunnies[i].position.x as i32,
bunnies[i].position.y as i32,
bunnies[i].color,
);
}

d.draw_rectangle(0, 0, screen_width, 40, Color::BLACK);
d.draw_text(
&format!("bunnies: {}", bunniesCount),
120,
10,
20,
Color::GREEN,
);
d.draw_text(
&format!(
"batched draw calls: {}",
1 + bunniesCount / MAX_BATCH_ELEMENTS,
),
320,
10,
20,
Color::MAROON,
);

d.draw_fps(10, 10);

//----------------------------------------------------------------------------------
});
}
4 changes: 4 additions & 0 deletions showcase/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ fn main() {
rstr!("rlgl standalone"),
example::others::rlgl_standalone::run,
),
(
rstr!("raylib [textures] example - bunnymark"),
example::textures::textures_bunnymark::run,
),
];
let mut sample = None;
let mut listViewActive = -1;
Expand Down

0 comments on commit f302516

Please sign in to comment.