Changes following dali-core patch "Renamed enum Geometry::GeometryType to Geometry...
[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/images/atlas.h>
23 #include <dali/devel-api/adaptor-framework/bitmap-loader.h>
24 #include <dali/public-api/rendering/geometry.h>
25 #include <dali/public-api/rendering/texture.h>
26
27 namespace DemoHelper
28 {
29
30 Dali::PixelData LoadPixelData( const char* imagePath,
31                                Dali::ImageDimensions size,
32                                Dali::FittingMode::Type fittingMode,
33                                Dali::SamplingMode::Type samplingMode )
34 {
35   Dali::BitmapLoader loader = Dali::BitmapLoader::New( imagePath, size, fittingMode, samplingMode );
36   loader.Load();
37   return loader.GetPixelData();
38 }
39
40
41 Dali::Atlas LoadImage( const char* imagePath,
42                        Dali::ImageDimensions size = Dali::ImageDimensions(),
43                        Dali::FittingMode::Type fittingMode = Dali::FittingMode::DEFAULT,
44                        Dali::SamplingMode::Type samplingMode = Dali::SamplingMode::DEFAULT )
45 {
46   Dali::PixelData pixelData = LoadPixelData(imagePath, size, fittingMode, samplingMode);
47   Dali::Atlas image  =Dali:: Atlas::New( pixelData.GetWidth(), pixelData.GetHeight(), pixelData.GetPixelFormat() );
48   image.Upload( pixelData, 0u, 0u );
49
50   return image;
51 }
52
53 Dali::Texture LoadTexture( const char* imagePath,
54                            Dali::ImageDimensions size = Dali::ImageDimensions(),
55                            Dali::FittingMode::Type fittingMode = Dali::FittingMode::DEFAULT,
56                            Dali::SamplingMode::Type samplingMode = Dali::SamplingMode::DEFAULT )
57 {
58   Dali::PixelData pixelData = LoadPixelData(imagePath, size, fittingMode, samplingMode);
59   Dali::Texture texture  = Dali::Texture::New( Dali::TextureType::TEXTURE_2D,
60                                                pixelData.GetPixelFormat(),
61                                                pixelData.GetWidth(),
62                                                pixelData.GetHeight() );
63   texture.Upload( pixelData );
64
65   return texture;
66 }
67
68 /**
69  * @brief Load an bitmap resource.
70  *
71  * If it is required to scaled-down to no more than the stage dimensions,
72  * uses image scaling mode FittingMode::SCALE_TO_FILL to resize the image at
73  * load time to cover the entire stage with pixels with no borders,
74  * and filter mode BOX_THEN_LINEAR to sample the image with
75  * maximum quality.
76  */
77
78 Dali::Atlas LoadStageFillingImage( const char* imagePath )
79 {
80   Dali::Vector2 stageSize = Dali::Stage::GetCurrent().GetSize();
81   return LoadImage( imagePath, Dali::ImageDimensions( stageSize.x, stageSize.y ), Dali::FittingMode::SCALE_TO_FILL, Dali::SamplingMode::BOX_THEN_LINEAR );
82 }
83
84 Dali::Texture LoadStageFillingTexture( const char* imagePath )
85 {
86   Dali::Vector2 stageSize = Dali::Stage::GetCurrent().GetSize();
87   return LoadTexture( imagePath, Dali::ImageDimensions( stageSize.x, stageSize.y ), Dali::FittingMode::SCALE_TO_FILL, Dali::SamplingMode::BOX_THEN_LINEAR );
88 }
89
90 Dali::Geometry CreateTexturedQuad()
91 {
92   struct Vertex
93   {
94     Dali::Vector2 position;
95     Dali::Vector2 texCoord;
96   };
97
98   static const Vertex data[] = {{ Dali::Vector2( -0.5f, -0.5f ), Dali::Vector2( 0.0f, 0.0f ) },
99                                 { Dali::Vector2(  0.5f, -0.5f ), Dali::Vector2( 1.0f, 0.0f ) },
100                                 { Dali::Vector2( -0.5f,  0.5f ), Dali::Vector2( 0.0f, 1.0f ) },
101                                 { Dali::Vector2(  0.5f,  0.5f ), Dali::Vector2( 1.0f, 1.0f ) }};
102
103   Dali::PropertyBuffer vertexBuffer;
104   Dali::Property::Map vertexFormat;
105   vertexFormat["aPosition"] = Dali::Property::VECTOR2;
106   vertexFormat["aTexCoord"] = Dali::Property::VECTOR2;
107
108   //Create a vertex buffer for vertex positions and texture coordinates
109   vertexBuffer = Dali::PropertyBuffer::New( vertexFormat );
110   vertexBuffer.SetData( data, 4u );
111
112   //Create the geometry
113   Dali::Geometry geometry = Dali::Geometry::New();
114   geometry.AddVertexBuffer( vertexBuffer );
115   geometry.SetType(Dali::Geometry::TRIANGLE_STRIP );
116
117   return geometry;
118 }
119 } // DemoHelper
120
121 #endif // DALI_DEMO_UTILITY_H