Commit 0625c16eafc903675c7229d732ab36c23d1c9070

Authored by Xiangyin Ma
1 parent da1a5669

Use Atlas in ItemView Demo

Change-Id: I703035788451cb057a7834199136cc70d22c87e4
examples/item-view/item-view-example.cpp
... ... @@ -99,6 +99,10 @@ const char* IMAGE_PATHS[] = {
99 99  
100 100 const unsigned int NUM_IMAGES = sizeof(IMAGE_PATHS) / sizeof(char*);
101 101  
  102 +const unsigned int IMAGE_WIDTH = 256;
  103 +const unsigned int IMAGE_HEIGHT = 256;
  104 +const unsigned int NUM_IMAGE_PER_ROW_IN_ATLAS = 8;
  105 +
102 106 AlphaFunction ALPHA_FUNCTIONS[] = { AlphaFunctions::Linear,
103 107 AlphaFunctions::EaseIn,
104 108 AlphaFunctions::EaseOut };
... ... @@ -312,6 +316,7 @@ public:
312 316 stage.Add( mReplaceButton );
313 317  
314 318 // Create the item view actor
  319 + mImageAtlas = CreateImageAtlas();
315 320 mItemView = ItemView::New(*this);
316 321 mItemView.SetParentOrigin(ParentOrigin::CENTER);
317 322 mItemView.SetAnchorPoint(AnchorPoint::CENTER);
... ... @@ -876,8 +881,12 @@ public: // From ItemFactory
876 881 virtual Actor NewItem(unsigned int itemId)
877 882 {
878 883 // Create an image actor for this item
879   - Image image = ResourceImage::New( IMAGE_PATHS[itemId % NUM_IMAGES] );
880   - Actor actor = ImageActor::New(image);
  884 + unsigned int imageId = itemId % NUM_IMAGES;
  885 + ImageActor::PixelArea pixelArea( (imageId%NUM_IMAGE_PER_ROW_IN_ATLAS)*IMAGE_WIDTH,
  886 + (imageId/NUM_IMAGE_PER_ROW_IN_ATLAS)*IMAGE_HEIGHT,
  887 + IMAGE_WIDTH,
  888 + IMAGE_HEIGHT );
  889 + Actor actor = ImageActor::New(mImageAtlas, pixelArea);
881 890 actor.SetPosition( INITIAL_OFFSCREEN_POSITION );
882 891  
883 892 // Add a border image child actor
... ... @@ -936,6 +945,23 @@ public: // From ItemFactory
936 945 private:
937 946  
938 947 /**
  948 + * Create an Atlas to tile the images inside.
  949 + */
  950 + Atlas CreateImageAtlas()
  951 + {
  952 + const unsigned int atlas_width = IMAGE_WIDTH*NUM_IMAGE_PER_ROW_IN_ATLAS;
  953 + const unsigned int atlas_height = IMAGE_HEIGHT*ceil( static_cast<float>(NUM_IMAGES)/ static_cast<float>(NUM_IMAGE_PER_ROW_IN_ATLAS));
  954 + Atlas atlas = Atlas::New(atlas_width, atlas_height, Pixel::RGB888);
  955 +
  956 + for( unsigned int i = 0; i < NUM_IMAGES; i++ )
  957 + {
  958 + atlas.Upload( IMAGE_PATHS[i], (i%NUM_IMAGE_PER_ROW_IN_ATLAS)*IMAGE_WIDTH, (i/NUM_IMAGE_PER_ROW_IN_ATLAS)*IMAGE_HEIGHT );
  959 + }
  960 +
  961 + return atlas;
  962 + }
  963 +
  964 + /**
939 965 * Sets/Updates the title of the View
940 966 * @param[in] title The new title for the view.
941 967 */
... ... @@ -1107,6 +1133,7 @@ private:
1107 1133  
1108 1134 ItemView mItemView;
1109 1135 Image mBorderImage;
  1136 + Atlas mImageAtlas;
1110 1137 unsigned int mCurrentLayout;
1111 1138 float mDurationSeconds;
1112 1139  
... ...