mirror of
https://gitee.com/wa-lang/wa.git
synced 2025-12-06 09:18:53 +08:00
snake: 补充缺少的 wasi 方法
This commit is contained in:
@@ -17,33 +17,33 @@ buf为帧缓存指针
|
||||
func updateCanvas_JS(id: u32, buf: *u32)
|
||||
|
||||
//画布事件回调函数原型
|
||||
type OnTouch func (x, y: u32)
|
||||
type OnTouch func(x, y: u32)
|
||||
type OnKey func(key: u32)
|
||||
|
||||
//画布对象
|
||||
type Canvas struct {
|
||||
device_id: u32 //画布对象对应的网页DOM对象id
|
||||
width: u32 //画布宽度,以像素为单位
|
||||
height: u32 //画布高度,以像素为单位
|
||||
frame_buf: []u32 //画布帧缓存,容量为Width * Height
|
||||
device_id :u32 //画布对象对应的网页DOM对象id
|
||||
width :u32 //画布宽度,以像素为单位
|
||||
height :u32 //画布高度,以像素为单位
|
||||
frame_buf :[]u32 //画布帧缓存,容量为Width * Height
|
||||
}
|
||||
|
||||
//画布事件
|
||||
type CanvasEvents struct {
|
||||
Device_id: u32 //画布设备ID
|
||||
OnMouseDown: OnTouch //鼠标按下时的回调处理函数
|
||||
OnMouseUp: OnTouch //鼠标松开时的回调处理函数
|
||||
OnKeyDown: OnKey //键盘按下时的回调处理函数
|
||||
OnKeyUp: OnKey //键盘弹起时的回调处理函数
|
||||
Device_id :u32 //画布设备ID
|
||||
OnMouseDown :OnTouch //鼠标按下时的回调处理函数
|
||||
OnMouseUp :OnTouch //鼠标松开时的回调处理函数
|
||||
OnKeyDown :OnKey //键盘按下时的回调处理函数
|
||||
OnKeyUp :OnKey //键盘弹起时的回调处理函数
|
||||
}
|
||||
|
||||
//创建一个宽度为w像素、高度为h像素的画布对象
|
||||
func NewCanvas(w, h: u32) => *Canvas {
|
||||
var canvas Canvas
|
||||
var canvas: Canvas
|
||||
canvas.device_id = newCanvas_JS(w, h)
|
||||
canvas.width = w
|
||||
canvas.height = h
|
||||
canvas.frame_buf = make([]u32, w * h)
|
||||
canvas.frame_buf = make([]u32, w*h)
|
||||
|
||||
return &canvas
|
||||
}
|
||||
@@ -65,12 +65,12 @@ func Canvas.GetHeight() => u32 {
|
||||
|
||||
//获取画布对象坐标为(x, y)处的像素颜色值
|
||||
func Canvas.GetPixel(x, y: u32) => u32 {
|
||||
return this.frame_buf[y * this.width + x]
|
||||
return this.frame_buf[y*this.width+x]
|
||||
}
|
||||
|
||||
//设置画布对象坐标(x, y)处的颜色值为color
|
||||
func Canvas.SetPixel(x, y, color: u32) {
|
||||
this.frame_buf[y * this.width + x] = color
|
||||
this.frame_buf[y*this.width+x] = color
|
||||
}
|
||||
|
||||
//用color指定的颜色清除整个画布
|
||||
@@ -82,7 +82,7 @@ func Canvas.Clear(color: u32) {
|
||||
|
||||
//将画布对象的帧缓存更新至网页DOM对象
|
||||
func Canvas.Flush() {
|
||||
updateCanvas_JS(this.device_id, &this.frame_buf[0])
|
||||
updateCanvas_JS(this.device_id, &this.frame_buf[0])
|
||||
}
|
||||
|
||||
var canvas_events: []CanvasEvents
|
||||
@@ -103,8 +103,8 @@ func AttachCanvasEvents(e: CanvasEvents) {
|
||||
id为画布DOM对象对应的Canvas对象id
|
||||
(x, y)为画布像素坐标系坐标
|
||||
*/
|
||||
func OnMouseDown(id: u32, x, y:u32) {
|
||||
for _, i := range canvas_events {
|
||||
func OnMouseDown(id: u32, x, y: u32) {
|
||||
for _, i := range canvas_events {
|
||||
if i.Device_id == id {
|
||||
i.OnMouseDown(x, y)
|
||||
return
|
||||
@@ -117,8 +117,8 @@ func OnMouseDown(id: u32, x, y:u32) {
|
||||
id为画布DOM对象对应的Canvas对象id
|
||||
(x, y)为画布像素坐标系坐标
|
||||
*/
|
||||
func OnMouseUp(id: u32, x, y:u32) {
|
||||
for _, i := range canvas_events {
|
||||
func OnMouseUp(id: u32, x, y: u32) {
|
||||
for _, i := range canvas_events {
|
||||
if i.Device_id == id {
|
||||
i.OnMouseUp(x, y)
|
||||
return
|
||||
@@ -131,8 +131,8 @@ func OnMouseUp(id: u32, x, y:u32) {
|
||||
id为画布DOM对象对应的Canvas对象id
|
||||
key为键位。37=← 38=↑ 39=→ 40=↓
|
||||
*/
|
||||
func OnKeyDown(id, key:u32) {
|
||||
for _, i := range canvas_events {
|
||||
func OnKeyDown(id, key: u32) {
|
||||
for _, i := range canvas_events {
|
||||
if i.Device_id == id {
|
||||
i.OnKeyDown(key)
|
||||
return
|
||||
@@ -145,8 +145,8 @@ func OnKeyDown(id, key:u32) {
|
||||
id为画布DOM对象对应的Canvas对象id
|
||||
key为键位
|
||||
*/
|
||||
func OnKeyUp(id, key:u32) {
|
||||
for _, i := range canvas_events {
|
||||
func OnKeyUp(id, key: u32) {
|
||||
for _, i := range canvas_events {
|
||||
if i.Device_id == id {
|
||||
i.OnKeyUp(key)
|
||||
return
|
||||
|
||||
@@ -9,15 +9,54 @@
|
||||
let app = this;
|
||||
let importsObject = {
|
||||
wasi_snapshot_preview1: new function () {
|
||||
this.args_sizes_get = (arg0, arg1) => {
|
||||
return 0;
|
||||
}
|
||||
this.args_get = (arg0, arg1) => {
|
||||
return 0;
|
||||
}
|
||||
this.fd_write = (fd, iovs, iovsCount, resultSize) => {
|
||||
return 0;
|
||||
}
|
||||
this.args_get = () => { return 0; }
|
||||
this.args_sizes_get = () => { return 0; }
|
||||
|
||||
this.clock_res_get = () => { return 0; }
|
||||
this.clock_time_get = () => { return 0; }
|
||||
this.environ_get = () => { return 0; }
|
||||
this.environ_sizes_get = () => { return 0; }
|
||||
|
||||
this.fd_advise = () => { return 0; }
|
||||
this.fd_allocate = () => { return 0; }
|
||||
this.fd_close = () => { return 0; }
|
||||
this.fd_datasync = () => { return 0; }
|
||||
this.fd_fdstat_get = () => { return 0; }
|
||||
this.fd_fdstat_set_flags = () => { return 0; }
|
||||
this.fd_fdstat_set_rights = () => { return 0; }
|
||||
this.fd_filestat_get = () => { return 0; }
|
||||
this.fd_filestat_set_size = () => { return 0; }
|
||||
this.fd_filestat_set_times = () => { return 0; }
|
||||
this.fd_pread = () => { return 0; }
|
||||
this.fd_prestat_get = () => { return 0; }
|
||||
this.fd_prestat_dir_name = () => { return 0; }
|
||||
this.fd_pwrite = () => { return 0; }
|
||||
this.fd_read = () => { return 0; }
|
||||
this.fd_readdir = () => { return 0; }
|
||||
this.fd_renumber = () => { return 0; }
|
||||
this.fd_seek = () => { return 0; }
|
||||
this.fd_sync = () => { return 0; }
|
||||
this.fd_tell = () => { return 0; }
|
||||
this.fd_write = () => { return 0; }
|
||||
this.path_create_directory = () => { return 0; }
|
||||
this.path_filestat_get = () => { return 0; }
|
||||
this.path_filestat_set_times = () => { return 0; }
|
||||
this.path_link = () => { return 0; }
|
||||
this.path_open = () => { return 0; }
|
||||
this.path_readlink = () => { return 0; }
|
||||
this.path_remove_directory = () => { return 0; }
|
||||
this.path_rename = () => { return 0; }
|
||||
this.path_symlink = () => { return 0; }
|
||||
this.path_unlink_file = () => { return 0; }
|
||||
|
||||
this.poll_oneoff = () => { return 0; }
|
||||
this.proc_exit = () => { return 0; }
|
||||
this.random_get = () => { return 0; }
|
||||
this.sched_yield = () => { return 0; }
|
||||
this.sock_accept = () => { return 0; }
|
||||
this.sock_recv = () => { return 0; }
|
||||
this.sock_send = () => { return 0; }
|
||||
this.sock_shutdown = () => { return 0; }
|
||||
},
|
||||
wa_js_env: new function () {
|
||||
this.waPrintI32 = (i) => {
|
||||
|
||||
@@ -8,6 +8,56 @@
|
||||
init(url) {
|
||||
let app = this;
|
||||
let importsObject = {
|
||||
wasi_snapshot_preview1: new function () {
|
||||
this.args_get = () => { return 0; }
|
||||
this.args_sizes_get = () => { return 0; }
|
||||
|
||||
this.clock_res_get = () => { return 0; }
|
||||
this.clock_time_get = () => { return 0; }
|
||||
this.environ_get = () => { return 0; }
|
||||
this.environ_sizes_get = () => { return 0; }
|
||||
|
||||
this.fd_advise = () => { return 0; }
|
||||
this.fd_allocate = () => { return 0; }
|
||||
this.fd_close = () => { return 0; }
|
||||
this.fd_datasync = () => { return 0; }
|
||||
this.fd_fdstat_get = () => { return 0; }
|
||||
this.fd_fdstat_set_flags = () => { return 0; }
|
||||
this.fd_fdstat_set_rights = () => { return 0; }
|
||||
this.fd_filestat_get = () => { return 0; }
|
||||
this.fd_filestat_set_size = () => { return 0; }
|
||||
this.fd_filestat_set_times = () => { return 0; }
|
||||
this.fd_pread = () => { return 0; }
|
||||
this.fd_prestat_get = () => { return 0; }
|
||||
this.fd_prestat_dir_name = () => { return 0; }
|
||||
this.fd_pwrite = () => { return 0; }
|
||||
this.fd_read = () => { return 0; }
|
||||
this.fd_readdir = () => { return 0; }
|
||||
this.fd_renumber = () => { return 0; }
|
||||
this.fd_seek = () => { return 0; }
|
||||
this.fd_sync = () => { return 0; }
|
||||
this.fd_tell = () => { return 0; }
|
||||
this.fd_write = () => { return 0; }
|
||||
this.path_create_directory = () => { return 0; }
|
||||
this.path_filestat_get = () => { return 0; }
|
||||
this.path_filestat_set_times = () => { return 0; }
|
||||
this.path_link = () => { return 0; }
|
||||
this.path_open = () => { return 0; }
|
||||
this.path_readlink = () => { return 0; }
|
||||
this.path_remove_directory = () => { return 0; }
|
||||
this.path_rename = () => { return 0; }
|
||||
this.path_symlink = () => { return 0; }
|
||||
this.path_unlink_file = () => { return 0; }
|
||||
|
||||
this.poll_oneoff = () => { return 0; }
|
||||
this.proc_exit = () => { return 0; }
|
||||
this.random_get = () => { return 0; }
|
||||
this.sched_yield = () => { return 0; }
|
||||
this.sock_accept = () => { return 0; }
|
||||
this.sock_recv = () => { return 0; }
|
||||
this.sock_send = () => { return 0; }
|
||||
this.sock_shutdown = () => { return 0; }
|
||||
},
|
||||
wa_js_env: new function () {
|
||||
this.waPrintI32 = (i) => {
|
||||
app._wa_print_buf += i
|
||||
@@ -101,6 +151,7 @@
|
||||
WebAssembly.instantiateStreaming(fetch(url), importsObject).then(res => {
|
||||
this._inst = res.instance;
|
||||
this._inst.exports._start();
|
||||
const timer = setInterval(gameLoop, 150);
|
||||
})
|
||||
}
|
||||
|
||||
@@ -133,5 +184,4 @@
|
||||
|
||||
window['waApp'] = new WaApp();
|
||||
window['waApp'].init("./snake.wasm")
|
||||
const timer = setInterval(gameLoop, IS_MOBILE ? 150 : 100);
|
||||
})()
|
||||
Reference in New Issue
Block a user