XCode C++ (Part 2)

Neil HaddleyMarch 8, 2023

OpenGL GLFW

macOSxcodeopenglglfwc-plus-plus

Building an OpenGL application using C++ and XCode

I compiled a minimal OpenGL on my M1 MacBook Air.

I used GLFW (a cross platform library for OpenGL).

Overview

I used GLFW to create an application window with a given width and height and the title "Hello World". In a loop, the SwapBuffers function is called until the window is closed.

I created a new Command Line Tool project

I created a new Command Line Tool project

I named the project Hello OpenGL

I named the project Hello OpenGL

I reviewed the General Settings

I reviewed the General Settings

I reviewed the default hello world code

I reviewed the default hello world code

I visited the GLFW web site

I visited the GLFW web site

I reviewed the Hello OpenGL example

I reviewed the Hello OpenGL example

I saw missing library/header files

I saw missing library/header files

brew install glfw

brew install glfw

installed to folder /opt/homebrew/Cellar/glfw/3.3.8

installed to folder /opt/homebrew/Cellar/glfw/3.3.8

I added a reference to /opt/homebrew/Cellar/glfw/3.3.8/ header files

I added a reference to /opt/homebrew/Cellar/glfw/3.3.8/ header files

/opt/homebrew/Cellar/glfw/3.3.8/include

/opt/homebrew/Cellar/glfw/3.3.8/include

I added the /opt/homebrew/Cellar/glfw/3.3.8/include reference

I added the /opt/homebrew/Cellar/glfw/3.3.8/include reference

I added a reference to libraries

I added a reference to libraries

I selected OpenGL.framework

I selected OpenGL.framework

I added items

I added items

I added files

I added files

I added a reference to the dynamically loaded library /opt/homebrew/Cellar/glfw/3.3.8/lib/libglfw.3.3.dylib

I added a reference to the dynamically loaded library /opt/homebrew/Cellar/glfw/3.3.8/lib/libglfw.3.3.dylib

I updated the library references

I updated the library references

I ran the Hello OpenGL application

I ran the Hello OpenGL application

main.cpp

C++
1#include <GLFW/glfw3.h>
2
3int main(void)
4{
5    GLFWwindow* window;
6
7    /* Initialize the library */
8    if (!glfwInit())
9        return -1;
10
11    /* Create a windowed mode window and its OpenGL context */
12    window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
13    if (!window)
14    {
15        glfwTerminate();
16        return -1;
17    }
18
19    /* Make the window's context current */
20    glfwMakeContextCurrent(window);
21
22    /* Loop until the user closes the window */
23    while (!glfwWindowShouldClose(window))
24    {
25        /* Render here */
26        glClear(GL_COLOR_BUFFER_BIT);
27
28        /* Swap front and back buffers */
29        glfwSwapBuffers(window);
30
31        /* Poll for and process events */
32        glfwPollEvents();
33    }
34
35    glfwTerminate();
36    return 0;
37}