bunnymark.odin 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // This is a port of https://www.raylib.com/examples/textures/loader.html?name=textures_bunnymark
  2. package karl2d_bunnymark
  3. import k2 "../../.."
  4. import "core:math/rand"
  5. import "core:log"
  6. import "core:fmt"
  7. import "core:time"
  8. MAX_BUNNIES :: 50000
  9. Bunny :: struct {
  10. position: k2.Vec2,
  11. speed: k2.Vec2,
  12. rot: f32,
  13. rot_speed: f32,
  14. color: k2.Color,
  15. }
  16. bunnies: [dynamic]Bunny
  17. tex_bunny: k2.Texture
  18. init :: proc() {
  19. SCREEN_WIDTH :: 800
  20. SCREEN_HEIGHT :: 450
  21. k2.init(SCREEN_WIDTH, SCREEN_HEIGHT, "bunnymark (raylib port)", window_creation_flags = { .Resizable })
  22. tex_bunny = k2.load_texture_from_bytes(#load("wabbit_alpha.png"))
  23. }
  24. step :: proc(dt: f32) -> bool {
  25. if k2.mouse_button_is_held(.Left) {
  26. for _ in 0..<100 {
  27. append(&bunnies, Bunny {
  28. position = k2.get_mouse_position(),
  29. speed = {
  30. rand.float32_range(-250, 250)/60,
  31. rand.float32_range(-250, 250)/60,
  32. },
  33. rot_speed = rand.float32_range(-5, 5),
  34. color = {
  35. u8(rand.int_max(190) + 50),
  36. u8(rand.int_max(160) + 80),
  37. u8(rand.int_max(140) + 100),
  38. 255,
  39. },
  40. })
  41. }
  42. }
  43. for &b in bunnies {
  44. b.position += b.speed
  45. b.rot += b.rot_speed
  46. if b.position.x > f32(k2.get_screen_width()) || b.position.x < 0 {
  47. b.speed.x *= -1
  48. b.rot_speed = rand.float32_range(-5, 5)
  49. }
  50. if b.position.y > f32(k2.get_screen_height()) || b.position.y < 0 {
  51. b.speed.y *= -1
  52. b.rot_speed = rand.float32_range(-5, 5)
  53. }
  54. }
  55. k2.process_events()
  56. k2.clear(k2.RL_WHITE)
  57. src := k2.Rect {
  58. 0, 0,
  59. f32(tex_bunny.width), f32(tex_bunny.height),
  60. }
  61. for &b in bunnies {
  62. dest := src
  63. dest.x = b.position.x
  64. dest.y = b.position.y
  65. k2.draw_texture_ex(tex_bunny, src, dest, {dest.w/2, dest.h/2}, b.rot, b.color)
  66. }
  67. if k2.key_went_down(.B) {
  68. fmt.println(len(bunnies))
  69. fmt.println(1/dt)
  70. }
  71. k2.draw_text(fmt.tprintf("num bunnies: %v -- fps: %v", len(bunnies), 1/dt), {10, 20}, 40, k2.BLACK)
  72. k2.present()
  73. free_all(context.temp_allocator)
  74. return true
  75. }
  76. shutdown :: proc() {
  77. delete(bunnies)
  78. k2.destroy_texture(tex_bunny)
  79. k2.shutdown()
  80. }
  81. main :: proc() {
  82. context.logger = log.create_console_logger()
  83. init()
  84. prev_time := time.now()
  85. for !k2.shutdown_wanted() {
  86. cur_time := time.now()
  87. dt := f32(time.duration_seconds(time.diff(prev_time, cur_time)))
  88. prev_time = cur_time
  89. step(dt)
  90. }
  91. shutdown()
  92. }