Skip to content

Instantly share code, notes, and snippets.

View slembcke's full-sized avatar
👣
Gruntled

Scott Lembcke slembcke

👣
Gruntled
View GitHub Profile
@slembcke
slembcke / DeformableBitmapDemo.m
Created September 12, 2011 21:38
Chipmunk Pro deformable terrain: DeformableBitmapDemo.m
#import "DeformableBitmapDemo.h"
@implementation DeformableBitmapDemo
@synthesize space;
#define PIXEL_SIZE 4
#define TILE_SIZE 128
- (id) init
1.0 -> 0.0
0.9 -> 1/19
0.8 -> 1/9
0.7 -> 3/17
0.6 -> 1/4
0.5 -> 1/3
0.4 -> 3/7
0.3 -> 7/13
0.2 -> 2/3
0.1 -> 9/11
-(void) draw;
{
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisable(GL_TEXTURE_2D);
glPushMatrix(); {
glScalef(CC_CONTENT_SCALE_FACTOR(), CC_CONTENT_SCALE_FACTOR(), 1.0);
glLineWidth(1.0f);
cpSpaceEachShape(space.space, drawShape, NULL);
#if __has_feature(objc_arc)
typedef __strong id CCARRAY_ID;
#define CCARRAY_RETAIN(obj) obj
#define CCARRAY_RELEASE(obj) (void)obj
#else
typedef id CCARRAY_ID;
#define CCARRAY_RETAIN(obj) [obj retain]
#define CCARRAY_RELEASE(obj) [obj release]
#endif
@slembcke
slembcke / gist:1424609
Created December 2, 2011 19:52
Closest Point on Segment
cpVect a, b; // segment endpoints
cpVect p; // point you are testing
cpVect delta = cpvsub(b, a);
cpFloat closest_t = cpvdot(delta, cpvsub(p, a))/cpvlengthsq(delta);
cpVect closest = cpvlerp(a, b, cpfclamp01(closest_t));
@slembcke
slembcke / gist:1449811
Created December 9, 2011 02:15
fwidth() AA
#extension GL_OES_standard_derivatives : enable
varying lowp vec4 frag_color;
varying lowp vec2 frag_texcoord;
void main()
{
#if GL_OES_standard_derivatives == 1
gl_FragColor = frag_color*smoothstep(0.0, length(fwidth(frag_texcoord)), 1.0 - length(frag_texcoord));
#else
// If 'self' is assigned to obj.delegate and I don't explicitly copy the block
// it crashes with an EXC_BAD_ACCESS in objc_msgSend() calling 'retain' on
NSArray *constructors = [NSArray arrayWithObjects:
[^(cpVect pos){
GameObject *obj = ...;
obj.delegate = self;
return obj;
} copy],
// Some old PNG screenshot code that I used in a Ruby binding.
static VALUE
rbas_screen_shot(VALUE self, VALUE filename)
{
int bytes = AS_SCREEN_W * AS_SCREEN_H * 3;
GLubyte pixels[bytes];
glReadPixels(0, 0, AS_SCREEN_W, AS_SCREEN_H, GL_RGB, GL_UNSIGNED_BYTE, pixels);
char *name = StringValuePtr(filename);
@slembcke
slembcke / test.c
Created April 14, 2012 20:21
C struct "inheritance"
#include <stdio.h>
struct Person {
char *name;
int age;
};
struct Employee {
// You can cast a pointer to an Employee struct to
// a Person struct because you put a person struct in
@slembcke
slembcke / nearestPoint.c
Created April 20, 2012 17:33
Nearest Point Queries
// ** (vanilla) Point Queries:
// For the shape specific function, you give it a point and it returns true/false if the point is inside:
cpBool cpShapePointQuery(cpShape *shape, cpVect p);
// The "easy" space query function takes a point and some filter parmeters and returns the shape at the point or NULL:
cpShape *cpSpacePointQueryFirst(cpSpace *space, cpVect point, cpLayers layers, cpGroup group);
// The callback based iterator version looks like this:
typedef void (*cpSpacePointQueryFunc)(cpShape *shape, void *data);