#include #include static OSStatus DrawHandler(EventHandlerCallRef inHandlerCallRef, EventRef inEvent, void* inUserData); static void Heartbeat(EventLoopTimerRef inTimer, void *inRefCon); static CGImageRef GetCGImageFromPath(CFStringRef inPath); #define allseconds (4 * 60 * 60) int day, sec, man; void JustDrawSomething(CGContextRef cg, CGRect *bounds); static void createpdf(void) { CGContextRef pdfcg; CFStringRef path; CFURLRef url; CGRect solbounds = {0, 0, 800, 800}; CGRect artbounds = {-5, -5, 810, 810}; path = CFStringCreateWithCString(NULL, "LeWitt.pdf", kCFStringEncodingUTF8); url = CFURLCreateWithFileSystemPath(NULL, path, kCFURLPOSIXPathStyle, 0); CFRelease(path); pdfcg = CGPDFContextCreateWithURL(url, &artbounds, NULL); CFRelease(url); CGContextBeginPage(pdfcg, &artbounds); JustDrawSomething(pdfcg, &solbounds); CGContextEndPage(pdfcg); CGContextRelease(pdfcg); } int main(void) { IBNibRef nibRef; WindowRef window; EventTypeSpec type; HIViewRef contentView; CreateNibReference(CFSTR("main"), &nibRef); SetMenuBarFromNib(nibRef, CFSTR("MenuBar")); CreateWindowFromNib(nibRef, CFSTR("MainWindow"), &window); HIViewFindByID(HIViewGetRoot(window), kHIViewWindowContentID, &contentView); type.eventClass = kEventClassControl; type.eventKind = kEventControlDraw; InstallEventHandler(HIViewGetEventTarget(contentView), DrawHandler, 1, &type, contentView, NULL); // InstallEventLoopTimer(GetCurrentEventLoop(), 1.0, 1.0, Heartbeat, contentView, NULL); createpdf(); ShowWindow(window); RunApplicationEventLoop(); return 0; } static CGImageRef image = NULL; //------------> return the color, given man <------------// float *getpencil(CGContextRef cg, int man){ static float black[4] = {0.0, 0.0, 0.0, .05}; static float blue[4] = {0.0, 0.0, 1.0, .05}; static float red[4] = {1.0, 0.0, 0.0, .05}; static float yellow[4] = {1.0, 1.0, 0.0, .05}; switch (man) { // set color to black for man 1 case 1: CGContextSetLineWidth(cg, 2.0); //fun with changing line widths based on the man, could do opacity too return black; // set color to red for man 2 case 2: CGContextSetLineWidth(cg, 3.0); return red; // set color to yellow for man 3 case 3: CGContextSetLineWidth(cg, 2.0); return yellow; //set color to blue for man 4 case 4: CGContextSetLineWidth(cg, 4.0); // man #4 seems to have grabbed one of those giant pieces of chalk return blue; // he is a rebel. } } //------------> return the square number, given the day and man <------------// int where(int day, int man){ switch(day){ case 1: return man; case 2: if(man == 1) return 2; else if (man == 2) return 3; else if (man == 3) return 4; else if(man == 4) return 1; case 3: if(man == 1) return 3; else if (man == 2) return 4; else if (man == 3) return 1; else if(man == 4) return 2; case 4: if(man == 1) return 4; else if (man == 2) return 1; else if (man == 3) return 2; else if(man == 4) return 3; } } //------------> draw a line, in the given square <------------// void draw(CGContextRef cg, int where){ float minX, minY, maxX, maxY; float startx, starty, endx, endy; float a; //set the 'boundaries' for x and y depending on which 'square' it is switch(where){ //square 1, bottom left case 1: minX = 5; minY = 5; maxX = 395; maxY = 395; a = 1; break; //square 2, bottom right case 2: minX = 405; minY = 5; maxX = 795; maxY = 395; a = 5.495; break; //square 3, top right case 3: minX = 405; minY = 405; maxX = 795; maxY = 795; a = 1; break; //square 4, top left case 4: minX = 5; minY = 405; maxX = 395; maxY = 795; a = 5.495; break; } //choose a random startx and starty. anytime a start or endpoint is outside of the bounds for the square, reassign a random value. do{ startx = rand()%800; starty = rand()%800; endx = startx + cos(a) * 40; endy = starty + sin(a) * 40; }while(startx <= minX || startx >= maxX || starty <= minY || starty >= maxY || endx <= minX || endx >= maxX || endy <= minY || endy >= maxY); //draw a line CGContextMoveToPoint(cg, startx, starty); CGContextAddLineToPoint(cg, endx, endy); CGContextStrokePath(cg); } //------------> LOOKIE HERE JUST DRAW SOMETHING!!!! <------------// void JustDrawSomething(CGContextRef cg, CGRect *bounds) { //set color space... kCGColorSpaceGenericRGB; //make a black background to draw on. like a sidewalk. CGContextSetRGBFillColor(cg, 0.0, 0.0, 0.0, 1.0); CGContextFillRect(cg, CGRectMake(0,0,800,800)); //...and default line color //for some reason, CGSetStrokeColor will not work unless this is called first. //huh. maybe a default has to be set or else nothing will work? but you can't set //default with CGSetStrokeColor, only alter(override) the initial color. CGContextSetRGBStrokeColor(cg, 1.0,1.0,1.0,.25); //these two draw lines dividing the window into the appearance of 4 parts. CGContextSetLineWidth(cg, 2.5); CGContextMoveToPoint(cg, 0, 400); CGContextAddLineToPoint(cg, 800, 400); CGContextStrokePath(cg); CGContextMoveToPoint(cg, 400, 0); CGContextAddLineToPoint(cg, 400, 800); CGContextStrokePath(cg); //------------> the instructions <------------// //each day, for four days... for(day = 1; day <= 4; day++){ //...each second, for four hours... for(sec = 1; sec <= allseconds; sec++){ //...each man will... for(man = 1; man <=4; man++){ //...grab the colored pencil...(or sidewalk chalk) CGContextSetStrokeColor(cg, getpencil(cg, man)); //...and draw a line! draw(cg, where(day, man)); } } } #if 0 CGContextSelectFont(cg, "Helvetica", 18, kCGEncodingMacRoman); CGContextShowTextAtPoint(cg, 100, 100, "Hello Sol!", 12); #endif #if 0 if (image == NULL) { image = GetCGImageFromPath(CFSTR("lewitt.jpg")); } rect = CGRectMake(150, 250, 300, 200); CGContextDrawImage(cg, rect, image); #endif } #if 0 static void Heartbeat(EventLoopTimerRef inTimer, void *inRefCon) { theView = (HIViewRef)inUserData; } #endif static OSStatus DrawHandler(EventHandlerCallRef inHandlerCallRef, EventRef inEvent, void *inUserData) { HIViewRef theView; CGContextRef cg; CGRect viewBounds; theView = (HIViewRef)inUserData; GetEventParameter(inEvent, kEventParamCGContextRef, typeCGContextRef, NULL, sizeof(CGContextRef), NULL, &cg); CallNextEventHandler(inHandlerCallRef, inEvent); HIViewGetBounds(theView, &viewBounds); CGContextTranslateCTM(cg, 0, viewBounds.size.height); CGContextScaleCTM(cg, 1.0, -1.0); JustDrawSomething(cg, &viewBounds); return 0; } static CGImageRef GetCGImageFromPath(CFStringRef inPath) { CGImageRef imageRef; Handle dataRef; OSType dataRefType; GraphicsImportComponent gi; ComponentResult result; dataRef = NULL; gi = 0; // create the data reference result = QTNewDataReferenceFromFullPathCFString(inPath, kQTNativeDefaultPathStyle, 0, &dataRef, &dataRefType); if (dataRef == NULL || result != 0) exit(1); // get the graphics importer for the data ref GetGraphicsImporterForDataRefWithFlags(dataRef, dataRefType, &gi, 0); if (gi == NULL) exit(1); GraphicsImportCreateCGImage(gi, &imageRef, 0); if (imageRef == NULL) exit(1); CloseComponent(gi); DisposeHandle(dataRef); return imageRef; }