| 69 | } |
| 70 | |
| 71 | s32 compile(CompileOptions &opts) |
| 72 | { |
| 73 | Buffer buf = opts.read(); |
| 74 | |
| 75 | TempAllocator4096 ta; |
| 76 | JsonObject obj(ta); |
| 77 | RETURN_IF_ERROR(sjson::parse(obj, buf)); |
| 78 | |
| 79 | JsonArray frames(ta); |
| 80 | RETURN_IF_ERROR(sjson::parse_array(frames, obj["frames"])); |
| 81 | |
| 82 | // Read width/height |
| 83 | const f32 width = RETURN_IF_ERROR(sjson::parse_float(obj["width"])); |
| 84 | const f32 height = RETURN_IF_ERROR(sjson::parse_float(obj["height"])); |
| 85 | const u32 num_frames = array::size(frames); |
| 86 | |
| 87 | // Parse frames. |
| 88 | Array<SpriteFrame> sprite_frames(default_allocator()); |
| 89 | s32 err = parse_frames(sprite_frames, frames, opts); |
| 90 | ENSURE_OR_RETURN(SPRITE_ANIMATION_RESOURCE, err == 0, opts); |
| 91 | |
| 92 | // Fill verices. |
| 93 | Array<f32> vertices(default_allocator()); |
| 94 | for (u32 ii = 0; ii < num_frames; ++ii) { |
| 95 | const SpriteFrame &sf = sprite_frames[ii]; |
| 96 | |
| 97 | // Generate UV coords |
| 98 | const f32 u0 = (sf.region.x) / width; |
| 99 | const f32 v0 = (sf.region.w + sf.region.y) / height; |
| 100 | const f32 u1 = (sf.region.z + sf.region.x) / width; |
| 101 | const f32 v1 = (sf.region.y) / height; |
| 102 | |
| 103 | // Generate positions |
| 104 | f32 x0 = (sf.region.x - sf.pivot.x) / CROWN_DEFAULT_PIXELS_PER_METER; |
| 105 | f32 y0 = (sf.region.w + sf.region.y - sf.pivot.y) / CROWN_DEFAULT_PIXELS_PER_METER; |
| 106 | f32 x1 = (sf.region.z + sf.region.x - sf.pivot.x) / CROWN_DEFAULT_PIXELS_PER_METER; |
| 107 | f32 y1 = (sf.region.y - sf.pivot.y) / CROWN_DEFAULT_PIXELS_PER_METER; |
| 108 | |
| 109 | // Invert Y axis |
| 110 | y0 = y0 == 0.0f ? y0 : -y0; |
| 111 | y1 = y1 == 0.0f ? y1 : -y1; |
| 112 | |
| 113 | // Output vertex data |
| 114 | // |
| 115 | // D -- C |
| 116 | // | | |
| 117 | // A -- B |
| 118 | // |
| 119 | array::push_back(vertices, x0); // A.x |
| 120 | array::push_back(vertices, y0); // A.y |
| 121 | array::push_back(vertices, 0.0f); // A.z |
| 122 | array::push_back(vertices, u0); // A.u |
| 123 | array::push_back(vertices, v0); // A.v |
| 124 | |
| 125 | array::push_back(vertices, x1); // B.x |
| 126 | array::push_back(vertices, y0); // B.y |
| 127 | array::push_back(vertices, 0.0f); // B.z |
| 128 | array::push_back(vertices, u1); // B.u |
nothing calls this directly
no test coverage detected