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

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

ios: clean up code.

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