Skip to content

Commit 3ab5b3f

Browse files
committed
[client] move assets to C code
1 parent 7342eec commit 3ab5b3f

File tree

6 files changed

+73
-34
lines changed

6 files changed

+73
-34
lines changed

build.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,20 @@ function buildJs() {
3737
}
3838

3939
async function buildClient() {
40+
await cmdAsync("clang", [
41+
"-Wall", "-Wextra", "-ggdb",
42+
"-I"+SRC_FOLDER,
43+
"-I"+SRC_FOLDER+"cws/",
44+
"-o", BUILD_FOLDER+"packer",
45+
SRC_FOLDER+"packer.c",
46+
"-lm",
47+
]);
48+
49+
await cmdAsync(BUILD_FOLDER+"packer", [
50+
BUILD_FOLDER+"pack.h",
51+
]);
52+
4053
await Promise.all([
41-
cmdAsync("clang", [
42-
"-Wall", "-Wextra", "-ggdb",
43-
"-I"+SRC_FOLDER,
44-
"-I"+SRC_FOLDER+"cws/",
45-
"-o", BUILD_FOLDER+"packer",
46-
SRC_FOLDER+"packer.c",
47-
"-lm",
48-
]),
4954
cmdAsync("clang", [
5055
"-Wall", "-Wextra",
5156
"--target=wasm32",
@@ -57,6 +62,7 @@ async function buildClient() {
5762
"-Wall", "-Wextra",
5863
"--target=wasm32",
5964
"-I", SRC_FOLDER+"cws/",
65+
"-I", BUILD_FOLDER,
6066
"-c", SRC_FOLDER+"client.c",
6167
"-o", BUILD_FOLDER+"client.wasm.o",
6268
]),
@@ -69,6 +75,7 @@ async function buildClient() {
6975
]),
7076
])
7177

78+
7279
return cmdAsync("c3c", [
7380
"compile",
7481
"-D", "PLATFORM_WEB",

client.wasm

-1.06 KB
Binary file not shown.

src/client.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <stdint.h>
22
#include "common.h"
33
#include "sort.h"
4+
#include "pack.h"
45

56
// TODO: It would be cool if we could use nob.h here.
67
// Unfortunately nob depends on stdlib. We should make it so it's optional.
@@ -600,6 +601,26 @@ void key_up(uint32_t key_code) {
600601
}
601602
}
602603

604+
static bool streq(const char *s1, const char *s2) {
605+
while (*s1 && *s2) {
606+
if (*s1++ != *s2++) return false;
607+
}
608+
return !(*s1 || *s2);
609+
}
610+
611+
Asset *asset_by_filename(const char *filename) {
612+
for (size_t i = 0; i < assets_count; ++i) {
613+
if (streq(assets[i].filename, filename)) {
614+
return &assets[i];
615+
}
616+
}
617+
return NULL;
618+
}
619+
620+
void *pack_ptr_by_offset(size_t offset) {
621+
return &pack[offset];
622+
}
623+
603624
// TODO: "magnet" items into the player
604625
// TODO: Blast particles should fade out as they age
605626
// TODO: Bomb collision should take into account the bomb's size

src/client.c3

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ fn void* allocate_temporary_buffer(usz size) @extern("allocate_temporary_buffer"
3838
return mem::tcalloc(size);
3939
}
4040

41-
$exec("build/packer");
42-
4341
struct Color {
4442
char r;
4543
char g;
@@ -305,37 +303,32 @@ fn void render_other_players(SpritePool *sprite_pool, Image *player_image) {
305303
};
306304
}
307305

308-
fn Asset *asset_by_filename(String filename) {
309-
foreach (&asset: assets) {
310-
if (asset.filename == filename) {
311-
return asset;
312-
}
313-
}
314-
return null;
315-
}
306+
307+
extern fn void* pack_ptr_by_offset(usz offset) @extern("pack_ptr_by_offset");
308+
extern fn Asset *asset_by_filename(ZString filename) @extern("asset_by_filename");
316309

317310
fn void render_game(float delta_time, float time) @extern("render_game") @wasm {
318311
Asset *asset = null;
319312

320313
asset = asset_by_filename("assets/images/custom/key.png");
321314
assert(asset);
322-
Image key_image = {asset.width, asset.height, (Color*)&pack[asset.offset]};
315+
Image key_image = {asset.width, asset.height, (Color*)pack_ptr_by_offset(asset.offset)};
323316

324317
asset = asset_by_filename("assets/images/custom/bomb.png");
325318
assert(asset);
326-
Image bomb_image = {asset.width, asset.height, (Color*)&pack[asset.offset]};
319+
Image bomb_image = {asset.width, asset.height, (Color*)pack_ptr_by_offset(asset.offset)};
327320

328321
asset = asset_by_filename("assets/images/custom/particle.png");
329322
assert(asset);
330-
Image particle_image = {asset.width, asset.height, (Color*)&pack[asset.offset]};
323+
Image particle_image = {asset.width, asset.height, (Color*)pack_ptr_by_offset(asset.offset)};
331324

332325
asset = asset_by_filename("assets/images/custom/wall.png");
333326
assert(asset);
334-
Image wall_image = {asset.width, asset.height, (Color*)&pack[asset.offset]};
327+
Image wall_image = {asset.width, asset.height, (Color*)pack_ptr_by_offset(asset.offset)};
335328

336329
asset = asset_by_filename("assets/images/custom/player.png");
337330
assert(asset);
338-
Image player_image = {asset.width, asset.height, (Color*)&pack[asset.offset]};
331+
Image player_image = {asset.width, asset.height, (Color*)pack_ptr_by_offset(asset.offset)};
339332

340333
update_all_players(delta_time);
341334
update_items(&sprite_pool, time, common::items_ptr(), common::items_len(), &key_image, &bomb_image);

src/common.c3

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import std::hash::fnv32a;
99

1010
// WARNING! struct Asset definition must be in sync with the one in common.h
1111
struct Asset {
12-
String filename;
12+
ZString filename;
1313
usz offset;
1414
usz width;
1515
usz height;

src/packer.c

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,21 @@ const char *IMAGE_FILES[] = {
1717
"assets/images/custom/wall.png",
1818
};
1919

20-
String_Builder pack;
21-
Assets assets;
20+
String_Builder pack = {0};
21+
Assets assets = {0};
22+
String_Builder out = {0};
2223

23-
int main()
24+
int main(int argc, char **argv)
2425
{
26+
const char *program_name = shift(argv, argc);
27+
28+
if (argc <= 0) {
29+
fprintf(stderr, "Usage: %s <output>\n", program_name);
30+
fprintf(stderr, "ERROR: no output file path is provided\n");
31+
return 1;
32+
}
33+
const char *output_path = shift(argv, argc);
34+
2535
for (size_t i = 0; i < ARRAY_LEN(IMAGE_FILES); ++i) {
2636
const char *filename = IMAGE_FILES[i];
2737
int x, y;
@@ -37,19 +47,27 @@ int main()
3747
sb_append_buf(&pack, pixels, size);
3848
}
3949

40-
printf("Asset[] assets = {\n");
50+
sb_appendf(&out, "Asset assets[] = {\n");
4151
for (size_t i = 0; i < assets.count; ++i) {
4252
Asset asset = assets.items[i];
43-
printf(" {\"%s\", %zu, %zu, %zu},\n", asset.filename, asset.offset, asset.width, asset.height);
53+
sb_appendf(&out, " {\"%s\", %zu, %zu, %zu},\n", asset.filename, asset.offset, asset.width, asset.height);
4454
}
45-
printf("};\n");
55+
sb_appendf(&out, "};\n");
56+
sb_appendf(&out, "#define assets_count %zu\n", assets.count);
4657

47-
printf("char[*] pack = {");
58+
sb_appendf(&out, "unsigned char pack[] = {\n");
4859
String_View pack_view = sb_to_sv(pack);
49-
for (size_t i = 0; i < pack_view.count; ++i) {
50-
printf("%u,", (unsigned char)pack_view.data[i]);
60+
size_t width = 15;
61+
for (size_t i = 0; i < pack_view.count;) {
62+
sb_appendf(&out, " ");
63+
for (size_t j = 0; j < width && i < pack_view.count; ++j, ++i) {
64+
sb_appendf(&out, "0x%02X,", (unsigned char)pack_view.data[i]);
65+
}
66+
sb_appendf(&out, "\n");
5167
}
52-
printf("};\n");
68+
sb_appendf(&out, "};\n");
69+
sb_appendf(&out, "#define pack_count %zu\n", pack_view.count);
5370

71+
if (!write_entire_file(output_path, out.items, out.count)) return 1;
5472
return 0;
5573
}

0 commit comments

Comments
 (0)