2 * Copyright (c) 2022 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include "dali-scene3d/public-api/loader/utils.h"
26 #include "dali/public-api/animation/constraints.h"
27 #include "dali/public-api/common/vector-wrapper.h"
37 thread_local char sExceptionFlingerMessageBuffer[ExceptionFlinger::MESSAGE_BUFFER_SIZE]{};
40 StreamBuffer::StreamBuffer(char* buffer, size_t size) noexcept(true)
42 setp(buffer, buffer + size);
45 ExceptionFlinger::Impl::~Impl() noexcept(false)
47 throw DaliException(mLocation, GetMessageBuffer());
50 ExceptionFlinger::ExceptionFlinger(const char* location) noexcept(true)
52 mStreamBuffer(GetMessageBuffer(), MESSAGE_BUFFER_SIZE - 1),
53 mStream(&mStreamBuffer)
57 ExceptionFlinger::~ExceptionFlinger() noexcept(false)
62 char* ExceptionFlinger::GetMessageBuffer() noexcept(true)
64 return sExceptionFlingerMessageBuffer;
67 std::string FormatString(const char* format, ...)
74 size_t sizeRequired = vsnprintf(nullptr, 0, format, vl);
78 std::string result(sizeRequired, '\0');
79 vsnprintf(&result[0], sizeRequired, format, vl2);
85 std::string LoadTextFile(const char* path, bool* fail)
87 std::ifstream inFile(path);
90 std::istreambuf_iterator<char> eos;
91 std::istreambuf_iterator<char> i(inFile.rdbuf());
92 return std::string(i, eos);
101 Geometry MakeTexturedQuadGeometry(TexturedQuadOptions::Type options)
103 Property::Map properties;
104 properties.Insert("aPosition", Property::VECTOR3);
105 properties.Insert("aTexCoord", Property::VECTOR2);
107 std::vector<uint8_t> bytes;
115 {Vector3(-0.5f, 0.5f, 0.0f), Vector2(0.0f, .0f)},
116 {Vector3(0.5f, 0.5f, 0.0f), Vector2(1.0f, .0f)},
117 {Vector3(-0.5f, -0.5f, 0.0f), Vector2(0.0f, 1.0f)},
118 {Vector3(0.5f, -0.5f, 0.0f), Vector2(1.0f, 1.0f)}};
120 bytes.resize(sizeof(vertices));
121 stride = sizeof(vertices[0]);
122 uvOffset = reinterpret_cast<const uint8_t*>(&vertices[0].aTexCoord) - reinterpret_cast<const uint8_t*>(&vertices[0]);
124 std::memcpy(bytes.data(), vertices, sizeof(vertices));
126 if(MaskMatch(options, TexturedQuadOptions::FLIP_VERTICAL))
128 Vector2* uv = reinterpret_cast<Vector2*>(reinterpret_cast<uint8_t*>(bytes.data()) + uvOffset);
129 for(int i = 0; i < 4; ++i)
131 uv->y = 1.0f - uv->y;
132 uv = reinterpret_cast<Vector2*>(reinterpret_cast<uint8_t*>(uv) + stride);
136 VertexBuffer vertexBuffer = VertexBuffer::New(properties);
137 vertexBuffer.SetData(bytes.data(), bytes.size() / stride);
139 Geometry geometry = Geometry::New();
140 geometry.AddVertexBuffer(vertexBuffer);
141 geometry.SetType(Geometry::TRIANGLE_STRIP);
145 void ToUnixFileSeparators(std::string& path)
147 std::replace(path.begin(), path.end(), '\\', '/');
150 } // namespace Loader
151 } // namespace Scene3D