/*
 Copyright (C) 2009-2011 id Software LLC, a ZeniMax Media company. 
 
 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
 */

#import "iphone_glViewController.h"
#import "EAGLView.h"
#import <QuartzCore/CADisplayLink.h>
#include "doomiphone.h"

const static int   DISPLAY_LINK_FRAME_INTERVAL = 2;

// Need one buffer frame when transitioning from IB menus to the OpenGL game view.
// Otherwise, occasionally the IB view stays onscreen during the Doom loading frame.
// This seems to make precaching take way to long (about a whole minute).
// This flag will be set to true in StartDisplay, and reset to false after one display link
// frame has fired. 
static bool inTransition = false;

@implementation iphone_glViewController

@synthesize displayLink;

/*
 ========================
 runFrame
 ========================
 */
- (void)runFrame {
    
	// Skip the frist frame after coming from IB menus, to give the view a chance to switch to
	// OpenGL. For some reason, not doing this causes precaching to take way too long.
	if ( inTransition ) {
		inTransition = false;
		return;
	}
	
    // Update the Game
    iphoneAsyncTic(); 
    
    // Render the Game.
	iphoneFrame();
}

/*
 ========================
 initWithNibName
 ========================
 */
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        // Create the OpenGL View.
        EAGLView *glView = [[EAGLView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
        self.view = glView;
        [glView release];
        
        
        // Setup the Display Link
        CADisplayLink *aDisplayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(runFrame)];
        [ aDisplayLink setFrameInterval: DISPLAY_LINK_FRAME_INTERVAL];
        [ aDisplayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
        [ self setDisplayLink: aDisplayLink ];
        
    }
    return self;
}

/*
========================
shouldAutorotateToInterfaceOrientation
========================
*/
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
	// Return YES for supported orientations.
	return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}

/*
 ========================
 viewDidLoad
 ========================
 */
- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Stop the Display link.
    [self.displayLink invalidate];
    self.displayLink = nil;
}


/*
 ========================
 didReceiveMemoryWarning
 ========================
 */
- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
}

/*
 ========================
 viewDidUnload
 ========================
 */
- (void)viewDidUnload {
    [super viewDidUnload];
}

/*
 ========================
 dealloc
 ========================
 */
- (void)dealloc {
    [super dealloc];
}

/*
 ========================
 StartDisplay
 ========================
 */
- (void) StartDisplay {
	inTransition = true;
    displayLink.paused = NO;
}

/*
 ========================
 StopDisplay
 ========================
 */
- (void) StopDisplay {
    displayLink.paused = YES;
}

/*
 ========================
 IsDisplaying
 ========================
 */
- (bool) IsDisplaying {
    return displayLink.paused;
}

@end
