Skip to content

Commit

Permalink
updates in draw functions
Browse files Browse the repository at this point in the history
- changes in draw function to make it more generic
- draw the App Icon on the screen when no GIF file is found
  • Loading branch information
Waitsnake committed Nov 10, 2018
1 parent 22df24b commit 18b6260
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 27 deletions.
2 changes: 1 addition & 1 deletion AnimatedGif/AnimatedGifView.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
- (NSTimeInterval)getDurationFromGifFile:(NSString*)gifFileName;
- (void) receiveDisplaysChangeNote: (NSNotification*) note;
- (void) drawAttributedString:(NSAttributedString *)attributedString atPoint:(NSPoint)point;
- (void) drawImage:(void *)pixelsBytes atRect:(NSRect) rect;
- (void) drawImage:(void *)pixelsBytes pixelWidth:(NSInteger)width pixelHeight:(NSInteger)height hasAlpha: (Boolean)alpha atRect:(NSRect) rect;

@property (nonatomic, retain) NSOpenGLView* glView;
@property (assign) IBOutlet NSPanel *optionsPanel;
Expand Down
74 changes: 48 additions & 26 deletions AnimatedGif/AnimatedGifView.m
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ - (NSOpenGLView *)createGLView
return glview;
}

- (void) drawRect:(NSRect)rect
{
// not needed since we use animateOneFrame() and left empty so that super method of NSView is not called to save same CPU time
}

- (void)setFrameSize:(NSSize)newSize
{
[super setFrameSize:newSize];
Expand Down Expand Up @@ -299,13 +304,35 @@ - (void)animateOneFrame
[attribs setObject: [NSColor redColor] forKey: NSForegroundColorAttributeName];
NSAttributedString *nogifAtStr = [[NSAttributedString alloc] initWithString:NSLocalizedStringFromTableInBundle(@"nogif",@"Localizable",[NSBundle bundleForClass:[self class]],nil) attributes:attribs];
NSAttributedString *selectAtStr = [[NSAttributedString alloc] initWithString:NSLocalizedStringFromTableInBundle(@"select",@"Localizable",[NSBundle bundleForClass:[self class]],nil) attributes:attribs];
[self drawAttributedString:nogifAtStr atPoint:NSMakePoint (screenRect.size.width/2 - [nogifAtStr size].width/2, screenRect.size.height/2 - screenRect.size.height/4)];
[self drawAttributedString:selectAtStr atPoint:NSMakePoint (screenRect.size.width/2 - [selectAtStr size].width/2, screenRect.size.height/2)];
[self drawAttributedString:nogifAtStr atPoint:NSMakePoint (screenRect.size.width/2 - [nogifAtStr size].width/2, screenRect.size.height/4-[nogifAtStr size].height/2)];
[self drawAttributedString:selectAtStr atPoint:NSMakePoint (screenRect.size.width/2 - [selectAtStr size].width/2, screenRect.size.height/2-[selectAtStr size].height/2)];

NSImage *iconImg = [[NSBundle bundleForClass:[self class]] imageForResource:@"thumbnail.tiff"];
if (iconImg)
{
NSBitmapImageRep *iconRep = [NSBitmapImageRep imageRepWithData:[iconImg TIFFRepresentation]];
if (iconRep)
{
NSSize iconSize;
if ([self isPreview])
{
iconSize = NSMakeSize([iconRep size].width/2, [iconRep size].height/2);
}
else
{
iconSize = NSMakeSize([iconRep size].width*2, [iconRep size].height*2);
}
void *pixelDataIcon= [iconRep bitmapData];;
if (pixelDataIcon != NULL)
{
[self drawImage:pixelDataIcon pixelWidth: [iconRep pixelsWide] pixelHeight:[iconRep pixelsHigh] hasAlpha:[iconRep hasAlpha] atRect:NSMakeRect(screenRect.size.width/2 - iconSize.width/2, screenRect.size.height/4*3 - iconSize.height/2, iconSize.width, iconSize.height)];
}
}
}

glPopMatrix();

glFlush();
[self.glView.openGLContext flushBuffer];
[self setNeedsDisplay:YES];

[self.glView.openGLContext flushBuffer]; // Swap Buffers and can only used after setting up OpenGL view with option NSOpenGLPFADoubleBuffer otherwise use glFlush()

}
else
Expand Down Expand Up @@ -350,7 +377,7 @@ - (void)animateOneFrame
if (tiles == TILE_OPT_1)
{
// only draw one tile
[self drawImage:pixelData atRect:targetRect];
[self drawImage:pixelData pixelWidth: [gifRep pixelsWide] pixelHeight:[gifRep pixelsHigh] hasAlpha:[gifRep hasAlpha] atRect:targetRect];
}
else
{
Expand All @@ -364,26 +391,21 @@ - (void)animateOneFrame
NSRect r13 = NSMakeRect(targetRect.origin.x, targetRect.origin.y+targetRect.size.height/3*2, targetRect.size.width/3, targetRect.size.height/3);
NSRect r23 = NSMakeRect(targetRect.origin.x+targetRect.size.width/3, targetRect.origin.y+targetRect.size.height/3*2, targetRect.size.width/3, targetRect.size.height/3);
NSRect r33 = NSMakeRect(targetRect.origin.x+targetRect.size.width/3*2, targetRect.origin.y+targetRect.size.height/3*2, targetRect.size.width/3, targetRect.size.height/3);
[self drawImage:pixelData atRect:r11];
[self drawImage:pixelData atRect:r21];
[self drawImage:pixelData atRect:r31];
[self drawImage:pixelData atRect:r12];
[self drawImage:pixelData atRect:r22];
[self drawImage:pixelData atRect:r32];
[self drawImage:pixelData atRect:r13];
[self drawImage:pixelData atRect:r23];
[self drawImage:pixelData atRect:r33];
[self drawImage:pixelData pixelWidth: [gifRep pixelsWide] pixelHeight:[gifRep pixelsHigh] hasAlpha:[gifRep hasAlpha] atRect:r11];
[self drawImage:pixelData pixelWidth: [gifRep pixelsWide] pixelHeight:[gifRep pixelsHigh] hasAlpha:[gifRep hasAlpha] atRect:r21];
[self drawImage:pixelData pixelWidth: [gifRep pixelsWide] pixelHeight:[gifRep pixelsHigh] hasAlpha:[gifRep hasAlpha] atRect:r31];
[self drawImage:pixelData pixelWidth: [gifRep pixelsWide] pixelHeight:[gifRep pixelsHigh] hasAlpha:[gifRep hasAlpha] atRect:r12];
[self drawImage:pixelData pixelWidth: [gifRep pixelsWide] pixelHeight:[gifRep pixelsHigh] hasAlpha:[gifRep hasAlpha] atRect:r22];
[self drawImage:pixelData pixelWidth: [gifRep pixelsWide] pixelHeight:[gifRep pixelsHigh] hasAlpha:[gifRep hasAlpha] atRect:r32];
[self drawImage:pixelData pixelWidth: [gifRep pixelsWide] pixelHeight:[gifRep pixelsHigh] hasAlpha:[gifRep hasAlpha] atRect:r13];
[self drawImage:pixelData pixelWidth: [gifRep pixelsWide] pixelHeight:[gifRep pixelsHigh] hasAlpha:[gifRep hasAlpha] atRect:r23];
[self drawImage:pixelData pixelWidth: [gifRep pixelsWide] pixelHeight:[gifRep pixelsHigh] hasAlpha:[gifRep hasAlpha] atRect:r33];
}

