99dfe791c0110e295ff7b11f7c722076db981ac3
[platform/core/uifw/dali-demo.git] / examples / compressed-texture-formats / compressed-texture-formats-example.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 <dali-toolkit/dali-toolkit.h>
19
20 using namespace Dali;
21 using Dali::Toolkit::TextLabel;
22
23 const char* IMAGE_FILENAME_ETC         =        DEMO_IMAGE_DIR "tx-etc1.ktx";
24 const char* IMAGE_FILENAME_ASTC_LINEAR =        DEMO_IMAGE_DIR "tx-astc-4x4-linear.ktx";
25 const char* IMAGE_FILENAME_ASTC_LINEAR_NATIVE = DEMO_IMAGE_DIR "tx-astc-4x4-linear-native.astc";
26
27 /**
28  * @brief This example shows 3 images, each of a different compressed texture type.
29  * If built and run on a OpenGL ES 3.1 compatable target, then all 3 images will display.
30  * Otherwise, the top image will display and the other 2 will appear as black squares.
31  */
32 class CompressedTextureFormatsController : public ConnectionTracker
33 {
34 public:
35
36   CompressedTextureFormatsController( Application& application )
37   : mApplication( application )
38   {
39     // Connect to the Application's Init signal
40     mApplication.InitSignal().Connect( this, &CompressedTextureFormatsController::Create );
41   }
42
43   ~CompressedTextureFormatsController()
44   {
45     // Nothing to do here;
46   }
47
48   // The Init signal is received once (only) during the Application lifetime
49   void Create( Application& application )
50   {
51     // Get a handle to the stage
52     Stage stage = Stage::GetCurrent();
53     stage.SetBackgroundColor( Color::WHITE );
54
55     // Setup a TableView to hold a grid of images and labels.
56     Toolkit::TableView table = Toolkit::TableView::New( 3u, 2u );
57     table.SetAnchorPoint( AnchorPoint::CENTER );
58     table.SetParentOrigin( ParentOrigin::CENTER );
59     table.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
60     table.SetRelativeWidth( 0u, 0.5f );
61     table.SetRelativeWidth( 1u, 0.5f );
62     table.SetRelativeHeight( 0u, 1.0f / 3.0f );
63     table.SetRelativeHeight( 1u, 1.0f / 3.0f );
64     table.SetRelativeHeight( 2u, 1.0f / 3.0f );
65
66
67     // Add text labels.
68     TextLabel textLabel = TextLabel::New( "ETC1 (KTX):" );
69     textLabel.SetAnchorPoint( AnchorPoint::CENTER );
70     textLabel.SetParentOrigin( ParentOrigin::CENTER );
71     textLabel.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
72     textLabel.SetProperty( Toolkit::TextLabel::Property::MULTI_LINE, true );
73     table.AddChild( textLabel, Toolkit::TableView::CellPosition( 0u, 0u ) );
74     table.SetCellAlignment( Toolkit::TableView::CellPosition( 0u, 0u ), HorizontalAlignment::LEFT, VerticalAlignment::CENTER );
75
76     textLabel = TextLabel::New( "ASTC (KTX) 4x4 linear:" );
77     textLabel.SetAnchorPoint( AnchorPoint::CENTER );
78     textLabel.SetParentOrigin( ParentOrigin::CENTER );
79     textLabel.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
80     textLabel.SetProperty( Toolkit::TextLabel::Property::MULTI_LINE, true );
81     table.AddChild( textLabel, Toolkit::TableView::CellPosition( 1u, 0u ) );
82     table.SetCellAlignment( Toolkit::TableView::CellPosition( 1u, 0u ), HorizontalAlignment::LEFT, VerticalAlignment::CENTER );
83
84     textLabel = TextLabel::New( "ASTC (Native) 4x4 linear:" );
85     textLabel.SetAnchorPoint( AnchorPoint::CENTER );
86     textLabel.SetParentOrigin( ParentOrigin::CENTER );
87     textLabel.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
88     textLabel.SetProperty( Toolkit::TextLabel::Property::MULTI_LINE, true );
89     table.AddChild( textLabel, Toolkit::TableView::CellPosition( 2u, 0u ) );
90     table.SetCellAlignment( Toolkit::TableView::CellPosition( 2u, 0u ), HorizontalAlignment::LEFT, VerticalAlignment::CENTER );
91
92     // Add images.
93     Toolkit::ImageView imageView = Toolkit::ImageView::New( ResourceImage::New( IMAGE_FILENAME_ETC ) );
94     imageView.SetAnchorPoint( AnchorPoint::CENTER );
95     imageView.SetParentOrigin( ParentOrigin::CENTER );
96     table.AddChild( imageView, Toolkit::TableView::CellPosition( 0u, 1u ) );
97
98     imageView = Toolkit::ImageView::New( ResourceImage::New( IMAGE_FILENAME_ASTC_LINEAR ) );
99     imageView.SetAnchorPoint( AnchorPoint::CENTER );
100     imageView.SetParentOrigin( ParentOrigin::CENTER );
101     table.AddChild( imageView, Toolkit::TableView::CellPosition( 1u, 1u ) );
102
103     imageView = Toolkit::ImageView::New( ResourceImage::New( IMAGE_FILENAME_ASTC_LINEAR_NATIVE ) );
104     imageView.SetAnchorPoint( AnchorPoint::CENTER );
105     imageView.SetParentOrigin( ParentOrigin::CENTER );
106     table.AddChild( imageView, Toolkit::TableView::CellPosition( 2u, 1u ) );
107
108     stage.Add( table );
109
110     // Respond to a click anywhere on the stage
111     stage.GetRootLayer().TouchedSignal().Connect( this, &CompressedTextureFormatsController::OnTouch );
112   }
113
114   bool OnTouch( Actor actor, const TouchEvent& touch )
115   {
116     // quit the application
117     mApplication.Quit();
118     return true;
119   }
120
121 private:
122   Application&  mApplication;
123 };
124
125 void RunTest( Application& application )
126 {
127   CompressedTextureFormatsController test( application );
128
129   application.MainLoop();
130 }
131
132 // Entry point for Linux & Tizen applications
133 //
134 int main( int argc, char **argv )
135 {
136   Application application = Application::New( &argc, &argv );
137
138   RunTest( application );
139
140   return 0;
141 }