Skip to content

Instantly share code, notes, and snippets.

@OneSadCookie
Created January 15, 2012 19:35
Show Gist options
  • Save OneSadCookie/1616950 to your computer and use it in GitHub Desktop.
Save OneSadCookie/1616950 to your computer and use it in GitHub Desktop.
closure example
- (void)check:(NSEvent *)event onCard:(void (^)(SMCardID, CGPoint))cardHandler onPile:(void (^)(SMPile, CGPoint))pileHandler
{
CGPoint where = [event locationInWindow];
[[self openGLContext] makeCurrentContext];
glBindBuffer(GL_PIXEL_PACK_BUFFER, _pbo);
uint32_t const *pickData = glMapBuffer(GL_PIXEL_PACK_BUFFER, GL_READ_ONLY);
uint32_t pixel = pickData[(size_t)where.x + 1024 * (size_t)where.y];
glUnmapBuffer(GL_PIXEL_PACK_BUFFER);
glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
uint32_t r = (pixel >> 16) & 0xff;
CGPoint st = CGPointMake(
((pixel >> 8) & 0xff) / 255.0,
1.0 - ((pixel >> 0) & 0xff) / 255.0);
if (r != 0)
{
SMCardID cardID = (r / 2) - 1;
if (cardID < 104)
{
cardHandler(cardID, st);
}
else
{
assert(cardID < 104 + SMPileCount);
pileHandler(cardID - 104, st);
}
}
}
- (void)mouseDown:(NSEvent *)event
{
[self check:event
onCard:^(SMCardID cardID, CGPoint st)
{
// NSLog(@"%C%@", SMSuitToChar([_game suitOfFaceUpCard:cardID]), SMRankToString([_game rankOfFaceUpCard:cardID]));
_floating = cardID;
_floatingOffset = st;
[self setFloatingPos:event];
[self setNeedsDisplay:YES];
}
onPile:^(SMPile pile, CGPoint st)
{
// nothing
}];
}
- (void)check:(NSEvent *)event wasCard:(BOOL*)wasCard outCardID:(SMCardID*)outCardID outPile:(SMPile*)outPile outPoint:(CGPoint*)outPoint
{
CGPoint where = [event locationInWindow];
[[self openGLContext] makeCurrentContext];
glBindBuffer(GL_PIXEL_PACK_BUFFER, _pbo);
uint32_t const *pickData = glMapBuffer(GL_PIXEL_PACK_BUFFER, GL_READ_ONLY);
uint32_t pixel = pickData[(size_t)where.x + 1024 * (size_t)where.y];
glUnmapBuffer(GL_PIXEL_PACK_BUFFER);
glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
uint32_t r = (pixel >> 16) & 0xff;
CGPoint st = CGPointMake(
((pixel >> 8) & 0xff) / 255.0,
1.0 - ((pixel >> 0) & 0xff) / 255.0);
if (r != 0)
{
*outPoint = st;
SMCardID cardID = (r / 2) - 1;
if (cardID < 104)
{
*wasCard = YES;
*outCardID = cardID;
}
else
{
assert(cardID < 104 + SMPileCount);
*wasCard = NO;
*outPileID = cardID - 104;
}
}
}
- (void)mouseDown:(NSEvent *)event
{
BOOL wasCard;
SMCardID cardID;
SMPile pile;
CGPoint st;
[self check:event wasCard:&wasCard outCardID:&cardID outPile:&pile outPoint:&st];
if (wasCard)
{
// NSLog(@"%C%@", SMSuitToChar([_game suitOfFaceUpCard:cardID]), SMRankToString([_game rankOfFaceUpCard:cardID]));
_floating = cardID;
_floatingOffset = st;
[self setFloatingPos:event];
[self setNeedsDisplay:YES];
}
else
{
// nothing
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment