-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathViewController.m
157 lines (119 loc) · 5.31 KB
/
ViewController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#import "ViewController.h"
#import "MyTexture2D.h"
const NSString* prince_animation_keys[] = {@"prince0", @"prince1", @"prince2", @"prince3", @"prince4", @"prince5", @"prince6", @"prince7", @"prince8", @"prince9", @"prince10", @"prince11", @"prince12"};
const int prince_animation_key_count = 13;
const NSString* potion_animation_keys[] = {@"potion0", @"potion1", @"potion2", @"potion3", @"potion4", @"potion5", @"potion6", @"potion7", @"potion8", @"potion9", @"potion10", @"potion11", @"potion12"};
const int potion_animation_key_count = 13;
@interface ViewController ()
@property (strong, nonatomic) EAGLContext* context;
@property (nonatomic, strong) MyTexture2D* wheel;
@property (nonatomic, strong) MyTexture2D* box;
@property (nonatomic, strong) MyTexture2D* spritesheet;
@property (nonatomic, strong) MyTexture2D* textureAtlas;
@property (nonatomic, assign) float angle;
@property (nonatomic, assign) float princeIndex;
@property (nonatomic, assign) float potionIndex;
- (void)setupGL;
- (void)tearDownGL;
- (void)loadTextures;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
GLKView *view = (GLKView *)self.view;
self.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1];
view.context = self.context;
if (!self.context) {
NSLog(@"Failed to create ES context");
}
// Configure renderbuffers created by the view
view.drawableColorFormat = GLKViewDrawableColorFormatRGB565;
view.drawableDepthFormat = GLKViewDrawableDepthFormatNone;
//view.drawableStencilFormat = GLKViewDrawableStencilFormat8;
// Enable multisampling
//view.drawableMultisample = GLKViewDrawableMultisample4X;
self.preferredFramesPerSecond = 60;
[self setupGL];
[self loadTextures];
}
- (void)dealloc {
[self tearDownGL];
if ([EAGLContext currentContext] == self.context) {
[EAGLContext setCurrentContext:nil];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
if ([self isViewLoaded] && ([[self view] window] == nil)) {
self.view = nil;
[self tearDownGL];
if ([EAGLContext currentContext] == self.context) {
[EAGLContext setCurrentContext:nil];
}
self.context = nil;
}
// Dispose of any resources that can be recreated.
}
- (void)setupGL {
[EAGLContext setCurrentContext:self.context];
glMatrixMode(GL_PROJECTION);
glOrthof(0, self.view.bounds.size.width, 0, self.view.bounds.size.height, -1, 1);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_TEXTURE_2D);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
}
- (void)loadTextures {
UIImage* wheelImage = [UIImage imageNamed:@"wheel.png"];
self.wheel = [[MyTexture2D alloc] initWithImage:wheelImage];
UIImage* boxImage = [UIImage imageNamed:@"box.png"];
self.box = [[MyTexture2D alloc] initWithImage:boxImage];
UIImage* spritesheetImage = [UIImage imageNamed:@"spritesheet.png"];
self.spritesheet = [[MyTexture2D alloc] initWithImage:spritesheetImage];
UIImage* textureAtlasImage = [UIImage imageNamed:@"textureatlas.png"];
self.textureAtlas = [[MyTexture2D alloc] initWithImage:textureAtlasImage atlasFilename:@"textureatlas.txt"];
NSLog(@"TextureAtlas content size: %0.1f x %0.1f", self.textureAtlas.contentSize.width, self.textureAtlas.contentSize.height);
NSLog(@"TextureAtlas texture size: %d x %d", self.textureAtlas.pixelsWide, self.textureAtlas.pixelsHigh);
NSLog(@"TextureAtlas count: %d", self.textureAtlas.count);
self.princeIndex = 0.0f;
self.potionIndex = 0.0f;
}
- (void)tearDownGL {
[EAGLContext setCurrentContext:self.context];
}
#pragma mark - GLKView and GLKViewController delegate methods
- (void)update {
// Animate
self.angle += 1;
if (self.angle > 360) {
self.angle -= 360;
}
self.princeIndex += 0.5;
if (self.princeIndex > (prince_animation_key_count - 1)) {
self.princeIndex -= 8; // only repeat the last 8 frames
}
_potionIndex += 0.125;
if (self.potionIndex > (potion_animation_key_count - 1)) {
self.potionIndex -= potion_animation_key_count;
}
}
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect {
glLoadIdentity();
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// Rotated textures
[self.wheel drawAtPoint:CGPointMake(106, 384)];
[self.wheel drawAtPoint:CGPointMake(106, 288) rotatedBy:self.angle];
[self.box drawAtPoint:CGPointMake(106, 192) rotatedBy:self.angle];
[self.wheel drawAtPoint:CGPointMake(106, 192) rotatedBy:-self.angle];
[self.box drawAtPoint:CGPointMake(106, 96) rotatedBy:-self.angle];
// Texture from a sprite sheet
[self.spritesheet drawAsSpriteSheetAtPoint:CGPointMake(212, 288) sheetDimensions:CGSizeMake(4, 4) index:(int)(self.princeIndex)];
// Textures from a texture atlas
[self.textureAtlas drawFromAtlasAtPoint:CGPointMake(212, 192) key:(NSString*)prince_animation_keys[(int)self.princeIndex]];
[self.textureAtlas drawFromAtlasAtPoint:CGPointMake(212, 96) key:(NSString*)potion_animation_keys[(int)self.potionIndex]];
}
@end