Merge branch 'main' into dev
This commit is contained in:
@@ -381,7 +381,8 @@ assetload::postfunc assetload::model(
|
||||
|
||||
auto text = io::read_string(path);
|
||||
try {
|
||||
auto model = vcm::parse(path.string(), text).release();
|
||||
auto model = vcm::parse(path.string(), text, path.extension() == ".xml")
|
||||
.release();
|
||||
return [=](Assets* assets) {
|
||||
request_textures(loader, *model);
|
||||
assets->store(std::unique_ptr<model::Model>(model), name);
|
||||
|
||||
+3
-3
@@ -163,11 +163,11 @@ static std::unique_ptr<model::Model> load_model(const xmlelement& root) {
|
||||
}
|
||||
|
||||
std::unique_ptr<model::Model> vcm::parse(
|
||||
std::string_view file, std::string_view src
|
||||
std::string_view file, std::string_view src, bool usexml
|
||||
) {
|
||||
try {
|
||||
auto doc = io::path(std::string(file)).extension() == ".xml"
|
||||
? xml::parse(file, src) : xml::parse_vcm(file, src, "model");
|
||||
auto doc =
|
||||
usexml ? xml::parse(file, src) : xml::parse_vcm(file, src, "model");
|
||||
const auto& root = *doc->getRoot();
|
||||
if (root.getTag() != "model") {
|
||||
throw std::runtime_error(
|
||||
|
||||
+3
-1
@@ -8,5 +8,7 @@ namespace model {
|
||||
}
|
||||
|
||||
namespace vcm {
|
||||
std::unique_ptr<model::Model> parse(std::string_view file, std::string_view src);
|
||||
std::unique_ptr<model::Model> parse(
|
||||
std::string_view file, std::string_view src, bool usexml
|
||||
);
|
||||
}
|
||||
|
||||
@@ -55,7 +55,9 @@ static int l_parse_model(lua::State* L) {
|
||||
auto name = lua::require_string(L, 3);
|
||||
|
||||
if (format == "xml" || format == "vcm") {
|
||||
engine->getAssets()->store(vcm::parse(name, string), name);
|
||||
engine->getAssets()->store(
|
||||
vcm::parse(name, string, format == "xml"), name
|
||||
);
|
||||
} else {
|
||||
throw std::runtime_error("unknown format " + util::quote(std::string(format)));
|
||||
}
|
||||
|
||||
+36
-18
@@ -5,20 +5,30 @@
|
||||
namespace util {
|
||||
template<typename T, int capacity>
|
||||
class stack_vector {
|
||||
struct buffer {
|
||||
alignas(alignof(T)) char* data[sizeof(T) * capacity];
|
||||
|
||||
T* ptr() {
|
||||
return reinterpret_cast<T*>(data);
|
||||
}
|
||||
const T* ptr() const {
|
||||
return reinterpret_cast<const T*>(data);
|
||||
}
|
||||
};
|
||||
public:
|
||||
stack_vector() : size_(0) {}
|
||||
|
||||
stack_vector(const stack_vector<T, capacity>& other)
|
||||
: size_(other.size_) {
|
||||
for (int i = 0; i < size_; ++i) {
|
||||
new (&data_[i]) T(other.data_[i]);
|
||||
new (&data_.ptr()[i]) T(other.data_.ptr()[i]);
|
||||
}
|
||||
}
|
||||
|
||||
stack_vector(stack_vector<T, capacity>&& other) noexcept
|
||||
: size_(other.size_) {
|
||||
for (int i = 0; i < size_; ++i) {
|
||||
new (&data_[i]) T(std::move(other.data_[i]));
|
||||
new (&data_.ptr()[i]) T(std::move(other.data_.ptr()[i]));
|
||||
}
|
||||
other.size_ = 0;
|
||||
}
|
||||
@@ -29,17 +39,25 @@ namespace util {
|
||||
"initializer list exceeds stack vector capacity"
|
||||
);
|
||||
for (const auto& value : init) {
|
||||
new (&data_[size_++]) T(value);
|
||||
new (&data_.ptr()[size_++]) T(value);
|
||||
}
|
||||
}
|
||||
|
||||
~stack_vector() {
|
||||
clear();
|
||||
}
|
||||
~stack_vector() = default;
|
||||
|
||||
void push_back(const T& value) {
|
||||
if (size_ < capacity) {
|
||||
data_[size_++] = value;
|
||||
auto data = reinterpret_cast<char*>(data_.ptr() + (size_++));
|
||||
new (data) T(value);
|
||||
} else {
|
||||
throw std::overflow_error("stack vector capacity exceeded");
|
||||
}
|
||||
}
|
||||
|
||||
void push_back(T&& value) {
|
||||
if (size_ < capacity) {
|
||||
auto data = reinterpret_cast<char*>(data_.ptr() + (size_++));
|
||||
new (data) T(std::move(value));
|
||||
} else {
|
||||
throw std::overflow_error("stack vector capacity exceeded");
|
||||
}
|
||||
@@ -47,7 +65,7 @@ namespace util {
|
||||
|
||||
void pop_back() {
|
||||
if (size_ > 0) {
|
||||
data_[size_ - 1].~T();
|
||||
data_.ptr()[size_ - 1].~T();
|
||||
--size_;
|
||||
} else {
|
||||
throw std::underflow_error("stack vector is empty");
|
||||
@@ -56,31 +74,31 @@ namespace util {
|
||||
|
||||
void clear() {
|
||||
for (int i = 0; i < size_; ++i) {
|
||||
data_[i].~T();
|
||||
data_.ptr()[i].~T();
|
||||
}
|
||||
size_ = 0;
|
||||
}
|
||||
|
||||
T& operator[](int index) {
|
||||
return data_[index];
|
||||
return data_.ptr()[index];
|
||||
}
|
||||
|
||||
const T& operator[](int index) const {
|
||||
return data_[index];
|
||||
return data_.ptr()[index];
|
||||
}
|
||||
|
||||
T& at(int index) {
|
||||
if (index < 0 || index >= size_) {
|
||||
throw std::out_of_range("index out of range");
|
||||
}
|
||||
return data_[index];
|
||||
return data_.ptr()[index];
|
||||
}
|
||||
|
||||
const T& at(int index) const {
|
||||
if (index < 0 || index >= size_) {
|
||||
throw std::out_of_range("index out of range");
|
||||
}
|
||||
return data_[index];
|
||||
return data_.ptr()[index];
|
||||
}
|
||||
|
||||
int size() const {
|
||||
@@ -96,22 +114,22 @@ namespace util {
|
||||
}
|
||||
|
||||
auto begin() {
|
||||
return data_;
|
||||
return data_.ptr();
|
||||
}
|
||||
|
||||
auto end() {
|
||||
return data_ + size_;
|
||||
return data_.ptr() + size_;
|
||||
}
|
||||
|
||||
auto begin() const {
|
||||
return data_;
|
||||
return data_.ptr();
|
||||
}
|
||||
|
||||
auto end() const {
|
||||
return data_ + size_;
|
||||
return data_.ptr() + size_;
|
||||
}
|
||||
private:
|
||||
T data_[capacity];
|
||||
buffer data_;
|
||||
int size_;
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user