source: trunk/monsterz/ios/classes/MonsterzViewController.mm @ 731

Last change on this file since 731 was 731, checked in by sam, 10 years ago

ios: add cleaned up XCode project.

File size: 3.5 KB
Line 
1//
2//  MonsterzViewController.m
3//  Monsterz
4//
5//  Created by user on 05.03.11.
6//  Copyright 2011 __MyCompanyName__. All rights reserved.
7//
8
9#import <QuartzCore/QuartzCore.h>
10
11#include "core.h"
12#include "lolgl.h"
13#include "loldebug.h"
14
15using namespace lol;
16
17#include "interface.h"
18
19#import "MonsterzViewController.h"
20#import "EAGLView.h"
21
22@interface MonsterzViewController ()
23@property (nonatomic, retain) EAGLContext *context;
24@property (nonatomic, assign) CADisplayLink *displayLink;
25@end
26
27@implementation MonsterzViewController
28
29@synthesize animating, context, displayLink;
30
31- (void)awakeFromNib
32{
33        Ticker::Setup(30.0f);
34        Video::Setup(320, 480);
35       
36        new Interface();
37        new DebugFps(20, 20);
38       
39    EAGLContext *aContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1];
40    if (!aContext)
41        NSLog(@"Failed to create ES context");
42    else if (![EAGLContext setCurrentContext:aContext])
43        NSLog(@"Failed to set ES context current");
44   
45        self.context = aContext;
46        [aContext release];
47       
48    [(EAGLView *)self.view setContext:context];
49    [(EAGLView *)self.view setFramebuffer];
50   
51    animating = FALSE;
52    animationFrameInterval = 1;
53    self.displayLink = nil;
54}
55
56- (void)dealloc
57{
58    // Tear down context.
59    if ([EAGLContext currentContext] == context)
60        [EAGLContext setCurrentContext:nil];
61   
62    [context release];
63   
64    [super dealloc];
65}
66
67- (void)viewWillAppear:(BOOL)animated
68{
69    [self startAnimation];
70   
71    [super viewWillAppear:animated];
72}
73
74- (void)viewWillDisappear:(BOOL)animated
75{
76    [self stopAnimation];
77   
78    [super viewWillDisappear:animated];
79}
80
81- (void)viewDidUnload
82{
83        [super viewDidUnload];
84
85    // Tear down context.
86    if ([EAGLContext currentContext] == context)
87        [EAGLContext setCurrentContext:nil];
88        self.context = nil;     
89}
90
91- (NSInteger)animationFrameInterval
92{
93    return animationFrameInterval;
94}
95
96- (void)setAnimationFrameInterval:(NSInteger)frameInterval
97{
98    /*
99         Frame interval defines how many display frames must pass between each time the display link fires.
100         The display link will only fire 30 times a second when the frame internal is two on a display that refreshes 60 times a second. The default frame interval setting of one will fire 60 times a second when the display refreshes at 60 times a second. A frame interval setting of less than one results in undefined behavior.
101         */
102    if (frameInterval >= 1)
103    {
104        animationFrameInterval = frameInterval;
105       
106        if (animating)
107        {
108            [self stopAnimation];
109            [self startAnimation];
110        }
111    }
112}
113
114- (void)startAnimation
115{
116    if (!animating)
117    {
118        CADisplayLink *aDisplayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(drawFrame)];
119        [aDisplayLink setFrameInterval:animationFrameInterval];
120        [aDisplayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
121        self.displayLink = aDisplayLink;
122       
123        animating = TRUE;
124    }
125}
126
127- (void)stopAnimation
128{
129    if (animating)
130    {
131        [self.displayLink invalidate];
132        self.displayLink = nil;
133        animating = FALSE;
134    }
135}
136
137- (void)drawFrame
138{
139    [(EAGLView *)self.view setFramebuffer];
140   
141        Ticker::ClampFps();
142        Ticker::TickGame();
143        Ticker::TickDraw();
144
145    [(EAGLView *)self.view presentFramebuffer];
146}
147
148- (void)didReceiveMemoryWarning
149{
150    // Releases the view if it doesn't have a superview.
151    [super didReceiveMemoryWarning];
152   
153    // Release any cached data, images, etc. that aren't in use.
154}
155
156@end
Note: See TracBrowser for help on using the repository browser.