DEV Community

edo1z
edo1z

Posted on

1 1

M1 macのc++でGLFWを使ってみる

OpenGLについてまだ全然分かっていませんが、GLFWを使ってアニメーションを表示したいと思っています。とりあえず、M1 macのc++でウインドウを表示しました。

環境

> uname -m
arm64
> brew config
macOS: 12.5-arm64
Rosetta 2: false
Enter fullscreen mode Exit fullscreen mode

GLFWをインストール

> brew install glfw
> brew --prefix glfw
/opt/homebrew/opt/glfw
Enter fullscreen mode Exit fullscreen mode

コンパイル・実行してみるコード


インストールしただけだとエラーになる

❯ g++ -o main main.cpp -lglfw -framework Cocoa -framework OpenGL -framework IOKit -framework CoreVideo
main.cpp:1:10: fatal error: 'GLFW/glfw3.h' file not found
#include <GLFW/glfw3.h>
Enter fullscreen mode Exit fullscreen mode

CPATH, LIBRARY_PATHを環境変数に追加

> vim ~/.zshrc
# 下記を追加
export CPATH=/opt/homebrew/include
export LIBRARY_PATH=/opt/homebrew/lib
Enter fullscreen mode Exit fullscreen mode

コンパイル・実行成功

  • コンパイル成功したけど、warningが沢山出る。これはAppleがOpenGLを基本廃止(非推奨)にしているかららしい。
❯ g++ -o main main.cpp -lglfw -framework Cocoa -framework OpenGL -framework IOKit -framework CoreVideo
main.cpp:23:5: warning: 'glClear' is deprecated: first deprecated in macOS 10.14 - OpenGL API deprecated. (Define GL_SILENCE_DEPRECATION to silence these warnings) [-Wdeprecated-declarations]
    glClear(GL_COLOR_BUFFER_BIT);
    ^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/OpenGL.framework/Headers/gl.h:2394:13: note: 'glClear' has been explicitly marked deprecated here
extern void glClear (GLbitfield mask) OPENGL_DEPRECATED(10.0, 10.14);
Enter fullscreen mode Exit fullscreen mode

warningを非表示にしてみる

  • コードに下記を追加する。
    • #define GL_SILENCE_DEPRECATION によって、非表示になる。
#ifdef __APPLE__
#define GL_SILENCE_DEPRECATION
#include <GLFW/glfw3.h>
#endif
Enter fullscreen mode Exit fullscreen mode

Makefileを作る

CPPFLAGS := -I src/includes
FRAMEWORKS := -framework Cocoa -framework OpenGL -framework IOKit -framework CoreVideo
LIBS := -lglfw

BUILD_DIR := ./bin
SRC_DIRS := ./src
SRCS := $(shell find $(SRC_DIRS) -name *.cpp)
OBJS := $(addprefix $(BUILD_DIR)/, $(subst .cpp,.o, $(notdir $(SRCS))))

$(BUILD_DIR)/main: $(OBJS)
    clang++ -o $@ $(LIBS) $(FRAMEWORKS) $^ 
$(BUILD_DIR)/%.o: src/%.cpp
    clang++ -c -o $@ $(CPPFLAGS) $<

do:
    @bin/main
clean:
    rm -rf bin
    mkdir bin

Enter fullscreen mode Exit fullscreen mode

Debugできない

  • vimspectorで、.vimspector.jsonを下記のように設定してみたけど、エラーになる。
  • -lglfwがあるとエラーになるっぽい。
{
  "configurations": {
    "CodeLLDB": {
      "adapter": "CodeLLDB",
      "variables": {
        "BUILDME": {
          "shell": "clang++ -o ${workspaceRoot}/bin/debug -g -lglfw -framework Cocoa -framework OpenGL -framework IOKit -framework CoreVideo ${workspaceRoot}/src/main.cpp"
        }
      },
      "configuration": {
        "request": "launch",
        "program": "${workspaceRoot}/bin/debug",
        "stdio": [null, null, null]
      }
    }
  }
}

Enter fullscreen mode Exit fullscreen mode

青いウインドウを表示するコード

#ifdef __APPLE__
#define GL_SILENCE_DEPRECATION
#include <GLFW/glfw3.h>
#endif

#include <iostream>
using namespace std;

static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) {
  if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
    glfwSetWindowShouldClose(window, GLFW_TRUE);
}

int main(void) {
  GLFWwindow* window;
  if (!glfwInit()) return -1;

  glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
  glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
  glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

  window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
  if (!window) {
    cout << "failed to create window." << endl;
    glfwTerminate();
    return -1;
  }
  cout << "opened window" << endl;
  glfwSetKeyCallback(window, key_callback);
  glfwMakeContextCurrent(window);
  glfwSwapInterval(1);

  const GLubyte* renderer = glGetString(GL_RENDERER);
  const GLubyte* version = glGetString(GL_VERSION);
  std::cout << "Renderer: " << renderer << std::endl;
  std::cout << "OpenGL version supported: " << version << std::endl;

  glClearColor(0.0f, 0.3f, 0.6f, 0.3f);

  while (!glfwWindowShouldClose(window)) {
    glClear(GL_COLOR_BUFFER_BIT);
    glfwSwapBuffers(window);
    glfwPollEvents();
  }

  glfwTerminate();
  cout << "closed window" << endl;
}
Enter fullscreen mode Exit fullscreen mode

リポジトリ

https://github.com/web3ten0/cpp-glfw-1


表示されるウインドウ

表示される青いウインドウ

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay