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

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

ios: update XCode project and fix a GL test compilation issue.

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(vec2i(320, 480));
37
38    //new Interface();
39    //new DebugFps(20, 20);
40    new DebugQuad();
41
42    EAGLContext *aContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1];
43    if (!aContext)
44        NSLog(@"Failed to create ES context");
45    else if (![EAGLContext setCurrentContext:aContext])
46        NSLog(@"Failed to set ES context current");
47
48    self.context = aContext;
49    [aContext release];
50
51    [(EAGLView *)self.view setContext:context];
52    [(EAGLView *)self.view setFramebuffer];
53
54    animating = FALSE;
55    animationFrameInterval = 1;
56    self.displayLink = nil;
57}
58
59- (void)dealloc
60{
61    // Tear down context.
62    if ([EAGLContext currentContext] == context)
63        [EAGLContext setCurrentContext:nil];
64
65    [context release];
66    [super dealloc];
67}
68
69- (void)viewWillAppear:(BOOL)animated
70{
71    [self startAnimation];
72    [super viewWillAppear:animated];
73}
74
75- (void)viewWillDisappear:(BOOL)animated
76{
77    [self stopAnimation];
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    /* Frame interval defines how many display frames must pass between each
99     * time the display link fires. The display link will only fire 30 times
100     * a second when the frame internal is two on a display that refreshes
101     * 60 times a second. The default frame interval setting of one will
102     * fire 60 times a second when the display refreshes at 60 times a
103     * second. A frame interval setting of less than one results in undefined
104     * behavior. */
105    if (frameInterval >= 1)
106    {
107        animationFrameInterval = frameInterval;
108
109        if (animating)
110        {
111            [self stopAnimation];
112            [self startAnimation];
113        }
114    }
115}
116
117- (void)startAnimation
118{
119    if (!animating)
120    {
121        CADisplayLink *aDisplayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(drawFrame)];
122        [aDisplayLink setFrameInterval:animationFrameInterval];
123        [aDisplayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
124        self.displayLink = aDisplayLink;
125
126        animating = TRUE;
127    }
128}
129
130- (void)stopAnimation
131{
132    if (animating)
133    {
134        [self.displayLink invalidate];
135        self.displayLink = nil;
136        animating = FALSE;
137    }
138}
139
140- (void)drawFrame
141{
142    [(EAGLView *)self.view setFramebuffer];
143    Ticker::ClampFps();
144    Ticker::TickGame();
145    Ticker::TickDraw();
146    [(EAGLView *)self.view presentFramebuffer];
147}
148
149- (void)didReceiveMemoryWarning
150{
151    // Releases the view if it doesn't have a superview.
152    [super didReceiveMemoryWarning];
153    // Release any cached data, images, etc. that aren't in use.
154}
155
156@end
157
Note: See TracBrowser for help on using the repository browser.