summaryrefslogtreecommitdiff
path: root/shared/utility.h
blob: 76a814b820c26b021e99208d358a28c19d048f3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#ifndef DALI_DEMO_UTILITY_H
#define DALI_DEMO_UTILITY_H

/*
 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

#include <dali/dali.h>
#include <dali/devel-api/adaptor-framework/bitmap-loader.h>
#include <dali/public-api/rendering/geometry.h>
#include <dali/public-api/rendering/texture.h>

namespace DemoHelper
{

Dali::PixelData LoadPixelData( const char* imagePath,
                               Dali::ImageDimensions size,
                               Dali::FittingMode::Type fittingMode,
                               Dali::SamplingMode::Type samplingMode )
{
  Dali::BitmapLoader loader = Dali::BitmapLoader::New( imagePath, size, fittingMode, samplingMode );
  loader.Load();
  return loader.GetPixelData();
}

Dali::Texture LoadTexture( const char* imagePath,
                           Dali::ImageDimensions size = Dali::ImageDimensions(),
                           Dali::FittingMode::Type fittingMode = Dali::FittingMode::DEFAULT,
                           Dali::SamplingMode::Type samplingMode = Dali::SamplingMode::DEFAULT )
{
  Dali::PixelData pixelData = LoadPixelData(imagePath, size, fittingMode, samplingMode);
  Dali::Texture texture  = Dali::Texture::New( Dali::TextureType::TEXTURE_2D,
                                               pixelData.GetPixelFormat(),
                                               pixelData.GetWidth(),
                                               pixelData.GetHeight() );
  texture.Upload( pixelData );

  return texture;
}

Dali::Texture LoadStageFillingTexture( const char* imagePath )
{
  Dali::Vector2 stageSize = Dali::Stage::GetCurrent().GetSize();
  return LoadTexture( imagePath, Dali::ImageDimensions( stageSize.x, stageSize.y ), Dali::FittingMode::SCALE_TO_FILL, Dali::SamplingMode::BOX_THEN_LINEAR );
}

Dali::Geometry CreateTexturedQuad()
{
  struct Vertex
  {
    Dali::Vector2 position;
    Dali::Vector2 texCoord;
  };

  static const Vertex data[] = {{ Dali::Vector2( -0.5f, -0.5f ), Dali::Vector2( 0.0f, 0.0f ) },
                                { Dali::Vector2(  0.5f, -0.5f ), Dali::Vector2( 1.0f, 0.0f ) },
                                { Dali::Vector2( -0.5f,  0.5f ), Dali::Vector2( 0.0f, 1.0f ) },
                                { Dali::Vector2(  0.5f,  0.5f ), Dali::Vector2( 1.0f, 1.0f ) }};

  Dali::PropertyBuffer vertexBuffer;
  Dali::Property::Map vertexFormat;
  vertexFormat["aPosition"] = Dali::Property::VECTOR2;
  vertexFormat["aTexCoord"] = Dali::Property::VECTOR2;

  //Create a vertex buffer for vertex positions and texture coordinates
  vertexBuffer = Dali::PropertyBuffer::New( vertexFormat );
  vertexBuffer.SetData( data, 4u );

  //Create the geometry
  Dali::Geometry geometry = Dali::Geometry::New();
  geometry.AddVertexBuffer( vertexBuffer );
  geometry.SetType(Dali::Geometry::TRIANGLE_STRIP );

  return geometry;
}
} // DemoHelper

#endif // DALI_DEMO_UTILITY_H