minimal_web.odin 2.0 KB

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