JS.zig 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. const utils = @import("utils.zig");
  2. const Ball2D = utils.Ball2D;
  3. const RGBColor = utils.RGBColor;
  4. const Vector2D = utils.Vector2D;
  5. extern fn jsRandom() f32;
  6. extern fn jsClearRectangle(x: f32, y: f32, width: f32, height: f32) void;
  7. extern fn jsDrawCircle(x: f32, y: f32, radius: f32, r: u8, g: u8, b: u8, a: f32) void;
  8. extern fn jsDrawRectangle(x: f32, y: f32, width: f32, height: f32, r: u8, g: u8, b: u8, a: f32) void;
  9. extern fn jsUpdateScore(score: u32) void;
  10. // Lots of imported functions that log to console. See related comment in script.js.
  11. extern fn jsConsoleLogu32(n: u32) void;
  12. extern fn jsConsoleLogf32(n: f32) void;
  13. extern fn jsConsoleLogbool(b: bool) void;
  14. extern fn jsConsoleLogVector2D(x: f32, y: f32) void;
  15. pub inline fn random() f32 {
  16. return jsRandom();
  17. }
  18. pub inline fn clearRectangle(pos: Vector2D, width: f32, height: f32) void {
  19. jsClearRectangle(pos.x, pos.y, width, height);
  20. }
  21. pub inline fn drawBall2D(ball: Ball2D) void {
  22. const p = ball.pos;
  23. const r = ball.radius;
  24. const c = ball.color;
  25. jsDrawCircle(p.x, p.y, r, c.r, c.g, c.b, c.a);
  26. }
  27. pub inline fn drawRectangle(pos: Vector2D, width: f32, height: f32, color: RGBColor) void {
  28. jsDrawRectangle(pos.x, pos.y, width, height, color.r, color.g, color.b, color.a);
  29. }
  30. pub inline fn updateScore(score: u32) void {
  31. jsUpdateScore(score);
  32. }
  33. // TODO: Write a wasm Writer.
  34. pub fn consoleLog(comptime T: type, val: T) void {
  35. switch (T) {
  36. u32 => {
  37. jsConsoleLogu32(val);
  38. },
  39. f32 => {
  40. jsConsoleLogf32(val);
  41. },
  42. bool => {
  43. jsConsoleLogbool(val);
  44. },
  45. Vector2D => {
  46. jsConsoleLogVector2D(val.x, val.y);
  47. },
  48. else => {
  49. @compileError("consoleLog does not support the given type");
  50. },
  51. }
  52. }