Skip to content
This repository was archived by the owner on Feb 19, 2025. It is now read-only.

Commit 9f59922

Browse files
committed
json example
1 parent 51545c2 commit 9f59922

File tree

7 files changed

+84
-1814
lines changed

7 files changed

+84
-1814
lines changed

add.c

Lines changed: 0 additions & 8 deletions
This file was deleted.

fetch-json.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include "actrwasm.h"
2+
#include "actrfetch.h"
3+
#include "actrmap.h"
4+
#include "actrcanvas.h"
5+
#include "actrasync.h"
6+
#include "actrjson.h"
7+
8+
// fetch json example
9+
10+
int asyncHandle = 0;
11+
char * text = "loading will begin shortly...";
12+
13+
[[clang::export_name("actr_async_result")]]
14+
void actr_async_result(int handle, int success)
15+
{
16+
if (handle == asyncHandle) {
17+
if (success == 1) {
18+
text = actr_json_get_string(1, "MyObject.one");
19+
} else {
20+
// this will happen if the fetch request fails
21+
asyncHandle = 0;
22+
text = "fetch error, retrying...";
23+
}
24+
}
25+
}
26+
27+
[[clang::export_name("actr_step")]]
28+
void actr_step(double delta)
29+
{
30+
if (asyncHandle == 0)
31+
{
32+
text = "loading...";
33+
asyncHandle = actr_fetch_json("https://mrnathanstiles.github.io/test.json", 1);
34+
if (asyncHandle < 1) {
35+
// this would happen if the url is invalid or rejected
36+
// this will happen if actr_async_result is not exported
37+
text = "url error, retrying...";
38+
asyncHandle = 0;
39+
}
40+
}
41+
42+
43+
actr_canvas2d_fillStyle(0, 0, 0, 100);
44+
actr_canvas2d_fillRect(-10, -10, 9999, 9999);
45+
46+
actr_canvas2d_fillStyle(255, 255, 255, 100);
47+
actr_canvas2d_fillText(10, 20, text, strlen(text));
48+
49+
}

header/actrjson.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,21 @@
1+
#include "actrwasm.h"
2+
13
#ifndef ACTRJSON_H
24
#define ACTRJSON_H
5+
6+
int actr_json_get_int_length(int jsonId, char * path, int pathLength);
7+
float actr_json_get_float_length(int jsonId, char * path, int pathLength);
8+
char * actr_json_get_string_length(int jsonId, char * path, int pathLength);
9+
10+
int actr_json_get_int(int jsonId, char * path) {
11+
return actr_json_get_int_length(jsonId, path, strlen(path));
12+
}
13+
float actr_json_get_float(int jsonId, char * path) {
14+
return actr_json_get_float_length(jsonId, path, strlen(path));
15+
}
16+
char * actr_json_get_string(int jsonId, char * path) {
17+
return actr_json_get_string_length(jsonId, path, strlen(path));
18+
}
19+
320
#endif
421

pub.bat

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,50 @@
11
@echo off
2+
23
clear
4+
35
SET script=%1
46
SET server=%2
57

6-
78
if "%script%" == "" (
89
echo Example use
910
echo ./pub.bat scriptFile.c
1011
exit
1112
)
1213

13-
if "%script%" == "script.c" (
14-
SET apiKey=pmu58grtf9vbyyxsy0ozr1l78wr1otlg4vaz7o1xpcz8amfd3x
15-
)
16-
1714
if "%script%" == "fetch-text.c" (
1815
SET apiKey=pjy2drrc8hhhvk9gx7c8ga3h3aa6l4en9rtk5sa8rbisgz0hpd
1916
)
20-
17+
18+
if "%script%" == "fetch-json.c" (
19+
SET apiKey=w3pwm3k3bzdj9jskjzo8c5axm5ffd8ygz51tzgi6q9jgp98jjj
20+
)
21+
2122
if "%script%" == "myScript" (
2223
SET apiKey=myApiKey
2324
)
2425

2526
if "%apiKey%" == "" (
26-
echo apiKey not assigned for file
27+
echo apiKey not assigned for file
28+
exit
29+
)
30+
31+
if %server% == local (
32+
SET server=https://localhost:7086/api/Wasm/Upload/
33+
) else (
34+
SET server=https://www.d1ag0n.com/api/Wasm/Upload/
2735
)
2836

2937
echo Building...
38+
3039
clang %script% -I header --target=wasm32-unknown-unknown ^
3140
--optimize=3 -nostdlib -nostdlibinc -nostdinc -nostdinc++ ^
3241
-Wl,--no-entry -Wl,--export-all -Wl,--error-limit=0 ^
3342
-Wl,--allow-undefined --wasm-opt --output %script%.wasm
3443

35-
if %server% == local (
36-
SET server=https://localhost:7086/api/Wasm/Upload/
37-
echo SET LOCAL %server%
38-
) else (
39-
SET server=https://www.d1ag0n.com/api/Wasm/Upload/
40-
echo SET REMOTE %server%
41-
)
42-
echo %server%
44+
4345

4446
if %ERRORLEVEL% == 0 (
45-
echo Build success uploading...
46-
47+
echo Uploading...
4748
curl -k -F "data=@./%script%.wasm" "%server%%apiKey%%"
4849
) else (
4950
echo Build failed

script.html

Lines changed: 0 additions & 201 deletions
This file was deleted.

0 commit comments

Comments
 (0)