Commit 0b58b0a13c3515cf23eac247257a695e85b2c6dd
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>
Showing
1 changed file
with
71 additions
and
19 deletions
examples/benchmark-2dphysics/benchmark-2d-physics-controller.cpp
| ... | ... | @@ -62,6 +62,12 @@ const cpBitmask COLLISION_MASK{0xfF}; |
| 62 | 62 | |
| 63 | 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 | 72 | * @brief The physics demo using Chipmunk2D APIs. |
| 67 | 73 | */ |
| ... | ... | @@ -86,13 +92,33 @@ public: |
| 86 | 92 | mWindow.GetRootLayer().TouchedSignal().Connect(this, &Physics2dBenchmarkController::OnTouched); |
| 87 | 93 | mWindow.SetBackgroundColor(Color::DARK_SLATE_GRAY); |
| 88 | 94 | |
| 89 | - CreateAnimationSimulation(); | |
| 95 | + mType = BenchmarkType::ANIMATION; | |
| 96 | + | |
| 97 | + CreateSimulation(); | |
| 90 | 98 | |
| 91 | 99 | mTimer = Timer::New(ANIMATION_TIME); |
| 92 | 100 | mTimer.TickSignal().Connect(this, &Physics2dBenchmarkController::AnimationSimFinished); |
| 93 | 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 | 122 | void CreateAnimationSimulation() |
| 97 | 123 | { |
| 98 | 124 | Window::WindowSize windowSize = mWindow.GetSize(); |
| ... | ... | @@ -154,19 +180,26 @@ public: |
| 154 | 180 | |
| 155 | 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 | 203 | return false; |
| 171 | 204 | } |
| 172 | 205 | |
| ... | ... | @@ -189,7 +222,7 @@ public: |
| 189 | 222 | auto actor = Actor::DownCast(source.GetTarget()); |
| 190 | 223 | if(actor) |
| 191 | 224 | { |
| 192 | - int index = actor["index"]; | |
| 225 | + int index = actor["index"]; | |
| 193 | 226 | mBallVelocity[index].x = fabsf(mBallVelocity[index].x); |
| 194 | 227 | ContinueAnimation(); |
| 195 | 228 | } |
| ... | ... | @@ -200,7 +233,7 @@ public: |
| 200 | 233 | auto actor = Actor::DownCast(source.GetTarget()); |
| 201 | 234 | if(actor) |
| 202 | 235 | { |
| 203 | - int index = actor["index"]; | |
| 236 | + int index = actor["index"]; | |
| 204 | 237 | mBallVelocity[index].x = -fabsf(mBallVelocity[index].x); |
| 205 | 238 | ContinueAnimation(); |
| 206 | 239 | } |
| ... | ... | @@ -211,7 +244,7 @@ public: |
| 211 | 244 | auto actor = Actor::DownCast(source.GetTarget()); |
| 212 | 245 | if(actor) |
| 213 | 246 | { |
| 214 | - int index = actor["index"]; | |
| 247 | + int index = actor["index"]; | |
| 215 | 248 | mBallVelocity[index].y = -fabsf(mBallVelocity[index].y); |
| 216 | 249 | ContinueAnimation(); |
| 217 | 250 | } |
| ... | ... | @@ -222,7 +255,7 @@ public: |
| 222 | 255 | auto actor = Actor::DownCast(source.GetTarget()); |
| 223 | 256 | if(actor) |
| 224 | 257 | { |
| 225 | - int index = actor["index"]; | |
| 258 | + int index = actor["index"]; | |
| 226 | 259 | mBallVelocity[index].y = fabsf(mBallVelocity[index].y); |
| 227 | 260 | ContinueAnimation(); |
| 228 | 261 | } |
| ... | ... | @@ -339,15 +372,32 @@ public: |
| 339 | 372 | |
| 340 | 373 | void OnTerminate(Application& application) |
| 341 | 374 | { |
| 375 | + UnparentAndReset(mAnimationSimRootActor); | |
| 342 | 376 | UnparentAndReset(mPhysicsRoot); |
| 343 | 377 | } |
| 344 | 378 | |
| 345 | 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 | 403 | bool OnTouched(Dali::Actor actor, const Dali::TouchEvent& touch) |
| ... | ... | @@ -371,6 +421,8 @@ private: |
| 371 | 421 | Application& mApplication; |
| 372 | 422 | Window mWindow; |
| 373 | 423 | |
| 424 | + BenchmarkType mType{BenchmarkType::ANIMATION}; | |
| 425 | + | |
| 374 | 426 | PhysicsAdaptor mPhysicsAdaptor; |
| 375 | 427 | std::vector<PhysicsActor> mBalls; |
| 376 | 428 | Matrix mPhysicsTransform; | ... | ... |