whiteoutlib 0.1.2

Read and write Blizzard game assets from Rust: models (MDX, M2, M3), textures (BLP, DDS, PNG, JPEG, BMP, TGA, TIFF, GIF) and archives (CASC, MPQ).
Documentation
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2026 Fernando Sahmkow
// AUTOGENERATED by tools/codegen/emit_c.py — do not edit.
// Regenerate via:  python -m tools.codegen.codegen utils --backend c-source

#include "whiteout_utils.h"

#include <cstring>
#include <new>
#include <span>
#include <string>
#include <vector>

#include <whiteout/utils/vertex_buffer.h>

namespace { using namespace whiteout; }

namespace {

// `whiteout::u8` is `std::uint8_t`, so a single overload suffices. We use
// the project's type so the reinterpret_cast is a no-op everywhere the
// codegen runs.
inline whiteout_Bytes wrapBytes(std::vector<whiteout::u8>&& v) {
    auto* owned = new std::vector<whiteout::u8>(std::move(v));
    return { reinterpret_cast<const uint8_t*>(owned->data()), owned->size(), owned };
}

inline whiteout_Bytes emptyBytes() {
    return { nullptr, 0, nullptr };
}

inline whiteout_CString wrapCString(std::string&& s) {
    auto* owned = new std::string(std::move(s));
    return { owned->c_str(), owned->size(), owned };
}

inline whiteout_CString emptyCString() {
    return { nullptr, 0, nullptr };
}

} // anonymous
// ── UtilsVertexBuffer ─────────────────────────────────────────────────

extern "C" {

whiteout_UtilsVertexBuffer* whiteout_utils_UtilsVertexBuffer_new(void) {
    return reinterpret_cast<whiteout_UtilsVertexBuffer*>(new whiteout::utils::VertexBuffer());
}

void whiteout_utils_UtilsVertexBuffer_delete(whiteout_UtilsVertexBuffer* self) {
    delete reinterpret_cast<whiteout::utils::VertexBuffer*>(self);
}

uint64_t whiteout_utils_UtilsVertexBuffer_vertexCount(const whiteout_UtilsVertexBuffer* self) {
    return reinterpret_cast<const whiteout::utils::VertexBuffer*>(self)->vertexCount();
}

uint64_t whiteout_utils_UtilsVertexBuffer_vertexSize(const whiteout_UtilsVertexBuffer* self) {
    return reinterpret_cast<const whiteout::utils::VertexBuffer*>(self)->vertexSize();
}

uint64_t whiteout_utils_UtilsVertexBuffer_UVsNum(const whiteout_UtilsVertexBuffer* self) {
    return reinterpret_cast<const whiteout::utils::VertexBuffer*>(self)->UVsNum();
}

int32_t whiteout_utils_UtilsVertexBuffer_hasVertexColors(const whiteout_UtilsVertexBuffer* self) {
    return reinterpret_cast<const whiteout::utils::VertexBuffer*>(self)->hasVertexColors();
}

size_t whiteout_utils_UtilsVertexBuffer_get_data_count(const whiteout_UtilsVertexBuffer* self) {
    return reinterpret_cast<const whiteout::utils::VertexBuffer*>(self)->data.size();
}

void whiteout_utils_UtilsVertexBuffer_resize_data(whiteout_UtilsVertexBuffer* self, size_t count) {
    reinterpret_cast<whiteout::utils::VertexBuffer*>(self)->data.resize(count);
}

const uint8_t* whiteout_utils_UtilsVertexBuffer_get_data_data(const whiteout_UtilsVertexBuffer* self) {
    const auto& __v = reinterpret_cast<const whiteout::utils::VertexBuffer*>(self)->data;
    return __v.empty() ? nullptr : reinterpret_cast<const uint8_t*>(__v.data());
}

void whiteout_utils_UtilsVertexBuffer_assign_data(whiteout_UtilsVertexBuffer* self, const uint8_t* data, size_t count) {
    auto& __v = reinterpret_cast<whiteout::utils::VertexBuffer*>(self)->data;
    __v.resize(count);
    if (count) std::memcpy(__v.data(), data, count * sizeof(whiteout::u8));
}

size_t whiteout_utils_UtilsVertexBuffer_get_layout_count(const whiteout_UtilsVertexBuffer* self) {
    return reinterpret_cast<const whiteout::utils::VertexBuffer*>(self)->layout.size();
}

void whiteout_utils_UtilsVertexBuffer_resize_layout(whiteout_UtilsVertexBuffer* self, size_t count) {
    reinterpret_cast<whiteout::utils::VertexBuffer*>(self)->layout.resize(count);
}

whiteout_UtilsVertexBufferAttribute* whiteout_utils_UtilsVertexBuffer_get_layout_at(whiteout_UtilsVertexBuffer* self, size_t index) {
    return reinterpret_cast<whiteout_UtilsVertexBufferAttribute*>(&reinterpret_cast<whiteout::utils::VertexBuffer*>(self)->layout[index]);
}

uint32_t whiteout_utils_UtilsVertexBuffer_get_vertex_stride(const whiteout_UtilsVertexBuffer* self) {
    return reinterpret_cast<const whiteout::utils::VertexBuffer*>(self)->vertex_stride;
}

void whiteout_utils_UtilsVertexBuffer_set_vertex_stride(whiteout_UtilsVertexBuffer* self, uint32_t value) {
    reinterpret_cast<whiteout::utils::VertexBuffer*>(self)->vertex_stride = value;
}

} // extern "C"

// ── UtilsVertexBufferAttribute ─────────────────────────────────────────────────

