/* hello.c: one avatar, waving, saved as avatar.png. hello [out.png] [--dir ] Credit: show "Powered by 360sona" where your users see the avatar. */ #include "sona.h" #include #include #include static void *read_all(const char *path, size_t *len) { FILE *f = fopen(path, "rb"); void *p = NULL; long n; if (!f) return NULL; if (fseek(f, 0, SEEK_END) == 0 && (n = ftell(f)) > 0 && fseek(f, 0, SEEK_SET) == 0 && (p = malloc((size_t)n))) if (fread(p, 1, (size_t)n, f) != (size_t)n) { free(p); p = NULL; } if (p) *len = (size_t)n; fclose(f); return p; } static int fail(const char *what, sona_ctx *ctx, sona_result r) { sona_error_info e = sona_last_error(ctx); fprintf(stderr, "%s: %s%s%s\n", what, sona_result_name(r), e.message ? ": " : "", e.message ? e.message : ""); if (e.fix) fprintf(stderr, " %s\n", e.fix); return 1; } int main(int argc, char **argv) { sona_config cfg; sona_ctx *ctx; sona_avatar *avatar; sona_headless *h; sona_render_options ro; sona_result r; uint8_t *rgba, *png; size_t licence_len = 0, png_len; void *licence; const char *dir = NULL, *path = "avatar.png"; FILE *out; if (argc > 4 && strcmp(argv[argc - 2], "--dir") == 0) { dir = argv[argc - 1]; argc -= 2; } if (argc < 3) { fprintf(stderr, "usage: hello [out.png] [--dir ]\n"); return 2; } if (argc > 3) path = argv[3]; if (!(licence = read_all(argv[1], &licence_len))) { fprintf(stderr, "cannot read %s\n", argv[1]); return 1; } /* A context: the licence and where the avatar files come from. */ sona_config_defaults(&cfg); cfg.app_id = getenv("SONA_APP_ID"); /* NULL: the app id your download is made for */ cfg.licence = licence; cfg.licence_len = licence_len; cfg.sources[0].kind = dir ? SONA_SOURCE_DIR : SONA_SOURCE_GATE; /* the gate: item files from 360sona */ cfg.sources[0].location = dir; cfg.source_count = 1; r = sona_open(&cfg, &ctx); free(licence); if (r != SONA_OK) return fail("sona_open", NULL, r); /* An avatar from its manifest. It loads over the next updates. */ r = sona_avatar_add_base64(ctx, argv[2], NULL, &avatar); if (r != SONA_OK) return fail("sona_avatar_add_base64", ctx, r); /* No window: one picture, 1.5 s into the wave. */ r = sona_headless_open(ctx, NULL, &h); if (r != SONA_OK) return fail("sona_headless_open", ctx, r); sona_render_defaults(&ro); ro.width = 480; ro.height = 600; ro.clip = "GenericWave"; ro.time = 1.5f; rgba = malloc((size_t)ro.width * ro.height * 4); r = rgba ? sona_headless_render(h, avatar, &ro, rgba) : SONA_E_MEMORY; if (r != SONA_OK) return fail("sona_headless_render", ctx, r); /* A PNG with the credit in its text chunks. */ r = sona_png_encode(ctx, rgba, ro.width, ro.height, &png, &png_len); if (r != SONA_OK) return fail("sona_png_encode", ctx, r); out = fopen(path, "wb"); if (!out || fwrite(png, 1, png_len, out) != png_len) { fprintf(stderr, "cannot write the picture\n"); return 1; } fclose(out); printf("saved %s (%u x %u)\n", path, ro.width, ro.height); sona_free(ctx, png); free(rgba); sona_headless_close(h); sona_close(ctx); return 0; }