DALi Version 1.2.29
[platform/core/uifw/dali-demo.git] / shared / utility.h
1 #ifndef DALI_DEMO_UTILITY_H
2 #define DALI_DEMO_UTILITY_H
3
4 /*
5  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 #include <dali/dali.h>
22 #include <dali/devel-api/adaptor-framework/bitmap-loader.h>
23 #include <dali/public-api/rendering/geometry.h>
24 #include <dali/public-api/rendering/texture.h>
25
26 namespace DemoHelper
27 {
28
29 Dali::PixelData LoadPixelData( const char* imagePath,
30                                Dali::ImageDimensions size,
31                                Dali::FittingMode::Type fittingMode,
32                                Dali::SamplingMode::Type samplingMode )
33 {
34   Dali::BitmapLoader loader = Dali::BitmapLoader::New( imagePath, size, fittingMode, samplingMode );
35   loader.Load();
36   return loader.GetPixelData();
37 }
38
39 Dali::Texture LoadTexture( const char* imagePath,
40                            Dali::ImageDimensions size = Dali::ImageDimensions(),
41                            Dali::FittingMode::Type fittingMode = Dali::FittingMode::DEFAULT,
42                            Dali::SamplingMode::Type samplingMode = Dali::SamplingMode::DEFAULT )
43 {
44   Dali::PixelData pixelData = LoadPixelData(imagePath, size, fittingMode, samplingMode);
45   Dali::Texture texture  = Dali::Texture::New( Dali::TextureType::TEXTURE_2D,
46                                                pixelData.GetPixelFormat(),
47                                                pixelData.GetWidth(),
48                                                pixelData.GetHeight() );
49   texture.Upload( pixelData );
50
51   return texture;
52 }
53
54 Dali::Texture LoadStageFillingTexture( const char* imagePath )
55 {
56   Dali::Vector2 stageSize = Dali::Stage::GetCurrent().GetSize();
57   return LoadTexture( imagePath, Dali::ImageDimensions( stageSize.x, stageSize.y ), Dali::FittingMode::SCALE_TO_FILL, Dali::SamplingMode::BOX_THEN_LINEAR );
58 }
59
60 Dali::Geometry CreateTexturedQuad()
61 {
62   struct Vertex
63   {
64     Dali::Vector2 position;
65     Dali::Vector2 texCoord;
66   };
67
68   static const Vertex data[] = {{ Dali::Vector2( -0.5f, -0.5f ), Dali::Vector2( 0.0f, 0.0f ) },
69                                 { Dali::Vector2(  0.5f, -0.5f ), Dali::Vector2( 1.0f, 0.0f ) },
70                                 { Dali::Vector2( -0.5f,  0.5f ), Dali::Vector2( 0.0f, 1.0f ) },
71                                 { Dali::Vector2(  0.5f,  0.5f ), Dali::Vector2( 1.0f, 1.0f ) }};
72
73   Dali::PropertyBuffer vertexBuffer;
74   Dali::Property::Map vertexFormat;
75   vertexFormat["aPosition"] = Dali::Property::VECTOR2;
76   vertexFormat["aTexCoord"] = Dali::Property::VECTOR2;
77
78   //Create a vertex buffer for vertex positions and texture coordinates
79   vertexBuffer = Dali::PropertyBuffer::New( vertexFormat );
80   vertexBuffer.SetData( data, 4u );
81
82   //Create the geometry
83   Dali::Geometry geometry = Dali::Geometry::New();
84   geometry.AddVertexBuffer( vertexBuffer );
85   geometry.SetType(Dali::Geometry::TRIANGLE_STRIP );
86
87   return geometry;
88 }
89 } // DemoHelper
90
91 #endif // DALI_DEMO_UTILITY_H