extern "C" {

whiteout_UtilsVertexBufferAttribute* whiteout_utils_UtilsVertexBufferAttribute_new(void) {
    return reinterpret_cast<whiteout_UtilsVertexBufferAttribute*>(new whiteout::utils::VertexBuffer::Attribute());
}

void whiteout_utils_UtilsVertexBufferAttribute_delete(whiteout_UtilsVertexBufferAttribute* self) {
    delete reinterpret_cast<whiteout::utils::VertexBuffer::Attribute*>(self);
}

int32_t whiteout_utils_UtilsVertexBufferAttribute_get_attr_class(const whiteout_UtilsVertexBufferAttribute* self) {
    return static_cast<int32_t>(reinterpret_cast<const whiteout::utils::VertexBuffer::Attribute*>(self)->attr_class);
}

void whiteout_utils_UtilsVertexBufferAttribute_set_attr_class(whiteout_UtilsVertexBufferAttribute* self, int32_t value) {
    reinterpret_cast<whiteout::utils::VertexBuffer::Attribute*>(self)->attr_class = static_cast<whiteout::utils::AttributeClass>(value);
}

int32_t whiteout_utils_UtilsVertexBufferAttribute_get_encoding(const whiteout_UtilsVertexBufferAttribute* self) {
    return static_cast<int32_t>(reinterpret_cast<const whiteout::utils::VertexBuffer::Attribute*>(self)->encoding);
}

void whiteout_utils_UtilsVertexBufferAttribute_set_encoding(whiteout_UtilsVertexBufferAttribute* self, int32_t value) {
    reinterpret_cast<whiteout::utils::VertexBuffer::Attribute*>(self)->encoding = static_cast<whiteout::utils::AttributeEncoding>(value);
}

uint64_t whiteout_utils_UtilsVertexBufferAttribute_get_component_count(const whiteout_UtilsVertexBufferAttribute* self) {
    return reinterpret_cast<const whiteout::utils::VertexBuffer::Attribute*>(self)->component_count;
}

void whiteout_utils_UtilsVertexBufferAttribute_set_component_count(whiteout_UtilsVertexBufferAttribute* self, uint64_t value) {
    reinterpret_cast<whiteout::utils::VertexBuffer::Attribute*>(self)->component_count = value;
}

uint64_t whiteout_utils_UtilsVertexBufferAttribute_get_offset(const whiteout_UtilsVertexBufferAttribute* self) {
    return reinterpret_cast<const whiteout::utils::VertexBuffer::Attribute*>(self)->offset;
}

void whiteout_utils_UtilsVertexBufferAttribute_set_offset(whiteout_UtilsVertexBufferAttribute* self, uint64_t value) {
    reinterpret_cast<whiteout::utils::VertexBuffer::Attribute*>(self)->offset = value;
}

} // extern "C"

// ── UtilsVertexBufferBuilder ─────────────────────────────────────────────────

extern "C" {

whiteout_UtilsVertexBufferBuilder* whiteout_utils_UtilsVertexBufferBuilder_new(void) {
    return reinterpret_cast<whiteout_UtilsVertexBufferBuilder*>(new whiteout::utils::VertexBufferBuilder());
}

void whiteout_utils_UtilsVertexBufferBuilder_delete(whiteout_UtilsVertexBufferBuilder* self) {
    delete reinterpret_cast<whiteout::utils::VertexBufferBuilder*>(self);
}

struct whiteout_UtilsVertexBufferBuilder* whiteout_utils_UtilsVertexBufferBuilder_declareFloatAttribute(whiteout_UtilsVertexBufferBuilder* self, const float* data, size_t data_size, uint64_t components, int32_t attr_class, int32_t encoding, uint64_t align) {
    auto& __r = reinterpret_cast<whiteout::utils::VertexBufferBuilder*>(self)->declareFloatAttribute(std::span<const whiteout::f32>(data, data_size), components, static_cast<whiteout::utils::AttributeClass>(attr_class), static_cast<whiteout::utils::AttributeEncoding>(encoding), align);
    return const_cast<struct whiteout_UtilsVertexBufferBuilder*>(
        reinterpret_cast<const struct whiteout_UtilsVertexBufferBuilder*>(&__r));
}

struct whiteout_UtilsVertexBufferBuilder* whiteout_utils_UtilsVertexBufferBuilder_declareIntAttribute(whiteout_UtilsVertexBufferBuilder* self, const uint32_t* data, size_t data_size, uint64_t components, int32_t attr_class, int32_t encoding, uint64_t align) {
    auto& __r = reinterpret_cast<whiteout::utils::VertexBufferBuilder*>(self)->declareIntAttribute(std::span<const whiteout::u32>(data, data_size), components, static_cast<whiteout::utils::AttributeClass>(attr_class), static_cast<whiteout::utils::AttributeEncoding>(encoding), align);
    return const_cast<struct whiteout_UtilsVertexBufferBuilder*>(
        reinterpret_cast<const struct whiteout_UtilsVertexBufferBuilder*>(&__r));
}

struct whiteout_UtilsVertexBuffer* whiteout_utils_UtilsVertexBufferBuilder_build(const whiteout_UtilsVertexBufferBuilder* self, void* pool) {
    return reinterpret_cast<struct whiteout_UtilsVertexBuffer*>(
        new whiteout::utils::VertexBuffer(reinterpret_cast<const whiteout::utils::VertexBufferBuilder*>(self)->build(reinterpret_cast<whiteout::interfaces::WorkerPool*>(pool))));
}

} // extern "C"