Commit 0b58b0a13c3515cf23eac247257a695e85b2c6dd

Authored by Eunki, Hong
Committed by Adeel Kazmi
1 parent f6389206

Fix crash when resize window during Animation mode

When we are running on animation ball, mPhysicsAdaptor don't have body.
If then, when we resize the window, it will be asserted

Change-Id: I2c71a2b1e845d7257f581fcc41f997794ee78fa5
Signed-off-by: Eunki, Hong <eunkiki.hong@samsung.com>
examples/benchmark-2dphysics/benchmark-2d-physics-controller.cpp
@@ -62,6 +62,12 @@ const cpBitmask COLLISION_MASK{0xfF}; @@ -62,6 +62,12 @@ const cpBitmask COLLISION_MASK{0xfF};
62 62
63 const cpBitmask BALL_COLLIDES_WITH{BALL_GROUP | BOUNDS_GROUP}; 63 const cpBitmask BALL_COLLIDES_WITH{BALL_GROUP | BOUNDS_GROUP};
64 64
  65 +enum BenchmarkType
  66 +{
  67 + ANIMATION,
  68 + PHYSICS_2D,
  69 +};
  70 +
65 /** 71 /**
66 * @brief The physics demo using Chipmunk2D APIs. 72 * @brief The physics demo using Chipmunk2D APIs.
67 */ 73 */
@@ -86,13 +92,33 @@ public: @@ -86,13 +92,33 @@ public:
86 mWindow.GetRootLayer().TouchedSignal().Connect(this, &Physics2dBenchmarkController::OnTouched); 92 mWindow.GetRootLayer().TouchedSignal().Connect(this, &Physics2dBenchmarkController::OnTouched);
87 mWindow.SetBackgroundColor(Color::DARK_SLATE_GRAY); 93 mWindow.SetBackgroundColor(Color::DARK_SLATE_GRAY);
88 94
89 - CreateAnimationSimulation(); 95 + mType = BenchmarkType::ANIMATION;
  96 +
  97 + CreateSimulation();
90 98
91 mTimer = Timer::New(ANIMATION_TIME); 99 mTimer = Timer::New(ANIMATION_TIME);
92 mTimer.TickSignal().Connect(this, &Physics2dBenchmarkController::AnimationSimFinished); 100 mTimer.TickSignal().Connect(this, &Physics2dBenchmarkController::AnimationSimFinished);
93 mTimer.Start(); 101 mTimer.Start();
94 } 102 }
95 103
  104 + void CreateSimulation()
  105 + {
  106 + switch(mType)
  107 + {
  108 + case BenchmarkType::ANIMATION:
  109 + default:
  110 + {
  111 + CreateAnimationSimulation();
  112 + break;
  113 + }
  114 + case BenchmarkType::PHYSICS_2D:
  115 + {
  116 + CreatePhysicsSimulation();
  117 + break;
  118 + }
  119 + }
  120 + }
  121 +
