Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
zaki-yama committed Jul 20, 2021
1 parent f6ecf36 commit bd9c48f
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
7 changes: 7 additions & 0 deletions the-art-of-webassembly/ch08/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
- [Setting a Pixel Color](#setting-a-pixel-color)
- [Drawing the Object](#drawing-the-object)
- [Setting and Getting Object Attributes](#setting-and-getting-object-attributes)
- [The $main Function](#the-main-function)

<!-- /TOC -->

Expand Down Expand Up @@ -75,3 +76,9 @@
- attribute の get と set でアドレスの計算は共通化できる (DRY) けど、しばしばパフォーマンス低下につながることがあるのでやらない
- また、この後の Chapter でさらに DRY をやめることになる
- 他のアセンブリ言語においては、マクロは DRY 原則とパフォーマンスを両立させるためのすばらしい方法だが、wat2wasm は現在マクロをサポートしていない

### The $main Function

- 毎フレーム JS から呼ばれる処理
- オブジェクトを velocity に基づいて動かし、衝突判定し、色を塗る
Defining Local Variables
97 changes: 97 additions & 0 deletions the-art-of-webassembly/ch08/collide.wat
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,101 @@
i32.load ;; load the pointer above
;; returns the attribute
)

;; move and detect collisions between all of the objects in our app
(func $main (export "main")
(local $i i32) ;; outer loop index
(local $j i32) ;; inner loop index
(local $outer_ptr i32) ;; pointer to outer loop object
(local $inner_ptr i32) ;; pointer to inner loop object

(local $x1 i32) ;; outer loop object x coordinate
(local $x2 i32) ;; inner loop object x coordinate
(local $y1 i32) ;; outer loop object y coordinate
(local $y2 i32) ;; inner loop object y coordinate

(local $xdist i32) ;; distance between objects on x axis
(local $ydist i32) ;; distance between objects on y axis

(local $i_hit i32) ;; i object hit boolean flag
(local $xv i32) ;; x velocity
(local $vy i32) ;; y velocity

(loop $move_loop
;; get x attribute
(call $get_obj_attr (local.get $i) (global.get $x_offset))
local.set $x1

;; get y attribute
(call $get_obj_attr (local.get $i) (global.get $y_offset))
local.set $y1

;; get x velocity attribute
(call $get_obj_attr (local.get $i) (global.get $xv_offset))
local.set $xv

;; get y velocity attribute
(call $get_obj_attr (local.get $i) (global.get $yv_offset))
local.set $yv

;; add velocity to x and force it to stay in the canvas bounds
(i32.add (local.get $xv) (local.get $x1))
i32.const 0x1ff ;; 511 decimal
i32.and ;; clear high-order 23 bits
local.set $x1

;; add velocity to y and force it to stay in the canvas bounds
(i32.add (local.get $yv) (local.get $y1))
i32.const 0x1ff ;; 511 decimal
i32.and ;; clear high-order 23 bits
local.set $y1

;; set the x attribute in linear memory
(call $set_obj_attr
(local.get $i)
(global.get $x_offset)
(local.get $x1)
)

;; set the y attribute in linear memory
(call $set_obj_attr
(local.get $i)
(global.get $y_offset)
(local.get $y1)
)

local.get $i
i32.const 1
i32.add
local.tee $i ;; increment $i

global.get $obj_cnt
i32.lt_u ;; $i < $obj_cnt

if ;; if $i < $obj_cnt branch back to top of $move_loop
br $move_loop
end
)

i32.const 0
local.set $i

(loop $outer_loop (block $outer_break
i32.const 0
local.tee $j ;; setting j to 0

;; $i_hit is a boolean value. 0 for false, 1 for true
local.set $i_hit ;; setting i_hit to 0

;; get x attribute for object $i
(call $get_obj_attr (local.get $i) (global.get $x_offset))
local.set $xi

;; get y attribute for object $i
(call $get_obj_attr (local.get $i) (global.get $y_offset))
local.set $yi




)

0 comments on commit bd9c48f

Please sign in to comment.