macOSEntryPoint

Basic Tutorial that shows step-by-step how to initialise OpenGL ES 2.0, use it for drawing a triangle and terminate it.

 /*!*********************************************************************************************************************
 \File         OpenGLESHelloAPI_macOS.mm
 \Title        OpenGL ES 2.0 HelloAPI Tutorial
 \Author       PowerVR by Imagination, Developer Technology Team
 \Copyright    Copyright (c) Imagination Technologies Limited.
 \brief  Basic Tutorial that shows step-by-step how to initialise OpenGL ES 2.0, use it for drawing a triangle and terminate it.
               Entry Point: main
 ***********************************************************************************************************************/
 
 #import <AppKit/NSApplication.h>
 #import <AppKit/NSWindow.h>
 #import <AppKit/NSScreen.h>
 #import <AppKit/NSView.h>
 
 #include "OpenGLESHelloAPI.h"
 
 unsigned int Kfps (120.0);
 
 
 /*!*********************************************************************************************************************
  Class AppController
  ***********************************************************************************************************************/
 @class AppController;
 
 @interface AppController : NSObject <NSApplicationDelegate>
 {
 @private
 	NSTimer*         m_timer;		// Timer for rendering OpenGL content
 	NSWindow*        m_window;   	// Window
     
 	OpenGLESAPI* helloAPI;
 }
 @end
 
 @implementation AppController
 
 /*!*********************************************************************************************************************
 \param[in] 		notification
 \brief	Called when the application has finished launching.
 ***********************************************************************************************************************/
 - (void) applicationDidFinishLaunching:(NSNotification *)notification
 {

Initialise EGL and OpenGL ES variables.

     helloAPI = new OpenGLESAPI();
 

Create the window.

     NSRect frame = NSMakeRect(0,0,helloAPI._surfaceData.width, helloAPI._surfaceData.height);
     m_window = [[NSWindow alloc] initWithContentRect:frame styleMask:NSMiniaturizableWindowMask | NSTitledWindowMask | NSClosableWindowMask
 											 backing:NSBackingStoreBuffered defer:NO];
     
 
     
     if(!m_window)
     {
         NSLog(@"Failed to allocate the window.");
         [self terminateApp];
     }
     
     [m_window setTitle:@"OpenGLESHelloAPI"];
     

Create the view.

     helloAPI->_surfaceData.window = [[NSView alloc] initWithFrame:frame];
     

Add the created view to the window.

     [m_window setContentView:(NSView*)helloAPI->_surfaceData.window];
     [m_window makeKeyAndOrderFront:nil];
 

Add an observer so when the window is closed, the app is terminated.

     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(terminateApp) name:NSWindowWillCloseNotification
                                                object:m_window];
     
     if(!helloAPI->initializeGLES() || !helloAPI.initializeEGL())
     {
         [self terminateApp];
     }
 	
    
 
 

Setup a timer to redraw the view at a regular interval.

     m_timer = [NSTimer scheduledTimerWithTimeInterval:(1.0 / Kfps) target:self selector:@selector(render) userInfo:nil repeats:YES];
 }
 
 /*!*********************************************************************************************************************
 \brief   Called when the app is about to terminate.
 ***********************************************************************************************************************/
 - (void) applicationWillTerminate:(NSNotification *)notification
 {

Release the timer.

     [m_timer invalidate];
 
     helloAPI->deInitialize();
 	helloAPI->releaseEGLState();
     
     [m_window release];
     m_window = nil;
 }
 
 /*!*********************************************************************************************************************
 \brief   Attempts to immediately terminate the application.
 ***********************************************************************************************************************/
 - (void) terminateApp
 {
     [NSApp terminate:nil];
 }
 
 - (void) render
 {
     if(!helloAPI->drawFrame() || !helloAPI->swapEGLBuffers())
     {
         [self terminateApp];
     }
 }
 @end
 
 /*!*********************************************************************************************************************
 \param[in]			argc           Number of arguments passed to the application, ignored.
 \param[in]			argv           Command line strings passed to the application, ignored.
 \return		Result code to send to the Operating System
 \brief	Main function of the program, executes other functions.
 ***********************************************************************************************************************/
 int main(int argc, char **argv){	return NSApplicationMain(argc, (const char **)argv);  }