bunnymark.odin 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. MAX_BUNNIES :: 50000
  8. Bunny :: struct {
  9. position: k2.Vec2,
  10. speed: k2.Vec2,
  11. rot: f32,
  12. rot_speed: f32,
  13. color: k2.Color,
  14. }
  15. bunnies: [dynamic]Bunny
  16. tex_bunny: k2.Texture
  17. init :: proc() {
  18. SCREEN_WIDTH :: 800
  19. SCREEN_HEIGHT :: 450
  20. k2.init(SCREEN_WIDTH, SCREEN_HEIGHT, "bunnymark (raylib port)", window_creation_flags = { .Resizable })
  21. tex_bunny = k2.load_texture_from_bytes(#load("wabbit_alpha.png"))
  22. }
  23. step :: proc() -> bool {
  24. k2.new_frame()
  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. k2.draw_text(
  68. fmt.tprintf("num bunnies: %v -- fps: %v", len(bunnies), 1/k2.get_frame_time()),
  69. {10, 20},
  70. 40,
  71. k2.BLACK,
  72. )
  73. k2.present()
  74. free_all(context.temp_allocator)
  75. return !k2.shutdown_wanted()
  76. }
  77. shutdown :: proc() {
  78. delete(bunnies)
  79. k2.destroy_texture(tex_bunny)
  80. k2.shutdown()
  81. }
  82. main :: proc() {
  83. context.logger = log.create_console_logger()
  84. init()
  85. run := true
  86. for run {
  87. run = step()
  88. }
  89. shutdown()
  90. }