| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- // This is a port of https://www.raylib.com/examples/textures/loader.html?name=textures_bunnymark
- package karl2d_bunnymark
- import k2 "../../.."
- import "core:math/rand"
- import "core:log"
- import "core:fmt"
- MAX_BUNNIES :: 50000
- Bunny :: struct {
- position: k2.Vec2,
- speed: k2.Vec2,
- rot: f32,
- rot_speed: f32,
- color: k2.Color,
- }
- bunnies: [dynamic]Bunny
- tex_bunny: k2.Texture
- init :: proc() {
- SCREEN_WIDTH :: 800
- SCREEN_HEIGHT :: 450
- k2.init(SCREEN_WIDTH, SCREEN_HEIGHT, "bunnymark (raylib port)", window_creation_flags = { .Resizable })
- tex_bunny = k2.load_texture_from_bytes(#load("wabbit_alpha.png"))
- }
- step :: proc() -> bool {
- k2.new_frame()
- if k2.mouse_button_is_held(.Left) {
- for _ in 0..<100 {
- append(&bunnies, Bunny {
- position = k2.get_mouse_position(),
- speed = {
- rand.float32_range(-250, 250)/60,
- rand.float32_range(-250, 250)/60,
- },
- rot_speed = rand.float32_range(-5, 5),
- color = {
- u8(rand.int_max(190) + 50),
- u8(rand.int_max(160) + 80),
- u8(rand.int_max(140) + 100),
- 255,
- },
- })
- }
- }
- for &b in bunnies {
- b.position += b.speed
- b.rot += b.rot_speed
- if b.position.x > f32(k2.get_screen_width()) || b.position.x < 0 {
- b.speed.x *= -1
- b.rot_speed = rand.float32_range(-5, 5)
- }
- if b.position.y > f32(k2.get_screen_height()) || b.position.y < 0 {
- b.speed.y *= -1
- b.rot_speed = rand.float32_range(-5, 5)
- }
- }
- k2.process_events()
- k2.clear(k2.RL_WHITE)
- src := k2.Rect {
- 0, 0,
- f32(tex_bunny.width), f32(tex_bunny.height),
- }
- for &b in bunnies {
- dest := src
- dest.x = b.position.x
- dest.y = b.position.y
- k2.draw_texture_ex(tex_bunny, src, dest, {dest.w/2, dest.h/2}, b.rot, b.color)
- }
- k2.draw_text(
- fmt.tprintf("num bunnies: %v -- fps: %v", len(bunnies), 1/k2.get_frame_time()),
- {10, 20},
- 40,
- k2.BLACK,
- )
- k2.present()
- free_all(context.temp_allocator)
- return !k2.shutdown_wanted()
- }
- shutdown :: proc() {
- delete(bunnies)
- k2.destroy_texture(tex_bunny)
- k2.shutdown()
- }
- main :: proc() {
- context.logger = log.create_console_logger()
- init()
- run := true
- for run {
- run = step()
- }
- shutdown()
- }
|