aboutsummaryrefslogtreecommitdiff
path: root/stub/pico8.wat
blob: aaa4461247faeb3b3da008515aed66b535731b33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
(module
  (import "env" "memory" (memory 4))
  (import "env" "_emscripten_set_main_loop"
    (func $emscripten_set_main_loop (param i32 i32 i32)))
  (import "env" "_SDL_SetVideoMode"
    (func $SDL_SetVideoMode (param i32 i32 i32 i32) (result i32)))
  (import "env" "_SDL_GetMouseState"
    (func $SDL_GetMouseState (param i32 i32) (result i32)))
  (import "env" "_SDL_LockSurface"
    (func $SDL_LockSurface (param i32) (result i32)))
  (import "env" "_SDL_UnlockSurface"
    (func $SDL_UnlockSurface (param i32)))

  (func (export "___emscripten_environ_constructor"))

  (global $ptr (mut i32) (i32.const 65536))
  (func $alloc (export "stackAlloc") (export "_malloc")
    (param $size i32) (result i32)
    (local $ret i32)
    (local.set $ret (global.get $ptr))
    (global.set $ptr
      (i32.and
        (i32.add
          (i32.add
            (global.get $ptr)
            (local.get $size))
          (i32.const 15))
        (i32.const -16)))
    (local.get $ret))

  (global $surface (mut i32) (i32.const 0))
  (global $mouse (mut i32) (i32.const 0))

  (func (export "_main") (param i32 i32) (result i32)
    (global.set $surface
      (call $SDL_SetVideoMode
        (i32.const 128)
        (i32.const 128)
        (i32.const 32)
        (i32.const 0)))
    (global.set $mouse
      (call $alloc (i32.const 8)))
    (call $emscripten_set_main_loop
      (i32.const 1)
      (i32.const 0)
      (i32.const 0))
    (i32.const 0))

  (func (export "dynCall_v") (param i32)
    (local $pixels i32)
    (local $w i32)
    (local $h i32)
    (local $pitch i32)
    (local $mx i32)
    (local $my i32)
    (local $buttons i32)
    (local $x i32)
    (local $y i32)
    (local $addr i32)
    (local $color i32)

    (local.set $buttons
      (call $SDL_GetMouseState
        (global.get $mouse)
        (i32.add (global.get $mouse) (i32.const 4))))
    (local.set $mx (i32.load (global.get $mouse)))
    (local.set $my (i32.load (i32.add (global.get $mouse) (i32.const 4))))

    (drop
      (call $SDL_LockSurface (global.get $surface)))

    (local.set $w (i32.load (i32.add (global.get $surface) (i32.const 8))))
    (local.set $h (i32.load (i32.add (global.get $surface) (i32.const 12))))
    (local.set $pitch (i32.load (i32.add (global.get $surface) (i32.const 16))))
    (local.set $pixels (i32.load (i32.add (global.get $surface) (i32.const 20))))

    (local.set $y (i32.const 0))
    (block $y_done
      (loop $y_loop
        (br_if $y_done (i32.ge_s (local.get $y) (local.get $h)))
        (local.set $x (i32.const 0))
        (block $x_done
          (loop $x_loop
            (br_if $x_done (i32.ge_s (local.get $x) (local.get $w)))
            (local.set $addr
              (i32.add
                (local.get $pixels)
                (i32.add
                  (i32.mul (local.get $y) (local.get $pitch))
                  (i32.shl (local.get $x) (i32.const 2)))))
            (if
              (i32.or
                (i32.eq (local.get $x) (local.get $mx))
                (i32.eq (local.get $y) (local.get $my)))
              (then
                (local.set $color (i32.const 0xff000000))
                (if (i32.and (local.get $buttons) (i32.const 1))
                  (then
                    (local.set $color
                      (i32.or (local.get $color) (i32.const 0x000000ff)))))
                (if (i32.and (local.get $buttons) (i32.const 2))
                  (then
                    (local.set $color
                      (i32.or (local.get $color) (i32.const 0x0000ff00)))))
                (if (i32.and (local.get $buttons) (i32.const 4))
                  (then
                    (local.set $color
                      (i32.or (local.get $color) (i32.const 0x00ff0000)))))
              )
              (else
                (local.set $color (i32.const 0xff532b1d))))
            (i32.store (local.get $addr) (local.get $color))
            (local.set $x (i32.add (local.get $x) (i32.const 1)))
            (br $x_loop)))
        (local.set $y (i32.add (local.get $y) (i32.const 1)))
        (br $y_loop)))

    (call $SDL_UnlockSurface (global.get $surface)))

  (func (export "_mix_0") (param $a i32) (param $b i32) (result i32)
    (i32.mul
      (local.get $a)
      (local.get $b)))
)