minimal_web.odin 1.9 KB

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