bunnymark.odin 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. main :: proc() {
  17. context.logger = log.create_console_logger()
  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_file("wabbit_alpha.png")
  22. bunnies: [dynamic]Bunny
  23. prev_time := time.now()
  24. for !k2.shutdown_wanted() {
  25. cur_time := time.now()
  26. dt := f32(time.duration_seconds(time.diff(prev_time, cur_time)))
  27. prev_time = cur_time
  28. if k2.mouse_button_is_held(.Left) {
  29. for _ in 0..<100 {
  30. append(&bunnies, Bunny {
  31. position = k2.get_mouse_position(),
  32. speed = {
  33. rand.float32_range(-250, 250)/60,
  34. rand.float32_range(-250, 250)/60,
  35. },
  36. rot_speed = rand.float32_range(-5, 5),
  37. color = {
  38. u8(rand.int_max(190) + 50),
  39. u8(rand.int_max(160) + 80),
  40. u8(rand.int_max(140) + 100),
  41. 255,
  42. },
  43. })
  44. }
  45. }
  46. for &b in bunnies {
  47. b.position += b.speed
  48. b.rot += b.rot_speed
  49. if b.position.x > f32(k2.get_screen_width()) || b.position.x < 0 {
  50. b.speed.x *= -1
  51. b.rot_speed = rand.float32_range(-5, 5)
  52. }
  53. if b.position.y > f32(k2.get_screen_height()) || b.position.y < 0 {
  54. b.speed.y *= -1
  55. b.rot_speed = rand.float32_range(-5, 5)
  56. }
  57. }
  58. k2.process_events()
  59. k2.clear(k2.RL_WHITE)
  60. src := k2.Rect {
  61. 0, 0,
  62. f32(tex_bunny.width), f32(tex_bunny.height),
  63. }
  64. for &b in bunnies {
  65. dest := src
  66. dest.x = b.position.x
  67. dest.y = b.position.y
  68. k2.draw_texture_ex(tex_bunny, src, dest, {dest.w/2, dest.h/2}, b.rot, b.color)
  69. }
  70. if k2.key_went_down(.B) {
  71. fmt.println(len(bunnies))
  72. fmt.println(1/dt)
  73. }
  74. k2.present()
  75. }
  76. delete(bunnies)
  77. k2.destroy_texture(tex_bunny)
  78. k2.shutdown()
  79. }