Skip to content

Commit 2d1ed7b

Browse files
committed
[common] Move items array to C
1 parent 27e2fe9 commit 2d1ed7b

6 files changed

Lines changed: 71 additions & 56 deletions

File tree

client.wasm

43 Bytes
Binary file not shown.

src/client.c3

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -451,24 +451,24 @@ fn void update_particles(Image *image, SpritePool *sprite_pool, float deltaTime,
451451
}
452452
}
453453

454-
fn void kill_all_items(Item[]* items) {
455-
foreach (&item: *items) {
456-
item.alive = false;
454+
fn void kill_all_items(Item* items, usz items_count) {
455+
for (usz i = 0; i < items_count; ++i) {
456+
items[i].alive = false;
457457
}
458458
}
459459

460-
fn bool apply_items_collected_batch_message(ItemsCollectedBatchMessage *message, Item[]* items) {
460+
fn bool apply_items_collected_batch_message(ItemsCollectedBatchMessage *message, Item* items, usz items_count) {
461461
usz count = (message.byte_length - ItemsCollectedBatchMessage.sizeof)/int.sizeof;
462462

463463
for (usz i = 0; i < count; ++i) {
464464
int itemIndex = message.payload[i];
465465

466-
if (!(0 <= itemIndex && itemIndex < items.len)) {
466+
if (!(0 <= itemIndex && itemIndex < items_count)) {
467467
io::printn(string::tformat("Received bogus-amogus ItemCollected message from server. Invalid index %d", itemIndex));
468468
return false;
469469
}
470470

471-
Item *item = &(*items)[itemIndex];
471+
Item *item = &items[itemIndex];
472472

473473
if (item.alive) {
474474
item.alive = false;
@@ -479,15 +479,15 @@ fn bool apply_items_collected_batch_message(ItemsCollectedBatchMessage *message,
479479
return true;
480480
}
481481

482-
fn bool apply_items_spawned_batch_message(ItemsSpawnedBatchMessage *message, Item[]* items) {
482+
fn bool apply_items_spawned_batch_message(ItemsSpawnedBatchMessage *message, Item* items, usz items_count) {
483483
usz count = (message.byte_length - ItemsCollectedBatchMessage.sizeof)/ItemSpawned.sizeof;
484484
for (usz i = 0; i < count; ++i) {
485485
int itemIndex = message.payload[i].itemIndex;
486-
if (!(0 <= itemIndex && itemIndex < items.len)) {
486+
if (!(0 <= itemIndex && itemIndex < items_count)) {
487487
io::printn(string::tformat("Received bogus-amogus ItemSpawned message from server. Invalid item index %d", itemIndex));
488488
return false;
489489
}
490-
Item *item = &(*items)[itemIndex];
490+
Item *item = &items[itemIndex];
491491
item.alive = true;
492492
item.kind = message.payload[i].itemKind;
493493
item.position.x = message.payload[i].x;
@@ -497,8 +497,9 @@ fn bool apply_items_spawned_batch_message(ItemsSpawnedBatchMessage *message, Ite
497497
return true;
498498
}
499499

500-
fn void render_items(SpritePool *sprite_pool, Item[]* items, float time, Image *key_image, Image *bomb_image) {
501-
foreach (item: *items) {
500+
fn void render_items(SpritePool *sprite_pool, Item* items, usz items_count, float time, Image *key_image, Image *bomb_image) {
501+
for (usz i = 0; i < items_count; ++i) {
502+
Item *item = &items[i];
502503
if (item.alive) {
503504
float z = 0.25f + ITEM_AMP - ITEM_AMP*math::sin(ITEM_FREQ*(float)math::PI*time + item.position.x + item.position.y);
504505
switch (item.kind) {
@@ -511,21 +512,22 @@ fn void render_items(SpritePool *sprite_pool, Item[]* items, float time, Image *
511512
}
512513
}
513514

514-
fn void update_items_offline(Item[]* items) {
515-
foreach (item_index, &item: *items) {
515+
fn void update_items_offline(Item *items, usz items_count) {
516+
for (usz item_index = 0; item_index < items_count; ++item_index) {
517+
Item *item = &items[item_index];
516518
if (common::collect_item(me, item)) {
517519
platform::play_sound(ITEM_PICKUP, me.position.x, me.position.y, item.position.x, item.position.y);
518520
}
519521
}
520522
}
521523

522-
fn void update_items(SpritePool *sprite_pool, float time, Item[] *items, Image *key_image, Image *bomb_image) {
524+
fn void update_items(SpritePool *sprite_pool, float time, Item *items, usz items_count, Image *key_image, Image *bomb_image) {
523525
// Rendering the items as sprites
524-
render_items(sprite_pool, items, time, key_image, bomb_image);
526+
render_items(sprite_pool, items, items_count, time, key_image, bomb_image);
525527

526528
// Offline mode. Updating items state without asking the server.
527529
if (platform::is_offline_mode()) {
528-
update_items_offline(items);
530+
update_items_offline(items, items_count);
529531
}
530532
}
531533

@@ -618,10 +620,10 @@ const Control[*] CONTROL_KEYS = {
618620
{83, MOVING_BACKWARD},
619621
};
620622

621-
fn bool apply_hello_message_to_me(HelloPlayer hello_player, Item[]* items) {
623+
fn bool apply_hello_message_to_me(HelloPlayer hello_player, Item *items, usz items_count) {
622624
// TODO: maybe we should reset everything (bombs, etc) on hello message
623625
// So to let the server recreate the world properly
624-
kill_all_items(items);
626+
kill_all_items(items, items_count);
625627
me.id = hello_player.id;
626628
me.position.x = hello_player.x;
627629
me.position.y = hello_player.y;
@@ -800,7 +802,7 @@ fn void render_game(float delta_time, float time) @extern("render_game") @wasm {
800802
Image player_image = {asset.width, asset.height, (Color*)&pack[asset.offset]};
801803

802804
update_all_players(&common::scene, delta_time);
803-
update_items(&sprite_pool, time, &common::items, &key_image, &bomb_image);
805+
update_items(&sprite_pool, time, common::items_ptr(), common::items_len(), &key_image, &bomb_image);
804806
update_bombs_on_client_side(&sprite_pool, &particle_pool, &bomb_image, &common::scene, delta_time, &common::bombs);
805807
update_particles(&particle_image, &sprite_pool, delta_time, &common::scene, &particle_pool);
806808

@@ -842,7 +844,7 @@ fn uint ping_msecs() @extern("ping_msecs") @wasm {
842844

843845
fn bool process_message(Message *message) @extern("process_message") @wasm {
844846
if (common::verify_hello_message(message)) {
845-
apply_hello_message_to_me(((HelloMessage*)message).payload, &common::items);
847+
apply_hello_message_to_me(((HelloMessage*)message).payload, common::items_ptr(), common::items_len());
846848
return true;
847849
}
848850
if (common::verify_players_joined_batch_message(message)) {
@@ -862,11 +864,11 @@ fn bool process_message(Message *message) @extern("process_message") @wasm {
862864
return true;
863865
}
864866
if (common::verify_items_collected_batch_message(message)) {
865-
if (!apply_items_collected_batch_message((ItemsCollectedBatchMessage*)message, &common::items)) return false;
867+
if (!apply_items_collected_batch_message((ItemsCollectedBatchMessage*)message, common::items_ptr(), common::items_len())) return false;
866868
return true;
867869
}
868870
if (common::verify_items_spawned_batch_message(message)) {
869-
if (!apply_items_spawned_batch_message((ItemsSpawnedBatchMessage*)message, &common::items)) return false;
871+
if (!apply_items_spawned_batch_message((ItemsSpawnedBatchMessage*)message, common::items_ptr(), common::items_len())) return false;
870872
return true;
871873
}
872874
if (common::verify_bombs_spawned_batch_message(message)) {

src/common.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,47 @@ ItemsSpawnedBatchMessage* reconstruct_state_of_items(Item *items, size_t items_c
9999
return message;
100100
}
101101

102+
static Item items[] = {
103+
{
104+
.kind = ITEM_BOMB,
105+
.position = {1.5, 3.5},
106+
.alive = true,
107+
},
108+
{
109+
.kind = ITEM_KEY,
110+
.position = {2.5, 1.5},
111+
.alive = true,
112+
},
113+
{
114+
.kind = ITEM_KEY,
115+
.position = {3, 1.5},
116+
.alive = true,
117+
},
118+
{
119+
.kind = ITEM_KEY,
120+
.position = {3.5, 1.5},
121+
.alive = true,
122+
},
123+
{
124+
.kind = ITEM_KEY,
125+
.position = {4.0, 1.5},
126+
.alive = true,
127+
},
128+
{
129+
.kind = ITEM_KEY,
130+
.position = {4.5, 1.5},
131+
.alive = true,
132+
},
133+
};
134+
135+
Item *items_ptr() {
136+
return items;
137+
}
138+
139+
size_t items_len() {
140+
return sizeof(items)/sizeof(items[0]);
141+
}
142+
102143
// Bombs //////////////////////////////
103144

104145
int throw_bomb(Vector2 position, float direction, Bombs *bombs) {

src/common.c3

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -118,38 +118,8 @@ struct Item {
118118
Vector2 position;
119119
}
120120

121-
Item[] items = {
122-
{
123-
.kind = ItemKind.BOMB,
124-
.position = {1.5, 3.5},
125-
.alive = true,
126-
},
127-
{
128-
.kind = ItemKind.KEY,
129-
.position = {2.5, 1.5},
130-
.alive = true,
131-
},
132-
{
133-
.kind = ItemKind.KEY,
134-
.position = {3, 1.5},
135-
.alive = true,
136-
},
137-
{
138-
.kind = ItemKind.KEY,
139-
.position = {3.5, 1.5},
140-
.alive = true,
141-
},
142-
{
143-
.kind = ItemKind.KEY,
144-
.position = {4.0, 1.5},
145-
.alive = true,
146-
},
147-
{
148-
.kind = ItemKind.KEY,
149-
.position = {4.5, 1.5},
150-
.alive = true,
151-
},
152-
};
121+
extern fn Item *items_ptr() @extern("items_ptr");
122+
extern fn usz items_len() @extern("items_len");
153123

154124
extern fn bool collect_item(Player player, Item *item) @extern("collect_item");
155125

src/common.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ typedef struct {
9494
} Item;
9595

9696
bool collect_item(Player player, Item *item);
97+
Item *items_ptr();
98+
size_t items_len();
9799

98100
// Bombs //////////////////////////////
99101

src/server.c3

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,11 @@ fn uint tick() @extern("tick") @wasm {
8181
float delta_time = (float)(timestamp - previous_timestamp)/1000.0f;
8282
previous_timestamp = timestamp;
8383

84-
process_joined_players(common::items.ptr, common::items.len);
84+
process_joined_players(common::items_ptr(), common::items_len());
8585
process_left_players();
8686
process_moving_players();
8787
process_thrown_bombs(&common::bombs);
88-
process_world_simulation(common::items.ptr, common::items.len, &common::scene, &common::bombs, delta_time);
88+
process_world_simulation(common::items_ptr(), common::items_len(), &common::scene, &common::bombs, delta_time);
8989
process_pings();
9090

9191
uint tickTime = now_msecs() - timestamp;

0 commit comments

Comments
 (0)