minimal_web.odin 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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.LIGHT_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.LIGHT_GREEN)
  36. k2.draw_circle({120, 40}, 30, k2.DARK_RED)
  37. k2.draw_circle({120, 40}, 20, k2.RED)
  38. k2.draw_rect({4, 95, 512, 152}, k2.color_alpha(k2.DARK_GRAY, 192))
  39. k2.draw_text("Hellöpe!", {10, 100}, 48, k2.LIGHT_RED)
  40. msg1 := fmt.tprintf("Time since start: %.3f s", t)
  41. msg2 := fmt.tprintf("Last frame time: %.5f s", k2.get_frame_time())
  42. k2.draw_text(msg1, {10, 148}, 48, k2.ORANGE)
  43. k2.draw_text(msg2, {10, 196}, 48, k2.LIGHT_PURPLE)
  44. k2.present()
  45. free_all(context.temp_allocator)
  46. return !k2.shutdown_wanted()
  47. }
  48. shutdown :: proc() {
  49. k2.destroy_texture(tex)
  50. k2.shutdown()
  51. }
  52. // This is not run by the web version, but it makes this program also work on non-web!
  53. main :: proc() {
  54. context.logger = log.create_console_logger()
  55. init()
  56. run := true
  57. for run {
  58. run = step()
  59. }
  60. shutdown()
  61. }