screenshots 4 ios
authorkoda
Tue, 08 Feb 2011 02:43:07 +0100
changeset 4935 27fb500dd6b1
parent 4934 727da72f754a
child 4936 d65d438acd23
screenshots 4 ios
hedgewars/hwengine.pas
project_files/HedgewarsMobile/Classes/InGameMenuViewController.h
project_files/HedgewarsMobile/Classes/InGameMenuViewController.m
project_files/HedgewarsMobile/Classes/OverlayViewController.m
--- a/hedgewars/hwengine.pas	Mon Feb 07 23:22:14 2011 +0100
+++ b/hedgewars/hwengine.pas	Tue Feb 08 02:43:07 2011 +0100
@@ -116,7 +116,6 @@
         WriteLnToConsole('Saving ' + s + '...');
         playSound(sndShutter);
         MakeScreenshot(s);
-        //SDL_SaveBMP_RW(SDLPrimSurface, SDL_RWFromFile(Str2PChar(s), 'wb'), 1)
     end;
 end;
 
--- a/project_files/HedgewarsMobile/Classes/InGameMenuViewController.h	Mon Feb 07 23:22:14 2011 +0100
+++ b/project_files/HedgewarsMobile/Classes/InGameMenuViewController.h	Tue Feb 08 02:43:07 2011 +0100
@@ -24,11 +24,13 @@
 
 @interface InGameMenuViewController : UITableViewController <UIActionSheetDelegate> {
     NSArray *menuList;
+    BOOL shouldTakeScreenshot;
 }
 
 @property (nonatomic,retain) NSArray *menuList;
 
 -(void) present;
 -(void) dismiss;
+-(void) saveCurrentScreenToPhotoAlbum:(UIAlertView *)alert;
 
 @end
--- a/project_files/HedgewarsMobile/Classes/InGameMenuViewController.m	Mon Feb 07 23:22:14 2011 +0100
+++ b/project_files/HedgewarsMobile/Classes/InGameMenuViewController.m	Tue Feb 08 02:43:07 2011 +0100
@@ -25,6 +25,9 @@
 #import "CommodityFunctions.h"
 #import "SDL_sysvideo.h"
 #import "SDL_uikitkeyboard.h"
+#import "OpenGLES/ES1/gl.h"
+
+#define VIEW_HEIGHT 200
 
 @implementation InGameMenuViewController
 @synthesize menuList;
@@ -43,6 +46,7 @@
     NSArray *array = [[NSArray alloc] initWithObjects:
                       NSLocalizedString(@"Show Help", @""),
                       NSLocalizedString(@"Tag", @""),
+                      NSLocalizedString(@"Snapshot",@""),
                       NSLocalizedString(@"End Game", @""),
                       nil];
     self.menuList = array;
@@ -67,11 +71,11 @@
 -(void) present {
     CGRect screen = [[UIScreen mainScreen] bounds];
     self.view.backgroundColor = [UIColor clearColor];
-    self.view.frame = CGRectMake(screen.size.height, 0, 200, 170);
+    self.view.frame = CGRectMake(screen.size.height, 0, 200, VIEW_HEIGHT);
 
     [UIView beginAnimations:@"showing popover" context:NULL];
     [UIView setAnimationDuration:0.35];
-    self.view.frame = CGRectMake(screen.size.height-200, 0, 200, 170);
+    self.view.frame = CGRectMake(screen.size.height-200, 0, 200, VIEW_HEIGHT);
     [UIView commitAnimations];
 }
 
@@ -80,13 +84,33 @@
         CGRect screen = [[UIScreen mainScreen] bounds];
         [UIView beginAnimations:@"hiding popover" context:NULL];
         [UIView setAnimationDuration:0.35];
-        self.view.frame = CGRectMake(screen.size.height, 0, 200, 170);
+        self.view.frame = CGRectMake(screen.size.height, 0, 200, VIEW_HEIGHT);
         [UIView commitAnimations];
         [self.view performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:0.35];
     }
 
     HW_chatEnd();
     SDL_iPhoneKeyboardHide((SDL_Window *)HW_getSDLWindow());
+
+    if (shouldTakeScreenshot) {
+        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Please wait"
+                                                        message:nil
+                                                       delegate:nil
+                                              cancelButtonTitle:nil
+                                              otherButtonTitles:nil];
+        [alert show];
+        UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc]
+                                              initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
+        indicator.center = CGPointMake(alert.bounds.size.width / 2, alert.bounds.size.height - 45);
+        [indicator startAnimating];
+        [alert addSubview:indicator];
+        [indicator release];
+
+        // all these hacks because of the PAUSE caption on top of everything...
+        [self performSelector:@selector(saveCurrentScreenToPhotoAlbum:) withObject:alert afterDelay:0.3];
+    }
+    shouldTakeScreenshot = NO;
+
 }
 
 #pragma mark -
