382e118cc96e499092ca1d04e2ba44ef838d7859
[platform/core/uifw/dali-toolkit.git] / dali-scene-loader / public-api / utils.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // INTERNAL
19 #include "dali-scene-loader/public-api/utils.h"
20
21 // EXTERNAL
22 #include "dali/public-api/common/vector-wrapper.h"
23 #include "dali/public-api/animation/constraints.h"
24 #include <iostream>
25 #include <fstream>
26 #include <cstring>
27 #include <stdarg.h>
28
29 namespace Dali
30 {
31 namespace SceneLoader
32 {
33 namespace
34 {
35 thread_local char sExceptionFlingerMessageBuffer[ExceptionFlinger::MESSAGE_BUFFER_SIZE]{};
36 }
37
38 char* ExceptionFlinger::GetMessageBuffer() noexcept(true)
39 {
40   return sExceptionFlingerMessageBuffer;
41 }
42
43 std::string FormatString(const char* format, ...)
44 {
45   va_list vl;
46   va_start(vl, format);
47   va_list vl2;
48   va_copy(vl2, vl);
49
50   size_t sizeRequired = vsnprintf(nullptr, 0, format, vl);
51   va_end(vl);
52
53   ++sizeRequired;
54   std::string result(sizeRequired, '\0');
55   vsnprintf(&result[0], sizeRequired, format, vl2);
56   va_end(vl2);
57
58   return result;
59 }
60
61 std::string LoadTextFile(const char * path, bool* fail)
62 {
63   std::ifstream inFile(path);
64   if (inFile)
65   {
66     std::istreambuf_iterator<char> eos;
67     std::istreambuf_iterator<char> i(inFile.rdbuf());
68     return std::string(i, eos);
69   }
70   else if (fail)
71   {
72     *fail = true;
73   }
74   return std::string();
75 }
76
77 Geometry MakeTexturedQuadGeometry(TexturedQuadOptions::Type options)
78 {
79   Property::Map properties;
80   properties.Insert("aPosition", Property::VECTOR3);
81   properties.Insert("aTexCoord", Property::VECTOR2);
82
83   std::vector<uint8_t> bytes;
84   size_t stride = 0;
85   size_t uvOffset = 0;
86   struct
87   {
88     Vector3 aPosition;
89     Vector2 aTexCoord;
90   } vertices[] = {
91     { Vector3(-0.5f, 0.5f, 0.0f), Vector2(0.0f, .0f) },
92     { Vector3(0.5f, 0.5f, 0.0f), Vector2(1.0f, .0f) },
93     { Vector3(-0.5f, -0.5f, 0.0f), Vector2(0.0f, 1.0f) },
94     { Vector3(0.5f, -0.5f, 0.0f), Vector2(1.0f, 1.0f) }
95   };
96
97   bytes.resize(sizeof(vertices));
98   stride = sizeof(vertices[0]);
99   uvOffset = reinterpret_cast<const uint8_t*>(&vertices[0].aTexCoord) - reinterpret_cast<const uint8_t*>(&vertices[0]);
100
101   std::memcpy(bytes.data(), vertices, sizeof(vertices));
102
103   if (MaskMatch(options, TexturedQuadOptions::FLIP_VERTICAL))
104   {
105     Vector2* uv = reinterpret_cast<Vector2*>(reinterpret_cast<uint8_t*>(bytes.data()) + uvOffset);
106     for (int i = 0; i < 4; ++i)
107     {
108       uv->y = 1.0f - uv->y;
109       uv = reinterpret_cast<Vector2*>(reinterpret_cast<uint8_t*>(uv) + stride);
110     }
111   }
112
113   VertexBuffer vertexBuffer = VertexBuffer::New(properties);
114   vertexBuffer.SetData(bytes.data(), bytes.size() / stride);
115
116   Geometry geometry = Geometry::New();
117   geometry.AddVertexBuffer(vertexBuffer);
118   geometry.SetType(Geometry::TRIANGLE_STRIP);
119   return geometry;
120 }
121
122 void ToUnixFileSeparators(std::string& path)
123 {
124   std::replace(path.begin(), path.end(), '\\', '/');
125 }
126
127 }
128 }