KeyEvent class pimpling
[platform/core/uifw/dali-demo.git] / examples / deferred-shading / deferred-shading.cpp
index 425e533..e09032f 100644 (file)
@@ -445,8 +445,8 @@ Renderer CreateRenderer(TextureSet textures, Geometry geometry, Shader shader, u
 //=============================================================================
 void CenterActor(Actor actor)
 {
-  actor.SetAnchorPoint( AnchorPoint::CENTER );
-  actor.SetParentOrigin( ParentOrigin::CENTER );
+  actor.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER );
+  actor.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
 }
 
 //=============================================================================
@@ -458,6 +458,26 @@ void RegisterDepthProperties(float depth, float near, Handle& h)
 }
 
 //=============================================================================
+/// Create a String whose size can be evaluated at compile time
+struct ConstantString
+{
+  const char * const string;
+  const uint16_t size;
+
+  template<uint16_t inputSize>
+  constexpr ConstantString(const char (&input)[inputSize])
+  : string(input),
+    size(inputSize)
+  {
+  }
+};
+
+constexpr ConstantString POSITION_STRING("position");
+constexpr ConstantString RADIUS_STRING("radius");
+constexpr ConstantString COLOR_STRING("color");
+constexpr uint16_t LIGHT_SOURCE_BUFFER_SIZE(128u);
+
+//=============================================================================
 class DeferredShadingExample : public ConnectionTracker
 {
 public:
@@ -481,24 +501,22 @@ public:
 private:
   void Create(Application& app)
   {
-    // Grab stage, configure layer
-    Stage stage = Stage::GetCurrent();
-    auto rootLayer = stage.GetRootLayer();
-    rootLayer.SetBehavior(Layer::LAYER_3D);
+    // Grab window, configure layer
+    Window window = app.GetWindow();
+    auto rootLayer = window.GetRootLayer();
+    rootLayer.SetProperty( Layer::Property::BEHAVIOR, Layer::LAYER_3D );
 
-    auto stageSize = stage.GetSize();
-    auto stageHalfSize = stageSize * .5f;
-    auto invStageHalfSize = Vector2::ONE / stageHalfSize;
+    Vector2 windowSize = window.GetSize();
 
-    float unit = stageSize.y / 24.f;
+    float unit = windowSize.y / 24.f;
 
     // Get camera - we'll be re-using the same old camera in the two passes.
-    RenderTaskList tasks = stage.GetRenderTaskList();
+    RenderTaskList tasks = window.GetRenderTaskList();
     CameraActor camera = tasks.GetTask(0).GetCameraActor();
 
     auto zCameraPos = camera.GetProperty(Actor::Property::POSITION_Z).Get<float>();
-    camera.SetFarClippingPlane(zCameraPos + stageSize.y * .5f);
-    camera.SetNearClippingPlane(zCameraPos - stageSize.y * .5f);
+    camera.SetFarClippingPlane(zCameraPos + windowSize.y * .5f);
+    camera.SetNearClippingPlane(zCameraPos - windowSize.y * .5f);
 
     const float zNear = camera.GetNearClippingPlane();
     const float zFar = camera.GetFarClippingPlane();
@@ -509,7 +527,7 @@ private:
     CenterActor(sceneRoot);
 
     mSceneRoot = sceneRoot;
-    stage.Add(sceneRoot);
+    window.Add(sceneRoot);
 
     // Create an axis to spin our actors around.
     auto axis = Actor::New();
@@ -526,7 +544,6 @@ private:
     Renderer meshRenderer = CreateRenderer(noTexturesThanks, mesh, preShader,
         OPTION_DEPTH_TEST | OPTION_DEPTH_WRITE);
     meshRenderer.SetProperty(Renderer::Property::FACE_CULLING_MODE, FaceCullingMode::BACK);
-    meshRenderer.RegisterProperty("uInvStageHalfSize", invStageHalfSize);
     RegisterDepthProperties(depth, zNear, meshRenderer);
     float c = 1.f;
     for (auto v: {
@@ -559,13 +576,13 @@ private:
       CenterActor(a);
 
       Vector3 position{ v * unit * 5.f };
-      a.SetPosition(position);
+      a.SetProperty( Actor::Property::POSITION, position );
 
       float scale = (c + ((v.x + v.y + v.z) + c * 3.f) * .5f) / (c * 4.f);
       Vector3 size{ Vector3::ONE * scale * unit * 2.f };
-      a.SetSize(size);
+      a.SetProperty( Actor::Property::SIZE, size);
 
-      a.SetColor(Color::WHITE * .25f +
+      a.SetProperty( Actor::Property::COLOR,Color::WHITE * .25f +
           (Color::RED * (v.x + c) / (c * 2.f) +
           Color::GREEN * (v.y + c) / (c * 2.f) +
           Color::BLUE * (v.z + c) / (c * 2.f)) * .015625f);
@@ -575,8 +592,8 @@ private:
     }
 
     // Create off-screen textures, fbo and render task.
-    uint32_t width = static_cast<uint32_t>(stageSize.x);
-    uint32_t height = static_cast<uint32_t>(stageSize.y);
+    uint32_t width = static_cast<uint32_t>(windowSize.x);
+    uint32_t height = static_cast<uint32_t>(windowSize.y);
 
     Texture rttNormal = Texture::New(TextureType::TEXTURE_2D, Pixel::Format::RGB888,
         width, height);
@@ -590,7 +607,7 @@ private:
     fbo.AttachColorTexture(rttColor);
 
     RenderTask sceneRender = tasks.CreateTask();
-    sceneRender.SetViewportSize(stageSize);
+    sceneRender.SetViewportSize(windowSize);
     sceneRender.SetFrameBuffer(fbo);
     sceneRender.SetCameraActor(camera);
     sceneRender.SetSourceActor(sceneRoot);
@@ -605,7 +622,7 @@ private:
     // Create final image for deferred shading
     auto finalImage = Actor::New();
     CenterActor(finalImage);
-    finalImage.SetSize(stageSize);
+    finalImage.SetProperty( Actor::Property::SIZE, windowSize);
 
     TextureSet finalImageTextures = TextureSet::New();
     finalImageTextures.SetTexture(0, rttNormal);
@@ -621,7 +638,6 @@ private:
     Shader shdMain = Shader::New(MAINPASS_VSH, MAINPASS_FSH);
     Geometry finalImageGeom = CreateTexturedQuadGeometry(true);
     Renderer finalImageRenderer = CreateRenderer(finalImageTextures, finalImageGeom, shdMain);
-    finalImageRenderer.RegisterProperty("uStageHalfSize", stageHalfSize);
     RegisterDepthProperties(depth, zNear, finalImageRenderer);
 
     auto propInvProjection = finalImageRenderer.RegisterProperty("uInvProjection", Matrix::IDENTITY);
@@ -637,7 +653,7 @@ private:
     finalImage.AddRenderer(finalImageRenderer);
 
     mFinalImage = finalImage;
-    stage.Add(finalImage);
+    window.Add(finalImage);
 
     // Create a node for our lights
     auto lights = Actor::New();
@@ -684,16 +700,16 @@ private:
     animLights.Play();
 
     // Event handling
-    stage.KeyEventSignal().Connect(this, &DeferredShadingExample::OnKeyEvent);
+    window.KeyEventSignal().Connect(this, &DeferredShadingExample::OnKeyEvent);
 
     mPanDetector = PanGestureDetector::New();
     mPanDetector.DetectedSignal().Connect(this, &DeferredShadingExample::OnPan);
-    mPanDetector.Attach(stage.GetRootLayer());
+    mPanDetector.Attach(window.GetRootLayer());
   }
 
   void Destroy(Application& app)
   {
-    Stage::GetCurrent().GetRenderTaskList().RemoveTask(mSceneRender);
+    app.GetWindow().GetRenderTaskList().RemoveTask(mSceneRender);
     mSceneRender.Reset();
 
     UnparentAndReset(mSceneRoot);
@@ -711,17 +727,17 @@ private:
     auto iPropLightColor = light.RegisterProperty("lightcolor", color);
 
     // Create light source uniforms on lighting shader.
-    char buffer[128];
-    char* writep = buffer + sprintf(buffer, "uLights[%d].", mNumLights);
+    char buffer[LIGHT_SOURCE_BUFFER_SIZE];
+    char* writep = buffer + snprintf(buffer, LIGHT_SOURCE_BUFFER_SIZE, "uLights[%d].", mNumLights);
     ++mNumLights;
 
-    strcpy(writep, "position");
+    strncpy(writep, POSITION_STRING.string, POSITION_STRING.size);
     auto oPropLightPos = renderer.RegisterProperty(buffer, position);
 
-    strcpy(writep, "radius");
+    strncpy(writep, RADIUS_STRING.string, RADIUS_STRING.size);
     auto oPropLightRadius = renderer.RegisterProperty(buffer, radius);
 
-    strcpy(writep, "color");
+    strncpy(writep, COLOR_STRING.string, COLOR_STRING.size);
     auto oPropLightColor = renderer.RegisterProperty(buffer, color);
 
     // Constrain the light position, radius and color to lighting shader uniforms.
@@ -761,7 +777,7 @@ private:
 
   void OnKeyEvent(const KeyEvent& event)
   {
-    if(event.state == KeyEvent::Down)
+    if(event.GetState() == KeyEvent::Down)
     {
       if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
       {