| 1 | /* Copyright (c) 2007 Scott Lembcke |
| 2 | * |
| 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | * of this software and associated documentation files (the "Software"), to deal |
| 5 | * in the Software without restriction, including without limitation the rights |
| 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | * copies of the Software, and to permit persons to whom the Software is |
| 8 | * furnished to do so, subject to the following conditions: |
| 9 | * |
| 10 | * The above copyright notice and this permission notice shall be included in |
| 11 | * all copies or substantial portions of the Software. |
| 12 | * |
| 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 19 | * SOFTWARE. |
| 20 | */ |
| 21 | |
| 22 | /* |
| 23 | IMPORTANT - READ ME! |
| 24 | |
| 25 | This file sets up a simple interface that the individual demos can use to get |
| 26 | a Chipmunk space running and draw what's in it. In order to keep the Chipmunk |
| 27 | examples clean and simple, they contain no graphics code. All drawing is done |
| 28 | by accessing the Chipmunk structures at a very low level. It is NOT |
| 29 | recommended to write a game or application this way as it does not scale |
| 30 | beyond simple shape drawing and is very dependent on implementation details |
| 31 | about Chipmunk which may change with little to no warning. |
| 32 | */ |
| 33 | |
| 34 | #include <stdbool.h> |
| 35 | #include <stdio.h> |
| 36 | #include <string.h> |
| 37 | #include <limits.h> |
| 38 | #include <stdarg.h> |
| 39 | |
| 40 | #include "chipmunk/chipmunk_private.h" |
| 41 | #include "ChipmunkDemo.h" |
| 42 | #include "ChipmunkDemoTextSupport.h" |
| 43 | |
| 44 | #include "sokol/sokol.h" |
| 45 | |
| 46 | #undef Convex |
| 47 | |
| 48 | static ChipmunkDemo demos[32]; |
| 49 | static int demo_count; |
| 50 | static int demo_index; |
| 51 | |
| 52 | static cpBool paused = cpFalse; |
| 53 | static cpBool step = cpFalse; |
| 54 | |
| 55 | static cpSpace *space; |
| 56 | |
| 57 | static double Accumulator; |
| 58 | static double LastTime; |
| 59 | int ChipmunkDemoTicks; |
| 60 | double ChipmunkDemoTime; |
| 61 | |
| 62 | cpVect ChipmunkDemoMouse; |
| 63 | cpBool ChipmunkDemoRightClick; |
| 64 | cpBool ChipmunkDemoRightDown; |
| 65 | cpVect ChipmunkDemoKeyboard; |
| 66 | |
| 67 | static cpBody *mouse_body = NULL; |
| 68 | static cpConstraint *mouse_joint = NULL; |
| 69 | |
| 70 | char const *ChipmunkDemoMessageString = NULL; |
| 71 | |
| 72 | #define GRABBABLE_MASK_BIT (1<<31) |
| 73 | cpShapeFilter GRAB_FILTER = {CP_NO_GROUP, GRABBABLE_MASK_BIT, GRABBABLE_MASK_BIT}; |
| 74 | cpShapeFilter NOT_GRABBABLE_FILTER = {CP_NO_GROUP, ~GRABBABLE_MASK_BIT, ~GRABBABLE_MASK_BIT}; |
| 75 | |
| 76 | cpVect view_translate = {0, 0}; |
| 77 | cpFloat view_scale = 1.0; |
| 78 | |
| 79 | static void ShapeFreeWrap(cpSpace *space, cpShape *shape, void *unused){ |
| 80 | cpSpaceRemoveShape(space, shape); |
| 81 | cpShapeFree(shape); |
| 82 | } |
| 83 | |
| 84 | static void PostShapeFree(cpShape *shape, cpSpace *space){ |
| 85 | cpSpaceAddPostStepCallback(space, (cpPostStepFunc)ShapeFreeWrap, shape, NULL); |
| 86 | } |
| 87 | |
| 88 | static void ConstraintFreeWrap(cpSpace *space, cpConstraint *constraint, void *unused){ |
| 89 | cpSpaceRemoveConstraint(space, constraint); |
| 90 | cpConstraintFree(constraint); |
| 91 | } |
| 92 | |
| 93 | static void PostConstraintFree(cpConstraint *constraint, cpSpace *space){ |
| 94 | cpSpaceAddPostStepCallback(space, (cpPostStepFunc)ConstraintFreeWrap, constraint, NULL); |
| 95 | } |
| 96 | |
| 97 | static void BodyFreeWrap(cpSpace *space, cpBody *body, void *unused){ |
| 98 | cpSpaceRemoveBody(space, body); |
| 99 | cpBodyFree(body); |
| 100 | } |
| 101 | |
| 102 | static void PostBodyFree(cpBody *body, cpSpace *space){ |
| 103 | cpSpaceAddPostStepCallback(space, (cpPostStepFunc)BodyFreeWrap, body, NULL); |
| 104 | } |
| 105 | |
| 106 | // Safe and future proof way to remove and free all objects that have been added to the space. |
| 107 | void |
| 108 | ChipmunkDemoFreeSpaceChildren(cpSpace *space) |
| 109 | { |
| 110 | // Must remove these BEFORE freeing the body or you will access dangling pointers. |
| 111 | cpSpaceEachShape(space, (cpSpaceShapeIteratorFunc)PostShapeFree, space); |
| 112 | cpSpaceEachConstraint(space, (cpSpaceConstraintIteratorFunc)PostConstraintFree, space); |
| 113 | |
| 114 | cpSpaceEachBody(space, (cpSpaceBodyIteratorFunc)PostBodyFree, space); |
| 115 | } |
| 116 | |
| 117 | static void |
| 118 | DrawCircle(cpVect p, cpFloat a, cpFloat r, cpSpaceDebugColor outline, cpSpaceDebugColor fill, cpDataPointer data) |
| 119 | {ChipmunkDebugDrawCircle(p, a, r, outline, fill);} |
| 120 | |
| 121 | static void |
| 122 | DrawSegment(cpVect a, cpVect b, cpSpaceDebugColor color, cpDataPointer data) |
| 123 | {ChipmunkDebugDrawSegment(a, b, color);} |
| 124 | |
| 125 | static void |
| 126 | DrawFatSegment(cpVect a, cpVect b, cpFloat r, cpSpaceDebugColor outline, cpSpaceDebugColor fill, cpDataPointer data) |
| 127 | {ChipmunkDebugDrawFatSegment(a, b, r, outline, fill);} |
| 128 | |
| 129 | static void |
| 130 | DrawPolygon(int count, const cpVect *verts, cpFloat r, cpSpaceDebugColor outline, cpSpaceDebugColor fill, cpDataPointer data) |
| 131 | {ChipmunkDebugDrawPolygon(count, verts, r, outline, fill);} |
| 132 | |
| 133 | static void |
| 134 | DrawDot(cpFloat size, cpVect pos, cpSpaceDebugColor color, cpDataPointer data) |
| 135 | {ChipmunkDebugDrawDot(size, pos, color);} |
| 136 | |
| 137 | |
| 138 | static cpSpaceDebugColor Colors[] = { |
| 139 | {0xb5/255.0f, 0x89/255.0f, 0x00/255.0f, 1.0f}, |
| 140 | {0xcb/255.0f, 0x4b/255.0f, 0x16/255.0f, 1.0f}, |
| 141 | {0xdc/255.0f, 0x32/255.0f, 0x2f/255.0f, 1.0f}, |
| 142 | {0xd3/255.0f, 0x36/255.0f, 0x82/255.0f, 1.0f}, |
| 143 | {0x6c/255.0f, 0x71/255.0f, 0xc4/255.0f, 1.0f}, |
| 144 | {0x26/255.0f, 0x8b/255.0f, 0xd2/255.0f, 1.0f}, |
| 145 | {0x2a/255.0f, 0xa1/255.0f, 0x98/255.0f, 1.0f}, |
| 146 | {0x85/255.0f, 0x99/255.0f, 0x00/255.0f, 1.0f}, |
| 147 | }; |
| 148 | |
| 149 | static cpSpaceDebugColor |
| 150 | ColorForShape(cpShape *shape, cpDataPointer data) |
| 151 | { |
| 152 | if(cpShapeGetSensor(shape)){ |
| 153 | return LAColor(1.0f, 0.1f); |
| 154 | } else { |
| 155 | cpBody *body = cpShapeGetBody(shape); |
| 156 | |
| 157 | if(cpBodyIsSleeping(body)){ |
| 158 | return RGBAColor(0x58/255.0f, 0x6e/255.0f, 0x75/255.0f, 1.0f); |
| 159 | } else if(body->sleeping.idleTime > shape->space->sleepTimeThreshold) { |
| 160 | return RGBAColor(0x93/255.0f, 0xa1/255.0f, 0xa1/255.0f, 1.0f); |
| 161 | } else { |
| 162 | uint32_t val = (uint32_t)shape->hashid; |
| 163 | |
| 164 | // scramble the bits up using Robert Jenkins' 32 bit integer hash function |
| 165 | val = (val+0x7ed55d16) + (val<<12); |
| 166 | val = (val^0xc761c23c) ^ (val>>19); |
| 167 | val = (val+0x165667b1) + (val<<5); |
| 168 | val = (val+0xd3a2646c) ^ (val<<9); |
| 169 | val = (val+0xfd7046c5) + (val<<3); |
| 170 | val = (val^0xb55a4f09) ^ (val>>16); |
| 171 | return Colors[val & 0x7]; |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | void |
| 177 | ChipmunkDemoDefaultDrawImpl(cpSpace *space) |
| 178 | { |
| 179 | cpSpaceDebugDrawOptions drawOptions = { |
| 180 | DrawCircle, |
| 181 | DrawSegment, |
| 182 | DrawFatSegment, |
| 183 | DrawPolygon, |
| 184 | DrawDot, |
| 185 | |
| 186 | (cpSpaceDebugDrawFlags)(CP_SPACE_DEBUG_DRAW_SHAPES | CP_SPACE_DEBUG_DRAW_CONSTRAINTS | CP_SPACE_DEBUG_DRAW_COLLISION_POINTS), |
| 187 | |
| 188 | {0xEE/255.0f, 0xE8/255.0f, 0xD5/255.0f, 1.0f}, // Outline color |
| 189 | ColorForShape, |
| 190 | {0.0f, 0.75f, 0.0f, 1.0f}, // Constraint color |
| 191 | {1.0f, 0.0f, 0.0f, 1.0f}, // Collision point color |
| 192 | NULL, |
| 193 | }; |
| 194 | |
| 195 | cpSpaceDebugDraw(space, &drawOptions); |
| 196 | } |
| 197 | |
| 198 | static void |
| 199 | DrawInstructions() |
| 200 | { |
| 201 | static char title[1024]; |
| 202 | sprintf(title, "Demo(%c): %s", 'A' + demo_index, demos[demo_index].name); |
| 203 | ChipmunkDemoTextDrawString(cpv(-300, 220), title); |
| 204 | |
| 205 | ChipmunkDemoTextDrawString(cpv(-300, 200), |
| 206 | "Controls:\n" |
| 207 | "A - Z Switch demos. (return restarts)\n" |
| 208 | "Use the mouse to grab objects.\n" |
| 209 | ); |
| 210 | } |
| 211 | |
| 212 | static int max_arbiters = 0; |
| 213 | static int max_points = 0; |
| 214 | static int max_constraints = 0; |
| 215 | |
| 216 | static void |
| 217 | DrawInfo() |
| 218 | { |
| 219 | int arbiters = space->arbiters->num; |
| 220 | int points = 0; |
| 221 | |
| 222 | for(int i=0; i<arbiters; i++) |
| 223 | points += ((cpArbiter *)(space->arbiters->arr[i]))->count; |
| 224 | |
| 225 | int constraints = (space->constraints->num + points)*space->iterations; |
| 226 | |
| 227 | max_arbiters = arbiters > max_arbiters ? arbiters : max_arbiters; |
| 228 | max_points = points > max_points ? points : max_points; |
| 229 | max_constraints = constraints > max_constraints ? constraints : max_constraints; |
| 230 | |
| 231 | char buffer[1024]; |
| 232 | const char *format = |
| 233 | "Arbiters: %d (%d) - " |
| 234 | "Contact Points: %d (%d)\n" |
| 235 | "Other Constraints: %d, Iterations: %d\n" |
| 236 | "Constraints x Iterations: %d (%d)\n" |
| 237 | "Time:% 5.2fs, KE:% 5.2e"; |
| 238 | |
| 239 | cpArray *bodies = space->dynamicBodies; |
| 240 | cpFloat ke = 0.0f; |
| 241 | for(int i=0; i<bodies->num; i++){ |
| 242 | cpBody *body = (cpBody *)bodies->arr[i]; |
| 243 | if(body->m == INFINITY || body->i == INFINITY) continue; |
| 244 | |
| 245 | ke += body->m*cpvdot(body->v, body->v) + body->i*body->w*body->w; |
| 246 | } |
| 247 | |
| 248 | sprintf(buffer, format, |
| 249 | arbiters, max_arbiters, |
| 250 | points, max_points, |
| 251 | space->constraints->num, space->iterations, |
| 252 | constraints, max_constraints, |
| 253 | ChipmunkDemoTime, (ke < 1e-10f ? 0.0f : ke) |
| 254 | ); |
| 255 | |
| 256 | ChipmunkDemoTextDrawString(cpv(0, 220), buffer); |
| 257 | } |
| 258 | |
| 259 | static char PrintStringBuffer[1024*8]; |
| 260 | static char *PrintStringCursor; |
| 261 | |
| 262 | void |
| 263 | ChipmunkDemoPrintString(char const *fmt, ...) |
| 264 | { |
| 265 | if (PrintStringCursor == NULL) { |
| 266 | return; |
| 267 | } |
| 268 | |
| 269 | ChipmunkDemoMessageString = PrintStringBuffer; |
| 270 | |
| 271 | va_list args; |
| 272 | va_start(args, fmt); |
| 273 | int remaining = sizeof(PrintStringBuffer) - (PrintStringCursor - PrintStringBuffer); |
| 274 | int would_write = vsnprintf(PrintStringCursor, remaining, fmt, args); |
| 275 | if (would_write > 0 && would_write < remaining) { |
| 276 | PrintStringCursor += would_write; |
| 277 | } else { |
| 278 | // encoding error or overflow, prevent further use until reinitialized |
| 279 | PrintStringCursor = NULL; |
| 280 | } |
| 281 | va_end(args); |
| 282 | } |
| 283 | |
| 284 | static void |
| 285 | Tick(double dt) |
| 286 | { |
| 287 | if(!paused || step){ |
| 288 | PrintStringBuffer[0] = 0; |
| 289 | PrintStringCursor = PrintStringBuffer; |
| 290 | |
| 291 | // Completely reset the renderer only at the beginning of a tick. |
| 292 | // That way it can always display at least the last ticks' debug drawing. |
| 293 | ChipmunkDebugDrawClearRenderer(); |
| 294 | ChipmunkDemoTextClearRenderer(); |
| 295 | |
| 296 | cpVect new_point = cpvlerp(mouse_body->p, ChipmunkDemoMouse, 0.25f); |
| 297 | mouse_body->v = cpvmult(cpvsub(new_point, mouse_body->p), 60.0f); |
| 298 | mouse_body->p = new_point; |
| 299 | |
| 300 | demos[demo_index].updateFunc(space, dt); |
| 301 | |
| 302 | ChipmunkDemoTicks++; |
| 303 | ChipmunkDemoTime += dt; |
| 304 | |
| 305 | step = cpFalse; |
| 306 | ChipmunkDemoRightDown = cpFalse; |
| 307 | |
| 308 | ChipmunkDemoTextDrawString(cpv(-300, -200), ChipmunkDemoMessageString); |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | static void |
| 313 | Update(void) |
| 314 | { |
| 315 | double time = stm_sec(stm_now()); |
| 316 | double dt = time - LastTime; |
| 317 | if(dt > 0.2) dt = 0.2; |
| 318 | |
| 319 | double fixed_dt = demos[demo_index].timestep; |
| 320 | |
| 321 | for(Accumulator += dt; Accumulator > fixed_dt; Accumulator -= fixed_dt){ |
| 322 | Tick(fixed_dt); |
| 323 | } |
| 324 | |
| 325 | LastTime = time; |
| 326 | } |
| 327 | |
| 328 | static void |
| 329 | Display(void) |
| 330 | { |
| 331 | cpVect screen_size = {sapp_width(), sapp_height()}; |
| 332 | cpTransform view_matrix = cpTransformMult(cpTransformScale(view_scale, view_scale), cpTransformTranslate(view_translate)); |
| 333 | |
| 334 | float screen_scale = (float)cpfmin(screen_size.x/640.0, screen_size.y/480.0); |
| 335 | float hw = (float)screen_size.x*(0.5f/screen_scale); |
| 336 | float hh = (float)screen_size.y*(0.5f/screen_scale); |
| 337 | cpTransform projection_matrix = cpTransformOrtho(cpBBNew(-hw, -hh, hw, hh)); |
| 338 | |
| 339 | ChipmunkDebugDrawPointLineScale = 1.0f/(float)view_scale; |
| 340 | ChipmunkDebugDrawVPMatrix = cpTransformMult(projection_matrix, view_matrix); |
| 341 | |
| 342 | Update(); |
| 343 | |
| 344 | // Save the drawing commands from the most recent tick. |
| 345 | ChipmunkDebugDrawPushRenderer(); |
| 346 | ChipmunkDemoTextPushRenderer(); |
| 347 | demos[demo_index].drawFunc(space); |
| 348 | |
| 349 | // // Highlight the shape under the mouse because it looks neat. |
| 350 | // cpShape *nearest = cpSpacePointQueryNearest(space, ChipmunkDemoMouse, 0.0f, CP_ALL_LAYERS, CP_NO_GROUP, NULL); |
| 351 | // if(nearest) ChipmunkDebugDrawShape(nearest, RGBAColor(1.0f, 0.0f, 0.0f, 1.0f), LAColor(0.0f, 0.0f)); |
| 352 | |
| 353 | sg_pass_action action = { |
| 354 | .colors[0] = {.action = SG_ACTION_CLEAR, .val = {0x07/255.0f, 0x36/255.0f, 0x42/255.0f}}, |
| 355 | }; |
| 356 | sg_begin_default_pass(&action, (int)screen_size.x, (int)screen_size.y); |
| 357 | |
| 358 | // Draw the renderer contents and reset it back to the last tick's state. |
| 359 | ChipmunkDebugDrawFlushRenderer(); |
| 360 | |
| 361 | // // Now render all the UI text. |
| 362 | DrawInstructions(); |
| 363 | DrawInfo(); |
| 364 | |
| 365 | ChipmunkDemoTextMatrix = projection_matrix; |
| 366 | ChipmunkDemoTextFlushRenderer(); |
| 367 | |
| 368 | ChipmunkDebugDrawPopRenderer(); |
| 369 | ChipmunkDemoTextPopRenderer(); |
| 370 | |
| 371 | sg_end_pass(); |
| 372 | sg_commit(); |
| 373 | } |
| 374 | |
| 375 | static void |
| 376 | RunDemo(int index) |
| 377 | { |
| 378 | srand(45073); |
| 379 | |
| 380 | demo_index = index; |
| 381 | |
| 382 | ChipmunkDemoTicks = 0; |
| 383 | ChipmunkDemoTime = 0.0; |
| 384 | Accumulator = 0.0; |
| 385 | LastTime = stm_sec(stm_now()); |
| 386 | |
| 387 | mouse_joint = NULL; |
| 388 | ChipmunkDemoMessageString = ""; |
| 389 | max_arbiters = 0; |
| 390 | max_points = 0; |
| 391 | max_constraints = 0; |
| 392 | space = demos[demo_index].initFunc(); |
| 393 | |
| 394 | // Not supported by Sokol yet. |
| 395 | // static char title[1024]; |
| 396 | // sprintf(title, "Demo(%c): %s", 'a' + demo_index, demos[demo_index].name); |
| 397 | } |
| 398 | |
| 399 | static void |
| 400 | Keyboard(const sapp_event *event) |
| 401 | { |
| 402 | float translate_increment = 50.0f/(float)view_scale; |
| 403 | float scale_increment = 1.2f; |
| 404 | |
| 405 | if(event->type == SAPP_EVENTTYPE_CHAR && !event->key_repeat){ |
| 406 | int index = event->char_code - 'a'; |
| 407 | if(0 <= index && index < demo_count){ |
| 408 | demos[demo_index].destroyFunc(space); |
| 409 | RunDemo(index); |
| 410 | } |
| 411 | } else if(event->type == SAPP_EVENTTYPE_KEY_DOWN){ |
| 412 | switch(event->key_code){ |
| 413 | case SAPP_KEYCODE_SPACE:{ |
| 414 | if(!event->key_repeat){ |
| 415 | demos[demo_index].destroyFunc(space); |
| 416 | RunDemo(demo_index); |
| 417 | } |
| 418 | } break; |
| 419 | case SAPP_KEYCODE_GRAVE_ACCENT : { |
| 420 | if(!event->key_repeat) paused = !paused; |
| 421 | } break; |
| 422 | case SAPP_KEYCODE_1: { |
| 423 | step = cpTrue; |
| 424 | } break; |
| 425 | |
| 426 | case SAPP_KEYCODE_KP_4: view_translate.x += translate_increment; break; |
| 427 | case SAPP_KEYCODE_KP_6: view_translate.x -= translate_increment; break; |
| 428 | case SAPP_KEYCODE_KP_2: view_translate.y += translate_increment; break; |
| 429 | case SAPP_KEYCODE_KP_8: view_translate.y -= translate_increment; break; |
| 430 | case SAPP_KEYCODE_KP_7: view_scale /= scale_increment; break; |
| 431 | case SAPP_KEYCODE_KP_9: view_scale *= scale_increment; break; |
| 432 | case SAPP_KEYCODE_KP_5: { |
| 433 | view_translate.x = 0.0f; |
| 434 | view_translate.y = 0.0f; |
| 435 | view_scale = 1.0f; |
| 436 | } break; |
| 437 | |
| 438 | default: break; |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | if(!event->key_repeat){ |
| 443 | switch(event->key_code){ |
| 444 | case SAPP_KEYCODE_UP : ChipmunkDemoKeyboard.y += (event->type == SAPP_EVENTTYPE_KEY_DOWN ? 1.0 : -1.0); break; |
| 445 | case SAPP_KEYCODE_DOWN : ChipmunkDemoKeyboard.y += (event->type == SAPP_EVENTTYPE_KEY_DOWN ? -1.0 : 1.0); break; |
| 446 | case SAPP_KEYCODE_LEFT : ChipmunkDemoKeyboard.x += (event->type == SAPP_EVENTTYPE_KEY_DOWN ? -1.0 : 1.0); break; |
| 447 | case SAPP_KEYCODE_RIGHT : ChipmunkDemoKeyboard.x += (event->type == SAPP_EVENTTYPE_KEY_DOWN ? 1.0 : -1.0); break; |
| 448 | default: break; |
| 449 | } |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | static cpVect |
| 454 | MouseToSpace(const sapp_event *event) |
| 455 | { |
| 456 | // Calculate clip coord for mouse. |
| 457 | cpVect screen_size = cpv(sapp_width(), sapp_height()); |
| 458 | cpVect clip_coord = cpv(2*event->mouse_x/screen_size.x - 1, 1 - 2*event->mouse_y/screen_size.y); |
| 459 | |
| 460 | // Use the VP matrix to transform to world space. |
| 461 | cpTransform vp_inverse = cpTransformInverse(ChipmunkDebugDrawVPMatrix); |
| 462 | return cpTransformPoint(vp_inverse, clip_coord); |
| 463 | } |
| 464 | |
| 465 | static void |
| 466 | Click(const sapp_event *event) |
| 467 | { |
| 468 | cpVect mouse_pos = MouseToSpace(event); |
| 469 | |
| 470 | if(event->mouse_button == SAPP_MOUSEBUTTON_LEFT){ |
| 471 | if(event->type == SAPP_EVENTTYPE_MOUSE_DOWN){ |
| 472 | // give the mouse click a little radius to make it easier to click small shapes. |
| 473 | cpFloat radius = 5.0; |
| 474 | |
| 475 | cpPointQueryInfo info = {0}; |
| 476 | cpShape *shape = cpSpacePointQueryNearest(space, mouse_pos, radius, GRAB_FILTER, &info); |
| 477 | |
| 478 | if(shape && cpBodyGetMass(cpShapeGetBody(shape)) < INFINITY){ |
| 479 | // Use the closest point on the surface if the click is outside of the shape. |
| 480 | cpVect nearest = (info.distance > 0.0f ? info.point : mouse_pos); |
| 481 | |
| 482 | cpBody *body = cpShapeGetBody(shape); |
| 483 | mouse_joint = cpPivotJointNew2(mouse_body, body, cpvzero, cpBodyWorldToLocal(body, nearest)); |
| 484 | mouse_joint->maxForce = 50000.0f; |
| 485 | mouse_joint->errorBias = cpfpow(1.0f - 0.15f, 60.0f); |
| 486 | cpSpaceAddConstraint(space, mouse_joint); |
| 487 | } |
| 488 | } else if(mouse_joint){ |
| 489 | cpSpaceRemoveConstraint(space, mouse_joint); |
| 490 | cpConstraintFree(mouse_joint); |
| 491 | mouse_joint = NULL; |
| 492 | } |
| 493 | } else if(event->mouse_button == SAPP_MOUSEBUTTON_RIGHT){ |
| 494 | ChipmunkDemoRightDown = ChipmunkDemoRightClick = (event->type == SAPP_EVENTTYPE_MOUSE_DOWN); |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | static void |
| 499 | Event(const sapp_event *event) |
| 500 | { |
| 501 | switch(event->type){ |
| 502 | case SAPP_EVENTTYPE_CHAR: |
| 503 | case SAPP_EVENTTYPE_KEY_UP: |
| 504 | case SAPP_EVENTTYPE_KEY_DOWN: { |
| 505 | Keyboard(event); |
| 506 | } break; |
| 507 | |
| 508 | case SAPP_EVENTTYPE_MOUSE_MOVE: { |
| 509 | ChipmunkDemoMouse = MouseToSpace(event); |
| 510 | }; break; |
| 511 | |
| 512 | case SAPP_EVENTTYPE_MOUSE_UP: |
| 513 | case SAPP_EVENTTYPE_MOUSE_DOWN: { |
| 514 | Click(event); |
| 515 | } break; |
| 516 | |
| 517 | default: break; |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | static void |
| 522 | TimeTrial(int index, int count) |
| 523 | { |
| 524 | space = demos[index].initFunc(); |
| 525 | |
| 526 | double start_time = stm_sec(stm_now()); |
| 527 | double dt = demos[index].timestep; |
| 528 | |
| 529 | for(int i=0; i<count; i++) |
| 530 | demos[index].updateFunc(space, dt); |
| 531 | |
| 532 | double end_time = stm_sec(stm_now()); |
| 533 | |
| 534 | demos[index].destroyFunc(space); |
| 535 | |
| 536 | printf("Time(%c) = %8.2f ms (%s)\n", index + 'a', (end_time - start_time)*1e3f, demos[index].name); |
| 537 | fflush(stdout); |
| 538 | } |
| 539 | |
| 540 | extern ChipmunkDemo LogoSmash; |
| 541 | extern ChipmunkDemo PyramidStack; |
| 542 | extern ChipmunkDemo Plink; |
| 543 | extern ChipmunkDemo BouncyHexagons; |
| 544 | extern ChipmunkDemo Tumble; |
| 545 | extern ChipmunkDemo PyramidTopple; |
| 546 | extern ChipmunkDemo Planet; |
| 547 | extern ChipmunkDemo Springies; |
| 548 | extern ChipmunkDemo Pump; |
| 549 | extern ChipmunkDemo TheoJansen; |
| 550 | extern ChipmunkDemo Query; |
| 551 | extern ChipmunkDemo OneWay; |
| 552 | extern ChipmunkDemo Player; |
| 553 | extern ChipmunkDemo Joints; |
| 554 | extern ChipmunkDemo Tank; |
| 555 | extern ChipmunkDemo Chains; |
| 556 | extern ChipmunkDemo Crane; |
| 557 | extern ChipmunkDemo Buoyancy; |
| 558 | extern ChipmunkDemo ContactGraph; |
| 559 | extern ChipmunkDemo Slice; |
| 560 | extern ChipmunkDemo Convex; |
| 561 | extern ChipmunkDemo Unicycle; |
| 562 | extern ChipmunkDemo Sticky; |
| 563 | extern ChipmunkDemo Shatter; |
| 564 | extern ChipmunkDemo GJK; |
| 565 | |
| 566 | extern ChipmunkDemo bench_list[]; |
| 567 | extern int bench_count; |
| 568 | |
| 569 | static void |
| 570 | Init(void) |
| 571 | { |
| 572 | sg_desc desc = {0}; |
| 573 | sg_setup(&desc); |
| 574 | cpAssertHard(sg_isvalid(), "Could not init Sokol GFX."); |
| 575 | |
| 576 | ChipmunkDebugDrawInit(); |
| 577 | ChipmunkDemoTextInit(); |
| 578 | |
| 579 | mouse_body = cpBodyNewKinematic(); |
| 580 | RunDemo(demo_index); |
| 581 | } |
| 582 | |
| 583 | void Cleanup(void) { |
| 584 | sg_shutdown(); |
| 585 | } |
| 586 | |
| 587 | sapp_desc sokol_main(int argc, char* argv[]) { |
| 588 | demos[ 0] = LogoSmash; //A |
| 589 | demos[ 1] = PyramidStack; //B |
| 590 | demos[ 2] = Plink; //C |
| 591 | demos[ 3] = BouncyHexagons; //D |
| 592 | demos[ 4] = Tumble; //E |
| 593 | demos[ 5] = PyramidTopple; //F |
| 594 | demos[ 6] = Planet; //G |
| 595 | demos[ 7] = Springies; //H |
| 596 | demos[ 8] = Pump; //I |
| 597 | demos[ 9] = TheoJansen; //J |
| 598 | demos[10] = Query; //K |
| 599 | demos[11] = OneWay; //L |
| 600 | demos[12] = Joints; //M |
| 601 | demos[13] = Tank; //N |
| 602 | demos[14] = Chains; //O |
| 603 | demos[15] = Crane; //P |
| 604 | demos[16] = ContactGraph; //Q |
| 605 | demos[17] = Buoyancy; //R |
| 606 | demos[18] = Player; //S |
| 607 | demos[19] = Slice; //T |
| 608 | demos[20] = Convex; //U |
| 609 | demos[21] = Unicycle; //V |
| 610 | demos[22] = Sticky; //W |
| 611 | demos[23] = Shatter; //X |
| 612 | demo_count = 24; |
| 613 | |
| 614 | int trial = 0; |
| 615 | for(int i=0; i<argc; i++){ |
| 616 | if(strcmp(argv[i], "-bench") == 0){ |
| 617 | memcpy(demos, bench_list, bench_count*sizeof(ChipmunkDemo)); |
| 618 | demo_count = bench_count; |
| 619 | } else if(strcmp(argv[i], "-trial") == 0){ |
| 620 | trial = 1; |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | stm_setup(); |
| 625 | if(trial){ |
| 626 | for(int i=0; i<demo_count; i++) TimeTrial(i, 1000); |
| 627 | exit(0); |
| 628 | } else { |
| 629 | return (sapp_desc){ |
| 630 | .init_cb = Init, |
| 631 | .frame_cb = Display, |
| 632 | .event_cb = Event, |
| 633 | .cleanup_cb = Cleanup, |
| 634 | .width = 1024, |
| 635 | .height = 768, |
| 636 | .high_dpi = true, |
| 637 | .window_title = "Chipmunk2D", |
| 638 | }; |
| 639 | } |
| 640 | } |