96 void CreateAnimationSimulation() 122 void CreateAnimationSimulation()
97 { 123 {
98 Window::WindowSize windowSize = mWindow.GetSize(); 124 Window::WindowSize windowSize = mWindow.GetSize();
@@ -154,19 +180,26 @@ public: @@ -154,19 +180,26 @@ public:
154 180
155 bool AnimationSimFinished() 181 bool AnimationSimFinished()
156 { 182 {
157 - static bool first = true;  
158 - if(first) 183 + switch(mType)
159 { 184 {
160 - UnparentAndReset(mAnimationSimRootActor);  
161 - mBallAnimation.Stop();  
162 - mBallAnimation.Clear();  
163 - first = false; 185 + case BenchmarkType::ANIMATION:
  186 + default:
  187 + {
  188 + UnparentAndReset(mAnimationSimRootActor);
  189 + mBallAnimation.Stop();
  190 + mBallAnimation.Clear();
164 191
165 - CreatePhysicsSimulation();  
166 - return true;  
167 - } 192 + mType = BenchmarkType::PHYSICS_2D;
168 193
169 - mApplication.Quit(); 194 + CreateSimulation();
  195 + return true;
  196 + }
  197 + case BenchmarkType::PHYSICS_2D:
  198 + {
  199 + mApplication.Quit();
  200 + break;
  201 + }
  202 + }
170 return false; 203 return false;
171 } 204 }
172 205
@@ -189,7 +222,7 @@ public: @@ -189,7 +222,7 @@ public:
189 auto actor = Actor::DownCast(source.GetTarget()); 222 auto actor = Actor::DownCast(source.GetTarget());
190 if(actor) 223 if(actor)
191 { 224 {
192 - int index = actor["index"]; 225 + int index = actor["index"];
193 mBallVelocity[index].x = fabsf(mBallVelocity[index].x); 226 mBallVelocity[index].x = fabsf(mBallVelocity[index].x);
194 ContinueAnimation(); 227 ContinueAnimation();
195 } 228 }
@@ -200,7 +233,7 @@ public: @@ -200,7 +233,7 @@ public:
200 auto actor = Actor::DownCast(source.GetTarget()); 233 auto actor = Actor::DownCast(source.GetTarget());
201 if(actor) 234 if(actor)
202 { 235 {
203 - int index = actor["index"]; 236 + int index = actor["index"];
204 mBallVelocity[index].x = -fabsf(mBallVelocity[index].x); 237 mBallVelocity[index].x = -fabsf(mBallVelocity[index].x);
205 ContinueAnimation(); 238 ContinueAnimation();
206 } 239 }
@@ -211,7 +244,7 @@ public: @@ -211,7 +244,7 @@ public:
211 auto actor = Actor::DownCast(source.GetTarget()); 244 auto actor = Actor::DownCast(source.GetTarget());
212 if(actor) 245 if(actor)
213 { 246 {
214 - int index = actor["index"]; 247 + int index = actor["index"];
215 mBallVelocity[index].y = -fabsf(mBallVelocity[index].y); 248 mBallVelocity[index].y = -fabsf(mBallVelocity[index].y);
216 ContinueAnimation(); 249 ContinueAnimation();
217 } 250 }
@@ -222,7 +255,7 @@ public: @@ -222,7 +255,7 @@ public:
222 auto actor = Actor::DownCast(source.GetTarget()); 255 auto actor = Actor::DownCast(source.GetTarget());
223 if(actor) 256 if(actor)
224 { 257 {
225 - int index = actor["index"]; 258 + int index = actor["index"];
226 mBallVelocity[index].y = fabsf(mBallVelocity[index].y); 259 mBallVelocity[index].y = fabsf(mBallVelocity[index].y);
227 ContinueAnimation(); 260 ContinueAnimation();
228 } 261 }
@@ -339,15 +372,32 @@ public: @@ -339,15 +372,32 @@ public:
339 372
340 void OnTerminate(Application& application) 373 void OnTerminate(Application& application)
341 { 374 {
  375 + UnparentAndReset(mAnimationSimRootActor);
342 UnparentAndReset(mPhysicsRoot); 376 UnparentAndReset(mPhysicsRoot);
343 } 377 }
344 378
345 void OnWindowResize(Window window, Window::WindowSize newSize) 379 void OnWindowResize(Window window, Window::WindowSize newSize)
346 { 380 {
347 - auto scopedAccessor = mPhysicsAdaptor.GetPhysicsAccessor();  
348 - cpSpace* space = scopedAccessor->GetNative().Get<cpSpace*>();  
349 -  
350 - CreateBounds(space, newSize); 381 + switch(mType)
  382 + {
  383 + case BenchmarkType::ANIMATION:
  384 + default:
  385 + {
  386 + // TODO : Implement here if you want.
  387 + break;
  388 + }
  389 + case BenchmarkType::PHYSICS_2D:
  390 + {
  391 + if(mPhysicsAdaptor)
  392 + {
  393 + auto scopedAccessor = mPhysicsAdaptor.GetPhysicsAccessor();
  394 + cpSpace* space = scopedAccessor->GetNative().Get<cpSpace*>();
  395 +
  396 + CreateBounds(space, newSize);
  397 + }
  398 + break;
  399 + }
  400 + }
351 } 401 }
352 402
353 bool OnTouched(Dali::Actor actor, const Dali::TouchEvent& touch) 403 bool OnTouched(Dali::Actor actor, const Dali::TouchEvent& touch)
@@ -371,6 +421,8 @@ private: @@ -371,6 +421,8 @@ private:
371 Application& mApplication; 421 Application& mApplication;
372 Window mWindow; 422 Window mWindow;
373 423
  424 + BenchmarkType mType{BenchmarkType::ANIMATION};
  425 +
374 PhysicsAdaptor mPhysicsAdaptor; 426 PhysicsAdaptor mPhysicsAdaptor;
375 std::vector<PhysicsActor> mBalls; 427 std::vector<PhysicsActor> mBalls;
376 Matrix mPhysicsTransform; 428 Matrix mPhysicsTransform;