1 2 // written in the D programming language 3 4 module samples.LogoSmash; 5 6 import dchip.all; 7 8 import samples.ChipmunkDemo; 9 10 import drawSpace; 11 12 enum int image_width = 188; 13 enum int image_height = 35; 14 enum int image_row_length = 24; 15 16 enum byte[] image_bitmap = [ 17 15,-16,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7,-64,15,63,-32,-2,0,0,0,0,0,0,0, 18 0,0,0,0,0,0,0,0,0,0,0,31,-64,15,127,-125,-1,-128,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 19 0,0,0,127,-64,15,127,15,-1,-64,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,-1,-64,15,-2, 20 31,-1,-64,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,-1,-64,0,-4,63,-1,-32,0,0,0,0,0,0, 21 0,0,0,0,0,0,0,0,0,0,1,-1,-64,15,-8,127,-1,-32,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 22 1,-1,-64,0,-8,-15,-1,-32,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,-31,-1,-64,15,-8,-32, 23 -1,-32,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7,-15,-1,-64,9,-15,-32,-1,-32,0,0,0,0,0, 24 0,0,0,0,0,0,0,0,0,0,31,-15,-1,-64,0,-15,-32,-1,-32,0,0,0,0,0,0,0,0,0,0,0,0,0, 25 0,0,63,-7,-1,-64,9,-29,-32,127,-61,-16,63,15,-61,-1,-8,31,-16,15,-8,126,7,-31, 26 -8,31,-65,-7,-1,-64,9,-29,-32,0,7,-8,127,-97,-25,-1,-2,63,-8,31,-4,-1,15,-13, 27 -4,63,-1,-3,-1,-64,9,-29,-32,0,7,-8,127,-97,-25,-1,-2,63,-8,31,-4,-1,15,-13, 28 -2,63,-1,-3,-1,-64,9,-29,-32,0,7,-8,127,-97,-25,-1,-1,63,-4,63,-4,-1,15,-13, 29 -2,63,-33,-1,-1,-32,9,-25,-32,0,7,-8,127,-97,-25,-1,-1,63,-4,63,-4,-1,15,-13, 30 -1,63,-33,-1,-1,-16,9,-25,-32,0,7,-8,127,-97,-25,-1,-1,63,-4,63,-4,-1,15,-13, 31 -1,63,-49,-1,-1,-8,9,-57,-32,0,7,-8,127,-97,-25,-8,-1,63,-2,127,-4,-1,15,-13, 32 -1,-65,-49,-1,-1,-4,9,-57,-32,0,7,-8,127,-97,-25,-8,-1,63,-2,127,-4,-1,15,-13, 33 -1,-65,-57,-1,-1,-2,9,-57,-32,0,7,-8,127,-97,-25,-8,-1,63,-2,127,-4,-1,15,-13, 34 -1,-1,-57,-1,-1,-1,9,-57,-32,0,7,-1,-1,-97,-25,-8,-1,63,-1,-1,-4,-1,15,-13,-1, 35 -1,-61,-1,-1,-1,-119,-57,-32,0,7,-1,-1,-97,-25,-8,-1,63,-1,-1,-4,-1,15,-13,-1, 36 -1,-61,-1,-1,-1,-55,-49,-32,0,7,-1,-1,-97,-25,-8,-1,63,-1,-1,-4,-1,15,-13,-1, 37 -1,-63,-1,-1,-1,-23,-49,-32,127,-57,-1,-1,-97,-25,-1,-1,63,-1,-1,-4,-1,15,-13, 38 -1,-1,-63,-1,-1,-1,-16,-49,-32,-1,-25,-1,-1,-97,-25,-1,-1,63,-33,-5,-4,-1,15, 39 -13,-1,-1,-64,-1,-9,-1,-7,-49,-32,-1,-25,-8,127,-97,-25,-1,-1,63,-33,-5,-4,-1, 40 15,-13,-1,-1,-64,-1,-13,-1,-32,-49,-32,-1,-25,-8,127,-97,-25,-1,-2,63,-49,-13, 41 -4,-1,15,-13,-1,-1,-64,127,-7,-1,-119,-17,-15,-1,-25,-8,127,-97,-25,-1,-2,63, 42 -49,-13,-4,-1,15,-13,-3,-1,-64,127,-8,-2,15,-17,-1,-1,-25,-8,127,-97,-25,-1, 43 -8,63,-49,-13,-4,-1,15,-13,-3,-1,-64,63,-4,120,0,-17,-1,-1,-25,-8,127,-97,-25, 44 -8,0,63,-57,-29,-4,-1,15,-13,-4,-1,-64,63,-4,0,15,-17,-1,-1,-25,-8,127,-97, 45 -25,-8,0,63,-57,-29,-4,-1,-1,-13,-4,-1,-64,31,-2,0,0,103,-1,-1,-57,-8,127,-97, 46 -25,-8,0,63,-57,-29,-4,-1,-1,-13,-4,127,-64,31,-2,0,15,103,-1,-1,-57,-8,127, 47 -97,-25,-8,0,63,-61,-61,-4,127,-1,-29,-4,127,-64,15,-8,0,0,55,-1,-1,-121,-8, 48 127,-97,-25,-8,0,63,-61,-61,-4,127,-1,-29,-4,63,-64,15,-32,0,0,23,-1,-2,3,-16, 49 63,15,-61,-16,0,31,-127,-127,-8,31,-1,-127,-8,31,-128,7,-128,0,0 50 ]; 51 52 static int 53 get_pixel(int x, int y) 54 { 55 return (image_bitmap[(x>>3) + y*image_row_length]>>(~x&0x7)) & 1; 56 } 57 58 static cpSpace *space; 59 60 static void 61 update(int ticks) 62 { 63 enum int steps = 1; 64 enum cpFloat dt = 1.0f/60.0f/cast(cpFloat)steps; 65 66 for(int i=0; i<steps; i++){ 67 cpSpaceStep(space, dt); 68 } 69 } 70 71 static cpShape * 72 make_ball(cpFloat x, cpFloat y) 73 { 74 cpBody *body_ = cpBodyNew(1.0f, INFINITY); 75 body_.p = cpv(x, y); 76 77 cpShape *shape = cpCircleShapeNew(body_, 0.95f, cpvzero); 78 shape.e = 0.0f; shape.u = 0.0f; 79 80 return shape; 81 } 82 83 static cpSpace * 84 init() 85 { 86 space = cpSpaceNew(); 87 space.iterations = 1; 88 89 // The space will contain a very large number of similary sized objects. 90 // This is the perfect candidate for using the spatial hash. 91 // Generally you will never need to do this. 92 cpSpaceUseSpatialHash(space, 2.0f, 10000); 93 94 cpBody *body_; 95 cpShape *shape; 96 97 for(int y=0; y<image_height; y++){ 98 for(int x=0; x<image_width; x++){ 99 if(!get_pixel(x, y)) continue; 100 101 cpFloat x_jitter = 0.05f*frand(); 102 cpFloat y_jitter = 0.05f*frand(); 103 104 shape = make_ball(2*(x - image_width/2 + x_jitter), 2*(image_height/2 - y + y_jitter)); 105 cpSpaceAddBody(space, shape.body_); 106 cpSpaceAddShape(space, shape); 107 } 108 } 109 110 body_ = cpSpaceAddBody(space, cpBodyNew(INFINITY, INFINITY)); 111 body_.p = cpv(-1000.0f, -10.0f); 112 body_.v = cpv(400.0f, 0.0f); 113 114 shape = cpSpaceAddShape(space, cpCircleShapeNew(body_, 8.0f, cpvzero)); 115 shape.e = 0.0f; shape.u = 0.0f; 116 shape.layers = NOT_GRABABLE_MASK; 117 118 return space; 119 } 120 121 static void 122 destroy() 123 { 124 ChipmunkDemoFreeSpaceChildren(space); 125 // cpSpaceFree(space); 126 } 127 128 static const drawSpaceOptions draw_options = { 129 0, 0, 0, 2.0f, 3.0f, 0.0f, 130 }; 131 132 chipmunkDemo LogoSmash = { 133 "Logo Smash", 134 &draw_options, 135 &init, 136 &update, 137 &destroy, 138 };