Tuesday, 5 October 2010

Okeydoke.. Let's try this one..

The "Everyone says I should do it this way" method...

- (UIImage *)addImage:(UIImage *)top toImage:(UIImage *)bottom at:(CGRect)rect withBlend:(CGBlendMode)blend {  
UIGraphicsBeginImageContext(bottom.size);
// Draw image1
[bottom drawInRect:CGRectMake(0, 0, bottom.size.width, bottom.size.height)];

// Draw image2
[top drawInRect:rect blendMode:blend alpha:1];

UIImage *result = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return result;
}



The "This is the way I was doing it before" method.

void CreateBuffer(int width,int height) // Makes an empty buffer to draw onto
{
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
ctx = CGBitmapContextCreate(NULL,
width, height,
8, width * 4,
colorspace, kCGImageAlphaPremultipliedLast);
CGColorSpaceRelease(colorspace);
BufferHeight=height;
}

void LockBuffer(UIImage* ImageLock) // Creates a buffer from a previous image.
{
CGImageRef oldimage = ImageLock.CGImage;
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
ctx = CGBitmapContextCreate(NULL,
CGImageGetWidth(oldimage), CGImageGetHeight(oldimage),
8, CGImageGetWidth(oldimage) * 4,
colorspace, kCGImageAlphaPremultipliedLast);
CGColorSpaceRelease(colorspace);
BufferHeight=CGImageGetHeight(oldimage);

CGContextSetBlendMode(ctx, kCGBlendModeCopy);

CGRect r = CGRectMake(0,0,CGImageGetWidth(oldimage), CGImageGetHeight(oldimage));
CGContextDrawImage(ctx, r, oldimage);
}

void DrawToBuffer(UIImage* ImageDraw,int x,int y) // Draw a UIImage onto a Buffer
{
CGImageRef useimage = ImageDraw.CGImage;
CGContextSetBlendMode(ctx, kCGBlendModeOverlay);
// add new image

CGRect r = CGRectMake(x,y,CGImageGetWidth(useimage),CGImageGetHeight(useimage));
CGContextDrawImage(ctx, r, useimage);
}



UIImage* UnlockBuffer(int freebuffer)
{
// create resulting image
CGImageRef thisimage;
thisimage = CGBitmapContextCreateImage(ctx);
UIImage* newImage = [[UIImage alloc] initWithCGImage:thisimage];

CGImageRelease(thisimage);

if (freebuffer==1) {CGContextRelease(ctx);}

return newImage;
}



I'm pretty sure "Their" method would be the right one to go for, and up there it does indeed seem like a lot less code.
But let's see what it looks like when I'm actually using it, shall we?

Theirs first..
 start=millisecs();
img_Hearts=[jcore makeImage:CGSizeMake(128,128)];
img_Hearts=[jcore addImage:img_Heart toImage:img_Hearts at:CGRectMake(0,0,64,64) withBlend:kCGBlendModeCopy];
img_Hearts=[jcore addImage:img_Heart toImage:img_Hearts at:CGRectMake(64,0,64,64) withBlend:kCGBlendModeCopy];
img_Hearts=[jcore addImage:img_Heart toImage:img_Hearts at:CGRectMake(0,64,64,64) withBlend:kCGBlendModeCopy];
img_Hearts=[jcore addImage:img_Heart toImage:img_Hearts at:CGRectMake(64,64,64,64) withBlend:kCGBlendModeCopy];
view_Heart[0].image=img_Hearts;
Debuglog(@"A--> %f",millisecs()-start);


vs mine..
 start=millisecs();
CreateBuffer(128,128);
DrawToBuffer(img_Heart,0,0);
DrawToBuffer(img_Heart,0,64);
DrawToBuffer(img_Heart,64,0);
DrawToBuffer(img_Heart,64,64);
img_Hearts=UnlockBuffer(1);

view_Heart[0].image=img_Hearts;
Debuglog(@"B--> %f",millisecs()-start);


Same result, much feckin' shorter, much easier to type, much better on the eyes, pretty much just better!
FFS, coders, there was nothing wrong with "Basic"...
It worked, it looked half decent, it wasn't a mess, and you could damn well read it!

But, of course, the numbers of keys you press isn't really the issue. The actual issue is the end result.
So, which is better? "Better" code, or "shitty" JayStyle code?

A--> 0.002366
A--> 0.002013
A--> 0.002013
A--> 0.001553
A--> 0.001734
A--> 0.001661
A--> 0.001652
A--> 0.001997
A--> 0.001926
A--> 0.002022
B--> 0.001284
B--> 0.000755
B--> 0.001093
B--> 0.001089
B--> 0.000946
B--> 0.001051
B--> 0.001101
B--> 0.000967
B--> 0.001144
B--> 0.001089


Aww well, you can't argue with numbers!

1 comment:

  1. Shitty JayStyle code is better.

    ReplyDelete