24bfb1c2c616e92d0d65e37d7eacb353e6e1e9dd
[platform/core/uifw/dali-demo.git] / examples / fpp-game / game-model.cpp
1 /*
2  * Copyright (c) 2016 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 }
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().
49                                            Add( "aPosition", Dali::Property::VECTOR3 ).
50                                            Add( "aNormal", Dali::Property::VECTOR3 ).
51                                            Add( "aTexCoord", Dali::Property::VECTOR2 )
52                                            );
53
54   mVertexBuffer.SetData( bytes.data() + mHeader.dataBeginOffset, mHeader.vertexBufferSize/mHeader.vertexStride );
55
56   mGeometry = Dali::Geometry::New();
57   mGeometry.AddVertexBuffer( mVertexBuffer );
58   mGeometry.SetType( Dali::Geometry::TRIANGLES );
59
60   mUniqueId = HashString( filename );
61
62   mIsReady = true;
63 }
64
65 GameModel::~GameModel()
66 {
67 }
68
69 Dali::Geometry& GameModel::GetGeometry()
70 {
71   return mGeometry;
72 }
73
74 bool GameModel::IsReady()
75 {
76   return mIsReady;
77 }
78
79 uint32_t GameModel::GetUniqueId()
80 {
81   return mUniqueId;
82 }