瀏覽代碼

Make windows test a tolerable example

Camilla Löwy 5 年之前
父節點
當前提交
5c92cd9f52
共有 3 個文件被更改,包括 36 次插入88 次删除
  1. 3 1
      examples/CMakeLists.txt
  2. 32 84
      examples/windows.c
  3. 1 3
      tests/CMakeLists.txt

+ 3 - 1
examples/CMakeLists.txt

@@ -33,6 +33,7 @@ add_executable(sharing WIN32 MACOSX_BUNDLE sharing.c ${ICON} ${GLAD_GL})
 add_executable(splitview WIN32 MACOSX_BUNDLE splitview.c ${ICON} ${GLAD_GL})
 add_executable(triangle-opengl WIN32 MACOSX_BUNDLE triangle-opengl.c ${ICON} ${GLAD_GL})
 add_executable(wave WIN32 MACOSX_BUNDLE wave.c ${ICON} ${GLAD_GL})
+add_executable(windows WIN32 MACOSX_BUNDLE windows.c ${ICON} ${GLAD_GL})
 
 target_link_libraries(particles Threads::Threads)
 if (RT_LIBRARY)
@@ -40,7 +41,7 @@ if (RT_LIBRARY)
 endif()
 
 set(GUI_ONLY_BINARIES boing gears heightmap particles sharing splitview
-    triangle-opengl wave)
+    triangle-opengl wave windows)
 set(CONSOLE_BINARIES offscreen)
 
 set_target_properties(${GUI_ONLY_BINARIES} ${CONSOLE_BINARIES} PROPERTIES
@@ -67,6 +68,7 @@ if (APPLE)
     set_target_properties(triangle-opengl PROPERTIES MACOSX_BUNDLE_BUNDLE_NAME "OpenGL Triangle")
     set_target_properties(splitview PROPERTIES MACOSX_BUNDLE_BUNDLE_NAME "SplitView")
     set_target_properties(wave PROPERTIES MACOSX_BUNDLE_BUNDLE_NAME "Wave")
+    set_target_properties(windows PROPERTIES MACOSX_BUNDLE_BUNDLE_NAME "Windows")
 
     set_source_files_properties(glfw.icns PROPERTIES
                                 MACOSX_PACKAGE_LOCATION "Resources")

+ 32 - 84
tests/windows.c → examples/windows.c

@@ -1,5 +1,5 @@
 //========================================================================
-// Simple multi-window test
+// Simple multi-window example
 // Copyright (c) Camilla Löwy <[email protected]>
 //
 // This software is provided 'as-is', without any express or implied
@@ -22,10 +22,6 @@
 //    distribution.
 //
 //========================================================================
-//
-// This test creates four windows and clears each in a different color
-//
-//========================================================================
 
 #include <glad/gl.h>
 #define GLFW_INCLUDE_NONE
@@ -34,109 +30,60 @@
 #include <stdio.h>
 #include <stdlib.h>
 
-static GLFWwindow* windows[4];
-static const char* titles[] =
-{
-    "Red",
-    "Green",
-    "Blue",
-    "Yellow"
-};
-
-static const struct
-{
-    float r, g, b;
-} colors[] =
-{
-    { 0.95f, 0.32f, 0.11f },
-    { 0.50f, 0.80f, 0.16f },
-    {   0.f, 0.68f, 0.94f },
-    { 0.98f, 0.74f, 0.04f }
-};
-
-static void error_callback(int error, const char* description)
-{
-    fprintf(stderr, "Error: %s\n", description);
-}
-
-static void arrange_windows(void)
-{
-    int xbase, ybase;
-    glfwGetWindowPos(windows[0], &xbase, &ybase);
-
-    for (int i = 0;  i < 4;  i++)
-    {
-        int left, top, right, bottom;
-        glfwGetWindowFrameSize(windows[i], &left, &top, &right, &bottom);
-        glfwSetWindowPos(windows[i],
-                         xbase + (i & 1) * (200 + left + right),
-                         ybase + (i >> 1) * (200 + top + bottom));
-    }
-}
-
-static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
-{
-    if (action != GLFW_PRESS)
-        return;
-
-    switch (key)
-    {
-        case GLFW_KEY_SPACE:
-        {
-            int xpos, ypos;
-            glfwGetWindowPos(window, &xpos, &ypos);
-            glfwSetWindowPos(window, xpos, ypos);
-            break;
-        }
-
-        case GLFW_KEY_ESCAPE:
-            glfwSetWindowShouldClose(window, GLFW_TRUE);
-            break;
-
-        case GLFW_KEY_D:
-        {
-            for (int i = 0;  i < 4;  i++)
-            {
-                const int decorated = glfwGetWindowAttrib(windows[i], GLFW_DECORATED);
-                glfwSetWindowAttrib(windows[i], GLFW_DECORATED, !decorated);
-            }
-
-            arrange_windows();
-            break;
-        }
-    }
-}
-
 int main(int argc, char** argv)
 {
-    glfwSetErrorCallback(error_callback);
+    int xpos, ypos, height;
+    const char* description;
+    GLFWwindow* windows[4];
 
     if (!glfwInit())
+    {
+        glfwGetError(&description);
+        printf("Error: %s\n", description);
         exit(EXIT_FAILURE);
+    }
 
     glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
+    glfwWindowHint(GLFW_DECORATED, GLFW_FALSE);
+
+    glfwGetMonitorWorkarea(glfwGetPrimaryMonitor(), &xpos, &ypos, NULL, &height);
 
     for (int i = 0;  i < 4;  i++)
     {
+        const int size = height / 5;
+        const struct
+        {
+            float r, g, b;
+        } colors[] =
+        {
+            { 0.95f, 0.32f, 0.11f },
+            { 0.50f, 0.80f, 0.16f },
+            {   0.f, 0.68f, 0.94f },
+            { 0.98f, 0.74f, 0.04f }
+        };
+
         if (i > 0)
             glfwWindowHint(GLFW_FOCUS_ON_SHOW, GLFW_FALSE);
 
-        windows[i] = glfwCreateWindow(200, 200, titles[i], NULL, NULL);
+        windows[i] = glfwCreateWindow(size, size, "Multi-Window Example", NULL, NULL);
         if (!windows[i])
         {
+            glfwGetError(&description);
+            printf("Error: %s\n", description);
             glfwTerminate();
             exit(EXIT_FAILURE);
         }
 
-        glfwSetKeyCallback(windows[i], key_callback);
+        glfwSetWindowPos(windows[i],
+                         xpos + size * (1 + (i & 1)),
+                         ypos + size * (1 + (i >> 1)));
+        glfwSetInputMode(windows[i], GLFW_STICKY_KEYS, GLFW_TRUE);
 
         glfwMakeContextCurrent(windows[i]);
         gladLoadGL(glfwGetProcAddress);
         glClearColor(colors[i].r, colors[i].g, colors[i].b, 1.f);
     }
 
-    arrange_windows();
-
     for (int i = 0;  i < 4;  i++)
         glfwShowWindow(windows[i]);
 
@@ -148,7 +95,8 @@ int main(int argc, char** argv)
             glClear(GL_COLOR_BUFFER_BIT);
             glfwSwapBuffers(windows[i]);
 
-            if (glfwWindowShouldClose(windows[i]))
+            if (glfwWindowShouldClose(windows[i]) ||
+                glfwGetKey(windows[i], GLFW_KEY_ESCAPE))
             {
                 glfwTerminate();
                 exit(EXIT_SUCCESS);

+ 1 - 3
tests/CMakeLists.txt

@@ -40,7 +40,6 @@ add_executable(timeout WIN32 MACOSX_BUNDLE timeout.c ${GLAD_GL})
 add_executable(title WIN32 MACOSX_BUNDLE title.c ${GLAD_GL})
 add_executable(triangle-vulkan WIN32 triangle-vulkan.c ${GLAD_VULKAN})
 add_executable(window WIN32 MACOSX_BUNDLE window.c ${GLAD_GL})
-add_executable(windows WIN32 MACOSX_BUNDLE windows.c ${GLAD_GL})
 
 target_link_libraries(empty Threads::Threads)
 target_link_libraries(threads Threads::Threads)
@@ -50,7 +49,7 @@ if (RT_LIBRARY)
 endif()
 
 set(GUI_ONLY_BINARIES empty gamma icon inputlag joysticks tearing threads
-    timeout title triangle-vulkan window windows)
+    timeout title triangle-vulkan window)
 set(CONSOLE_BINARIES clipboard events msaa glfwinfo iconify monitors reopen
                      cursor)
 
@@ -73,7 +72,6 @@ if (APPLE)
     set_target_properties(threads PROPERTIES MACOSX_BUNDLE_BUNDLE_NAME "Threads")
     set_target_properties(timeout PROPERTIES MACOSX_BUNDLE_BUNDLE_NAME "Timeout")
     set_target_properties(title PROPERTIES MACOSX_BUNDLE_BUNDLE_NAME "Title")
-    set_target_properties(windows PROPERTIES MACOSX_BUNDLE_BUNDLE_NAME "Windows")
     set_target_properties(window PROPERTIES MACOSX_BUNDLE_BUNDLE_NAME "Window")
 
     set_target_properties(${GUI_ONLY_BINARIES} PROPERTIES