Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit 53a762c672d355bd2f0f21fe49a07272faba38b8 @rbsgn committed Apr 18, 2010
@@ -0,0 +1,68 @@
+/*
+
+File: AppController.h
+Abstract: The UIApplication delegate class, which is the central controller of
+the application.
+
+Version: 1.7
+
+Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Inc.
+("Apple") in consideration of your agreement to the following terms, and your
+use, installation, modification or redistribution of this Apple software
+constitutes acceptance of these terms. If you do not agree with these terms,
+please do not use, install, modify or redistribute this Apple software.
+
+In consideration of your agreement to abide by the following terms, and subject
+to these terms, Apple grants you a personal, non-exclusive license, under
+Apple's copyrights in this original Apple software (the "Apple Software"), to
+use, reproduce, modify and redistribute the Apple Software, with or without
+modifications, in source and/or binary forms; provided that if you redistribute
+the Apple Software in its entirety and without modifications, you must retain
+this notice and the following text and disclaimers in all such redistributions
+of the Apple Software.
+Neither the name, trademarks, service marks or logos of Apple Inc. may be used
+to endorse or promote products derived from the Apple Software without specific
+prior written permission from Apple. Except as expressly stated in this notice,
+no other rights or licenses, express or implied, are granted by Apple herein,
+including but not limited to any patent rights that may be infringed by your
+derivative works or by other works in which the Apple Software may be
+incorporated.
+
+The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
+WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
+WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
+COMBINATION WITH YOUR PRODUCTS.
+
+IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
+GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR
+DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF
+CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF
+APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+Copyright (C) 2009 Apple Inc. All Rights Reserved.
+
+*/
+
+//CLASS INTERFACES:
+
+@class PaintingWindow;
+@class PaintingView;
+@class SoundEffect;
+
+@interface AppController : NSObject <UIApplicationDelegate>
+{
+ PaintingWindow *window;
+ PaintingView *drawingView;
+
+ SoundEffect *erasingSound;
+ SoundEffect *selectSound;
+ CFTimeInterval lastTime;
+}
+
+@property (nonatomic, retain) IBOutlet PaintingWindow *window;
+@property (nonatomic, retain) IBOutlet PaintingView *drawingView;
+
+@end
@@ -0,0 +1,226 @@
+/*
+
+File: AppController.m
+Abstract: The UIApplication delegate class, which is the central controller of
+the application.
+
+Version: 1.7
+
+Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Inc.
+("Apple") in consideration of your agreement to the following terms, and your
+use, installation, modification or redistribution of this Apple software
+constitutes acceptance of these terms. If you do not agree with these terms,
+please do not use, install, modify or redistribute this Apple software.
+
+In consideration of your agreement to abide by the following terms, and subject
+to these terms, Apple grants you a personal, non-exclusive license, under
+Apple's copyrights in this original Apple software (the "Apple Software"), to
+use, reproduce, modify and redistribute the Apple Software, with or without
+modifications, in source and/or binary forms; provided that if you redistribute
+the Apple Software in its entirety and without modifications, you must retain
+this notice and the following text and disclaimers in all such redistributions
+of the Apple Software.
+Neither the name, trademarks, service marks or logos of Apple Inc. may be used
+to endorse or promote products derived from the Apple Software without specific
+prior written permission from Apple. Except as expressly stated in this notice,
+no other rights or licenses, express or implied, are granted by Apple herein,
+including but not limited to any patent rights that may be infringed by your
+derivative works or by other works in which the Apple Software may be
+incorporated.
+
+The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
+WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
+WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
+COMBINATION WITH YOUR PRODUCTS.
+
+IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
+GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR
+DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF
+CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF
+APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+Copyright (C) 2009 Apple Inc. All Rights Reserved.
+
+*/
+
+#import "AppController.h"
+#import "PaintingView.h"
+#import "SoundEffect.h"
+
+//CONSTANTS:
+
+#define kPaletteHeight 30
+#define kPaletteSize 5
+#define kMinEraseInterval 0.5
+
+// Padding for margins
+#define kLeftMargin 10.0
+#define kTopMargin 10.0
+#define kRightMargin 10.0
+
+//FUNCTIONS:
+/*
+ HSL2RGB Converts hue, saturation, luminance values to the equivalent red, green and blue values.
+ For details on this conversion, see Fundamentals of Interactive Computer Graphics by Foley and van Dam (1982, Addison and Wesley)
+ You can also find HSL to RGB conversion algorithms by searching the Internet.
+ See also http://en.wikipedia.org/wiki/HSV_color_space for a theoretical explanation
+ */
+static void HSL2RGB(float h, float s, float l, float* outR, float* outG, float* outB)
+{
+ float temp1,
+ temp2;
+ float temp[3];
+ int i;
+
+ // Check for saturation. If there isn't any just return the luminance value for each, which results in gray.
+ if(s == 0.0) {
+ if(outR)
+ *outR = l;
+ if(outG)
+ *outG = l;
+ if(outB)
+ *outB = l;
+ return;
+ }
+
+ // Test for luminance and compute temporary values based on luminance and saturation
+ if(l < 0.5)
+ temp2 = l * (1.0 + s);
+ else
+ temp2 = l + s - l * s;
+ temp1 = 2.0 * l - temp2;
+
+ // Compute intermediate values based on hue
+ temp[0] = h + 1.0 / 3.0;
+ temp[1] = h;
+ temp[2] = h - 1.0 / 3.0;
+
+ for(i = 0; i < 3; ++i) {
+
+ // Adjust the range
+ if(temp[i] < 0.0)
+ temp[i] += 1.0;
+ if(temp[i] > 1.0)
+ temp[i] -= 1.0;
+
+
+ if(6.0 * temp[i] < 1.0)
+ temp[i] = temp1 + (temp2 - temp1) * 6.0 * temp[i];
+ else {
+ if(2.0 * temp[i] < 1.0)
+ temp[i] = temp2;
+ else {
+ if(3.0 * temp[i] < 2.0)
+ temp[i] = temp1 + (temp2 - temp1) * ((2.0 / 3.0) - temp[i]) * 6.0;
+ else
+ temp[i] = temp1;
+ }
+ }
+ }
+
+ // Assign temporary values to R, G, B
+ if(outR)
+ *outR = temp[0];
+ if(outG)
+ *outG = temp[1];
+ if(outB)
+ *outB = temp[2];
+}
+
+//CLASS IMPLEMENTATIONS:
+
+@implementation AppController
+
+@synthesize window;
+@synthesize drawingView;
+
+- (void) applicationDidFinishLaunching:(UIApplication*)application
+{
+ CGRect rect = [[UIScreen mainScreen] applicationFrame];
+ CGFloat components[3];
+
+ // Create a segmented control so that the user can choose the brush color.
+ UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:
+ [NSArray arrayWithObjects:
+ [UIImage imageNamed:@"Red.png"],
+ [UIImage imageNamed:@"Yellow.png"],
+ [UIImage imageNamed:@"Green.png"],
+ [UIImage imageNamed:@"Blue.png"],
+ [UIImage imageNamed:@"Purple.png"],
+ nil]];
+
+ // Compute a rectangle that is positioned correctly for the segmented control you'll use as a brush color palette
+ CGRect frame = CGRectMake(rect.origin.x + kLeftMargin, rect.size.height - kPaletteHeight - kTopMargin, rect.size.width - (kLeftMargin + kRightMargin), kPaletteHeight);
+ segmentedControl.frame = frame;
+ // When the user chooses a color, the method changeBrushColor: is called.
+ [segmentedControl addTarget:self action:@selector(changeBrushColor:) forControlEvents:UIControlEventValueChanged];
+ segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
+ // Make sure the color of the color complements the black background
+ segmentedControl.tintColor = [UIColor darkGrayColor];
+ // Set the third color (index values start at 0)
+ segmentedControl.selectedSegmentIndex = 2;
+
+ // Add the control to the window
+ [window addSubview:segmentedControl];
+ // Now that the control is added, you can release it
+ [segmentedControl release];
+
+ // Define a starting color
+ HSL2RGB((CGFloat) 2.0 / (CGFloat)kPaletteSize, kSaturation, kLuminosity, &components[0], &components[1], &components[2]);
+ // Set the color using OpenGL
+ glColor4f(components[0], components[1], components[2], kBrushOpacity);
+
+ // Look in the Info.plist file and you'll see the status bar is hidden
+ // Set the style to black so it matches the background of the application
+ [application setStatusBarStyle:UIStatusBarStyleBlackTranslucent animated:NO];
+ // Now show the status bar, but animate to the style.
+ [application setStatusBarHidden:NO animated:YES];
+
+ // Load the sounds
+ NSBundle *mainBundle = [NSBundle mainBundle];
+ erasingSound = [[SoundEffect alloc] initWithContentsOfFile:[mainBundle pathForResource:@"Erase" ofType:@"caf"]];
+ selectSound = [[SoundEffect alloc] initWithContentsOfFile:[mainBundle pathForResource:@"Select" ofType:@"caf"]];
+
+ // Erase the view when recieving a notification named "shake" from the NSNotificationCenter object
+ // The "shake" nofification is posted by the PaintingWindow object when user shakes the device
+ [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(eraseView) name:@"shake" object:nil];
+}
+
+// Release resources when they are no longer needed,
+- (void) dealloc
+{
+ [selectSound release];
+ [erasingSound release];
+ [drawingView release];
+ [window release];
+
+ [super dealloc];
+}
+
+// Change the brush color
+- (void)changeBrushColor:(id)sender
+{
+ CGFloat components[3];
+
+ //Play sound
+ [selectSound play];
+
+ //Set the new brush color
+ HSL2RGB((CGFloat)[sender selectedSegmentIndex] / (CGFloat)kPaletteSize, kSaturation, kLuminosity, &components[0], &components[1], &components[2]);
+ glColor4f(components[0], components[1], components[2], kBrushOpacity);
+}
+
+// Called when receiving the "shake" notification; plays the erase sound and redraws the view
+-(void) eraseView
+{
+ if(CFAbsoluteTimeGetCurrent() > lastTime + kMinEraseInterval) {
+ [erasingSound play];
+ [drawingView erase];
+ lastTime = CFAbsoluteTimeGetCurrent();
+ }
+}
+
+@end
@@ -0,0 +1,92 @@
+/*
+
+File: PaintingView.h
+Abstract: The class responsible for the finger painting. The class wraps the
+CAEAGLLayer from CoreAnimation into a convenient UIView subclass. The view
+content is basically an EAGL surface you render your OpenGL scene into.
+
+Version: 1.7
+
+Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Inc.
+("Apple") in consideration of your agreement to the following terms, and your
+use, installation, modification or redistribution of this Apple software
+constitutes acceptance of these terms. If you do not agree with these terms,
+please do not use, install, modify or redistribute this Apple software.
+
+In consideration of your agreement to abide by the following terms, and subject
+to these terms, Apple grants you a personal, non-exclusive license, under
+Apple's copyrights in this original Apple software (the "Apple Software"), to
+use, reproduce, modify and redistribute the Apple Software, with or without
+modifications, in source and/or binary forms; provided that if you redistribute
+the Apple Software in its entirety and without modifications, you must retain
+this notice and the following text and disclaimers in all such redistributions
+of the Apple Software.
+Neither the name, trademarks, service marks or logos of Apple Inc. may be used
+to endorse or promote products derived from the Apple Software without specific
+prior written permission from Apple. Except as expressly stated in this notice,
+no other rights or licenses, express or implied, are granted by Apple herein,
+including but not limited to any patent rights that may be infringed by your
+derivative works or by other works in which the Apple Software may be
+incorporated.
+
+The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
+WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
+WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
+COMBINATION WITH YOUR PRODUCTS.
+
+IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
+GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR
+DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF
+CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF
+APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+Copyright (C) 2009 Apple Inc. All Rights Reserved.
+
+*/
+
+#import <UIKit/UIKit.h>
+#import <OpenGLES/EAGL.h>
+#import <OpenGLES/ES1/gl.h>
+#import <OpenGLES/ES1/glext.h>
+
+//CONSTANTS:
+
+#define kBrushOpacity (1.0 / 3.0)
+#define kBrushPixelStep 3
+#define kBrushScale 2
+#define kLuminosity 0.75
+#define kSaturation 1.0
+
+//CLASS INTERFACES:
+
+@interface PaintingView : UIView
+{
+@private
+ // The pixel dimensions of the backbuffer
+ GLint backingWidth;
+ GLint backingHeight;
+
+ EAGLContext *context;
+
+ // OpenGL names for the renderbuffer and framebuffers used to render to this view
+ GLuint viewRenderbuffer, viewFramebuffer;
+
+ // OpenGL name for the depth buffer that is attached to viewFramebuffer, if it exists (0 if it does not exist)
+ GLuint depthRenderbuffer;
+
+ GLuint brushTexture;
+ CGPoint location;
+ CGPoint previousLocation;
+ Boolean firstTouch;
+ Boolean needsErase;
+}
+
+@property(nonatomic, readwrite) CGPoint location;
+@property(nonatomic, readwrite) CGPoint previousLocation;
+
+-(void)erase;
+
+@end
Oops, something went wrong.

0 comments on commit 53a762c

Please sign in to comment.