//End phase
glPopMatrix();

glFlush();

[self.glView.openGLContext flushBuffer];

[self setNeedsDisplay:YES];

[self.glView.openGLContext flushBuffer]; // Swap Buffers and can only used after setting up OpenGL view with option NSOpenGLPFADoubleBuffer otherwise use glFlush()

//calculate next frame of GIF to show
if (currFrameCount < maxFrameCount-1)
Expand Down Expand Up @@ -1360,10 +1382,10 @@ - (void) drawAttributedString:(NSAttributedString *)attributedString atPoint:(NS
glDeleteTextures(1,&texturName);
}

- (void) drawImage:(void *)pixelsBytes atRect:(NSRect) rect
- (void) drawImage:(void *)pixelsBytes pixelWidth:(NSInteger)width pixelHeight:(NSInteger)height hasAlpha: (Boolean)alpha atRect:(NSRect) rect
{
glEnable(GL_TEXTURE_2D);
if ([gifRep hasAlpha] == TRUE) {
if (alpha == TRUE) {
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
}
Expand Down Expand Up @@ -1398,8 +1420,8 @@ - (void) drawImage:(void *)pixelsBytes atRect:(NSRect) rect
glTexImage2D(GL_TEXTURE_2D,
0,
GL_RGBA,
(GLint)[gifRep pixelsWide],
(GLint)[gifRep pixelsHigh],
(GLint)width,
(GLint)height,
0,
GL_RGBA,
GL_UNSIGNED_BYTE,
Expand Down
Binary file modified Release/AnimatedGif.saver.zip
Binary file not shown.

0 comments on commit 18b6260

Please sign in to comment.