the whole game
This commit is contained in:
163
project/iosproj/minecraftpe/EAGLView.m
Executable file
163
project/iosproj/minecraftpe/EAGLView.m
Executable file
@@ -0,0 +1,163 @@
|
||||
//
|
||||
// EAGLView.m
|
||||
// OpenGLES_iPhone
|
||||
//
|
||||
// Created by mmalc Crawford on 11/18/10.
|
||||
// Copyright 2010 Apple Inc. All rights reserved.
|
||||
//
|
||||
|
||||
#import "EAGLView.h"
|
||||
|
||||
#import <QuartzCore/QuartzCore.h>
|
||||
|
||||
@interface EAGLView (PrivateMethods)
|
||||
- (void)createFramebuffer;
|
||||
- (void)deleteFramebuffer;
|
||||
@end
|
||||
|
||||
@implementation EAGLView
|
||||
|
||||
@synthesize context;
|
||||
|
||||
// You must implement this method
|
||||
+ (Class)layerClass
|
||||
{
|
||||
return [CAEAGLLayer class];
|
||||
}
|
||||
|
||||
//The EAGL view is stored in the nib file. When it's unarchived it's sent -initWithCoder:.
|
||||
- (id)initWithCoder:(NSCoder*)coder
|
||||
{
|
||||
self = [super initWithCoder:coder];
|
||||
if (self) {
|
||||
CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer;
|
||||
|
||||
eaglLayer.opaque = TRUE;
|
||||
eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
|
||||
[NSNumber numberWithBool:FALSE], kEAGLDrawablePropertyRetainedBacking,
|
||||
kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat,
|
||||
nil];
|
||||
|
||||
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]
|
||||
&& [self respondsToSelector:@selector(setContentScaleFactor:)])
|
||||
{
|
||||
viewScale = [UIScreen mainScreen].scale;
|
||||
NSLog(@"Scale is : %f\n", viewScale);
|
||||
[self setContentScaleFactor: viewScale];
|
||||
eaglLayer.contentsScale = viewScale;
|
||||
}
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)dealloc
|
||||
{
|
||||
[self deleteFramebuffer];
|
||||
[context release];
|
||||
|
||||
[super dealloc];
|
||||
}
|
||||
|
||||
- (void)setContext:(EAGLContext *)newContext
|
||||
{
|
||||
if (context != newContext) {
|
||||
[self deleteFramebuffer];
|
||||
|
||||
[context release];
|
||||
context = [newContext retain];
|
||||
|
||||
[EAGLContext setCurrentContext:nil];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)createFramebuffer
|
||||
{
|
||||
if (context && !defaultFramebuffer) {
|
||||
[EAGLContext setCurrentContext:context];
|
||||
|
||||
// Create default framebuffer object.
|
||||
glGenFramebuffers(1, &defaultFramebuffer);
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, defaultFramebuffer);
|
||||
|
||||
// Create color render buffer and allocate backing store.
|
||||
glGenRenderbuffers(1, &colorRenderbuffer);
|
||||
glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer);
|
||||
[context renderbufferStorage:GL_RENDERBUFFER fromDrawable:(CAEAGLLayer *)self.layer];
|
||||
glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &framebufferWidth);
|
||||
glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &framebufferHeight);
|
||||
|
||||
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, colorRenderbuffer);
|
||||
|
||||
// Create depth buffer and allocate backing store
|
||||
glGenRenderbuffersOES(1, &_depthRenderBuffer);
|
||||
glBindRenderbufferOES(GL_RENDERBUFFER_OES, _depthRenderBuffer);
|
||||
|
||||
glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_DEPTH_COMPONENT24_OES, framebufferWidth, framebufferHeight);
|
||||
glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, _depthRenderBuffer);
|
||||
|
||||
NSLog(@"Created framebuffer with size %d, %d\n", framebufferWidth, framebufferHeight);
|
||||
|
||||
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
|
||||
NSLog(@"Failed to make complete framebuffer object %x", glCheckFramebufferStatus(GL_FRAMEBUFFER));
|
||||
}
|
||||
}
|
||||
|
||||
- (void)deleteFramebuffer
|
||||
{
|
||||
if (context) {
|
||||
[EAGLContext setCurrentContext:context];
|
||||
|
||||
if (defaultFramebuffer) {
|
||||
glDeleteFramebuffers(1, &defaultFramebuffer);
|
||||
defaultFramebuffer = 0;
|
||||
}
|
||||
|
||||
if (colorRenderbuffer) {
|
||||
glDeleteRenderbuffers(1, &colorRenderbuffer);
|
||||
colorRenderbuffer = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
- (void)setFramebuffer
|
||||
{
|
||||
if (context) {
|
||||
[EAGLContext setCurrentContext:context];
|
||||
|
||||
if (!defaultFramebuffer)
|
||||
[self createFramebuffer];
|
||||
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, defaultFramebuffer);
|
||||
|
||||
glViewport(0, 0, framebufferWidth, framebufferHeight);
|
||||
}
|
||||
}
|
||||
|
||||
- (BOOL)presentFramebuffer
|
||||
{
|
||||
BOOL success = FALSE;
|
||||
|
||||
if (context) {
|
||||
[EAGLContext setCurrentContext:context];
|
||||
|
||||
glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer);
|
||||
|
||||
success = [context presentRenderbuffer:GL_RENDERBUFFER];
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
- (void)layoutSubviews
|
||||
{
|
||||
// The framebuffer will be re-created at the beginning of the next setFramebuffer method call.
|
||||
[self deleteFramebuffer];
|
||||
}
|
||||
|
||||
- (BOOL) isMultipleTouchEnabled {
|
||||
return YES;
|
||||
}
|
||||
|
||||
|
||||
@end
|
||||
Reference in New Issue
Block a user