2017-08-14 104 views
-2

我想從AppDelegate.m的applicationWillResignActive函數調用GameViewController.mm的tearDownGL函數。從Objective C文件調用Objective C函數

如何從AppDelegate.m訪問GameViewController.mm的函數?

這裏是我的GameViewController.h文件:

#ifndef gameviewcontroller_h 
#define gameviewcontroller_h 

#import <UIKit/UIKit.h> 
#import <GLKit/GLKit.h> 



@interface GameViewController : GLKViewController 

@end 



#endif 

我GameViewController.mm是:

#import "GameViewController.h" 
#import <OpenGLES/ES2/glext.h> 

#include "Shader.h" 
#include "matrix_utils.h" 
#include "tga_image.h" 

#include <iostream> 
using std::cout; 
using std::endl; 

#include <vector> 
using std::vector; 

#include <chrono> 
using std::chrono::high_resolution_clock; 
using std::chrono::duration_cast; 
using std::chrono::milliseconds; 
using std::chrono::time_point; 


float last_click_float_x, last_click_float_y; 

Shader shader("shader.vert", "shader.frag"); 
unsigned int triangle_buffer; 
GLuint card_tex, rank_tex; 

@interface GameViewController() { 

} 

@property (strong, nonatomic) EAGLContext *context; 



@end 

@implementation GameViewController 


- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    self.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2]; 

    if (!self.context) { 
     NSLog(@"Failed to create ES context"); 
    } 

    GLKView *view = (GLKView *)self.view; 
    view.context = self.context; 
    view.drawableDepthFormat = GLKViewDrawableDepthFormat24; 

    [self setupGL]; 
} 


- (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. 
} 

- (BOOL)prefersStatusBarHidden { 
    return NO; 
} 


- (void)setupGL 
{ 
    NSLog(@"setupGL"); 

    [EAGLContext setCurrentContext:self.context]; 

    glClearColor(0.284313, 0.415686, 0, 1); 

    glEnable(GL_CULL_FACE); 

    glEnable(GL_BLEND); 
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 

    if(!shader.compileAndLink()) 
     NSLog(@"Failed to load shader"); 

    //tell OpenGL to use this shader for all coming rendering 
    glUseProgram(shader.getProgram()); 

    unsigned int m_renderbufferWidth, m_renderbufferHeight; 
    CGRect Rect=[[UIScreen mainScreen] bounds]; 
    m_renderbufferWidth = Rect.size.width; 
    m_renderbufferHeight = Rect.size.height; 

    float projection_modelview_mat[16]; 

    init_perspective_camera(45.0f, 
          float(m_renderbufferWidth)/float(m_renderbufferHeight), 
          0.01f, 10.0f, 
          0, 0, 1, // Camera position. 
          0, 0, 0, // Look at position. 
          0, 1, 0, // Up direction vector. 
          projection_modelview_mat); 

    GLint projection = glGetUniformLocation(shader.getProgram(), "mvp_matrix"); 
    glUniformMatrix4fv(projection, 1, GL_FALSE, &projection_modelview_mat[0]); 

    glGenBuffers(1, &triangle_buffer); 

    glActiveTexture(GL_TEXTURE0); 
    glGenTextures(1, &card_tex); 
    glBindTexture(GL_TEXTURE_2D, card_tex); 
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 

    NSString *fileRoot = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"card_texture.tga"]; 

    tga_32bit_image card_img; 

    card_img.load([fileRoot UTF8String]); 

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, card_img.width, card_img.height, 
       0, GL_RGBA, 
       GL_UNSIGNED_BYTE, &card_img.pixels[0]); 



    glActiveTexture(GL_TEXTURE1); 
    glGenTextures(1, &rank_tex); 
    glBindTexture(GL_TEXTURE_2D, rank_tex); 
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 

    fileRoot = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"rank_texture.tga"]; 

    tga_32bit_image rank_img; 

    rank_img.load([fileRoot UTF8String]); 

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, rank_img.width, rank_img.height, 
       0, GL_RGBA, 
       GL_UNSIGNED_BYTE, &rank_img.pixels[0]); 
} 





- (void)tearDownGL 
{ 
    NSLog(@"tearDownGL"); 


    [EAGLContext setCurrentContext:self.context]; 

    glDeleteBuffers(1, &triangle_buffer); 

    glDeleteTextures(1, &card_tex); 

    glDeleteTextures(1, &rank_tex); 

    NSLog(@"teardown"); 
} 

#pragma mark - GLKView and GLKViewController delegate methods 


- (void)update 
{ 



} 


void touch_up_pos(int x, int y, unsigned int m_renderbufferWidth, unsigned int m_renderbufferHeight) 
{ 
    const float pi = 4.0f*atanf(1.0f); 
    const float aspect = (float)(m_renderbufferWidth)/(float)(m_renderbufferHeight); 

    const float fx = 2.0f * ((float)(x)/(float)(m_renderbufferWidth - 1)) - 1.0f; 
    const float fy = 2.0f * ((float)(y)/(float)(m_renderbufferHeight - 1)) - 1.0f; 
    const float y_fov = pi/4; // pi/4 radians = 45 degrees 
    const float tangent = tan(y_fov/2.0f); 
    last_click_float_x = aspect * tangent* fx; 
    last_click_float_y = -tangent * fy; 

    NSLog(@"touch up pos %f %f", last_click_float_x, last_click_float_y); 
} 


