flatbuffer base
This commit is contained in:
120
flatbuffers_example_main.c
Normal file
120
flatbuffers_example_main.c
Normal file
@@ -0,0 +1,120 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "monster_reader.h"
|
||||
#include "monster_verifier.h"
|
||||
#include "monster_builder.h"
|
||||
|
||||
#undef ns
|
||||
#define ns(x) FLATBUFFERS_WRAP_NAMESPACE(MyGame_Sample, x)
|
||||
|
||||
#define c_vec_len(V) (sizeof(V)/sizeof((V)[0]))
|
||||
|
||||
int main()
|
||||
{
|
||||
flatcc_builder_t builder, *B = &builder;
|
||||
flatcc_builder_init(B);
|
||||
|
||||
printf("[+] Builder initialized\n");
|
||||
|
||||
flatbuffers_string_ref_t weapon_one_name =
|
||||
flatbuffers_string_create_str(B, "Sword");
|
||||
flatbuffers_string_ref_t weapon_two_name =
|
||||
flatbuffers_string_create_str(B, "Axe");
|
||||
|
||||
ns(Weapon_ref_t) sword =
|
||||
ns(Weapon_create(B, weapon_one_name, 3));
|
||||
ns(Weapon_ref_t) axe =
|
||||
ns(Weapon_create(B, weapon_two_name, 5));
|
||||
|
||||
ns(Weapon_vec_start(B));
|
||||
ns(Weapon_vec_push(B, sword));
|
||||
ns(Weapon_vec_push(B, axe));
|
||||
ns(Weapon_vec_ref_t) weapons = ns(Weapon_vec_end(B));
|
||||
|
||||
uint8_t treasure[] = {0,1,2,3,4,5,6,7,8,9};
|
||||
flatbuffers_uint8_vec_ref_t inventory =
|
||||
flatbuffers_uint8_vec_create(B, treasure, c_vec_len(treasure));
|
||||
|
||||
flatbuffers_string_ref_t name =
|
||||
flatbuffers_string_create_str(B, "Orc");
|
||||
|
||||
uint16_t hp = 300;
|
||||
uint16_t mana = 150;
|
||||
|
||||
ns(Vec3_t) pos = {1.0f, 2.0f, 3.0f};
|
||||
|
||||
ns(Vec3_vec_start(B));
|
||||
ns(Vec3_vec_push(B, &pos));
|
||||
ns(Vec3_vec_ref_t) path = ns(Vec3_vec_end(B));
|
||||
|
||||
ns(Equipment_union_ref_t) equipped =
|
||||
ns(Equipment_as_Weapon(axe));
|
||||
|
||||
ns(Monster_create_as_root(
|
||||
B, &pos, mana, hp, name,
|
||||
inventory, ns(Color_Red),
|
||||
weapons, equipped, path));
|
||||
|
||||
uint8_t *buf;
|
||||
size_t size;
|
||||
|
||||
buf = flatcc_builder_finalize_buffer(B, &size);
|
||||
|
||||
printf("[+] Buffer built, size = %zu bytes\n", size);
|
||||
|
||||
|
||||
// 🔍 Верификация
|
||||
if (ns(Monster_verify_as_root(buf, size))) {
|
||||
printf("[!] Buffer verification FAILED\n");
|
||||
return -1;
|
||||
}
|
||||
printf("[+] Buffer verification OK\n");
|
||||
|
||||
// 📖 Чтение
|
||||
ns(Monster_table_t) m = ns(Monster_as_root(buf));
|
||||
|
||||
printf("\n===== MONSTER DUMP =====\n");
|
||||
|
||||
printf("Name: %s\n", ns(Monster_name(m)));
|
||||
printf("HP: %u\n", ns(Monster_hp(m)));
|
||||
printf("Mana: %u\n", ns(Monster_mana(m)));
|
||||
|
||||
ns(Vec3_struct_t) p = ns(Monster_pos(m));
|
||||
printf("Position: x=%.2f y=%.2f z=%.2f\n",
|
||||
ns(Vec3_x(p)), ns(Vec3_y(p)), ns(Vec3_z(p)));
|
||||
|
||||
flatbuffers_uint8_vec_t inv = ns(Monster_inventory(m));
|
||||
size_t inv_len = flatbuffers_uint8_vec_len(inv);
|
||||
|
||||
printf("Inventory (%zu): ", inv_len);
|
||||
for (size_t i = 0; i < inv_len; i++) {
|
||||
printf("%u ", flatbuffers_uint8_vec_at(inv, i));
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
ns(Weapon_vec_t) wv = ns(Monster_weapons(m));
|
||||
size_t wlen = ns(Weapon_vec_len(wv));
|
||||
|
||||
printf("Weapons (%zu):\n", wlen);
|
||||
for (size_t i = 0; i < wlen; i++) {
|
||||
ns(Weapon_table_t) w = ns(Weapon_vec_at(wv, i));
|
||||
printf(" - %s (dmg=%u)\n",
|
||||
ns(Weapon_name(w)),
|
||||
ns(Weapon_damage(w)));
|
||||
}
|
||||
|
||||
if (ns(Monster_equipped_type(m)) == ns(Equipment_Weapon)) {
|
||||
ns(Weapon_table_t) w = ns(Monster_equipped(m));
|
||||
printf("Equipped: %s (dmg=%u)\n",
|
||||
ns(Weapon_name(w)),
|
||||
ns(Weapon_damage(w)));
|
||||
}
|
||||
|
||||
printf("========================\n\n");
|
||||
|
||||
free(buf);
|
||||
flatcc_builder_clear(B);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user