Cosmetic fixes in dali-scene-loader.
[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 StreamBuffer::StreamBuffer(char* buffer, size_t size) noexcept(true)
39 {
40   setp(buffer, buffer + size);
41 }
42
43 ExceptionFlinger::ExceptionFlinger(const char* location) noexcept(true)
44 : mLocation(location),
45   mStreamBuffer(GetMessageBuffer(), MESSAGE_BUFFER_SIZE - 1),
46   mStream(&mStreamBuffer)
47 {}
48
49 ExceptionFlinger::~ExceptionFlinger() noexcept(false)
50 {
51   operator<<('\0');
52   throw DaliException(mLocation, GetMessageBuffer());
53 }
54
55 char* ExceptionFlinger::GetMessageBuffer() noexcept(true)
56 {
57   return sExceptionFlingerMessageBuffer;
58 }
59
60 std::string FormatString(const char* format, ...)
61 {
62   va_list vl;
63   va_start(vl, format);
64   va_list vl2;
65   va_copy(vl2, vl);
66
67   size_t sizeRequired = vsnprintf(nullptr, 0, format, vl);
68   va_end(vl);
69
70   ++sizeRequired;
71   std::string result(sizeRequired, '\0');
72   vsnprintf(&result[0], sizeRequired, format, vl2);
73   va_end(vl2);
74
75   return result;
76 }
77
78 std::string LoadTextFile(const char * path, bool* fail)
79 {
80   std::ifstream inFile(path);
81   if (inFile)
82   {
83     std::istreambuf_iterator<char> eos;
84     std::istreambuf_iterator<char> i(inFile.rdbuf());
85     return std::string(i, eos);
86   }
87   else if (fail)
88   {
89     *fail = true;
90   }
91   return std::string();
92 }
93
94 Geometry MakeTexturedQuadGeometry(TexturedQuadOptions::Type options)
95 {
96   Property::Map properties;
97   properties.Insert("aPosition", Property::VECTOR3);
98   properties.Insert("aTexCoord", Property::VECTOR2);
99
100   std::vector<uint8_t> bytes;
101   size_t stride = 0;
102   size_t uvOffset = 0;
103   struct
104   {
105     Vector3 aPosition;
106     Vector2 aTexCoord;
107   } vertices[] = {
108     { Vector3(-0.5f, 0.5f, 0.0f), Vector2(0.0f, .0f) },
109     { Vector3(0.5f, 0.5f, 0.0f), Vector2(1.0f, .0f) },
110     { Vector3(-0.5f, -0.5f, 0.0f), Vector2(0.0f, 1.0f) },
111     { Vector3(0.5f, -0.5f, 0.0f), Vector2(1.0f, 1.0f) }
112   };
113
114   bytes.resize(sizeof(vertices));
115   stride = sizeof(vertices[0]);
116   uvOffset = reinterpret_cast<const uint8_t*>(&vertices[0].aTexCoord) - reinterpret_cast<const uint8_t*>(&vertices[0]);
117
118   std::memcpy(bytes.data(), vertices, sizeof(vertices));
119
120   if (MaskMatch(options, TexturedQuadOptions::FLIP_VERTICAL))
121   {
122     Vector2* uv = reinterpret_cast<Vector2*>(reinterpret_cast<uint8_t*>(bytes.data()) + uvOffset);
123     for (int i = 0; i < 4; ++i)
124     {
125       uv->y = 1.0f - uv->y;
126       uv = reinterpret_cast<Vector2*>(reinterpret_cast<uint8_t*>(uv) + stride);
127     }
128   }
129
130   VertexBuffer vertexBuffer = VertexBuffer::New(properties);
131   vertexBuffer.SetData(bytes.data(), bytes.size() / stride);
132
133   Geometry geometry = Geometry::New();
134   geometry.AddVertexBuffer(vertexBuffer);
135   geometry.SetType(Geometry::TRIANGLE_STRIP);
136   return geometry;
137 }
138
139 void ToUnixFileSeparators(std::string& path)
140 {
141   std::replace(path.begin(), path.end(), '\\', '/');
142 }
143
144 }
145 }