Example of First Person 3D game with DALI
[platform/core/uifw/dali-demo.git] / examples / fpp-game / game-texture.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 <stdio.h>
19
20 #include "game-texture.h"
21 #include "game-utils.h"
22
23 #include <dali-toolkit/public-api/image-loader/sync-image-loader.h>
24
25 GameTexture::GameTexture()
26 {
27 }
28
29 GameTexture::~GameTexture()
30 {
31 }
32
33 GameTexture::GameTexture( const char* filename ) :
34   mUniqueId( 0 )
35 {
36   Load( filename );
37 }
38
39 bool GameTexture::Load( const char* filename )
40 {
41   Dali::PixelData pixelData = Dali::Toolkit::SyncImageLoader::Load( filename );
42
43   if( !pixelData )
44   {
45     return false;
46   }
47
48   Dali::Texture texture = Dali::Texture::New( Dali::TextureType::TEXTURE_2D,
49                                   pixelData.GetPixelFormat(),
50                                   pixelData.GetWidth(),
51                                   pixelData.GetHeight() );
52   texture.Upload( pixelData );
53   texture.GenerateMipmaps();
54   Dali::TextureSet textureSet = Dali::TextureSet::New();
55   textureSet.SetTexture( 0, texture );
56   Dali::Sampler sampler = Dali::Sampler::New();
57   sampler.SetWrapMode( Dali::WrapMode::REPEAT, Dali::WrapMode::REPEAT, Dali::WrapMode::REPEAT );
58   sampler.SetFilterMode( Dali::FilterMode::LINEAR_MIPMAP_LINEAR, Dali::FilterMode::LINEAR );
59   textureSet.SetSampler( 0, sampler );
60
61   mTexture = texture;
62   mSampler = sampler;
63   mTextureSet = textureSet;
64
65   mUniqueId = GameUtils::HashString( filename );
66
67   mIsReady = true;
68
69   return true;
70 }
71
72 Dali::TextureSet& GameTexture::GetTextureSet()
73 {
74   return mTextureSet;
75 }
76
77 uint32_t GameTexture::GetUniqueId()
78 {
79   return mUniqueId;
80 }
81
82 bool GameTexture::IsReady()
83 {
84   return mIsReady;
85 }