Updated demos to use DALi clang-format
[platform/core/uifw/dali-demo.git] / examples / fpp-game / game-texture.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 <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 : mUniqueId(0),
27   mIsReady(false)
28 {
29 }
30
31 GameTexture::~GameTexture()
32 {
33 }
34
35 GameTexture::GameTexture(const char* filename)
36 : mUniqueId(0),
37   mIsReady(false)
38 {
39   Load(filename);
40 }
41
42 bool GameTexture::Load(const char* filename)
43 {
44   Dali::PixelData pixelData = Dali::Toolkit::SyncImageLoader::Load(filename);
45
46   if(!pixelData)
47   {
48     return false;
49   }
50
51   Dali::Texture texture = Dali::Texture::New(Dali::TextureType::TEXTURE_2D,
52                                              pixelData.GetPixelFormat(),
53                                              pixelData.GetWidth(),
54                                              pixelData.GetHeight());
55   texture.Upload(pixelData);
56   texture.GenerateMipmaps();
57   Dali::TextureSet textureSet = Dali::TextureSet::New();
58   textureSet.SetTexture(0, texture);
59   Dali::Sampler sampler = Dali::Sampler::New();
60   sampler.SetWrapMode(Dali::WrapMode::REPEAT, Dali::WrapMode::REPEAT, Dali::WrapMode::REPEAT);
61   sampler.SetFilterMode(Dali::FilterMode::LINEAR_MIPMAP_LINEAR, Dali::FilterMode::LINEAR);
62   textureSet.SetSampler(0, sampler);
63
64   mTexture    = texture;
65   mSampler    = sampler;
66   mTextureSet = textureSet;
67
68   mUniqueId = GameUtils::HashString(filename);
69
70   mIsReady = true;
71
72   return true;
73 }
74
75 Dali::TextureSet& GameTexture::GetTextureSet()
76 {
77   return mTextureSet;
78 }
79
80 uint32_t GameTexture::GetUniqueId()
81 {
82   return mUniqueId;
83 }
84
85 bool GameTexture::IsReady()
86 {
87   return mIsReady;
88 }