- (void) touchesEnded: (NSSet*) touches withEvent: (UIEvent*) event 
{ 
    UITouch *touch = [[event allTouches] anyObject]; 
    CGPoint location = [touch locationInView:touch.view]; 

    unsigned int m_renderbufferWidth, m_renderbufferHeight; 
    CGRect Rect=[[UIScreen mainScreen] bounds]; 
    m_renderbufferWidth = Rect.size.width; 
    m_renderbufferHeight = Rect.size.height; 

    touch_up_pos(location.x, location.y, m_renderbufferWidth, m_renderbufferHeight); 
} 

- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect 
{ 
    glClear(GL_COLOR_BUFFER_BIT); 

    glUseProgram(shader.getProgram()); 

    glUniform1i(glGetUniformLocation(shader.getProgram(), "tex"), 0); 
    //glUniform1i(glGetUniformLocation(shader.getProgram(), "tex"), 1); 

    vector<GLfloat> vertices = { 
     -0.2, -0.2, 0.0, 0.0, 0.0, // vertex 0, 3D position, 2D texture coordinate 
     0.2, -0.2, 0.0, 1.0, 0.0, // vertex 1 
     0.2, 0.2, 0.0, 1.0, 1.0, // vertex 2 
     -0.2, -0.2, 0.0, 0.0, 0.0, // vertex 0 
     0.2, 0.2, 0.0, 1.0, 1.0, // vertex 2 
     -0.2, 0.2, 0.0, 0.0, 1.0 // vertex 3 
    }; 

/* vertex_3 v; 

    for(size_t i = 0; i < vertices.size(); i += 5) 
    { 
     v.x = vertices[i + 0]; 
     v.y = vertices[i + 1]; 
     v.z = vertices[i + 2]; 

     v.rotate_y(3.14/3.0); 

     vertices[i + 0] = v.x; 
     vertices[i + 1] = v.y; 
     vertices[i + 2] = v.z; 

    } 
    */ 

    glBindBuffer(GL_ARRAY_BUFFER, triangle_buffer); 

    glBufferData(GL_ARRAY_BUFFER, vertices.size()*sizeof(GLfloat), &vertices[0], GL_STATIC_DRAW); 

    glEnableVertexAttribArray(glGetAttribLocation(shader.getProgram(), "position")); 
    glVertexAttribPointer(glGetAttribLocation(shader.getProgram(), "position"), 3, GL_FLOAT, GL_FALSE, 5*sizeof(GLfloat), NULL); 

    glEnableVertexAttribArray(glGetAttribLocation(shader.getProgram(), "tex_coord")); 
    glVertexAttribPointer(glGetAttribLocation(shader.getProgram(), "tex_coord"), 2, GL_FLOAT, GL_TRUE, 5*sizeof(GLfloat), (const GLvoid*)(3 * sizeof(GLfloat))); 

    glDrawArrays(GL_TRIANGLES, 0, vertices.size()); 


    /* 
    static time_point<high_resolution_clock> t0 = high_resolution_clock::now(); 

    time_point<high_resolution_clock> t1 = high_resolution_clock::now(); 

    long long unsigned int ms = duration_cast <milliseconds>(t1 - t0).count(); 

    t0 = t1; 

    cout << ms << endl; 
    */ 
} 




@end 

我AppDelegate.h文件是:

#ifndef appdelegate_h 
#define appdelegate_h 


#import <UIKit/UIKit.h> 

@interface AppDelegate : UIResponder <UIApplicationDelegate> 

@property (strong, nonatomic) UIWindow *window; 


@end 

#endif 

我AppDelegate.m文件是:

#include "AppDelegate.h" 
#include "GameViewController.h" 

@interface AppDelegate() 

@end 

@implementation AppDelegate 


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
    // Override point for customization after application launch. 
    return YES; 
} 


- (void)applicationWillResignActive:(UIApplication *)application { 




    NSLog(@"applicationWillResignActive"); 


    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. 
} 


- (void)applicationDidEnterBackground:(UIApplication *)application { 

    NSLog(@"applicationDidEnterBackground"); 
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 
} 


- (void)applicationWillEnterForeground:(UIApplication *)application { 


     NSLog(@"applicationWillEnterForeground"); 
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. 
} 


- (void)applicationDidBecomeActive:(UIApplication *)application { 

    NSLog(@"applicationDidBecomeActive"); 


    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 
} 


- (void)applicationWillTerminate:(UIApplication *)application { 

     NSLog(@"applicationWillTerminate"); 






    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 
} 









@end 
+1

這是非法的調用'的dealloc '。 - 另外,請在問題中顯示您的代碼。你有什麼好的理由來壓縮你的github代碼?把它放在git版本控制下,並上傳到github _as code_。 – matt

+1

你不應該直接調用dealloc。 –

+0

謝謝。我編輯了原帖以調用teardownGL函數而不是dealloc。我在帖子中包含了源代碼。 –

回答

0

事實證明,我需要使用AppDelegate.m下面的代碼

@interface GameViewController : GLKViewController 
- (void)tearDownGL; 
- (void)setupGL; 
@end 

添加到GameViewController.h,並撥打電話:

GameViewController *vc = [[GameViewController alloc]init]; 
[vc setupGL];