{"id":207333,"date":"2025-07-08T10:40:48","date_gmt":"2025-07-08T02:40:48","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207333\/"},"modified":"2025-07-08T10:40:48","modified_gmt":"2025-07-08T02:40:48","slug":"glfw-%e6%8b%96%e5%8a%a8%e7%aa%97%e5%8f%a3%e6%9c%80%e5%88%9d%e5%9c%a8-macos-%e4%b8%8a%e5%be%88%e6%bb%9e%e5%90%8e","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207333\/","title":{"rendered":"glfw \u62d6\u52a8\u7a97\u53e3\u6700\u521d\u5728 macOS \u4e0a\u5f88\u6ede\u540e"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>glfw \u62d6\u52a8\u7a97\u53e3\u6700\u521d\u5728 macOS \u4e0a\u5f88\u6ede\u540e<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-21 19:21:33<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u8981\u52aa\u529b\uff0c\u4f46\u662f\u4e0d\u8981\u6025\uff01\u4eca\u5929\u7684\u8fd9\u7bc7\u6587\u7ae0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aglfw \u62d6\u52a8\u7a97\u53e3\u6700\u521d\u5728 macOS \u4e0a\u5f88\u6ede\u540e\u300b<\/span>\u5c06\u4f1a\u4ecb\u7ecd\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u60f3\u6df1\u5165\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\uff0c\u53ef\u4ee5\u5173\u6ce8\u6211\uff01\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u6587\u7ae0\u7684\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u90fd\u80fd\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u521a\u521a\u4f7f\u7528 glfw \u521b\u5efa\u4e86\u4e00\u4e2a\u7a97\u53e3\u5e76\u5728 macos \u4e0a\u8fd0\u884c\u3002\u95ee\u9898\u662f\uff1a\u5982\u679c\u6211\u62d6\u52a8\u7a97\u53e3\u5e76\u56db\u5904\u79fb\u52a8\uff0c\u7a97\u53e3\u7684\u79fb\u52a8\u6700\u521d\u4f1a\u975e\u5e38\u6ede\u540e\uff0c\u4f46\u540e\u6765\u4f1a\u53d8\u5f97\u5e73\u6ed1\u3002 linux (ubuntu) \u4e0a\u4e0d\u4f1a\u51fa\u73b0\u6b64\u95ee\u9898\u3002\u4e3a\u4ec0\u4e48\u4ee5\u53ca\u5982\u4f55\u89e3\u51b3\u5b83\uff1f<\/p>\n<p>\u7cfb\u7edf\uff1amacos 10.15.7 (19h2) \u4ee3\u7801\uff1a<\/p>\n<pre>package main\n\nimport (\n    \"fmt\"\n    \"runtime\"\n\n    \"github.com\/go-gl\/gl\/v4.1-core\/gl\"\n    \"github.com\/go-gl\/glfw\/v3.3\/glfw\"\n)\n\nfunc init() {\n    runtime.LockOSThread()\n}\n\nfunc main() {\n    err := glfw.Init()\n    if err != nil {\n        panic(fmt.Errorf(\"failed to initialize GLFW: %w\", err))\n    }\n    defer glfw.Terminate()\n\n    glfw.WindowHint(glfw.Resizable, glfw.False)\n    glfw.WindowHint(glfw.ContextVersionMajor, 4)\n    glfw.WindowHint(glfw.ContextVersionMinor, 1)\n    glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)\n    glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.True)\n\n    win, err := glfw.CreateWindow(800, 600, \"glfw\", nil, nil)\n    if err != nil {\n        panic(err)\n    }\n    defer win.Destroy()\n    win.MakeContextCurrent()\n    if err := gl.Init(); err != nil {\n        panic(err)\n    }\n    win.SetTitle(fmt.Sprintf(\"%s\", gl.GoStr(gl.GetString(gl.VERSION))))\n    gl.ClearColor(1.0, 1.0, 1.0, 1.0)\n\n    for !win.ShouldClose() {\n        gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)\n        win.SwapBuffers()\n        glfw.PollEvents()\n    }\n}<\/pre>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u8be5\u95ee\u9898\u4e0e go \u65e0\u5173\u3002\u7528 c \u7f16\u5199\u7684\u793a\u4f8b\u4e5f\u91cd\u73b0\u4e86\u8be5\u95ee\u9898\uff1a<\/p>\n<pre>#include &lt;GLFW\/glfw3.h&gt;\n\nint main(void)\n{\n    GLFWwindow* window;\n\n    \/* Initialize the library *\/\n    if (!glfwInit())\n        return -1;\n\n    \/* Create a windowed mode window and its OpenGL context *\/\n    window = glfwCreateWindow(640, 480, \"Hello World\", NULL, NULL);\n    if (!window)\n    {\n        glfwTerminate();\n        return -1;\n    }\n\n    \/* Make the window's context current *\/\n    glfwMakeContextCurrent(window);\n\n    \/* Loop until the user closes the window *\/\n    while (!glfwWindowShouldClose(window))\n    {\n        \/* Render here *\/\n        glClear(GL_COLOR_BUFFER_BIT);\n\n        \/* Swap front and back buffers *\/\n        glfwSwapBuffers(window);\n\n        \/* Poll for and process events *\/\n        glfwPollEvents();\n    }\n\n    glfwTerminate();\n    return 0;\n}<\/pre>\n<p>\u7b80\u77ed\u7684\u56de\u7b54\u662f\uff1a\u5728 macos \u4e0a\u4f7f\u7528 <code>glfwwaitevents<\/code>\u3002<\/p>\n<p>\u957f\u7684\u7b54\u6848\u662f glfwpollevents \u5904\u7406\u4e8b\u4ef6\uff08\u5982\u679c\u6709\uff09\u3002\u5426\u5219\uff0c\u5b83\u7acb\u5373\u8fd4\u56de\u3002\u8fd9\u610f\u5473\u7740\uff08\u7b80\u5355\u7684\uff09\u7a0b\u5e8f\u4f1a\u5c3d\u53ef\u80fd\u5feb\u5730\u4e00\u904d\u53c8\u4e00\u904d\u5730\u6e05\u9664\u5c4f\u5e55\uff0c\u4ece\u800c\u6d88\u8017\u5927\u91cf cpu \u65f6\u95f4\u3002\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f glfwwaitevents\uff0c\u5982\u679c\u6ca1\u6709\u4e8b\u4ef6\u7b49\u5f85\uff0c\u5219\u4f1a\u963b\u585e\uff0c\u4ece\u800c\u6d88\u8017\u5f88\u5c11\u6216\u4e0d\u6d88\u8017 cpu \u65f6\u95f4\u3002\u53ef\u4ee5\u8bf4\uff0c\u4f7f\u7528\u5927\u91cf cpu \u7684\u7a0b\u5e8f\u4e0d\u5e94\u5bfc\u81f4\u7a97\u53e3\u6ede\u540e\uff0c\u4f46\u8fd9\u5b9e\u9645\u4e0a\u53d6\u51b3\u4e8e\u64cd\u4f5c\u7cfb\u7edf\u53ca\u5176\u5206\u914d\u8d44\u6e90\u7684\u65b9\u5f0f\u3002<\/p>\n<p>\u81f3\u4e8e\u4f7f\u7528\u8fd9\u4e24\u4e2a\u51fd\u6570\u4e2d\u7684\u54ea\u4e00\u4e2a\uff0c\u5b8c\u5168\u53d6\u51b3\u4e8e\u6211\u4eec\u8fd8\u5e0c\u671b\u7a0b\u5e8f\u6267\u884c\u4ec0\u4e48\u64cd\u4f5c\u3002\u5982\u679c\u6211\u4eec\u53ea\u60f3\u5bf9\u7528\u6237\u8f93\u5165\u505a\u51fa\u53cd\u5e94\u800c\u4e0d\u505a\u5176\u4ed6\u4e8b\u60c5\uff0cglfwwaitevents \u53ef\u80fd\u662f\u6b63\u786e\u7684\u9009\u62e9\u3002\u5982\u679c\u6211\u4eec\u60f3\u5728\u4e8b\u4ef6\u5faa\u73af\u4e2d\u505a\u5176\u4ed6\u4e8b\u60c5\uff0cglfwpollevents \u53ef\u80fd\u66f4\u5408\u9002\uff0c\u7279\u522b\u662f\u5982\u679c\u6211\u4eec\u60f3\u72ec\u7acb\u4e8e\u7528\u6237\u8f93\u5165\u91cd\u7ed8\u7a97\u53e3\u5185\u5bb9\u3002<\/p>\n<p>\u65c1\u767d\uff1aglfwswapinterval(1) \u4e5f\u53ef\u80fd\u4f1a\u907f\u514d\u5ef6\u8fdf\uff0c\u56e0\u4e3a\u5b83\u4f1a\u5bfc\u81f4 glfwswapbuffers \u963b\u585e\u51e0\u6beb\u79d2\uff0c\u518d\u6b21\u8ba9 cpu \u4f11\u606f\u3002<\/p>\n<p>\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8eGolang\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-207333","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207333","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=207333"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207333\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207333"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207333"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207333"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}