minimal_web.odin 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // A small program that draws some shapes, some texts and a texture.
  2. //
  3. // This is the same as `../minimal`, but adapted to work on web. Compile the web version by being in
  4. // the examples folder (the one above this one) and run:
  5. //
  6. // odin run build_web_example -- minimal_web
  7. //
  8. // The built web application will be in minimal_web/web/build
  9. package karl2d_minimal_example_web
  10. import k2 "../.."
  11. import "core:log"
  12. import "core:fmt"
  13. import "core:math"
  14. _ :: fmt
  15. tex: k2.Texture
  16. init :: proc() {
  17. k2.init(1080, 1080, "Karl2D Minimal Program")
  18. // Note that we #load the texture: This bakes it into the program's data. WASM has no filesystem
  19. // so in order to bundle textures with your game, you need to store them somewhere it can fetch
  20. // them.
  21. tex = k2.load_texture_from_bytes(#load("../minimal/sixten.jpg"))
  22. }
  23. step :: proc() -> bool {
  24. k2.new_frame()
  25. k2.process_events()
  26. k2.clear(k2.BLUE)
  27. t := k2.get_time()
  28. pos_x := f32(math.sin(t*10)*10)
  29. rot := f32(t*50)
  30. k2.draw_texture_ex(tex, {0, 0, f32(tex.width), f32(tex.height)}, {pos_x + 400, 450, 900, 500}, {450, 250}, rot)
  31. k2.draw_rect({10, 10, 60, 60}, k2.GREEN)
  32. k2.draw_rect({20, 20, 40, 40}, k2.BLACK)
  33. k2.draw_circle({120, 40}, 30, k2.BLACK)
  34. k2.draw_circle({120, 40}, 20, k2.GREEN)
  35. k2.draw_text("Hellöpe!", {10, 100}, 48, k2.WHITE)
  36. msg1 := fmt.tprintf("Time since start: %.3f s", t)
  37. msg2 := fmt.tprintf("Last frame time: %.5f s", k2.get_frame_time())
  38. k2.draw_text(msg1, {10, 148}, 48, k2.WHITE)
  39. k2.draw_text(msg2, {10, 196}, 48, k2.WHITE)
  40. k2.present()
  41. free_all(context.temp_allocator)
  42. return !k2.shutdown_wanted()
  43. }
  44. shutdown :: proc() {
  45. k2.destroy_texture(tex)
  46. k2.shutdown()
  47. }
  48. // This is not run by the web version, but it makes this program also work on non-web!
  49. main :: proc() {
  50. context.logger = log.create_console_logger()
  51. init()
  52. run := true
  53. for run {
  54. run = step()
  55. }
  56. shutdown()
  57. }