Updated demos to use DALi clang-format
[platform/core/uifw/dali-demo.git] / examples / fpp-game / game-model.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 #include "game-model.h"
19 #include "game-utils.h"
20
21 using namespace GameUtils;
22
23 namespace
24 {
25 // 'MODV' tag stored in the big-endian (network) order
26 const uint32_t MODV_TAG(0x4D4F4456);
27 } // namespace
28
29 GameModel::GameModel(const char* filename)
30 : mUniqueId(false),
31   mIsReady(false)
32 {
33   ByteArray bytes;
34   if(!LoadFile(filename, bytes))
35   {
36     return;
37   }
38
39   mHeader = *(reinterpret_cast<ModelHeader*>(bytes.data()));
40
41   // expect big-endian
42   if(MODV_TAG != mHeader.tag)
43   {
44     // jump to little-endian variant
45     mHeader = *(reinterpret_cast<ModelHeader*>(bytes.data() + bytes.size() / 2));
46   }
47
48   mVertexBuffer = Dali::VertexBuffer::New(Dali::Property::Map().Add("aPosition", Dali::Property::VECTOR3).Add("aNormal", Dali::Property::VECTOR3).Add("aTexCoord", Dali::Property::VECTOR2));
49
50   mVertexBuffer.SetData(bytes.data() + mHeader.dataBeginOffset, mHeader.vertexBufferSize / mHeader.vertexStride);
51
52   mGeometry = Dali::Geometry::New();
53   mGeometry.AddVertexBuffer(mVertexBuffer);
54   mGeometry.SetType(Dali::Geometry::TRIANGLES);
55
56   mUniqueId = HashString(filename);
57
58   mIsReady = true;
59 }
60
61 GameModel::~GameModel()
62 {
63 }
64
65 Dali::Geometry& GameModel::GetGeometry()
66 {
67   return mGeometry;
68 }
69
70 bool GameModel::IsReady()
71 {
72   return mIsReady;
73 }
74
75 uint32_t GameModel::GetUniqueId()
76 {
77   return mUniqueId;
78 }