@@ -96,7 +120,7 @@
 }
 
 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
-    return 3;
+    return 4;
 }
 
 -(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
@@ -117,16 +141,30 @@
 
 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
     UIActionSheet *actionSheet;
+    UIAlertView *alert;
 
     switch ([indexPath row]) {
         case 0:
             [[NSNotificationCenter defaultCenter] postNotificationName:@"show help ingame" object:nil];
+
             break;
         case 1:
             HW_chat();
             SDL_iPhoneKeyboardShow((SDL_Window *)HW_getSDLWindow());
+
             break;
         case 2:
+            alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Going to take a screenshot",@"")
+                                               message:NSLocalizedString(@"The game snapshot will be placed in your Photo Album and it will be taken as soon as the pause menu is dismissed",@"")
+                                              delegate:nil
+                                     cancelButtonTitle:NSLocalizedString(@"Ok, got it",@"")
+                                     otherButtonTitles:nil];
+            [alert show];
+            [alert release];
+            shouldTakeScreenshot = YES;
+
+            break;
+        case 3:
             // expand the view (and table) so that the actionsheet can be selected on the iPhone
             if (IS_IPAD() == NO) {
                 CGRect screen = [[UIScreen mainScreen] bounds];
@@ -160,7 +198,7 @@
         CGRect screen = [[UIScreen mainScreen] bounds];
         [UIView beginAnimations:@"table width less" context:NULL];
         [UIView setAnimationDuration:0.2];
-        self.view.frame = CGRectMake(screen.size.height-200, 0, 200, 170);
+        self.view.frame = CGRectMake(screen.size.height-200, 0, 200, VIEW_HEIGHT);
         [UIView commitAnimations];
     }
 
@@ -171,4 +209,59 @@
     }
 }
 
+#pragma mark save screenshot
+//by http://www.bit-101.com/blog/?p=1861
+// callback for CGDataProviderCreateWithData
+void releaseData(void *info, const void *data, size_t dataSize) {
+    DLog(@"freeing raw data\n");
+    free((void *)data);
+}
+
+// callback for UIImageWriteToSavedPhotosAlbum
+-(void) image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
+    DLog(@"Save finished\n");
+    [image release];
+    UIAlertView *alert = (UIAlertView *)contextInfo;
+    [alert dismissWithClickedButtonIndex:0 animated:YES];
+    [alert release];
+}
+
+// the resolution of the buffer is always equal to the hardware device even if scaled
+-(void) saveCurrentScreenToPhotoAlbum:(UIAlertView *)alert {
+    CGRect screenRect = [[UIScreen mainScreen] bounds];
+    int width = screenRect.size.width;
+    int height = screenRect.size.height;
+
+    NSInteger size = width * height * 4;
+    GLubyte *buffer = (GLubyte *) malloc(size * sizeof(GLubyte));
+    GLubyte *buffer_flipped = (GLubyte *) malloc(size * sizeof(GLubyte));
+
+    glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
+
+    // flip the data as glReadPixels here reads upside down
+    for(int y = 0; y <height; y++)
+        for(int x = 0; x <width * 4; x++)
+            buffer_flipped[(int)((height - 1 - y) * width * 4 + x)] = buffer[(int)(y * 4 * width + x)];
+    free(buffer);
+
+    CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer_flipped, size, releaseData);
+    CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
+    CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
+    CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
+    CGImageRef imageRef = CGImageCreate(width, height, 8, 32, 4 * width, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
+
+    CGColorSpaceRelease(colorSpaceRef);
+    CGDataProviderRelease(provider);
+
+    UIImage *image;
+    if ([UIImage respondsToSelector:@selector(imageWithCGImage:scale:orientation:)])
+        image = [[UIImage alloc] initWithCGImage:imageRef scale:1 orientation:UIImageOrientationRight];
+    else
+        image = [[UIImage alloc] initWithCGImage:imageRef];
+    CGImageRelease(imageRef);
+
+    UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), (void *)alert); // add callback for finish saving
+}
+
+
 @end
--- a/project_files/HedgewarsMobile/Classes/OverlayViewController.m	Mon Feb 07 23:22:14 2011 +0100
+++ b/project_files/HedgewarsMobile/Classes/OverlayViewController.m	Tue Feb 08 02:43:07 2011 +0100
@@ -474,7 +474,7 @@
             self.popupMenu = [[InGameMenuViewController alloc] initWithStyle:UITableViewStylePlain];
         if (self.popoverController == nil) {
             self.popoverController = [[UIPopoverController alloc] initWithContentViewController:self.popupMenu];
-            [self.popoverController setPopoverContentSize:CGSizeMake(220, 170) animated:YES];
+            [self.popoverController setPopoverContentSize:CGSizeMake(220, 200) animated:YES];
             [self.popoverController setPassthroughViews:[NSArray arrayWithObject:self.view]];
         }